From ceb076f32e60f4d2557d92973d4560714ccd537f Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Thu, 18 Aug 2022 18:34:20 -0700 Subject: [PATCH] Revert "Check the onboarding payload code type in MTROnboardingPayloadParser (#22024)" (#22026) This reverts commit 2a5f7fe21521014507377a8a216b9bf5e07f045c. --- .../payload/SetupPayloadParseCommand.mm | 7 +- .../CHIP/MTROnboardingPayloadParser.h | 1 + .../CHIP/MTROnboardingPayloadParser.m | 6 +- .../CHIPTests/MTRSetupPayloadParserTests.m | 71 ++++++++++++++++++- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm b/examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm index 5b98ddf03840b6..9cb8aba4fdde80 100644 --- a/examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm +++ b/examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm @@ -62,7 +62,12 @@ NSError * error; MTRSetupPayload * payload; MTROnboardingPayloadType codeType; - payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:codeString error:&error]; + if (IsQRCode(codeString)) { + codeType = MTROnboardingPayloadTypeQRCode; + } else { + codeType = MTROnboardingPayloadTypeManualCode; + } + payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:codeString ofType:codeType error:&error]; if (error) { LogNSError("Error: ", error); return CHIP_ERROR_INTERNAL; diff --git a/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.h b/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.h index a2509e233bee3f..cb812ddc41e67a 100644 --- a/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.h +++ b/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.h @@ -31,6 +31,7 @@ typedef NS_ENUM(NSUInteger, MTROnboardingPayloadType) { @interface MTROnboardingPayloadParser : NSObject + (nullable MTRSetupPayload *)setupPayloadForOnboardingPayload:(NSString *)onboardingPayload + ofType:(MTROnboardingPayloadType)type error:(NSError * __autoreleasing *)error; @end diff --git a/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.m b/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.m index 2a81d81b5f5539..3af4fe1a38bb9c 100644 --- a/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.m +++ b/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.m @@ -23,17 +23,17 @@ @implementation MTROnboardingPayloadParser + (nullable MTRSetupPayload *)setupPayloadForOnboardingPayload:(NSString *)onboardingPayload + ofType:(MTROnboardingPayloadType)type error:(NSError * __autoreleasing *)error { MTRSetupPayload * payload; - // MTROnboardingPayloadTypeNFC is of type QR code and handled same as QR code - MTROnboardingPayloadType type - = isQRCode(onboardingPayload) ? MTROnboardingPayloadTypeQRCode : MTROnboardingPayloadTypeManualCode; switch (type) { case MTROnboardingPayloadTypeManualCode: + case MTROnboardingPayloadTypeAdmin: payload = [self setupPayloadForManualCodeOnboardingPayload:onboardingPayload error:error]; break; case MTROnboardingPayloadTypeQRCode: + case MTROnboardingPayloadTypeNFC: payload = [self setupPayloadForQRCodeOnboardingPayload:onboardingPayload error:error]; break; default: diff --git a/src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m b/src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m index 67b6a9caad9c6a..aae5f60ead6575 100644 --- a/src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m +++ b/src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m @@ -38,7 +38,40 @@ @implementation MTRSetupPayloadParserTests - (void)testOnboardingPayloadParser_Manual_NoError { NSError * error; - MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015" error:&error]; + MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015" + ofType:MTROnboardingPayloadTypeManualCode + error:&error]; + + XCTAssertNotNil(payload); + XCTAssertNil(error); + + XCTAssertTrue(payload.hasShortDiscriminator); + XCTAssertEqual(payload.discriminator.unsignedIntegerValue, 10); + XCTAssertEqual(payload.setUpPINCode.unsignedIntegerValue, 123456780); + XCTAssertEqual(payload.vendorID.unsignedIntegerValue, 1); + XCTAssertEqual(payload.productID.unsignedIntegerValue, 1); + XCTAssertEqual(payload.commissioningFlow, MTRCommissioningFlowCustom); + XCTAssertEqual(payload.version.unsignedIntegerValue, 0); + XCTAssertNil(payload.rendezvousInformation); +} + +- (void)testOnboardingPayloadParser_Manual_WrongType +{ + NSError * error; + MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015" + ofType:MTROnboardingPayloadTypeQRCode + error:&error]; + + XCTAssertNil(payload); + XCTAssertEqual(error.code, MTRErrorCodeInvalidArgument); +} + +- (void)testOnboardingPayloadParser_Admin_NoError +{ + NSError * error; + MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015" + ofType:MTROnboardingPayloadTypeAdmin + error:&error]; XCTAssertNotNil(payload); XCTAssertNil(error); @@ -53,10 +86,22 @@ - (void)testOnboardingPayloadParser_Manual_NoError XCTAssertNil(payload.rendezvousInformation); } +- (void)testOnboardingPayloadParser_Admin_WrongType +{ + NSError * error; + MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015" + ofType:MTROnboardingPayloadTypeQRCode + error:&error]; + + XCTAssertNil(payload); + XCTAssertEqual(error.code, MTRErrorCodeInvalidArgument); +} + - (void)testOnboardingPayloadParser_QRCode_NoError { NSError * error; MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"MT:R5L90MP500K64J00000" + ofType:MTROnboardingPayloadTypeQRCode error:&error]; XCTAssertNotNil(payload); @@ -73,11 +118,23 @@ - (void)testOnboardingPayloadParser_QRCode_NoError XCTAssertEqual([payload.rendezvousInformation unsignedLongValue], MTRDiscoveryCapabilitiesSoftAP); } +- (void)testOnboardingPayloadParser_QRCode_WrongType +{ + NSError * error; + MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"MT:R5L90MP500K64J00000" + ofType:MTROnboardingPayloadTypeAdmin + error:&error]; + + XCTAssertNil(payload); + XCTAssertEqual(error.code, MTRErrorCodeIntegrityCheckFailed); +} + - (void)testOnboardingPayloadParser_NFC_NoError { NSError * error; MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"MT:R5L90MP500K64J0A33P0SET70.QT52B.E23-WZE0WISA0DK5N1K8SQ1RYCU1O0" + ofType:MTROnboardingPayloadTypeNFC error:&error]; XCTAssertNotNil(payload); @@ -94,6 +151,18 @@ - (void)testOnboardingPayloadParser_NFC_NoError XCTAssertEqual([payload.rendezvousInformation unsignedLongValue], MTRDiscoveryCapabilitiesSoftAP); } +- (void)testOnboardingPayloadParser_NFC_WrongType +{ + NSError * error; + MTRSetupPayload * payload = [MTROnboardingPayloadParser + setupPayloadForOnboardingPayload:@"MT:R5L90MP500K64J0A33P0SET70.QT52B.E23-WZE0WISA0DK5N1K8SQ1RYCU1O0" + ofType:MTROnboardingPayloadTypeManualCode + error:&error]; + + XCTAssertNil(payload); + XCTAssertEqual(error.code, MTRErrorCodeIntegrityCheckFailed); +} + - (void)testManualParser { NSError * error;