|
18 | 18 | #import <Matter/Matter.h> |
19 | 19 | #import <XCTest/XCTest.h> |
20 | 20 |
|
| 21 | +#import "MTRSetupPayload_Internal.h" |
| 22 | + |
21 | 23 | @interface MTRSetupPayloadTests : XCTestCase |
22 | 24 |
|
23 | 25 | @end |
@@ -562,4 +564,124 @@ - (void)testValidSetupPasscode |
562 | 564 | XCTAssertTrue([MTRSetupPayload isValidSetupPasscode:[MTRSetupPayload generateRandomSetupPasscode]]); |
563 | 565 | } |
564 | 566 |
|
| 567 | +- (void)testManualParser_BadCheckDigit_ReturnsIntegrityCheckFailed |
| 568 | +{ |
| 569 | + // "02684354589" is a manual pairing code with a wrong Verhoeff check digit. |
| 570 | + // It must surface as MTRErrorCodeIntegrityCheckFailed, not be flattened to |
| 571 | + // MTRErrorCodeInvalidArgument (which loses the user-actionable distinction |
| 572 | + // between "you typed a bad code" and other parse failures). |
| 573 | + NSError * error; |
| 574 | + MTRSetupPayload * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:@"02684354589" error:&error]; |
| 575 | + |
| 576 | + XCTAssertNil(payload); |
| 577 | + XCTAssertNotNil(error); |
| 578 | + XCTAssertEqualObjects(error.domain, MTRErrorDomain); |
| 579 | + XCTAssertEqual(error.code, MTRErrorCodeIntegrityCheckFailed); |
| 580 | +} |
| 581 | + |
| 582 | +- (void)testManualParser_InitWithManualPairingCodeError_BadCheckDigit |
| 583 | +{ |
| 584 | + NSError * error; |
| 585 | + MTRSetupPayload * payload = [[MTRSetupPayload alloc] initWithManualPairingCode:@"02684354589" error:&error]; |
| 586 | + |
| 587 | + XCTAssertNil(payload); |
| 588 | + XCTAssertNotNil(error); |
| 589 | + XCTAssertEqualObjects(error.domain, MTRErrorDomain); |
| 590 | + XCTAssertEqual(error.code, MTRErrorCodeIntegrityCheckFailed); |
| 591 | +} |
| 592 | + |
| 593 | +- (void)testManualParser_InitWithManualPairingCodeError_Valid |
| 594 | +{ |
| 595 | + NSError * error; |
| 596 | + MTRSetupPayload * payload = [[MTRSetupPayload alloc] initWithManualPairingCode:@"641286075300001000016" error:&error]; |
| 597 | + |
| 598 | + XCTAssertNotNil(payload); |
| 599 | + XCTAssertNil(error); |
| 600 | + XCTAssertTrue(payload.hasShortDiscriminator); |
| 601 | + XCTAssertEqual(payload.discriminator.unsignedIntegerValue, 10); |
| 602 | + XCTAssertEqual(payload.setupPasscode.unsignedIntegerValue, 12345670); |
| 603 | +} |
| 604 | + |
| 605 | +- (void)testManualParser_PopulatePayload_BadCheckDigit_ReturnsIntegrityCheckFailed |
| 606 | +{ |
| 607 | + // Cover the MTRManualSetupPayloadParser entry point too, so a regression |
| 608 | + // that bypasses MTRSetupPayload's new init still gets caught. |
| 609 | + MTRManualSetupPayloadParser * parser = |
| 610 | + [[MTRManualSetupPayloadParser alloc] initWithDecimalStringRepresentation:@"02684354589"]; |
| 611 | + NSError * error; |
| 612 | + MTRSetupPayload * payload = [parser populatePayload:&error]; |
| 613 | + |
| 614 | + XCTAssertNil(payload); |
| 615 | + XCTAssertNotNil(error); |
| 616 | + XCTAssertEqualObjects(error.domain, MTRErrorDomain); |
| 617 | + XCTAssertEqual(error.code, MTRErrorCodeIntegrityCheckFailed); |
| 618 | +} |
| 619 | + |
| 620 | +- (void)testOnboardingPayloadParser_Manual_BadCheckDigit_DistinguishesFromInvalidArgument |
| 621 | +{ |
| 622 | + // Regression pin: +setupPayloadWithOnboardingPayload:error: must surface |
| 623 | + // MTRErrorCodeIntegrityCheckFailed for a bad-Verhoeff manual pairing code, |
| 624 | + // and explicitly NOT flatten to MTRErrorCodeInvalidArgument (the pre-fix |
| 625 | + // behavior). Guards against re-introduction of the flattening in any |
| 626 | + // downstream call site routed through this factory. |
| 627 | + NSError * error; |
| 628 | + MTRSetupPayload * payload = [MTRSetupPayload setupPayloadWithOnboardingPayload:@"02684354589" error:&error]; |
| 629 | + |
| 630 | + XCTAssertNil(payload); |
| 631 | + XCTAssertNotNil(error); |
| 632 | + XCTAssertEqualObjects(error.domain, MTRErrorDomain); |
| 633 | + XCTAssertEqual(error.code, MTRErrorCodeIntegrityCheckFailed); |
| 634 | + XCTAssertNotEqual(error.code, MTRErrorCodeInvalidArgument); |
| 635 | +} |
| 636 | + |
| 637 | +- (void)testManualParser_BadCheckDigit_LegacyInitStillReturnsNil_AndNewInitNotFlattenedToInvalidArgument |
| 638 | +{ |
| 639 | + // Regression pin for both shapes of the API on a bad-Verhoeff code: |
| 640 | + // - Legacy -initWithManualPairingCode: must still return nil |
| 641 | + // (source-compat for callers that relied on nil-on-failure). |
| 642 | + // - New -initWithManualPairingCode:error: must surface |
| 643 | + // MTRErrorCodeIntegrityCheckFailed and explicitly NOT |
| 644 | + // MTRErrorCodeInvalidArgument (pre-fix flattening). |
| 645 | + NSString * badCode = @"02684354589"; |
| 646 | + |
| 647 | + MTRSetupPayload * legacyPayload = [[MTRSetupPayload alloc] initWithManualPairingCode:badCode]; |
| 648 | + XCTAssertNil(legacyPayload); |
| 649 | + |
| 650 | + NSError * error; |
| 651 | + MTRSetupPayload * newPayload = [[MTRSetupPayload alloc] initWithManualPairingCode:badCode error:&error]; |
| 652 | + XCTAssertNil(newPayload); |
| 653 | + XCTAssertNotNil(error); |
| 654 | + XCTAssertEqualObjects(error.domain, MTRErrorDomain); |
| 655 | + XCTAssertEqual(error.code, MTRErrorCodeIntegrityCheckFailed); |
| 656 | + XCTAssertNotEqual(error.code, MTRErrorCodeInvalidArgument); |
| 657 | +} |
| 658 | + |
| 659 | +- (void)testManualParser_InitWithManualPairingCodeError_BoundaryInputs |
| 660 | +{ |
| 661 | + // Boundary-input pin for -initWithManualPairingCode:error:. None of these |
| 662 | + // should crash, all should yield a nil payload, and any reported error |
| 663 | + // must be in MTRErrorDomain. Both &error and NULL out-param shapes are |
| 664 | + // exercised so a regression that null-derefs the out-param is caught. |
| 665 | + NSArray<NSString *> * badInputs = @[ |
| 666 | + @"", // zero-length |
| 667 | + @"0", // sub-minimum |
| 668 | + @"1234567890", // exact-length-but-invalid (10 digits, not a valid manual code) |
| 669 | + @"abcdefghijk", // non-numeric |
| 670 | + @" 02684354589 ", // whitespace-padded |
| 671 | + @"026843545890268435458902684354589026843545890", // oversized |
| 672 | + ]; |
| 673 | + |
| 674 | + for (NSString * input in badInputs) { |
| 675 | + NSError * error; |
| 676 | + MTRSetupPayload * payload = [[MTRSetupPayload alloc] initWithManualPairingCode:input error:&error]; |
| 677 | + XCTAssertNil(payload, @"input=%@ unexpectedly produced a payload", input); |
| 678 | + XCTAssertNotNil(error, @"input=%@ produced nil payload but no error", input); |
| 679 | + XCTAssertEqualObjects(error.domain, MTRErrorDomain, @"input=%@ wrong error domain", input); |
| 680 | + |
| 681 | + // NULL error out-param must not crash. |
| 682 | + MTRSetupPayload * payloadNoError = [[MTRSetupPayload alloc] initWithManualPairingCode:input error:NULL]; |
| 683 | + XCTAssertNil(payloadNoError, @"input=%@ unexpectedly produced a payload (NULL error)", input); |
| 684 | + } |
| 685 | +} |
| 686 | + |
565 | 687 | @end |
0 commit comments