FIRCLSInstallIdentifierModelTests.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "Crashlytics/Crashlytics/Models/FIRCLSInstallIdentifierModel.h"
  15. #import <XCTest/XCTest.h>
  16. #import "Crashlytics/Crashlytics/FIRCLSUserDefaults/FIRCLSUserDefaults.h"
  17. #import "Crashlytics/UnitTests/Mocks/FIRMockInstallations.h"
  18. static NSString *const FABInstallationUUIDKey = @"com.crashlytics.iuuid";
  19. static NSString *const FABInstallationADIDKey = @"com.crashlytics.install.adid";
  20. static NSString *const FIRCLSInstallationIIDHashKey = @"com.crashlytics.install.iid";
  21. static NSString *const FIRCLSTestHashOfInstanceID =
  22. @"ed0cf273a55b731a50c3356e8c5a9887b96e7a1a7b233967bff23676bcea896d";
  23. static NSString *const FIRCLSTestHashOfTestInstanceID =
  24. @"a5da68191a6ce5247c37b6dc93775891b3c4fc183d9c84f7a1c8670e680b9cd4";
  25. @interface FIRCLSInstallIdentifierModelTests : XCTestCase {
  26. FIRCLSUserDefaults *_defaults;
  27. }
  28. @end
  29. @implementation FIRCLSInstallIdentifierModelTests
  30. - (void)setUp {
  31. _defaults = [FIRCLSUserDefaults standardUserDefaults];
  32. [_defaults removeObjectForKey:FABInstallationUUIDKey];
  33. [_defaults removeObjectForKey:FABInstallationADIDKey];
  34. [_defaults removeObjectForKey:FIRCLSInstallationIIDHashKey];
  35. }
  36. - (void)tearDown {
  37. [_defaults removeObjectForKey:FABInstallationUUIDKey];
  38. [_defaults removeObjectForKey:FABInstallationADIDKey];
  39. [_defaults removeObjectForKey:FIRCLSInstallationIIDHashKey];
  40. }
  41. - (BOOL)blockOnRegeneration:(FIRCLSInstallIdentifierModel *)model {
  42. XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"promise"];
  43. BOOL __block retDidRotate = NO;
  44. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  45. [model regenerateInstallIDIfNeededWithBlock:^(BOOL didRotate) {
  46. retDidRotate = didRotate;
  47. [expectation fulfill];
  48. }];
  49. });
  50. [self waitForExpectations:@[ expectation ] timeout:1.0];
  51. return retDidRotate;
  52. }
  53. - (void)testCreateUUID {
  54. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  55. FIRCLSInstallIdentifierModel *model =
  56. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  57. XCTAssertNotNil(model.installID);
  58. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  59. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  60. }
  61. - (void)testCreateUUIDAndRotate {
  62. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  63. FIRCLSInstallIdentifierModel *model =
  64. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  65. XCTAssertNotNil(model.installID);
  66. BOOL didRotate = [self blockOnRegeneration:model];
  67. XCTAssertFalse(didRotate);
  68. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  69. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  70. XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
  71. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  72. }
  73. - (void)testCreateUUIDAndErrorGettingInstanceID {
  74. NSError *fakeError = [NSError errorWithDomain:NSCocoaErrorDomain code:-1 userInfo:@{}];
  75. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithError:fakeError];
  76. FIRCLSInstallIdentifierModel *model =
  77. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  78. XCTAssertNotNil(model.installID);
  79. BOOL didRotate = [self blockOnRegeneration:model];
  80. XCTAssertFalse(didRotate);
  81. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  82. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  83. XCTAssertEqualObjects(nil, [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  84. }
  85. - (void)testCreateUUIDNoIID {
  86. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
  87. FIRCLSInstallIdentifierModel *model =
  88. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  89. XCTAssertNotNil(model.installID);
  90. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  91. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  92. XCTAssertEqualObjects(nil, [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  93. }
  94. - (void)testIIDBecomesNil {
  95. // Set up the initial state with a valid iid and uuid.
  96. [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
  97. [_defaults setObject:@"old_instance_id" forKey:FIRCLSInstallationIIDHashKey];
  98. // Initialize the model with the a nil IID.
  99. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
  100. FIRCLSInstallIdentifierModel *model =
  101. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  102. XCTAssertNotNil(model.installID);
  103. // Test that the UUID did not change. The FIID can be nil if
  104. // there's no FIID cached, so we can't say whether to regenerate
  105. XCTAssertEqualObjects(model.installID, @"old_uuid");
  106. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  107. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  108. }
  109. - (void)testIIDChanges {
  110. // Set up the initial state with a valid iid and uuid.
  111. [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
  112. [_defaults setObject:@"old_instance_id" forKey:FIRCLSInstallationIIDHashKey];
  113. // Initialize the model with the a new IID.
  114. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"new_instance_id"];
  115. FIRCLSInstallIdentifierModel *model =
  116. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  117. XCTAssertNotNil(model.installID);
  118. BOOL didRotate = [self blockOnRegeneration:model];
  119. XCTAssertTrue(didRotate);
  120. // Test that the UUID changed.
  121. XCTAssertNotEqualObjects(model.installID, @"old_uuid");
  122. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  123. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  124. XCTAssertEqualObjects(FIRCLSTestHashOfInstanceID,
  125. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  126. }
  127. - (void)testIIDDoesntChange {
  128. // Set up the initial state with a valid iid and uuid.
  129. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  130. [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
  131. // Initialize the model with the a new IID.
  132. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  133. FIRCLSInstallIdentifierModel *model =
  134. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  135. XCTAssertNotNil(model.installID);
  136. BOOL didRotate = [self blockOnRegeneration:model];
  137. XCTAssertFalse(didRotate);
  138. // Test that the UUID changed.
  139. XCTAssertEqualObjects(model.installID, @"test_uuid");
  140. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  141. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  142. XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
  143. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  144. }
  145. - (void)testUUIDSetButNeverIIDNilIID {
  146. // Set up the initial state with a valid iid and uuid.
  147. [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
  148. // Initialize the model with the a nil IID.
  149. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
  150. FIRCLSInstallIdentifierModel *model =
  151. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  152. XCTAssertNotNil(model.installID);
  153. BOOL didRotate = [self blockOnRegeneration:model];
  154. XCTAssertFalse(didRotate);
  155. // Test that the UUID did not change. The FIID can be nil if
  156. // there's no FIID cached, so we can't say whether to regenerate
  157. XCTAssertEqualObjects(model.installID, @"old_uuid");
  158. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  159. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  160. XCTAssertEqualObjects([_defaults objectForKey:FIRCLSInstallationIIDHashKey], nil);
  161. }
  162. - (void)testUUIDSetButNeverIIDWithIID {
  163. // Set up the initial state with a valid iid and uuid.
  164. [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
  165. // Initialize the model with the a nil IID.
  166. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  167. FIRCLSInstallIdentifierModel *model =
  168. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  169. XCTAssertNotNil(model.installID);
  170. BOOL didRotate = [self blockOnRegeneration:model];
  171. XCTAssertFalse(didRotate);
  172. // Test that the UUID did not change. The FIID can be nil if
  173. // there's no FIID cached, so we can't say whether to regenerate
  174. XCTAssertEqualObjects(model.installID, @"old_uuid");
  175. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  176. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  177. XCTAssertEqualObjects([_defaults objectForKey:FIRCLSInstallationIIDHashKey],
  178. FIRCLSTestHashOfTestInstanceID);
  179. }
  180. - (void)testADIDWasSetButNeverIID {
  181. // Set up the initial state with a valid adid and uuid.
  182. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  183. [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
  184. // Initialize the model with the a new IID.
  185. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
  186. FIRCLSInstallIdentifierModel *model =
  187. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  188. XCTAssertNotNil(model.installID);
  189. BOOL didRotate = [self blockOnRegeneration:model];
  190. XCTAssertFalse(didRotate);
  191. // Test that the UUID didn't change.
  192. XCTAssertEqualObjects(model.installID, @"test_uuid");
  193. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  194. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  195. XCTAssertNil([_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  196. }
  197. - (void)testADIDWasSetAndIIDBecomesSet {
  198. // Set up the initial state with a valid adid and uuid.
  199. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  200. [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
  201. // Initialize the model with the a new IID.
  202. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  203. FIRCLSInstallIdentifierModel *model =
  204. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  205. XCTAssertNotNil(model.installID);
  206. BOOL didRotate = [self blockOnRegeneration:model];
  207. XCTAssertFalse(didRotate);
  208. // Test that the UUID didn't change.
  209. XCTAssertEqualObjects(model.installID, @"test_uuid");
  210. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  211. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  212. XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
  213. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  214. }
  215. - (void)testADIDAndIIDWereSet {
  216. // Set up the initial state with a valid iid, adid, and uuid.
  217. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  218. [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
  219. [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
  220. // Initialize the model with the a new IID.
  221. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  222. FIRCLSInstallIdentifierModel *model =
  223. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  224. XCTAssertNotNil(model.installID);
  225. BOOL didRotate = [self blockOnRegeneration:model];
  226. XCTAssertFalse(didRotate);
  227. // Test that the UUID didn't change.
  228. XCTAssertEqualObjects(model.installID, @"test_uuid");
  229. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  230. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  231. XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
  232. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  233. }
  234. - (void)testADIDAndIIDWereSet2 {
  235. // Set up the initial state with a valid iid, adid, and uuid.
  236. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  237. [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
  238. [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
  239. // Initialize the model with the a new IID.
  240. FIRMockInstallations *iid =
  241. [[FIRMockInstallations alloc] initWithFID:@"test_changed_instance_id"];
  242. FIRCLSInstallIdentifierModel *model =
  243. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  244. XCTAssertNotNil(model.installID);
  245. BOOL didRotate = [self blockOnRegeneration:model];
  246. XCTAssertTrue(didRotate);
  247. // Test that the UUID change.
  248. XCTAssertNotEqualObjects(model.installID, @"test_uuid");
  249. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  250. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  251. XCTAssertEqualObjects(@"f1e1e3969cd926d57448fcd02f6fd4e979739a87256a652a1781cfa0510408b3",
  252. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  253. }
  254. @end