Skip to content

Commit aba20dd

Browse files
authoredSep 25, 2024
Merge branch 'master' into opstate_2_56_msg_event
2 parents 448d627 + e9d7b2f commit aba20dd

File tree

7 files changed

+277
-195
lines changed

7 files changed

+277
-195
lines changed
 

‎src/darwin/Framework/CHIP/MTRCertificates.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ + (BOOL)isCertificate:(MTRCertificateDERBytes)certificate1 equalTo:(MTRCertifica
186186
MTR_LOG_ERROR("Can't extract public key from second certificate: %s", ErrorStr(err));
187187
return NO;
188188
}
189-
P256PublicKeySpan keySpan2(pubKey1.ConstBytes());
189+
P256PublicKeySpan keySpan2(pubKey2.ConstBytes());
190190

191191
if (!keySpan1.data_equal(keySpan2)) {
192192
return NO;

‎src/darwin/Framework/CHIP/MTRDevice_Concrete.mm

+2-4
Original file line numberDiff line numberDiff line change
@@ -3533,6 +3533,8 @@ - (NSArray *)_getAttributesToReportWithReportedValues:(NSArray<NSDictionary<NSSt
35333533
NSNumber * dataVersion = attributeDataValue[MTRDataVersionKey];
35343534
MTRClusterPath * clusterPath = [MTRClusterPath clusterPathWithEndpointID:attributePath.endpoint clusterID:attributePath.cluster];
35353535
if (dataVersion) {
3536+
[self _noteDataVersion:dataVersion forClusterPath:clusterPath];
3537+
35363538
// Remove data version from what we cache in memory
35373539
attributeDataValue = [self _dataValueWithoutDataVersion:attributeDataValue];
35383540
}
@@ -3545,10 +3547,6 @@ - (NSArray *)_getAttributesToReportWithReportedValues:(NSArray<NSDictionary<NSSt
35453547
#endif
35463548
// Now that we have grabbed previousValue, update our cache with the attribute value.
35473549
if (readCacheValueChanged) {
3548-
if (dataVersion) {
3549-
[self _noteDataVersion:dataVersion forClusterPath:clusterPath];
3550-
}
3551-
35523550
[self _pruneStoredDataForPath:attributePath missingFrom:attributeDataValue];
35533551

35543552
if (!_deviceConfigurationChanged) {

‎src/darwin/Framework/CHIP/MTRDiagnosticLogsDownloader.mm

+3-3
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ - (void)handleBDXTransferSessionBeginForFileDesignator:(NSString *)fileDesignato
476476
abortHandler:(AbortHandler)abortHandler;
477477
{
478478
assertChipStackLockedByCurrentThread();
479-
MTR_LOG("BDX Transfer Session Begin: %@", fileDesignator);
479+
MTR_LOG("BDX Transfer Session Begin for log download: %@", fileDesignator);
480480

481481
auto * download = [_downloads get:fileDesignator fabricIndex:fabricIndex nodeID:nodeID];
482482
VerifyOrReturn(nil != download, completion([MTRError errorForCHIPErrorCode:CHIP_ERROR_NOT_FOUND]));
@@ -492,7 +492,7 @@ - (void)handleBDXTransferSessionDataForFileDesignator:(NSString *)fileDesignator
492492
completion:(MTRStatusCompletion)completion
493493
{
494494
assertChipStackLockedByCurrentThread();
495-
MTR_LOG("BDX Transfer Session Data: %@: %@", fileDesignator, data);
495+
MTR_LOG("BDX Transfer Session Data for log download: %@: %@", fileDesignator, data);
496496

497497
auto * download = [_downloads get:fileDesignator fabricIndex:fabricIndex nodeID:nodeID];
498498
VerifyOrReturn(nil != download, completion([MTRError errorForCHIPErrorCode:CHIP_ERROR_NOT_FOUND]));
@@ -510,7 +510,7 @@ - (void)handleBDXTransferSessionEndForFileDesignator:(NSString *)fileDesignator
510510
error:(NSError * _Nullable)error
511511
{
512512
assertChipStackLockedByCurrentThread();
513-
MTR_LOG("BDX Transfer Session End: %@: %@", fileDesignator, error);
513+
MTR_LOG("BDX Transfer Session End for log download: %@: %@", fileDesignator, error);
514514

515515
auto * download = [_downloads get:fileDesignator fabricIndex:fabricIndex nodeID:nodeID];
516516
VerifyOrReturn(nil != download);

‎src/darwin/Framework/CHIPTests/MTRDeviceTests.m

+79-44
Original file line numberDiff line numberDiff line change
@@ -3746,11 +3746,16 @@ - (void)test035_TestMTRDeviceSubscriptionNotEstablishedOverXPC
37463746
}
37473747

37483748
- (NSArray<NSDictionary<NSString *, id> *> *)_testAttributeReportWithValue:(unsigned int)testValue
3749+
{
3750+
return [self _testAttributeReportWithValue:testValue dataVersion:testValue];
3751+
}
3752+
3753+
- (NSArray<NSDictionary<NSString *, id> *> *)_testAttributeReportWithValue:(unsigned int)testValue dataVersion:(unsigned int)dataVersion
37493754
{
37503755
return @[ @{
37513756
MTRAttributePathKey : [MTRAttributePath attributePathWithEndpointID:@(0) clusterID:@(MTRClusterIDTypeLevelControlID) attributeID:@(MTRAttributeIDTypeClusterLevelControlAttributeCurrentLevelID)],
37523757
MTRDataKey : @ {
3753-
MTRDataVersionKey : @(testValue),
3758+
MTRDataVersionKey : @(dataVersion),
37543759
MTRTypeKey : MTRUnsignedIntegerValueType,
37553760
MTRValueKey : @(testValue),
37563761
}
@@ -3809,7 +3814,7 @@ - (void)test036_TestStorageBehaviorConfiguration
38093814
[device setDelegate:delegate queue:queue];
38103815

38113816
// Use a counter that will be incremented for each report as the value.
3812-
unsigned int currentTestValue = 1;
3817+
__block unsigned int currentTestValue = 1;
38133818

38143819
// Initial setup: Inject report and see that the attribute persisted. No delay is
38153820
// expected for the first (priming) report.
@@ -3928,54 +3933,84 @@ - (void)test036_TestStorageBehaviorConfiguration
39283933
XCTAssertLessThan(reportToPersistenceDelay, baseTestDelayTime * 2 * 5 * 1.3);
39293934

39303935
// Test 4: test reporting excessively, and see that persistence does not happen until
3931-
// reporting frequency goes back above the threshold
3932-
reportEndTime = nil;
3933-
dataPersistedTime = nil;
3934-
XCTestExpectation * dataPersisted4 = [self expectationWithDescription:@"data persisted 4"];
3935-
delegate.onClusterDataPersisted = ^{
3936-
os_unfair_lock_lock(&lock);
3937-
if (!dataPersistedTime) {
3938-
dataPersistedTime = [NSDate now];
3939-
}
3940-
os_unfair_lock_unlock(&lock);
3941-
[dataPersisted4 fulfill];
3942-
};
3943-
3944-
// Set report times with short delay and check that the multiplier is engaged
3945-
[device unitTestSetMostRecentReportTimes:[NSMutableArray arrayWithArray:@[
3946-
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 0.1 * 4)],
3947-
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 0.1 * 3)],
3948-
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 0.1 * 2)],
3949-
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 0.1)],
3950-
]]];
3951-
3952-
// Inject report that makes MTRDevice detect the device is reporting excessively
3953-
[device unitTestInjectAttributeReport:[self _testAttributeReportWithValue:currentTestValue++] fromSubscription:YES];
3936+
// reporting frequency goes back below the threshold
3937+
__auto_type excessiveReportTest = ^(unsigned int testId, NSArray<NSDictionary<NSString *, id> *> * (^reportGenerator)(void), bool expectPersistence) {
3938+
reportEndTime = nil;
3939+
dataPersistedTime = nil;
3940+
XCTestExpectation * dataPersisted = [self expectationWithDescription:[NSString stringWithFormat:@"data persisted %u", testId]];
3941+
dataPersisted.inverted = !expectPersistence;
3942+
delegate.onClusterDataPersisted = ^{
3943+
os_unfair_lock_lock(&lock);
3944+
if (!dataPersistedTime) {
3945+
dataPersistedTime = [NSDate now];
3946+
}
3947+
os_unfair_lock_unlock(&lock);
3948+
[dataPersisted fulfill];
3949+
};
39543950

3955-
// Now keep reporting excessively for base delay time max times max multiplier, plus a bit more
3956-
NSDate * excessiveStartTime = [NSDate now];
3957-
for (;;) {
3958-
usleep((useconds_t) (baseTestDelayTime * 0.1 * USEC_PER_SEC));
3959-
[device unitTestInjectAttributeReport:[self _testAttributeReportWithValue:currentTestValue++] fromSubscription:YES];
3960-
NSTimeInterval elapsed = -[excessiveStartTime timeIntervalSinceNow];
3961-
if (elapsed > (baseTestDelayTime * 2 * 5 * 1.2)) {
3962-
break;
3951+
// Set report times with short delay and check that the multiplier is engaged
3952+
[device unitTestSetMostRecentReportTimes:[NSMutableArray arrayWithArray:@[
3953+
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 0.1 * 4)],
3954+
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 0.1 * 3)],
3955+
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 0.1 * 2)],
3956+
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 0.1)],
3957+
]]];
3958+
3959+
// Inject report that makes MTRDevice detect the device is reporting excessively
3960+
[device unitTestInjectAttributeReport:reportGenerator() fromSubscription:YES];
3961+
3962+
// Now keep reporting excessively for base delay time max times max multiplier, plus a bit more
3963+
NSDate * excessiveStartTime = [NSDate now];
3964+
for (;;) {
3965+
usleep((useconds_t) (baseTestDelayTime * 0.1 * USEC_PER_SEC));
3966+
[device unitTestInjectAttributeReport:reportGenerator() fromSubscription:YES];
3967+
NSTimeInterval elapsed = -[excessiveStartTime timeIntervalSinceNow];
3968+
if (elapsed > (baseTestDelayTime * 2 * 5 * 1.2)) {
3969+
break;
3970+
}
39633971
}
3964-
}
39653972

