120 lines
6.5 KiB
Objective-C
120 lines
6.5 KiB
Objective-C
//
|
|
// STDSJSONWebEncryptionTests.m
|
|
// Stripe3DS2Tests
|
|
//
|
|
// Created by Cameron Sabol on 1/29/19.
|
|
// Copyright © 2019 Stripe. All rights reserved.
|
|
//
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
#import "STDSStripe3DS2Error.h"
|
|
#import "NSString+JWEHelpers.h"
|
|
#import "STDSDirectoryServer.h"
|
|
#import "STDSDirectoryServerCertificate.h"
|
|
#import "STDSJSONWebEncryption.h"
|
|
#import "STDSSecTypeUtilities.h"
|
|
|
|
@interface STDSJSONWebEncryptionTests : XCTestCase
|
|
|
|
@end
|
|
|
|
@implementation STDSJSONWebEncryptionTests
|
|
|
|
- (void)testEncryption {
|
|
NSDictionary *json = @{@"a": @(0), @"b": @[@(0), @(1), @(2)], @"c": @{@"d": @"val"}};
|
|
NSError *error = nil;
|
|
|
|
NSString *encrypted = [STDSJSONWebEncryption encryptJSON:json forDirectoryServer:STDSDirectoryServerSTPTestRSA error:&error];
|
|
XCTAssertNotNil(encrypted, @"Should successfully encrypt with ul_test cert.");
|
|
XCTAssertNil(error, @"Successful encryption should not populate error.");
|
|
|
|
encrypted = [STDSJSONWebEncryption encryptJSON:json forDirectoryServer:STDSDirectoryServerSTPTestEC error:&error];
|
|
XCTAssertNotNil(encrypted, @"Should successfully encrypt with ec_test cert.");
|
|
XCTAssertNil(error, @"Successful encryption should not populate error.");
|
|
|
|
encrypted = [STDSJSONWebEncryption encryptJSON:json forDirectoryServer:STDSDirectoryServerULTestRSA error:&error];
|
|
XCTAssertNotNil(encrypted, @"Should successfully encrypt with STDSDirectoryServerULTestRSA.");
|
|
XCTAssertNil(error, @"Successful encryption should not populate error.");
|
|
|
|
encrypted = [STDSJSONWebEncryption encryptJSON:json forDirectoryServer:STDSDirectoryServerULTestEC error:&error];
|
|
XCTAssertNotNil(encrypted, @"Should successfully encrypt with STDSDirectoryServerULTestEC.");
|
|
XCTAssertNil(error, @"Successful encryption should not populate error.");
|
|
|
|
encrypted = [STDSJSONWebEncryption encryptJSON:json forDirectoryServer:STDSDirectoryServerUnknown error:&error];
|
|
XCTAssertNil(encrypted, @"Invalid server ID should return nil.");
|
|
XCTAssertEqual(error.code, STDSErrorCodeDecryptionVerification, @"Failed encryption should provide a STDSErrorCodeDecryptionVerification error.");
|
|
}
|
|
|
|
- (void)testEncryptionWithCustomCertificate {
|
|
NSDictionary *json = @{@"a": @(0), @"b": @[@(0), @(1), @(2)], @"c": @{@"d": @"val"}};
|
|
NSError *error = nil;
|
|
// Using ds-amex.pm.PEM
|
|
NSString *certificateString = @"MIIE0TCCA7mgAwIBAgIUXbeqM1duFcHk4dDBwT8o7Ln5wX8wDQYJKoZIhvcNAQEL"
|
|
"BQAwXjELMAkGA1UEBhMCVVMxITAfBgNVBAoTGEFtZXJpY2FuIEV4cHJlc3MgQ29t"
|
|
"cGFueTEsMCoGA1UEAxMjQW1lcmljYW4gRXhwcmVzcyBTYWZla2V5IElzc3Vpbmcg"
|
|
"Q0EwHhcNMTgwMjIxMjM0OTMxWhcNMjAwMjIxMjM0OTMwWjCB0DELMAkGA1UEBhMC"
|
|
"VVMxETAPBgNVBAgTCE5ldyBZb3JrMREwDwYDVQQHEwhOZXcgWW9yazE/MD0GA1UE"
|
|
"ChM2QW1lcmljYW4gRXhwcmVzcyBUcmF2ZWwgUmVsYXRlZCBTZXJ2aWNlcyBDb21w"
|
|
"YW55LCBJbmMuMTkwNwYDVQQLEzBHbG9iYWwgTmV0d29yayBUZWNobm9sb2d5IC0g"
|
|
"TmV0d29yayBBUEkgUGxhdGZvcm0xHzAdBgNVBAMTFlNESy5TYWZlS2V5LkVuY3J5"
|
|
"cHRLZXkwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDSFF9kTYbwRrxX"
|
|
"C6WcJJYio5TZDM62+CnjQRfggV3GMI+xIDtMIN8LL/jbWBTycu97vrNjNNv+UPhI"
|
|
"WzhF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tXGcK56iQdtKVPhFGOOEBugw7AcuRzv3f1GhvzAQZg==";
|
|
STDSDirectoryServerCertificate *certificate = [STDSDirectoryServerCertificate customCertificateWithData:[[NSData alloc] initWithBase64EncodedString:certificateString options:0]];
|
|
|
|
NSString *encrypted = [STDSJSONWebEncryption encryptJSON:json
|
|
withCertificate:certificate
|
|
directoryServerID:@"custom_test"
|
|
serverKeyID:nil
|
|
error:&error];
|
|
XCTAssertNotNil(encrypted, @"Should successfully encrypt with custom cert.");
|
|
XCTAssertNil(error, @"Successful encryption should not populate error.");
|
|
}
|
|
|
|
- (void)testDirectEncryptionWithKey {
|
|
NSDictionary *json = @{@"a": @(0), @"b": @[@(0), @(1), @(2)], @"c": @{@"d": @"val"}};
|
|
NSError *error = nil;
|
|
NSData *contentEncryptionKey = STDSCryptoRandomData(32);
|
|
NSString *encrypted = [STDSJSONWebEncryption directEncryptJSON:json
|
|
withContentEncryptionKey:contentEncryptionKey
|
|
forACSTransactionID:@"acs_id"
|
|
error:&error];
|
|
XCTAssertNotNil(encrypted, @"Should successfully encrypt with direct encryption key.");
|
|
XCTAssertNil(error, @"Successful encryption should not populate error.");
|
|
}
|
|
|
|
- (void)testDecryption {
|
|
NSDictionary *json = @{@"a": @(0), @"b": @[@(0), @(1), @(2)], @"c": @{@"d": @"val"}};
|
|
NSError *error = nil;
|
|
NSData *contentEncryptionKey = STDSCryptoRandomData(32);
|
|
NSString *encrypted = [STDSJSONWebEncryption directEncryptJSON:json
|
|
withContentEncryptionKey:contentEncryptionKey
|
|
forACSTransactionID:@"acs_id"
|
|
error:&error];
|
|
XCTAssertNotNil(encrypted, @"Should successfully encrypt with direct encryption key.");
|
|
XCTAssertNil(error, @"Successful encryption should not populate error.");
|
|
|
|
NSDictionary *decrypted = [STDSJSONWebEncryption decryptData:[encrypted dataUsingEncoding:NSUTF8StringEncoding]
|
|
withContentEncryptionKey:contentEncryptionKey
|
|
error:&error];
|
|
XCTAssertEqualObjects(decrypted, json, @"Decrypted is not equal to the encrypted json");
|
|
}
|
|
|
|
@end
|