Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add ios secure comparator tests #288

Merged
merged 6 commits into from
Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/examples/objc/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ target :"ThemisTest" do

pod 'themis', '0.9.6'

end

post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SECURE_COMPARATOR_ENABLED'
end
end
end
2 changes: 1 addition & 1 deletion docs/examples/objc/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ SPEC CHECKSUMS:
GRKOpenSSLFramework: b6172cc34db9b999002483878772a4bf2a1687ba
themis: 470fc7b5532b7ff098664185d389fbb1a5ac3603

PODFILE CHECKSUM: 2b15e4b7132ff7b147ed14efa1070b05ecd89078
PODFILE CHECKSUM: 78a572d0755000411d1f5b712a7f0154b3e6a705

COCOAPODS: 1.3.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0910"
LastUpgradeVersion = "0920"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
37 changes: 37 additions & 0 deletions docs/examples/objc/ThemisTest/ThemisTest/Classes/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#import "AppDelegate.h"
#import <objcthemis/objcthemis.h>

#define SECURE_COMPARATOR_ENABLED
#import <objcthemis/scomparator.h>


@interface AppDelegate ()

Expand Down Expand Up @@ -46,6 +49,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[self runExampleSecureCellSealMode];
[self runExampleSecureCellTokenProtectMode];
[self runExampleSecureCellImprint];


// Secure Comparator
[self runExampleSecureComparator];

return YES;
}
Expand Down Expand Up @@ -349,4 +356,34 @@ - (void)readingKeysFromFile {
}


- (void)runExampleSecureComparator {
NSLog(@"----------------- %s -----------------", sel_getName(_cmd));

NSString * sharedSecret = @"shared secret";
NSData * sharedSecretData = [sharedSecret dataUsingEncoding:NSUTF8StringEncoding];

TSComparator * client = [[TSComparator alloc] initWithMessageToCompare:sharedSecretData];
TSComparator * server = [[TSComparator alloc] initWithMessageToCompare:sharedSecretData];
NSError * error = nil;

// send this message to server
NSData * data = [client beginCompare:&error];
while ([client status] == TSComparatorNotReady || [server status] == TSComparatorNotReady) {
// receive from server
data = [server proceedCompare:data error:&error];

// proceed and send again
data = [client proceedCompare:data error:&error];
}

if ([client status] == TSComparatorMatch) {
// secrets match
NSLog(@"SecureComparator secrets match");
} else {
// secrets don't match
NSLog(@"SecureComparator secrets do not match");
}
}


@end
10 changes: 10 additions & 0 deletions docs/examples/swift/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ target :"ThemisSwift" do

pod 'themis', '0.9.6'

end


post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SECURE_COMPARATOR_ENABLED'
end
end
end
2 changes: 1 addition & 1 deletion docs/examples/swift/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ SPEC CHECKSUMS:
GRKOpenSSLFramework: b6172cc34db9b999002483878772a4bf2a1687ba
themis: 470fc7b5532b7ff098664185d389fbb1a5ac3603

PODFILE CHECKSUM: 5b1ff56e45ef21aa5645169defcdd17237374c37
PODFILE CHECKSUM: d579aa84867633a7ed4588acee91bdd1e8ab2067

COCOAPODS: 1.3.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0910"
LastUpgradeVersion = "0920"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
runExampleSecureMessageEncryptionDecryption()
runExampleSecureMessageSignVerify()

// Secure Comparator:
runExampleSecureComparator()

return true
}

Expand Down Expand Up @@ -337,4 +340,33 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}

