Skip to content

Commit 6cf976d

Browse files
bzbarsky-appleshgutte
authored andcommitted
Start a few more helper apps as part of MTRCommissionableBrowserTests. (project-chip#35386)
Lets us test that we properly see more than one thing that's commissionable.
1 parent eefbef6 commit 6cf976d

File tree

6 files changed

+97
-20
lines changed

6 files changed

+97
-20
lines changed

src/darwin/Framework/CHIPTests/MTRCommissionableBrowserTests.m

+23-10
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,21 @@
1717

1818
#import <Matter/Matter.h>
1919

20-
// system dependencies
21-
#import <XCTest/XCTest.h>
22-
20+
#import "MTRTestCase.h"
2321
#import "MTRTestKeys.h"
22+
#import "MTRTestServerAppRunner.h"
2423
#import "MTRTestStorage.h"
2524

2625
// Fixture 1: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1
27-
// Fixture 2: chip-all-clusters-app --KVS "$(mktemp -t chip-test-kvs)" --interface-id -1 \
28-
--dac_provider credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json \
29-
--product-id 32768 --discriminator 3839
3026

3127
static const uint16_t kLocalPort = 5541;
3228
static const uint16_t kTestVendorId = 0xFFF1u;
33-
static const __auto_type kTestProductIds = @[ @(0x8001u) ];
34-
static const __auto_type kTestDiscriminators = @[ @(3840u) ];
29+
static const __auto_type kTestProductIds = @[ @(0x8000u), @(0x8001u) ];
30+
static const __auto_type kTestDiscriminators = @[ @(2000), @(3839u), @(3840u) ];
3531
static const uint16_t kDiscoverDeviceTimeoutInSeconds = 10;
36-
static const uint16_t kExpectedDiscoveredDevicesCount = 1;
32+
static const uint16_t kExpectedDiscoveredDevicesCount = 3;
33+
34+
static bool sHelperAppsStarted = false;
3735

3836
// Singleton controller we use.
3937
static MTRDeviceController * sController = nil;
@@ -113,7 +111,7 @@ - (void)controller:(MTRDeviceController *)controller didRemoveCommissionableDevi
113111
}
114112
@end
115113

116-
@interface MTRCommissionableBrowserTests : XCTestCase
114+
@interface MTRCommissionableBrowserTests : MTRTestCase
117115
@end
118116

119117
@implementation MTRCommissionableBrowserTests
@@ -159,6 +157,21 @@ + (void)tearDown
159157
- (void)setUp
160158
{
161159
[super setUp];
160+
161+
if (!sHelperAppsStarted) {
162+
for (NSString * payload in @[
163+
@"MT:Y.K90SO527JA0648G00",
164+
@"MT:-24J0AFN00I40648G00",
165+
]) {
166+
__auto_type * appRunner = [[MTRTestServerAppRunner alloc] initCrossTestWithAppName:@"all-clusters"
167+
arguments:@[]
168+
payload:payload
169+
testcase:self];
170+
XCTAssertNotNil(appRunner);
171+
}
172+
sHelperAppsStarted = true;
173+
}
174+
162175
[self setContinueAfterFailure:NO];
163176
}
164177

src/darwin/Framework/CHIPTests/MTRPairingTests.m

-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ - (void)startServerApp
190190
arguments:@[
191191
@"--dac_provider",
192192
[self absolutePathFor:@"credentials/development/commissioner_dut/struct_cd_origin_pid_vid_correct/test_case_vector.json"],
193-
@"--product-id",
194-
@"32768",
195193
]
196194
payload:kOnboardingPayload
197195
testcase:self];

src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.h

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ NS_ASSUME_NONNULL_BEGIN
4747
* tearDown happens.
4848
*/
4949
- (void)launchTask:(NSTask *)task;
50+
51+
/**
52+
* Launch a cross-test task. The task will be automatically terminated when the testsuite
53+
* tearDown happens.
54+
*/
55+
- (void)launchCrossTestTask:(NSTask *)task;
5056
#endif // HAVE_NSTASK
5157

