FIRCLSInstallIdentifierModelTests.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. - (void)testCreateUUID {
  42. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  43. FIRCLSInstallIdentifierModel *model =
  44. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  45. XCTAssertNotNil(model.installID);
  46. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  47. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  48. }
  49. - (void)testCreateUUIDAndRotate {
  50. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  51. FIRCLSInstallIdentifierModel *model =
  52. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  53. XCTAssertNotNil(model.installID);
  54. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  55. sleep(1);
  56. XCTAssertFalse(didRotate);
  57. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  58. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  59. XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
  60. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  61. }
  62. - (void)testCreateUUIDAndErrorGettingInstanceID {
  63. NSError *fakeError = [NSError errorWithDomain:NSCocoaErrorDomain code:-1 userInfo:@{}];
  64. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithError:fakeError];
  65. FIRCLSInstallIdentifierModel *model =
  66. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  67. XCTAssertNotNil(model.installID);
  68. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  69. XCTAssertFalse(didRotate);
  70. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  71. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  72. XCTAssertEqualObjects(nil, [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  73. }
  74. - (void)testCreateUUIDNoIID {
  75. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
  76. FIRCLSInstallIdentifierModel *model =
  77. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  78. XCTAssertNotNil(model.installID);
  79. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  80. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  81. XCTAssertEqualObjects(nil, [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  82. }
  83. - (void)testIIDBecomesNil {
  84. // Set up the initial state with a valid iid and uuid.
  85. [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
  86. [_defaults setObject:@"old_instance_id" forKey:FIRCLSInstallationIIDHashKey];
  87. // Initialize the model with the a nil IID.
  88. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
  89. FIRCLSInstallIdentifierModel *model =
  90. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  91. XCTAssertNotNil(model.installID);
  92. // Test that the UUID did not change. The FIID can be nil if
  93. // there's no FIID cached, so we can't say whether to regenerate
  94. XCTAssertEqualObjects(model.installID, @"old_uuid");
  95. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  96. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  97. }
  98. - (void)testIIDChanges {
  99. // Set up the initial state with a valid iid and uuid.
  100. [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
  101. [_defaults setObject:@"old_instance_id" forKey:FIRCLSInstallationIIDHashKey];
  102. // Initialize the model with the a new IID.
  103. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"new_instance_id"];
  104. FIRCLSInstallIdentifierModel *model =
  105. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  106. XCTAssertNotNil(model.installID);
  107. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  108. XCTAssertTrue(didRotate);
  109. // Test that the UUID changed.
  110. XCTAssertNotEqualObjects(model.installID, @"old_uuid");
  111. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  112. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  113. XCTAssertEqualObjects(FIRCLSTestHashOfInstanceID,
  114. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  115. }
  116. - (void)testIIDDoesntChange {
  117. // Set up the initial state with a valid iid and uuid.
  118. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  119. [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
  120. // Initialize the model with the a new IID.
  121. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  122. FIRCLSInstallIdentifierModel *model =
  123. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  124. XCTAssertNotNil(model.installID);
  125. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  126. XCTAssertFalse(didRotate);
  127. // Test that the UUID changed.
  128. XCTAssertEqualObjects(model.installID, @"test_uuid");
  129. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  130. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  131. XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
  132. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  133. }
  134. - (void)testUUIDSetButNeverIIDNilIID {
  135. // Set up the initial state with a valid iid and uuid.
  136. [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
  137. // Initialize the model with the a nil IID.
  138. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
  139. FIRCLSInstallIdentifierModel *model =
  140. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  141. XCTAssertNotNil(model.installID);
  142. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  143. XCTAssertFalse(didRotate);
  144. // Test that the UUID did not change. The FIID can be nil if
  145. // there's no FIID cached, so we can't say whether to regenerate
  146. XCTAssertEqualObjects(model.installID, @"old_uuid");
  147. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  148. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  149. XCTAssertEqualObjects([_defaults objectForKey:FIRCLSInstallationIIDHashKey], nil);
  150. }
  151. - (void)testUUIDSetButNeverIIDWithIID {
  152. // Set up the initial state with a valid iid and uuid.
  153. [_defaults setObject:@"old_uuid" forKey:FABInstallationUUIDKey];
  154. // Initialize the model with the a nil IID.
  155. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  156. FIRCLSInstallIdentifierModel *model =
  157. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  158. XCTAssertNotNil(model.installID);
  159. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  160. XCTAssertFalse(didRotate);
  161. // Test that the UUID did not change. The FIID can be nil if
  162. // there's no FIID cached, so we can't say whether to regenerate
  163. XCTAssertEqualObjects(model.installID, @"old_uuid");
  164. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  165. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  166. XCTAssertEqualObjects([_defaults objectForKey:FIRCLSInstallationIIDHashKey],
  167. FIRCLSTestHashOfTestInstanceID);
  168. }
  169. - (void)testADIDWasSetButNeverIID {
  170. // Set up the initial state with a valid adid and uuid.
  171. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  172. [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
  173. // Initialize the model with the a new IID.
  174. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:nil];
  175. FIRCLSInstallIdentifierModel *model =
  176. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  177. XCTAssertNotNil(model.installID);
  178. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  179. XCTAssertFalse(didRotate);
  180. // Test that the UUID didn't change.
  181. XCTAssertEqualObjects(model.installID, @"test_uuid");
  182. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  183. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  184. XCTAssertNil([_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  185. }
  186. - (void)testADIDWasSetAndIIDBecomesSet {
  187. // Set up the initial state with a valid adid and uuid.
  188. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  189. [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
  190. // Initialize the model with the a new IID.
  191. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  192. FIRCLSInstallIdentifierModel *model =
  193. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  194. XCTAssertNotNil(model.installID);
  195. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  196. XCTAssertFalse(didRotate);
  197. // Test that the UUID didn't change.
  198. XCTAssertEqualObjects(model.installID, @"test_uuid");
  199. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  200. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  201. XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
  202. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  203. }
  204. - (void)testADIDAndIIDWereSet {
  205. // Set up the initial state with a valid iid, adid, and uuid.
  206. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  207. [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
  208. [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
  209. // Initialize the model with the a new IID.
  210. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  211. FIRCLSInstallIdentifierModel *model =
  212. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  213. XCTAssertNotNil(model.installID);
  214. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  215. XCTAssertFalse(didRotate);
  216. // Test that the UUID didn't change.
  217. XCTAssertEqualObjects(model.installID, @"test_uuid");
  218. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  219. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  220. XCTAssertEqualObjects(FIRCLSTestHashOfTestInstanceID,
  221. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  222. }
  223. - (void)testADIDAndIIDWereSet2 {
  224. // Set up the initial state with a valid iid, adid, and uuid.
  225. [_defaults setObject:@"test_uuid" forKey:FABInstallationUUIDKey];
  226. [_defaults setObject:@"test_adid" forKey:FABInstallationADIDKey];
  227. [_defaults setObject:FIRCLSTestHashOfTestInstanceID forKey:FIRCLSInstallationIIDHashKey];
  228. // Initialize the model with the a new IID.
  229. FIRMockInstallations *iid =
  230. [[FIRMockInstallations alloc] initWithFID:@"test_changed_instance_id"];
  231. FIRCLSInstallIdentifierModel *model =
  232. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  233. XCTAssertNotNil(model.installID);
  234. BOOL didRotate = [model regenerateInstallIDIfNeeded];
  235. XCTAssertTrue(didRotate);
  236. // Test that the UUID change.
  237. XCTAssertNotEqualObjects(model.installID, @"test_uuid");
  238. XCTAssertEqualObjects([_defaults objectForKey:FABInstallationUUIDKey], model.installID);
  239. XCTAssertNil([_defaults objectForKey:FABInstallationADIDKey]);
  240. XCTAssertEqualObjects(@"f1e1e3969cd926d57448fcd02f6fd4e979739a87256a652a1781cfa0510408b3",
  241. [_defaults objectForKey:FIRCLSInstallationIIDHashKey]);
  242. }
  243. @end