// MARK:- Secure Comparator
func runExampleSecureComparator() {
print("----------------------------------", #function)

let sharedMessage = "shared secret"
let client: TSComparator = TSComparator.init(messageToCompare: sharedMessage.data(using: .utf8)!)!
let server: TSComparator = TSComparator.init(messageToCompare: sharedMessage.data(using: .utf8)!)!

// send this message to server
var data = try? client.beginCompare()

while (client.status() == TSComparatorStateType.comparatorNotReady ||
server.status() == TSComparatorStateType.comparatorNotReady ) {

// receive from server
data = try? server.proceedCompare(data)

// proceed and send again
data = try? client.proceedCompare(data)
}

if (client.status() == TSComparatorStateType.comparatorMatch) {
// secrets match
print("SecureComparator secrets match")
} else {
// secrets don't match
print("SecureComparator secrets do not match")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@

#import <objcthemis/objcthemis.h>

#define SECURE_COMPARATOR_ENABLED
#import <objcthemis/scomparator.h>

#endif /* ThemisSwift_Bridging_Header_h */
9 changes: 9 additions & 0 deletions tests/objcthemis/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ target :"objthemis" do
# example should work with head
pod 'themis', :git => "https://github.com/cossacklabs/themis.git"

end

post_install do |installer_representation|
installer_representation.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SECURE_COMPARATOR_ENABLED'
end
end
end
4 changes: 2 additions & 2 deletions tests/objcthemis/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ EXTERNAL SOURCES:

CHECKOUT OPTIONS:
themis:
:commit: 8090c5067368a29e835cf25538d60ddafd625f31
:commit: bdbf3763b4553d5ba2949583781d9835e2592b48
:git: https://github.com/cossacklabs/themis.git

SPEC CHECKSUMS:
GRKOpenSSLFramework: b6172cc34db9b999002483878772a4bf2a1687ba
themis: 470fc7b5532b7ff098664185d389fbb1a5ac3603

PODFILE CHECKSUM: 88edddd6333586b5b3690dd262564d9b4bb3c36c
PODFILE CHECKSUM: 917761b1dcd55e14d1d072d4bed52a309ba47467

COCOAPODS: 1.3.1
8 changes: 8 additions & 0 deletions tests/objcthemis/objcthemis.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
7308F54F1F717C4500AE0411 /* SecureMessageTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7308F54D1F717BF300AE0411 /* SecureMessageTests.m */; };
7308F5531F717D2D00AE0411 /* SecureCellTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7308F5521F717D2D00AE0411 /* SecureCellTests.m */; };
7376D8671F7182B70003AF72 /* SecureMessageTestsSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7376D8661F7182B70003AF72 /* SecureMessageTestsSwift.swift */; };
73CBA59D2012402B003EC7AC /* SecureComparatorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 73CBA59C2012402B003EC7AC /* SecureComparatorTests.m */; };
73CBA59F20124BB8003EC7AC /* SecureComparatorTestsSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73CBA59E20124BB8003EC7AC /* SecureComparatorTestsSwift.swift */; };
73D358221F71F06B004E000A /* SecureCellTestsSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73D358211F71F06B004E000A /* SecureCellTestsSwift.swift */; };
/* End PBXBuildFile section */

