Skip to content

Commit c1f15b8

Browse files
committed
a bunch of boilerplate for the device controller in test
1 parent a51e83c commit c1f15b8

File tree

5 files changed

+179
-7
lines changed

5 files changed

+179
-7
lines changed

src/darwin/Framework/CHIP/Matter.h

+1
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@
6969
#import <Matter/MTRStorage.h>
7070
#import <Matter/MTRStructsObjc.h>
7171
#import <Matter/MTRThreadOperationalDataset.h>
72+
#import <Matter/NewMTRDeviceOverXPC.h>
7273

7374
#undef MTR_INCLUDED_FROM_UMBRELLA_HEADER
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
/**
3+
* Copyright (c) 2023 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import <Matter/Matter.h>
19+
20+
//#import "MTRDeviceController+XPC.h"
21+
#import "MTRDeviceControllerOverXPC_Internal.h"
22+
#import "MTRDeviceControllerXPCConnection.h"
23+
24+
NS_ASSUME_NONNULL_BEGIN
25+
26+
@interface NewMTRDeviceOverXPC : MTRDevice<MTRDeviceXPCServerProtocol>
27+
28+
- (MTRDevice *)deviceWithNodeID:(NSNumber *)nodeID
29+
controllerOverXPC:(MTRDeviceControllerOverXPC *)controllerOverXPC MTR_UNSTABLE_API;
30+
31+
@end
32+
33+
NS_ASSUME_NONNULL_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
/**
3+
* Copyright (c) 2023 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import "NewMTRDeviceOverXPC.h"
19+
//#import "MTRLogging_Internal.h"
20+
21+
@interface NewMTRDeviceOverXPC ()
22+
@property (nonatomic, strong, readonly, nullable) id<NSCopying> controllerID;
23+
@property (nonatomic, strong, readonly) MTRDeviceControllerOverXPC * controllerOverXPC;
24+
// @property (nonatomic, readonly) NSNumber * nodeID;
25+
@property (nonatomic, strong, readonly) MTRDeviceControllerXPCConnection * xpcConnection;
26+
@end
27+
28+
@implementation NewMTRDeviceOverXPC
29+
30+
// TODO: XPC connection should live in here
31+
32+
- (MTRDevice *)deviceWithNodeID:(NSNumber *)nodeID
33+
controllerOverXPC:(MTRDeviceControllerOverXPC *)controllerOverXPC {
34+
// TODO: more implementation
35+
_controllerID = controllerOverXPC.controllerXPCID;
36+
_controllerOverXPC = controllerOverXPC;
37+
return [MTRDevice deviceWithNodeID:nodeID controller:controllerOverXPC];
38+
}
39+
40+
- (void)pingWithResultHandler:(nonnull MTRDeviceXPCServerPingResult)resultHandler {
41+
resultHandler(@"PONG!");
42+
}
43+
44+
- (void)readAttributeWithEndpointID:(nonnull NSNumber *)endpointID clusterID:(nonnull NSNumber *)clusterID attributeID:(nonnull NSNumber *)attributeID params:(MTRReadParams * _Nullable)params resultHandler:(nonnull MTRDeviceXPCServerAttributeReadResult)resultHandler {
45+
// MTR_LOG_ERROR("readAttributeWithEndpointID not implemented yet");
46+
}
47+
48+
@end

src/darwin/Framework/CHIPTests/MTRDeviceXPCTests.m

+89-7
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,132 @@
1919

2020
#import <XCTest/XCTest.h>
2121

22+
#import "NewMTRDeviceOverXPC.h"
23+
24+
#import "MTRTestKeys.h"
25+
#import "MTRTestPerControllerStorage.h"
26+
27+
static const uint16_t kTestVendorId = 0xFFF1u;
28+
2229
@interface MTRDeviceXPCTests<NSXPCListenerDelegate> : XCTestCase
2330
@property (nonatomic, readonly, strong) NSXPCInterface * serviceInterface;
2431
@property (nonatomic, readonly, strong) NSXPCInterface * clientInterface;
2532
@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;
2636
@end
2737

28-
@implementation MTRDeviceXPCTests
29-
38+
@implementation MTRDeviceXPCTests {
39+
dispatch_queue_t _storageQueue;
40+
}
3041

3142
// NSXPCListener
3243

3344
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
3445
{
3546
return true;
3647
}
48+
3749
- (instancetype)init
3850
{
3951
_serviceInterface = [MTRDevice xpcInterfaceForServerProtocol];
4052
_clientInterface = [MTRDevice xpcInterfaceForClientProtocol];
53+
4154
return self;
4255
}
4356

4457
- (void)setUp {
4558
self.continueAfterFailure = NO;
4659

60+
_storageQueue = dispatch_queue_create("xpc.test.storage.queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
61+
4762
_xpcListener = [NSXPCListener anonymousListener];
4863
_xpcListener.delegate = (id<NSXPCListenerDelegate>)self;
4964
_serviceInterface = [MTRDevice xpcInterfaceForServerProtocol];
5065
_clientInterface = [MTRDevice xpcInterfaceForClientProtocol];
5166

5267
[_xpcListener resume];
53-
// TODO: create remote MTRDevice?
5468
}
5569

5670
- (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+
5873
[_xpcListener suspend];
5974
_xpcListener = nil;
6075
}
6176

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+
62118
- (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+
}];
66148
}
67149

68150
@end

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@
301301
99AECC802798A57F00B6355B /* MTRCommissioningParameters.mm in Sources */ = {isa = PBXBuildFile; fileRef = 99AECC7F2798A57E00B6355B /* MTRCommissioningParameters.mm */; };
302302
99C65E10267282F1003402F6 /* MTRControllerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 99C65E0F267282F1003402F6 /* MTRControllerTests.m */; };
303303
99D466E12798936D0089A18F /* MTRCommissioningParameters.h in Headers */ = {isa = PBXBuildFile; fileRef = 99D466E02798936D0089A18F /* MTRCommissioningParameters.h */; settings = {ATTRIBUTES = (Public, ); }; };
304+
9BB2E93A2C58162500541534 /* NewMTRDeviceOverXPC.h in Headers */ = {isa = PBXBuildFile; fileRef = 9BB2E9382C58162500541534 /* NewMTRDeviceOverXPC.h */; settings = {ATTRIBUTES = (Public, ); }; };
305+
9BB2E93B2C58162500541534 /* NewMTRDeviceOverXPC.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BB2E9392C58162500541534 /* NewMTRDeviceOverXPC.m */; };
304306
9BD359B92C545F9E00CCFE4D /* MTRDeviceXPCTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BD359B82C545F9E00CCFE4D /* MTRDeviceXPCTests.m */; };
305307
9BD359BB2C5461DB00CCFE4D /* MTRDevice+XPC.h in Headers */ = {isa = PBXBuildFile; fileRef = 9BD359BA2C5461DB00CCFE4D /* MTRDevice+XPC.h */; settings = {ATTRIBUTES = (Public, ); }; };
306308
9BD359BF2C5462B800CCFE4D /* MTRDevice+XPC.m in Sources */ = {isa = PBXBuildFile; fileRef = 9BD359BE2C5462AE00CCFE4D /* MTRDevice+XPC.m */; };
@@ -730,6 +732,8 @@
730732
99AECC7F2798A57E00B6355B /* MTRCommissioningParameters.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRCommissioningParameters.mm; sourceTree = "<group>"; };
731733
99C65E0F267282F1003402F6 /* MTRControllerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MTRControllerTests.m; sourceTree = "<group>"; };
732734
99D466E02798936D0089A18F /* MTRCommissioningParameters.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRCommissioningParameters.h; sourceTree = "<group>"; };
735+
9BB2E9382C58162500541534 /* NewMTRDeviceOverXPC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NewMTRDeviceOverXPC.h; sourceTree = "<group>"; };
736+
9BB2E9392C58162500541534 /* NewMTRDeviceOverXPC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NewMTRDeviceOverXPC.m; sourceTree = "<group>"; };
733737
9BD359B82C545F9E00CCFE4D /* MTRDeviceXPCTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MTRDeviceXPCTests.m; sourceTree = "<group>"; };
734738
9BD359BA2C5461DB00CCFE4D /* MTRDevice+XPC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MTRDevice+XPC.h"; sourceTree = "<group>"; };
735739
9BD359BE2C5462AE00CCFE4D /* MTRDevice+XPC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "MTRDevice+XPC.m"; sourceTree = "<group>"; };
@@ -1339,6 +1343,8 @@
13391343
51565CB32A7AD78D00469F18 /* MTRDeviceControllerStorageDelegate.h */,
13401344
5A6FEC9427B5976200F25F42 /* MTRDeviceControllerXPCConnection.h */,
13411345
5A6FEC9527B5983000F25F42 /* MTRDeviceControllerXPCConnection.mm */,
1346+
9BB2E9382C58162500541534 /* NewMTRDeviceOverXPC.h */,
1347+
9BB2E9392C58162500541534 /* NewMTRDeviceOverXPC.m */,
13421348
5A6FEC8B27B5609C00F25F42 /* MTRDeviceOverXPC.h */,
13431349
5A6FEC9727B5C6AF00F25F42 /* MTRDeviceOverXPC.mm */,
13441350
754784632BFE65B70089C372 /* MTRDeviceStorageBehaviorConfiguration.h */,
@@ -1657,6 +1663,7 @@
16571663
998F286D26D55E10001846C6 /* MTRKeypair.h in Headers */,
16581664
1ED276E426C5832500547A89 /* MTRCluster.h in Headers */,
16591665
3D843711294977000070D20A /* NSStringSpanConversion.h in Headers */,
1666+
9BB2E93A2C58162500541534 /* NewMTRDeviceOverXPC.h in Headers */,
16601667
B4FCD56A2B5EDBD300832859 /* MTRDiagnosticLogsType.h in Headers */,
16611668
5A6FEC9A27B5C89300F25F42 /* MTRDeviceControllerXPCConnection.h in Headers */,
16621669
5129BCFD26A9EE3300122DDF /* MTRError.h in Headers */,
@@ -2010,6 +2017,7 @@
20102017
88EBF8CF27FABDD500686BC1 /* MTRDeviceAttestationDelegateBridge.mm in Sources */,
20112018
5A6FEC9827B5C6AF00F25F42 /* MTRDeviceOverXPC.mm in Sources */,
20122019
514654492A72F9DF00904E61 /* MTRDemuxingStorage.mm in Sources */,
2020+
9BB2E93B2C58162500541534 /* NewMTRDeviceOverXPC.m in Sources */,
20132021
1E4D655229C30A8700BC3478 /* MTRCommissionableBrowser.mm in Sources */,
20142022
88FA798E2B7B257100CD4B6F /* MTRMetricsCollector.mm in Sources */,
20152023
3DA1A3562ABAB3B4004F0BB9 /* MTRAsyncWorkQueue.mm in Sources */,

0 commit comments

Comments
 (0)