5258
/**

src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCase.mm

+44-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,42 @@
1919

2020
#import "MTRTestCase.h"
2121

22+
#if HAVE_NSTASK
23+
// Tasks that are not scoped to a specific test, but rather to a specific test suite.
24+
static NSMutableSet<NSTask *> * runningCrossTestTasks;
25+
26+
static void ClearTaskSet(NSMutableSet<NSTask *> * __strong & tasks)
27+
{
28+
for (NSTask * task in tasks) {
29+
NSLog(@"Terminating task %@", task);
30+
[task terminate];
31+
}
32+
tasks = nil;
33+
}
34+
#endif // HAVE_NSTASK
35+
2236
@implementation MTRTestCase {
2337
#if HAVE_NSTASK
2438
NSMutableSet<NSTask *> * _runningTasks;
2539
#endif // NSTask
2640
}
2741

42+
+ (void)setUp
43+
{
44+
[super setUp];
45+
46+
#if HAVE_NSTASK
47+
runningCrossTestTasks = [[NSMutableSet alloc] init];
48+
#endif // HAVE_NSTASK
49+
}
50+
51+
+ (void)tearDown
52+
{
53+
#if HAVE_NSTASK
54+
ClearTaskSet(runningCrossTestTasks);
55+
#endif // HAVE_NSTASK
56+
}
57+
2858
- (void)setUp
2959
{
3060
#if HAVE_NSTASK
@@ -48,11 +78,7 @@ - (void)tearDown
4878
#endif
4979

5080
#if HAVE_NSTASK
51-
for (NSTask * task in _runningTasks) {
52-
NSLog(@"Terminating task %@", task);
53-
[task terminate];
54-
}
55-
_runningTasks = nil;
81+
ClearTaskSet(_runningTasks);
5682
#endif // HAVE_NSTASK
5783

5884
[super tearDown];
@@ -76,14 +102,26 @@ - (void)runTask:(NSTask *)task
76102
XCTAssertEqual([task terminationStatus], 0);
77103
}
78104

79-
- (void)launchTask:(NSTask *)task
105+
- (void)doLaunchTask:(NSTask *)task
80106
{
81107
NSError * launchError;
82108
[task launchAndReturnError:&launchError];
83109
XCTAssertNil(launchError);
110+
}
111+
112+
- (void)launchTask:(NSTask *)task
113+
{
114+
[self doLaunchTask:task];
84115

85116
[_runningTasks addObject:task];
86117
}
118+
119+
- (void)launchCrossTestTask:(NSTask *)task
120+
{
121+
[self doLaunchTask:task];
122+
123+
[runningCrossTestTasks addObject:task];
124+
}
87125
#endif // HAVE_NSTASK
88126

89127
- (NSString *)absolutePathFor:(NSString *)matterRootRelativePath

src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.h

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ NS_ASSUME_NONNULL_BEGIN
4545
*/
4646
- (instancetype)initWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase;
4747

48+
/**
49+
* Same thing, but initialize as a "cross test" helper, which is not killed at
50+
* the end of the current test (but is killed at the end of the current suite).
51+
*/
52+
- (instancetype)initCrossTestWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase;
53+
4854
/**
4955
* Get the unique index that will be used for the next initialization. This
5056
* allows including that index in the arguments provided.

src/darwin/Framework/CHIPTests/TestHelpers/MTRTestServerAppRunner.m

+18-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ @implementation MTRTestServerAppRunner {
3636
#endif
3737
}
3838

39-
- (instancetype)initWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase
39+
- (instancetype)initInternalWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase isCrossTest:(BOOL)isCrossTest
4040
{
4141
#if !HAVE_NSTASK
4242
XCTFail("Unable to start server app when we do not have NSTask");
@@ -75,6 +75,8 @@ - (instancetype)initWithAppName:(NSString *)name arguments:(NSArray<NSString *>
7575
[NSString stringWithFormat:@"%llu", passcode.unsignedLongLongValue],
7676
@"--KVS",
7777
[NSString stringWithFormat:@"/tmp/chip-%@-kvs%u", name, _uniqueIndex],
78+
@"--product-id",
79+
[NSString stringWithFormat:@"%u", parsedPayload.productID.unsignedShortValue],
7880
];
7981

8082
__auto_type * allArguments = [forcedArguments arrayByAddingObjectsFromArray:arguments];
@@ -90,14 +92,28 @@ - (instancetype)initWithAppName:(NSString *)name arguments:(NSArray<NSString *>
9092
_appTask.standardOutput = [NSFileHandle fileHandleForWritingAtPath:outFile];
9193
_appTask.standardError = [NSFileHandle fileHandleForWritingAtPath:errorFile];
9294

93-
[testcase launchTask:_appTask];
95+
if (isCrossTest) {
96+
[testcase launchCrossTestTask:_appTask];
97+
} else {
98+
[testcase launchTask:_appTask];
99+
}
94100

95101
NSLog(@"Started chip-%@-app (%@) with arguments %@ stdout=%@ and stderr=%@", name, _appTask, allArguments, outFile, errorFile);
96102

97103
return self;
98104
#endif // HAVE_NSTASK
99105
}
100106

107+
- (instancetype)initWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase
108+
{
109+
return [self initInternalWithAppName:name arguments:arguments payload:payload testcase:testcase isCrossTest:NO];
110+
}
111+
112+
- (instancetype)initCrossTestWithAppName:(NSString *)name arguments:(NSArray<NSString *> *)arguments payload:(NSString *)payload testcase:(MTRTestCase *)testcase
113+
{
114+
return [self initInternalWithAppName:name arguments:arguments payload:payload testcase:testcase isCrossTest:YES];
115+
}
116+
101117
+ (unsigned)nextUniqueIndex
102118
{
103119
return sAppRunnerIndex;

0 commit comments

Comments
 (0)