Expand All @@ -20,6 +22,8 @@
7308F5521F717D2D00AE0411 /* SecureCellTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SecureCellTests.m; sourceTree = "<group>"; };
7376D8651F7182B70003AF72 /* objthemis-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "objthemis-Bridging-Header.h"; sourceTree = "<group>"; };
7376D8661F7182B70003AF72 /* SecureMessageTestsSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecureMessageTestsSwift.swift; sourceTree = "<group>"; };
73CBA59C2012402B003EC7AC /* SecureComparatorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SecureComparatorTests.m; sourceTree = "<group>"; };
73CBA59E20124BB8003EC7AC /* SecureComparatorTestsSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecureComparatorTestsSwift.swift; sourceTree = "<group>"; };
73D358201F71E6F9004E000A /* StaticKeys.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = StaticKeys.h; sourceTree = "<group>"; };
73D358211F71F06B004E000A /* SecureCellTestsSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecureCellTestsSwift.swift; sourceTree = "<group>"; };
843EE74E1B5E1A36003F0138 /* objthemis.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = objthemis.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -62,6 +66,8 @@
isa = PBXGroup;
children = (
7308F5521F717D2D00AE0411 /* SecureCellTests.m */,
73CBA59E20124BB8003EC7AC /* SecureComparatorTestsSwift.swift */,
73CBA59C2012402B003EC7AC /* SecureComparatorTests.m */,
73D358201F71E6F9004E000A /* StaticKeys.h */,
73D358211F71F06B004E000A /* SecureCellTestsSwift.swift */,
7376D8661F7182B70003AF72 /* SecureMessageTestsSwift.swift */,
Expand Down Expand Up @@ -222,8 +228,10 @@
files = (
73D358221F71F06B004E000A /* SecureCellTestsSwift.swift in Sources */,
7376D8671F7182B70003AF72 /* SecureMessageTestsSwift.swift in Sources */,
73CBA59F20124BB8003EC7AC /* SecureComparatorTestsSwift.swift in Sources */,
7308F54F1F717C4500AE0411 /* SecureMessageTests.m in Sources */,
7308F5531F717D2D00AE0411 /* SecureCellTests.m in Sources */,
73CBA59D2012402B003EC7AC /* SecureComparatorTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
57 changes: 57 additions & 0 deletions tests/objcthemis/objthemis/SecureComparatorTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// SecureComparatorTests.m
// objthemis
//
// Created by Anastasiia on 1/19/18.
//

#import <XCTest/XCTest.h>
#import <objcthemis/objcthemis.h>


#define SECURE_COMPARATOR_ENABLED
#import <objcthemis/scomparator.h>


@interface SecureComparatorTests : XCTestCase

@end

@implementation SecureComparatorTests

- (void)testSecureComparatorEqualMessage {
NSString * sharedSecret = @"shared secret";
NSData * sharedSecretData = [sharedSecret dataUsingEncoding:NSUTF8StringEncoding];
TSComparator * alice = [[TSComparator alloc] initWithMessageToCompare:sharedSecretData];
TSComparator * bob = [[TSComparator alloc] initWithMessageToCompare:sharedSecretData];
NSError * error = nil;

NSData * data = [alice beginCompare:&error];
while ([alice status] == TSComparatorNotReady || [bob status] == TSComparatorNotReady) {
data = [bob proceedCompare:data error:&error];
data = [alice proceedCompare:data error:&error];
}

XCTAssertNil(error, @"result of comparison should be successful");
XCTAssertEqual([alice status], TSComparatorMatch, @"comparison should match");
XCTAssertEqual([bob status], TSComparatorMatch, @"comparison should match");
}

- (void)testSecureComparatorDifferentMessage {
TSComparator * alice = [[TSComparator alloc] initWithMessageToCompare:[@"some secret" dataUsingEncoding:NSUTF8StringEncoding]];
TSComparator * bob = [[TSComparator alloc] initWithMessageToCompare:[@"another secret" dataUsingEncoding:NSUTF8StringEncoding]];
NSError * error = nil;

NSData * data = [alice beginCompare:&error];
while ([alice status] == TSComparatorNotReady || [bob status] == TSComparatorNotReady) {
data = [bob proceedCompare:data error:&error];
data = [alice proceedCompare:data error:&error];
}

XCTAssertNil(error, @"result of comparison should be successful");
XCTAssertEqual([alice status], TSComparatorNotMatch, @"comparison should not match");
XCTAssertEqual([bob status], TSComparatorNotMatch, @"comparison should not match");
}


@end
44 changes: 44 additions & 0 deletions tests/objcthemis/objthemis/SecureComparatorTestsSwift.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// SecureComparatorTestsSwift.swift
// objthemis
//
// Created by Anastasiia on 1/19/18.
//

import XCTest

class SecureComparatorTestsSwift: XCTestCase {

func testSecureComparatorEqualMessage() {

let sharedMessage = "shared secret"
let alice: TSComparator = TSComparator.init(messageToCompare: sharedMessage.data(using: .utf8)!)!
let bob: TSComparator = TSComparator.init(messageToCompare: sharedMessage.data(using: .utf8)!)!

var data = try? alice.beginCompare()

while (alice.status() == TSComparatorStateType.notReady || bob.status() == TSComparatorStateType.notReady ) {
data = try? bob.proceedCompare(data)
data = try? alice.proceedCompare(data)
}

XCTAssertEqual(alice.status(), TSComparatorStateType.match, "comparison should match")
XCTAssertEqual(bob.status(), TSComparatorStateType.match, "comparison should match")
}

func testSecureComparatorDifferentMessage() {
let alice: TSComparator = TSComparator.init(messageToCompare: "one secret".data(using: .utf8)!)!
let bob: TSComparator = TSComparator.init(messageToCompare: "another secret".data(using: .utf8)!)!

var data = try? alice.beginCompare()

while (alice.status() == TSComparatorStateType.notReady || bob.status() == TSComparatorStateType.notReady ) {
data = try? bob.proceedCompare(data)
data = try? alice.proceedCompare(data)
}

XCTAssertEqual(alice.status(), TSComparatorStateType.notMatch, "comparison should not match")
XCTAssertEqual(bob.status(), TSComparatorStateType.notMatch, "comparison should not match")
}

}
3 changes: 3 additions & 0 deletions tests/objcthemis/objthemis/objthemis-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
#import <objcthemis/objcthemis.h>
#import "StaticKeys.h"

#define SECURE_COMPARATOR_ENABLED
#import <objcthemis/scomparator.h>

#endif /* objcthemis_Bridging_Header_h */