Skip to content

Commit

Permalink
Check the onboarding payload code type in MTROnboardingPayloadParser
Browse files Browse the repository at this point in the history
  • Loading branch information
nivi-apple committed Sep 1, 2022
1 parent 686547c commit 523e5d3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/darwin/Framework/CHIP/MTROnboardingPayloadParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/darwin/Framework/CHIP/MTROnboardingPayloadParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 1 addition & 39 deletions src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 523e5d3

Please sign in to comment.