From 8d80134baa143a73762438fd083f56db4724a92b Mon Sep 17 00:00:00 2001 From: Steve Kim Date: Mon, 7 Oct 2024 10:01:46 -0700 Subject: [PATCH] add defines for test package and secitem options test --- Package.swift | 6 +++ .../io/TLSContextTests.swift | 17 +++++++ .../MqttClient.xcodeproj/project.pbxproj | 15 +++++-- .../MqttClient/MqttClient/ContentView.swift | 44 +++++++++++++------ 4 files changed, 65 insertions(+), 17 deletions(-) diff --git a/Package.swift b/Package.swift index ea19400b..666bd1f1 100644 --- a/Package.swift +++ b/Package.swift @@ -134,6 +134,7 @@ cSettingsIO.append(.define("AWS_USE_EPOLL")) awsCIoPlatformExcludes.append("source/windows") awsCIoPlatformExcludes.append("source/linux") awsCIoPlatformExcludes.append("source/s2n") +cSettingsIO.append(.define("AWS_OS_APPLE")) cSettingsIO.append(.define("__APPLE__")) cSettingsIO.append(.define("AWS_USE_DISPATCH_QUEUE", .when(platforms: [.iOS, .tvOS]))) cSettingsIO.append(.define("AWS_USE_SECITEM", .when(platforms: [.iOS, .tvOS]))) @@ -310,6 +311,11 @@ packageTargets.append(contentsOf: [ path: "Test/AwsCommonRuntimeKitTests", resources: [ .process("Resources") + ], + cSettings: [ + .define("AWS_USE_SECITEM", .when(platforms: [.iOS, .tvOS])), + .define("AWS_USE_DISPATCH_QUEUE", .when(platforms: [.iOS, .tvOS])), + .define("AWS_USE_KQUEUE", .when(platforms: [.macOS])) ] ), .executableTarget( diff --git a/Test/AwsCommonRuntimeKitTests/io/TLSContextTests.swift b/Test/AwsCommonRuntimeKitTests/io/TLSContextTests.swift index ccc77a4f..0dae4ef3 100644 --- a/Test/AwsCommonRuntimeKitTests/io/TLSContextTests.swift +++ b/Test/AwsCommonRuntimeKitTests/io/TLSContextTests.swift @@ -39,4 +39,21 @@ class TLSContextTests: XCBaseTestCase { _ = TLSConnectionOptions(context: context) } #endif + +#if AWS_USE_SECITEM + func testCreateTlsContextWithSecitemOptions() throws { + let certPath = try getEnvironmentVarOrSkipTest(environmentVarName: "AWS_TEST_MQTT311_IOT_CORE_X509_CERT") + let privateKeyPath = try getEnvironmentVarOrSkipTest(environmentVarName: "AWS_TEST_MQTT311_IOT_CORE_X509_KEY") + + let certificateData = try Data(contentsOf: URL(fileURLWithPath: certPath)) + let privateKeyData = try Data(contentsOf: URL(fileURLWithPath: privateKeyPath)) + + let options = try TLSContextOptions.makeMTLS(certificateData: certificateData, privateKeyData: privateKeyData) + try options.setSecitemLabels(certLabel: "TEST_CERT_LABEL", keyLabel: "TEST_KEY_LABEL") + + let context = try TLSContext(options:options, mode: .client) + _ = TLSConnectionOptions(context: context) + } + +#endif } diff --git a/Test/IntegrationTests/MqttClient/MqttClient.xcodeproj/project.pbxproj b/Test/IntegrationTests/MqttClient/MqttClient.xcodeproj/project.pbxproj index 63868f26..c876d746 100644 --- a/Test/IntegrationTests/MqttClient/MqttClient.xcodeproj/project.pbxproj +++ b/Test/IntegrationTests/MqttClient/MqttClient.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ A63CEBC82C2387CD003A71B7 /* AwsCommonRuntimeKit in Frameworks */ = {isa = PBXBuildFile; productRef = A63CEBC72C2387CD003A71B7 /* AwsCommonRuntimeKit */; }; A6C798772C238DD30055AE09 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A6C798762C238DD30055AE09 /* Assets.xcassets */; }; A6C7987B2C2A2A340055AE09 /* AwsCommonRuntimeKit in Frameworks */ = {isa = PBXBuildFile; productRef = A6C7987A2C2A2A340055AE09 /* AwsCommonRuntimeKit */; }; + C88382BA2CAC799800EC5E5E /* AwsCommonRuntimeKit in Frameworks */ = {isa = PBXBuildFile; productRef = C88382B92CAC799800EC5E5E /* AwsCommonRuntimeKit */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -30,6 +31,7 @@ A6C7987B2C2A2A340055AE09 /* AwsCommonRuntimeKit in Frameworks */, A63CEBC62C2387A3003A71B7 /* AwsCommonRuntimeKit in Frameworks */, A63CEBC82C2387CD003A71B7 /* AwsCommonRuntimeKit in Frameworks */, + C88382BA2CAC799800EC5E5E /* AwsCommonRuntimeKit in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -82,6 +84,7 @@ A63CEBC52C2387A3003A71B7 /* AwsCommonRuntimeKit */, A63CEBC72C2387CD003A71B7 /* AwsCommonRuntimeKit */, A6C7987A2C2A2A340055AE09 /* AwsCommonRuntimeKit */, + C88382B92CAC799800EC5E5E /* AwsCommonRuntimeKit */, ); productName = MqttClient; productReference = A63CEBB12C238347003A71B7 /* MqttClient.app */; @@ -112,7 +115,7 @@ ); mainGroup = A63CEBA82C238347003A71B7; packageReferences = ( - A6C798792C2A2A340055AE09 /* XCLocalSwiftPackageReference "../../.." */, + C88382B82CAC799800EC5E5E /* XCLocalSwiftPackageReference "../../../../aws-crt-swift" */, ); productRefGroup = A63CEBB22C238347003A71B7 /* Products */; projectDirPath = ""; @@ -274,7 +277,7 @@ CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; DEVELOPMENT_ASSET_PATHS = ""; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = ML5AUYTTTK; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; @@ -350,9 +353,9 @@ /* End XCConfigurationList section */ /* Begin XCLocalSwiftPackageReference section */ - A6C798792C2A2A340055AE09 /* XCLocalSwiftPackageReference "../../.." */ = { + C88382B82CAC799800EC5E5E /* XCLocalSwiftPackageReference "../../../../aws-crt-swift" */ = { isa = XCLocalSwiftPackageReference; - relativePath = ../../..; + relativePath = "../../../../aws-crt-swift"; }; /* End XCLocalSwiftPackageReference section */ @@ -369,6 +372,10 @@ isa = XCSwiftPackageProductDependency; productName = AwsCommonRuntimeKit; }; + C88382B92CAC799800EC5E5E /* AwsCommonRuntimeKit */ = { + isa = XCSwiftPackageProductDependency; + productName = AwsCommonRuntimeKit; + }; /* End XCSwiftPackageProductDependency section */ }; rootObject = A63CEBA92C238347003A71B7 /* Project object */; diff --git a/Test/IntegrationTests/MqttClient/MqttClient/ContentView.swift b/Test/IntegrationTests/MqttClient/MqttClient/ContentView.swift index 35780779..17d34a92 100644 --- a/Test/IntegrationTests/MqttClient/MqttClient/ContentView.swift +++ b/Test/IntegrationTests/MqttClient/MqttClient/ContentView.swift @@ -8,6 +8,10 @@ import AwsCommonRuntimeKit // Update the host and port before run the app. let TEST_HOST = "" let TEST_PORT: UInt32 = 1883 + +let TLS_HOST = "a2yvr5l8sc9814-ats.iot.us-east-1.amazonaws.com" +let TLS_PORT: UInt32 = 8883 + var mqttTestContext = MqttTestContext() var client: Mqtt5Client? @@ -196,7 +200,7 @@ func createClient(clientOptions: MqttClientOptions, testContext: MqttTestContext /// Init CRT library func library_init() { - try? Logger.initialize(target: .standardOutput, level: .debug) + try? Logger.initialize(target: .standardOutput, level: .trace) CommonRuntimeKit.initialize() } @@ -206,20 +210,34 @@ func setupClientAndStart() { qos: .background) backgroundQueue.async { - - let ConnectPacket = MqttConnectOptions(keepAliveInterval: 60, clientId: createClientId()) - - let clientOptions = MqttClientOptions( - hostName: TEST_HOST, - port: TEST_PORT, - connectOptions: ConnectPacket, - connackTimeout: TimeInterval(10)) - do { - client = try createClient(clientOptions: clientOptions, testContext: mqttTestContext) - try connectClient(client: client!, testContext: mqttTestContext) + let certFile = "/Users/sbstevek/workplace/swift-bindings-secitem/aws-crt-swift/.vscode/sdk_mqtt5_thing_cert.pem" + let keyFile = "/Users/sbstevek/workplace/swift-bindings-secitem/aws-crt-swift/.vscode/sdk_mqtt5_thing_key.pem" + + + let tlsOptions = try TLSContextOptions.makeMTLS(certificatePath: certFile, privateKeyPath: keyFile) + try tlsOptions.setSecitemLabels(certLabel: "DUMB_CERT_LABEL", keyLabel: "Test_Key_Label") + + let tlsContext = try TLSContext(options: tlsOptions, mode: .client) + + let ConnectPacket = MqttConnectOptions(keepAliveInterval: 60, clientId: createClientId()) + + + let clientOptions = MqttClientOptions( + hostName: TLS_HOST, + port: TLS_PORT, + tlsCtx: tlsContext, + connectOptions: ConnectPacket, + connackTimeout: TimeInterval(10)) + + do { + client = try createClient(clientOptions: clientOptions, testContext: mqttTestContext) + try connectClient(client: client!, testContext: mqttTestContext) + } catch { + mqttTestContext.printView("Failed to setup client.") + } } catch { - mqttTestContext.printView("Failed to setup client.") + print("Error occurred: \(error)") } } }