3966-
// Check that persistence has not happened because it's now turned off
3967-
XCTAssertNil(dataPersistedTime);
3973+
// Check that persistence has not happened because it's now turned off
3974+
XCTAssertNil(dataPersistedTime);
39683975

3969-
// Now force report times to large number, to simulate time passage
3970-
[device unitTestSetMostRecentReportTimes:[NSMutableArray arrayWithArray:@[
3971-
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 10)],
3972-
]]];
3976+
// Now force report times to large number, to simulate time passage
3977+
[device unitTestSetMostRecentReportTimes:[NSMutableArray arrayWithArray:@[
3978+
[NSDate dateWithTimeIntervalSinceNow:-(baseTestDelayTime * 10)],
3979+
]]];
39733980

3974-
// And inject a report to trigger MTRDevice to recalculate that this device is no longer
3975-
// reporting excessively
3976-
[device unitTestInjectAttributeReport:[self _testAttributeReportWithValue:currentTestValue++] fromSubscription:YES];
3981+
// And inject a report to trigger MTRDevice to recalculate that this device is no longer
3982+
// reporting excessively
3983+
[device unitTestInjectAttributeReport:reportGenerator() fromSubscription:YES];
3984+
3985+
[self waitForExpectations:@[ dataPersisted ] timeout:60];
3986+
};
39773987

