FIRCLSInstallIdentifierModelTests.m 13 KB

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