From 523e5d3e93be7e25781ac3e6f2a061fd492740b8 Mon Sep 17 00:00:00 2001 From: Nivedita Sarkar Date: Thu, 18 Aug 2022 16:34:55 -0700 Subject: [PATCH] Check the onboarding payload code type in MTROnboardingPayloadParser --- .../payload/SetupPayloadParseCommand.mm | 8 +--- .../CHIP/MTROnboardingPayloadParser.h | 1 - .../CHIP/MTROnboardingPayloadParser.m | 10 ++++- .../CHIPTests/MTRSetupPayloadParserTests.m | 40 +------------------ 4 files changed, 10 insertions(+), 49 deletions(-) diff --git a/examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm b/examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm index 9cb8aba4fdde80..97241da2efcb84 100644 --- a/examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm +++ b/examples/darwin-framework-tool/commands/payload/SetupPayloadParseCommand.mm @@ -61,13 +61,7 @@ NSString * codeString = [NSString stringWithCString:mCode encoding:NSASCIIStringEncoding]; NSError * error; MTRSetupPayload * payload; - MTROnboardingPayloadType codeType; - if (IsQRCode(codeString)) { - codeType = MTROnboardingPayloadTypeQRCode; - } else { - codeType = MTROnboardingPayloadTypeManualCode; - } - payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:codeString ofType:codeType error:&error]; + payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:codeString 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 ce59ff7922bdec..a3edfce8e600aa 100644 --- a/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.h +++ b/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.h @@ -30,7 +30,6 @@ 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 df68311d700914..123da2918e8d5e 100644 --- a/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.m +++ b/src/darwin/Framework/CHIP/MTROnboardingPayloadParser.m @@ -22,17 +22,23 @@ @implementation MTROnboardingPayloadParser ++ (bool)isQRCode:(NSString *)codeString +{ + return [codeString hasPrefix:@"MT:"]; +} + + (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 = + [self isQRCode:onboardingPayload] ? MTROnboardingPayloadTypeQRCode : MTROnboardingPayloadTypeManualCode; switch (type) { case MTROnboardingPayloadTypeManualCode: 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 beb638f777528c..67b6a9caad9c6a 100644 --- a/src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m +++ b/src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m @@ -38,9 +38,7 @@ @implementation MTRSetupPayloadParserTests - (void)testOnboardingPayloadParser_Manual_NoError { NSError * error; - MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015" - ofType:MTROnboardingPayloadTypeManualCode - error:&error]; + MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015" error:&error]; XCTAssertNotNil(payload); XCTAssertNil(error); @@ -55,33 +53,10 @@ - (void)testOnboardingPayloadParser_Manual_NoError 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_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); @@ -103,7 +78,6 @@ - (void)testOnboardingPayloadParser_NFC_NoError NSError * error; MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"MT:R5L90MP500K64J0A33P0SET70.QT52B.E23-WZE0WISA0DK5N1K8SQ1RYCU1O0" - ofType:MTROnboardingPayloadTypeNFC error:&error]; XCTAssertNotNil(payload); @@ -120,18 +94,6 @@ - (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;