3978-
[self waitForExpectations:@[ dataPersisted4 ] timeout:60];
3988+
excessiveReportTest(
3989+
4, ^{
3990+
return [self _testAttributeReportWithValue:currentTestValue++];
3991+
}, true);
3992+
3993+
// Test 5: test reporting excessively with the same value and different data
3994+
// versions, and see that persistence does not happen until reporting
3995+
// frequency goes back below the threshold.
3996+
__block __auto_type dataVersion = currentTestValue;
3997+
// We incremented currentTestValue after injecting the last report. Make sure all the new
3998+
// reports use that last-reported value.
3999+
__auto_type lastReportedValue = currentTestValue - 1;
4000+
excessiveReportTest(
4001+
5, ^{
4002+
return [self _testAttributeReportWithValue:lastReportedValue dataVersion:dataVersion++];
4003+
}, true);
4004+
4005+
// Test 6: test reporting excessively with the same value and same data
4006+
// version, and see that persistence does not happen at all.
4007+
// We incremented dataVersion after injecting the last report. Make sure all the new
4008+
// reports use that last-reported value.
4009+
__block __auto_type lastReportedDataVersion = dataVersion - 1;
4010+
excessiveReportTest(
4011+
6, ^{
4012+
return [self _testAttributeReportWithValue:lastReportedValue dataVersion:lastReportedDataVersion];
4013+
}, false);
39794014

39804015
delegate.onReportEnd = nil;
39814016
delegate.onClusterDataPersisted = nil;

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

+16-5
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,26 @@ - (void)setUp
6565
- (void)tearDown
6666
{
6767
#if defined(ENABLE_LEAK_DETECTION) && ENABLE_LEAK_DETECTION
68+
/**
69+
* Unfortunately, doing this in "+ (void)tearDown" (the global suite teardown)
70+
* does not trigger a test failure even if the XCTAssertEqual below fails.
71+
*/
6872
if (_detectLeaks) {
6973
int pid = getpid();
7074
__auto_type * cmd = [NSString stringWithFormat:@"leaks %d", pid];
7175
int ret = system(cmd.UTF8String);
72-
/**
73-
* Unfortunately, doing this in "+ (void)tearDown" (the global suite teardown)
74-
* does not trigger a test failure even if the XCTAssertEqual fails.
75-
*/
76-
XCTAssertEqual(ret, 0, "LEAKS DETECTED");
76+
if (WIFEXITED(ret)) {
77+
// leaks ran to completion.
78+
XCTAssertEqual(WEXITSTATUS(ret), 0, "LEAKS DETECTED");
79+
} else {
80+
// leaks failed to actually run to completion (e.g. crashed or ran
81+
// into some other sort of failure trying to do its work). Ideally
82+
// we would fail our tests in that case, but this seems to be
83+
// happening a fair amount, and randomly, on the ARM GitHub runners.
84+
// Just log and ignore for now.
85+
XCTAssertFalse(WIFSTOPPED(ret), "Not expecting a stopped leaks");
86+
NSLog(@"Stopped by signal %d", WTERMSIG(ret));
87+
}
7788
}
7889
#endif
7990

0 commit comments

Comments
 (0)