From 4af77d75d7d469a85834cf3c41477de94dea48a5 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 30 May 2023 08:53:51 -0400 Subject: [PATCH] Ensure that we Platform::MemoryInit before serializing payloads on Darwin. (#26931) --- src/darwin/Framework/CHIP/MTRSetupPayload.mm | 8 +++ .../MTRSetupPayloadSerializerTests.m | 54 +++++++++++++++++++ .../Matter.xcodeproj/project.pbxproj | 4 ++ 3 files changed, 66 insertions(+) create mode 100644 src/darwin/Framework/CHIPTests/MTRSetupPayloadSerializerTests.m diff --git a/src/darwin/Framework/CHIP/MTRSetupPayload.mm b/src/darwin/Framework/CHIP/MTRSetupPayload.mm index 7cb5db8154c026..adfeb7cd3a9ac4 100644 --- a/src/darwin/Framework/CHIP/MTRSetupPayload.mm +++ b/src/darwin/Framework/CHIP/MTRSetupPayload.mm @@ -17,6 +17,7 @@ #import "MTRError.h" #import "MTRError_Internal.h" +#import "MTRFramework.h" #import "MTROnboardingPayloadParser.h" #import "MTRSetupPayload_Internal.h" #import "setup_payload/ManualSetupPayloadGenerator.h" @@ -30,6 +31,13 @@ @implementation MTRSetupPayload { chip::SetupPayload _chipSetupPayload; } ++ (void)initialize +{ + // Need to make sure we set up Platform memory stuff before we start + // serializing payloads. + MTRFrameworkInit(); +} + - (MTRDiscoveryCapabilities)convertRendezvousFlags:(const chip::Optional &)value { if (!value.HasValue()) { diff --git a/src/darwin/Framework/CHIPTests/MTRSetupPayloadSerializerTests.m b/src/darwin/Framework/CHIPTests/MTRSetupPayloadSerializerTests.m new file mode 100644 index 00000000000000..61a819f33b961f --- /dev/null +++ b/src/darwin/Framework/CHIPTests/MTRSetupPayloadSerializerTests.m @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2023 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// module headers +#import "MTRSetupPayload.h" + +// additional includes +#import "MTRError.h" + +// system dependencies +#import + +@interface MTRSetupPayloadSerializerTests : XCTestCase + +@end + +@implementation MTRSetupPayloadSerializerTests + +- (void)testSetupPayloadBasicQRCodeSerialize +{ + __auto_type * payload = [[MTRSetupPayload alloc] init]; + XCTAssertNotNil(payload); + + payload.version = @(0); + payload.vendorID = @(0xFFF1); + payload.productID = @(1); + payload.commissioningFlow = MTRCommissioningFlowStandard; + payload.discoveryCapabilities = MTRDiscoveryCapabilitiesOnNetwork; + payload.discriminator = @(0xabc); + payload.hasShortDiscriminator = NO; + payload.setupPasscode = @(12121212); + + NSError * error; + __auto_type * qrCode = [payload qrCodeString:&error]; + XCTAssertNil(error); + XCTAssertNotNil(qrCode); + XCTAssertEqualObjects(qrCode, @"MT:-24J06.H14BK9C7R900"); +} + +// Make sure to not add any tests that involve parsing setup payloads to this +// file. Those should go in MTRSetupPayloadParserTests.m. +@end diff --git a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj index e739b780e43497..d93ddc7cc9b841 100644 --- a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj +++ b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj @@ -155,6 +155,7 @@ 517BF3F0282B62B800A8B7DB /* MTRCertificates.h in Headers */ = {isa = PBXBuildFile; fileRef = 517BF3EE282B62B800A8B7DB /* MTRCertificates.h */; settings = {ATTRIBUTES = (Public, ); }; }; 517BF3F1282B62B800A8B7DB /* MTRCertificates.mm in Sources */ = {isa = PBXBuildFile; fileRef = 517BF3EF282B62B800A8B7DB /* MTRCertificates.mm */; }; 517BF3F3282B62CB00A8B7DB /* MTRCertificateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 517BF3F2282B62CB00A8B7DB /* MTRCertificateTests.m */; }; + 519498322A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 519498312A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m */; }; 51A2F1322A00402A00F03298 /* MTRDataValueParserTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 51A2F1312A00402A00F03298 /* MTRDataValueParserTests.m */; }; 51B22C1E2740CB0A008D5055 /* MTRStructsObjc.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B22C1D2740CB0A008D5055 /* MTRStructsObjc.h */; settings = {ATTRIBUTES = (Public, ); }; }; 51B22C222740CB1D008D5055 /* MTRCommandPayloadsObjc.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B22C212740CB1D008D5055 /* MTRCommandPayloadsObjc.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -445,6 +446,7 @@ 517BF3EE282B62B800A8B7DB /* MTRCertificates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRCertificates.h; sourceTree = ""; }; 517BF3EF282B62B800A8B7DB /* MTRCertificates.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRCertificates.mm; sourceTree = ""; }; 517BF3F2282B62CB00A8B7DB /* MTRCertificateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRCertificateTests.m; sourceTree = ""; }; + 519498312A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRSetupPayloadSerializerTests.m; sourceTree = ""; }; 51A2F1312A00402A00F03298 /* MTRDataValueParserTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRDataValueParserTests.m; sourceTree = ""; }; 51B22C1D2740CB0A008D5055 /* MTRStructsObjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRStructsObjc.h; sourceTree = ""; }; 51B22C212740CB1D008D5055 /* MTRCommandPayloadsObjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRCommandPayloadsObjc.h; sourceTree = ""; }; @@ -1071,6 +1073,7 @@ 5142E39729D377F000A206F0 /* MTROTAProviderTests.m */, 51A2F1312A00402A00F03298 /* MTRDataValueParserTests.m */, 51339B1E2A0DA64D00C798C1 /* MTRCertificateValidityTests.m */, + 519498312A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m */, B202529D2459E34F00F97062 /* Info.plist */, ); path = CHIPTests; @@ -1483,6 +1486,7 @@ 517BF3F3282B62CB00A8B7DB /* MTRCertificateTests.m in Sources */, 5142E39829D377F000A206F0 /* MTROTAProviderTests.m in Sources */, 51E24E73274E0DAC007CCF6E /* MTRErrorTestUtils.mm in Sources */, + 519498322A25581C00B3BABE /* MTRSetupPayloadSerializerTests.m in Sources */, 51A2F1322A00402A00F03298 /* MTRDataValueParserTests.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0;