| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- // Copyright 2019 Google
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #import "Crashlytics/Crashlytics/Models/FIRCLSInstallIdentifierModel.h"
- #import <XCTest/XCTest.h>
- #import "Crashlytics/Crashlytics/FIRCLSUserDefaults/FIRCLSUserDefaults.h"
- #import "Crashlytics/UnitTests/Mocks/FIRMockInstallations.h"
- static NSString *const FABInstallationUUIDKey = @"com.crashlytics.iuuid";
- static NSString *const FABInstallationADIDKey = @"com.crashlytics.install.adid";
- static NSString *const FIRCLSInstallationIIDHashKey = @"com.crashlytics.install.iid";
- static NSString *const FIRCLSTestHashOfInstanceID =
- @"ed0cf273a55b731a50c3356e8c5a9887b96e7a1a7b233967bff23676bcea896d";
- static NSString *const FIRCLSTestHashOfTestInstanceID =
- @"a5da68191a6ce5247c37b6dc93775891b3c4fc183d9c84f7a1c8670e680b9cd4";
- @interface FIRCLSInstallIdentifierModelTests : XCTestCase {
- FIRCLSUserDefaults *_defaults;
- }
- @end
- @implementation FIRCLSInstallIdentifierModelTests
- - (void)setUp {
- _defaults = [FIRCLSUserDefaults standardUserDefaults];
- [_defaults removeObjectForKey:FABInstallationUUIDKey];
- [_defaults removeObjectForKey:FABInstallationADIDKey];
- [_defaults removeObjectForKey:FIRCLSInstallationIIDHashKey];
- }
- - (void)tearDown {
- [_defaults removeObjectForKey:FABInstallationUUIDKey];
- [_defaults removeObjectForKey:FABInstallationADIDKey];
- [_defaults removeObjectForKey:FIRCLSInstallationIIDHashKey];
- }
- - (BOOL)blockOnRegeneration:(FIRCLSInstallIdentifierModel *)model {
- XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"promise"];
- BOOL __block retDidRotate = NO;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
- [model regenerateInstallIDIfNeededWithBlock:^(BOOL didRotate) {
- retDidRotate = didRotate;
- [expectation fulfill];
- }];
- });
- [self waitForExpectations:@[ expectation ] timeout:1.0];
- return retDidRotate;
- }
- - (void)testCreateUUID {
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- }
- - (void)testCreateUUIDAndRotate {
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertFalse(didRotate);
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
- [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- - (void)testCreateUUIDAndErrorGettingInstanceID {
- NSError *fakeError = [NSError errorWithDomain:NSCocoaErrorDomain code:-1 userInfo:@{}];
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithError:fakeError];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertFalse(didRotate);
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects(nil, [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- - (void)testCreateUUIDNoIID {
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects(nil, [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- - (void)testIIDBecomesNil {
- // Set up the initial state with a valid iid and uuid.
- [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
- [_defaults setObject:@"old_instance_id" forKey:FIRCLSInstallationIIDHashKey];
- // Initialize the model with the a nil IID.
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- // Test that the UUID did not change. The FIID can be nil if
- // there's no FIID cached, so we can't say whether to regenerate
- XCTAssertEqualObjects(model.installID, @"old_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- }
- - (void)testIIDChanges {
- // Set up the initial state with a valid iid and uuid.
- [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
- [_defaults setObject:@"old_instance_id" forKey:FIRCLSInstallationIIDHashKey];
- // Initialize the model with the a new IID.
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"new_instance_id"];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertTrue(didRotate);
- // Test that the UUID changed.
- XCTAssertNotEqualObjects(model.installID, @"old_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects(FIRCLSTestHashOfInstanceID,
- [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- - (void)testIIDDoesntChange {
- // Set up the initial state with a valid iid and uuid.
- [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
- [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
- // Initialize the model with the a new IID.
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertFalse(didRotate);
- // Test that the UUID changed.
- XCTAssertEqualObjects(model.installID, @"test_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
- [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- - (void)testUUIDSetButNeverIIDNilIID {
- // Set up the initial state with a valid iid and uuid.
- [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
- // Initialize the model with the a nil IID.
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertFalse(didRotate);
- // Test that the UUID did not change. The FIID can be nil if
- // there's no FIID cached, so we can't say whether to regenerate
- XCTAssertEqualObjects(model.installID, @"old_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects([_defaults objectForKey:FIRCLSInstallationIIDHashKey], nil);
- }
- - (void)testUUIDSetButNeverIIDWithIID {
- // Set up the initial state with a valid iid and uuid.
- [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
- // Initialize the model with the a nil IID.
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertFalse(didRotate);
- // Test that the UUID did not change. The FIID can be nil if
- // there's no FIID cached, so we can't say whether to regenerate
- XCTAssertEqualObjects(model.installID, @"old_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects([_defaults objectForKey:FIRCLSInstallationIIDHashKey],
- FIRCLSTestHashOfTestInstanceID);
- }
- - (void)testADIDWasSetButNeverIID {
- // Set up the initial state with a valid adid and uuid.
- [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
- [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
- // Initialize the model with the a new IID.
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertFalse(didRotate);
- // Test that the UUID didn't change.
- XCTAssertEqualObjects(model.installID, @"test_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertNil([_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- - (void)testADIDWasSetAndIIDBecomesSet {
- // Set up the initial state with a valid adid and uuid.
- [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
- [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
- // Initialize the model with the a new IID.
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertFalse(didRotate);
- // Test that the UUID didn't change.
- XCTAssertEqualObjects(model.installID, @"test_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
- [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- - (void)testADIDAndIIDWereSet {
- // Set up the initial state with a valid iid, adid, and uuid.
- [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
- [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
- [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
- // Initialize the model with the a new IID.
- FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertFalse(didRotate);
- // Test that the UUID didn't change.
- XCTAssertEqualObjects(model.installID, @"test_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
- [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- - (void)testADIDAndIIDWereSet2 {
- // Set up the initial state with a valid iid, adid, and uuid.
- [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
- [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
- [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
- // Initialize the model with the a new IID.
- FIRMockInstallations *iid =
- [[FIRMockInstallations alloc] initWithFID:@"test_changed_instance_id"];
- FIRCLSInstallIdentifierModel *model =
- [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
- XCTAssertNotNil(model.installID);
- BOOL didRotate = [self blockOnRegeneration:model];
- XCTAssertTrue(didRotate);
- // Test that the UUID change.
- XCTAssertNotEqualObjects(model.installID, @"test_uuid");
- XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
- XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
- XCTAssertEqualObjects(@"f1e1e3969cd926d57448fcd02f6fd4e979739a87256a652a1781cfa0510408b3",
- [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
- }
- @end
|