|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Project CHIP Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// module headers |
| 18 | +#import <Matter/Matter.h> |
| 19 | + |
| 20 | +#import "MTRErrorTestUtils.h" |
| 21 | +#import "MTRTestKeys.h" |
| 22 | +#import "MTRTestStorage.h" |
| 23 | + |
| 24 | +// system dependencies |
| 25 | +#import <XCTest/XCTest.h> |
| 26 | + |
| 27 | +static const uint16_t kPairingTimeoutInSeconds = 10; |
| 28 | +static const uint64_t kDeviceId = 0x12344321; |
| 29 | +static NSString * kOnboardingPayload = @"MT:-24J0AFN00KA0648G00"; |
| 30 | +static const uint16_t kLocalPort = 5541; |
| 31 | +static const uint16_t kTestVendorId = 0xFFF1u; |
| 32 | + |
| 33 | +// Singleton controller we use. |
| 34 | +static MTRDeviceController * sController = nil; |
| 35 | + |
| 36 | +// Keys we can use to restart the controller. |
| 37 | +static MTRTestKeys * sTestKeys = nil; |
| 38 | + |
| 39 | +@interface MTROperationalCertificateIssureTestDeviceControllerDelegate : NSObject <MTRDeviceControllerDelegate> |
| 40 | +@property (nonatomic, strong) XCTestExpectation * expectation; |
| 41 | +@end |
| 42 | + |
| 43 | +@implementation MTROperationalCertificateIssureTestDeviceControllerDelegate |
| 44 | +- (id)initWithExpectation:(XCTestExpectation *)expectation |
| 45 | +{ |
| 46 | + self = [super init]; |
| 47 | + if (self) { |
| 48 | + _expectation = expectation; |
| 49 | + } |
| 50 | + return self; |
| 51 | +} |
| 52 | + |
| 53 | +- (void)controller:(MTRDeviceController *)controller commissioningSessionEstablishmentDone:(NSError *)error |
| 54 | +{ |
| 55 | + XCTAssertEqual(error.code, 0); |
| 56 | + |
| 57 | + NSError * commissionError = nil; |
| 58 | + [sController commissionNodeWithID:@(kDeviceId) |
| 59 | + commissioningParams:[[MTRCommissioningParameters alloc] init] |
| 60 | + error:&commissionError]; |
| 61 | + XCTAssertNil(commissionError); |
| 62 | + |
| 63 | + // Keep waiting for controller:commissioningComplete: |
| 64 | +} |
| 65 | + |
| 66 | +- (void)controller:(MTRDeviceController *)controller commissioningComplete:(NSError *)error |
| 67 | +{ |
| 68 | + XCTAssertNotNil(error); |
| 69 | + XCTAssertEqual(error.domain, MTRErrorDomain); |
| 70 | + XCTAssertEqual(error.code, MTRErrorCodeIntegrityCheckFailed); |
| 71 | + [_expectation fulfill]; |
| 72 | + _expectation = nil; |
| 73 | +} |
| 74 | + |
| 75 | +@end |
| 76 | + |
| 77 | +@interface OperationalCertificateIssuer : NSObject <MTROperationalCertificateIssuer> |
| 78 | +@property (nonatomic, readonly) BOOL shouldSkipAttestationCertificateValidation; |
| 79 | +@end |
| 80 | + |
| 81 | +@implementation OperationalCertificateIssuer |
| 82 | + |
| 83 | +- (instancetype)init |
| 84 | +{ |
| 85 | + if (self = [super init]) { |
| 86 | + _shouldSkipAttestationCertificateValidation = NO; |
| 87 | + } |
| 88 | + return self; |
| 89 | +} |
| 90 | + |
| 91 | +- (void)issueOperationalCertificateForRequest:(MTROperationalCSRInfo *)csrInfo |
| 92 | + attestationInfo:(MTRAttestationInfo *)attestationInfo |
| 93 | + controller:(MTRDeviceController *)controller |
| 94 | + completion:(MTROperationalCertificateIssuedHandler)completion |
| 95 | +{ |
| 96 | + XCTAssertNotNil(csrInfo); |
| 97 | + XCTAssertNotNil(attestationInfo); |
| 98 | + XCTAssertEqual(controller, sController); |
| 99 | + |
| 100 | + completion(nil, [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeIntegrityCheckFailed userInfo:nil]); |
| 101 | +} |
| 102 | + |
| 103 | +@end |
| 104 | + |
| 105 | +@interface MTROperationalCertificateIssuerTests : XCTestCase |
| 106 | +@end |
| 107 | + |
| 108 | +@implementation MTROperationalCertificateIssuerTests |
| 109 | + |
| 110 | +- (void)setUp |
| 111 | +{ |
| 112 | + [super setUp]; |
| 113 | + [self setContinueAfterFailure:NO]; |
| 114 | +} |
| 115 | + |
| 116 | +- (void)testFailedCertificateIssuance |
| 117 | +{ |
| 118 | + XCTestExpectation * expectation = [self expectationWithDescription:@"Pairing Complete"]; |
| 119 | + |
| 120 | + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; |
| 121 | + XCTAssertNotNil(factory); |
| 122 | + |
| 123 | + __auto_type * storage = [[MTRTestStorage alloc] init]; |
| 124 | + __auto_type * factoryParams = [[MTRDeviceControllerFactoryParams alloc] initWithStorage:storage]; |
| 125 | + factoryParams.port = @(kLocalPort); |
| 126 | + |
| 127 | + BOOL ok = [factory startControllerFactory:factoryParams error:nil]; |
| 128 | + XCTAssertTrue(ok); |
| 129 | + |
| 130 | + __auto_type * testKeys = [[MTRTestKeys alloc] init]; |
| 131 | + XCTAssertNotNil(testKeys); |
| 132 | + |
| 133 | + sTestKeys = testKeys; |
| 134 | + |
| 135 | + __auto_type * certificateIssuer = [[OperationalCertificateIssuer alloc] init]; |
| 136 | + |
| 137 | + // Needs to match what startControllerOnExistingFabric calls elsewhere in |
| 138 | + // this file do. |
| 139 | + __auto_type * params = [[MTRDeviceControllerStartupParams alloc] initWithIPK:testKeys.ipk fabricID:@(1) nocSigner:testKeys]; |
| 140 | + params.vendorID = @(kTestVendorId); |
| 141 | + params.operationalCertificateIssuer = certificateIssuer; |
| 142 | + params.operationalCertificateIssuerQueue = dispatch_get_main_queue(); |
| 143 | + |
| 144 | + MTRDeviceController * controller = [factory createControllerOnNewFabric:params error:nil]; |
| 145 | + XCTAssertNotNil(controller); |
| 146 | + |
| 147 | + sController = controller; |
| 148 | + |
| 149 | + __auto_type * deviceControllerDelegate = |
| 150 | + [[MTROperationalCertificateIssureTestDeviceControllerDelegate alloc] initWithExpectation:expectation]; |
| 151 | + dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.device_controller_delegate", DISPATCH_QUEUE_SERIAL); |
| 152 | + |
| 153 | + [controller setDeviceControllerDelegate:deviceControllerDelegate queue:callbackQueue]; |
| 154 | + |
| 155 | + NSError * error; |
| 156 | + __auto_type * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:kOnboardingPayload error:&error]; |
| 157 | + XCTAssertNotNil(payload); |
| 158 | + XCTAssertNil(error); |
| 159 | + |
| 160 | + [controller setupCommissioningSessionWithPayload:payload newNodeID:@(kDeviceId) error:&error]; |
| 161 | + XCTAssertNil(error); |
| 162 | + |
| 163 | + [self waitForExpectationsWithTimeout:kPairingTimeoutInSeconds handler:nil]; |
| 164 | + |
| 165 | + [controller shutdown]; |
| 166 | + XCTAssertFalse([controller isRunning]); |
| 167 | + |
| 168 | + [factory stopControllerFactory]; |
| 169 | +} |
| 170 | + |
| 171 | +@end |
0 commit comments