|
19 | 19 |
|
20 | 20 | #import <XCTest/XCTest.h>
|
21 | 21 |
|
| 22 | +#import "NewMTRDeviceOverXPC.h" |
| 23 | + |
| 24 | +#import "MTRTestKeys.h" |
| 25 | +#import "MTRTestPerControllerStorage.h" |
| 26 | + |
| 27 | +static const uint16_t kTestVendorId = 0xFFF1u; |
| 28 | + |
22 | 29 | @interface MTRDeviceXPCTests<NSXPCListenerDelegate> : XCTestCase
|
23 | 30 | @property (nonatomic, readonly, strong) NSXPCInterface * serviceInterface;
|
24 | 31 | @property (nonatomic, readonly, strong) NSXPCInterface * clientInterface;
|
25 | 32 | @property (nonatomic, readonly, strong) NSXPCListener * xpcListener;
|
| 33 | +@property (nonatomic, readonly, strong) MTRDevice * remoteDevice; |
| 34 | +@property (nonatomic, readwrite, strong) MTRDeviceController * deviceController; |
| 35 | +@property (nonatomic, readwrite, strong) NSString * controllerUUID; |
26 | 36 | @end
|
27 | 37 |
|
28 |
| -@implementation MTRDeviceXPCTests |
29 |
| - |
| 38 | +@implementation MTRDeviceXPCTests { |
| 39 | + dispatch_queue_t _storageQueue; |
| 40 | +} |
30 | 41 |
|
31 | 42 | // NSXPCListener
|
32 | 43 |
|
33 | 44 | - (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
|
34 | 45 | {
|
35 | 46 | return true;
|
36 | 47 | }
|
| 48 | + |
37 | 49 | - (instancetype)init
|
38 | 50 | {
|
39 | 51 | _serviceInterface = [MTRDevice xpcInterfaceForServerProtocol];
|
40 | 52 | _clientInterface = [MTRDevice xpcInterfaceForClientProtocol];
|
| 53 | + |
41 | 54 | return self;
|
42 | 55 | }
|
43 | 56 |
|
44 | 57 | - (void)setUp {
|
45 | 58 | self.continueAfterFailure = NO;
|
46 | 59 |
|
| 60 | + _storageQueue = dispatch_queue_create("xpc.test.storage.queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL); |
| 61 | + |
47 | 62 | _xpcListener = [NSXPCListener anonymousListener];
|
48 | 63 | _xpcListener.delegate = (id<NSXPCListenerDelegate>)self;
|
49 | 64 | _serviceInterface = [MTRDevice xpcInterfaceForServerProtocol];
|
50 | 65 | _clientInterface = [MTRDevice xpcInterfaceForClientProtocol];
|
51 | 66 |
|
52 | 67 | [_xpcListener resume];
|
53 |
| - // TODO: create remote MTRDevice? |
54 | 68 | }
|
55 | 69 |
|
56 | 70 | - (void)tearDown {
|
57 |
| - // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 71 | + _storageQueue = nil; |
| 72 | + |
58 | 73 | [_xpcListener suspend];
|
59 | 74 | _xpcListener = nil;
|
60 | 75 | }
|
61 | 76 |
|
| 77 | +- (nullable MTRDeviceController *)startControllerWithRootKeys:(MTRTestKeys *)rootKeys |
| 78 | + operationalKeys:(MTRTestKeys *)operationalKeys |
| 79 | + fabricID:(NSNumber *)fabricID |
| 80 | + nodeID:(NSNumber *)nodeID |
| 81 | + storage:(MTRTestPerControllerStorage *)storage |
| 82 | + advertiseOperational:(BOOL)advertiseOperational |
| 83 | + error:(NSError * __autoreleasing *)error |
| 84 | +{ |
| 85 | + XCTAssertTrue(error != NULL); |
| 86 | + |
| 87 | + // Specify a fixed issuerID, so we get the same cert if we use the same keys. |
| 88 | + __auto_type * root = [MTRCertificates createRootCertificate:rootKeys issuerID:@(1) fabricID:nil error:error]; |
| 89 | + XCTAssertNil(*error); |
| 90 | + XCTAssertNotNil(root); |
| 91 | + |
| 92 | + __auto_type * operational = [MTRCertificates createOperationalCertificate:rootKeys |
| 93 | + signingCertificate:root |
| 94 | + operationalPublicKey:operationalKeys.publicKey |
| 95 | + fabricID:fabricID |
| 96 | + nodeID:nodeID |
| 97 | + caseAuthenticatedTags:nil |
| 98 | + error:error]; |
| 99 | + XCTAssertNil(*error); |
| 100 | + XCTAssertNotNil(operational); |
| 101 | + |
| 102 | + __auto_type * params = [[MTRDeviceControllerExternalCertificateParameters alloc] initWithStorageDelegate:storage |
| 103 | + storageDelegateQueue:_storageQueue |
| 104 | + uniqueIdentifier:storage.controllerID |
| 105 | + ipk:rootKeys.ipk |
| 106 | + vendorID:@(kTestVendorId) |
| 107 | + operationalKeypair:operationalKeys |
| 108 | + operationalCertificate:operational |
| 109 | + intermediateCertificate:nil |
| 110 | + rootCertificate:root]; |
| 111 | + XCTAssertNotNil(params); |
| 112 | + |
| 113 | + params.shouldAdvertiseOperational = advertiseOperational; |
| 114 | + |
| 115 | + return [[MTRDeviceController alloc] initWithParameters:params error:error]; |
| 116 | +} |
| 117 | + |
62 | 118 | - (void)testExample {
|
63 |
| - // This is an example of a functional test case. |
64 |
| - // Use XCTAssert and related functions to verify your tests produce the correct results. |
65 |
| - |
| 119 | + __auto_type * factory = [MTRDeviceControllerFactory sharedInstance]; |
| 120 | + XCTAssertNotNil(factory); |
| 121 | + |
| 122 | + __auto_type * rootKeys = [[MTRTestKeys alloc] init]; |
| 123 | + XCTAssertNotNil(rootKeys); |
| 124 | + |
| 125 | + __auto_type * operationalKeys = [[MTRTestKeys alloc] init]; |
| 126 | + XCTAssertNotNil(operationalKeys); |
| 127 | + |
| 128 | + // Pick some ids that no other test will be using. |
| 129 | + NSNumber * nodeID1 = @(0x1827364554637282); |
| 130 | + NSNumber * fabricID = @(0x1122334455667788); |
| 131 | + |
| 132 | + __auto_type * storageDelegate = [[MTRTestPerControllerStorage alloc] initWithControllerID:[NSUUID UUID]]; |
| 133 | + |
| 134 | + NSError * error; |
| 135 | + MTRDeviceController * controller = [self startControllerWithRootKeys:rootKeys |
| 136 | + operationalKeys:operationalKeys |
| 137 | + fabricID:fabricID |
| 138 | + nodeID:nodeID1 |
| 139 | + storage:storageDelegate |
| 140 | + advertiseOperational:NO |
| 141 | + error:&error]; |
| 142 | + |
| 143 | + NewMTRDeviceOverXPC * xpcDevice = (NewMTRDeviceOverXPC *)[NewMTRDeviceOverXPC deviceWithNodeID:@1234 controller:controller]; |
| 144 | + |
| 145 | + [xpcDevice pingWithResultHandler:^(NSString * _Nonnull result) { |
| 146 | + NSLog(@"ping result: %@", result); |
| 147 | + }]; |
66 | 148 | }
|
67 | 149 |
|
68 | 150 | @end
|
0 commit comments