Skip to content

Commit

Permalink
Check the onboarding payload code type in MTROnboardingPayloadParser (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
nivi-apple authored and isiu-apple committed Sep 16, 2022
1 parent 12393e2 commit 8616ba3
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@
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 @@ -31,7 +31,6 @@ typedef NS_ENUM(NSUInteger, MTROnboardingPayloadType) {
@interface MTROnboardingPayloadParser : NSObject

+ (nullable MTRSetupPayload *)setupPayloadForOnboardingPayload:(NSString *)onboardingPayload
ofType:(MTROnboardingPayloadType)type
error:(NSError * __autoreleasing *)error;

@end
Expand Down
6 changes: 3 additions & 3 deletions src/darwin/Framework/CHIP/MTROnboardingPayloadParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
71 changes: 1 addition & 70 deletions src/darwin/Framework/CHIPTests/MTRSetupPayloadParserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,7 @@ @implementation MTRSetupPayloadParserTests
- (void)testOnboardingPayloadParser_Manual_NoError
{
NSError * 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];
MTRSetupPayload * payload = [MTROnboardingPayloadParser setupPayloadForOnboardingPayload:@"636108753500001000015" error:&error];

XCTAssertNotNil(payload);
XCTAssertNil(error);
Expand All @@ -86,22 +53,10 @@ - (void)testOnboardingPayloadParser_Admin_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);
Expand All @@ -118,23 +73,11 @@ - (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);
Expand All @@ -151,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 8616ba3

Please sign in to comment.