RCNConfigSettingsTest.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseRemoteConfig/Sources/Private/RCNConfigSettings.h"
  19. #import "FirebaseRemoteConfig/Sources/RCNConfigConstants.h"
  20. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  21. #import "FirebaseRemoteConfig/Tests/Unit/RCNTestUtilities.h"
  22. @interface RCNConfigSettings (ExposedTestCase)
  23. - (RCNConfigFetchRequest *)nextRequestWithUserProperties:(NSDictionary *)userProperties
  24. fetchedConfig:(NSDictionary *)fetchedConfig;
  25. - (void)updateConfigContentWithResponse:(RCNConfigFetchResponse *)response;
  26. - (void)updateFetchTimeWithSuccessFetch:(BOOL)isSuccessfulFetch;
  27. - (BOOL)hasCachedData;
  28. - (BOOL)isCachedDataFresh;
  29. @end
  30. @interface RCNConfigSettingsTest : XCTestCase {
  31. RCNConfigSettings *_mockSettings;
  32. }
  33. @end
  34. @implementation RCNConfigSettingsTest
  35. - (void)setUp {
  36. [super setUp];
  37. // Mock the read/write DB operations, which are not needed in these tests.
  38. _mockSettings = [[RCNConfigSettings alloc] initWithDatabaseManager:nil];
  39. }
  40. - (void)testCrashShouldNotHappenWithoutMainBundleID {
  41. id mockBundle = OCMPartialMock([NSBundle mainBundle]);
  42. OCMStub([NSBundle mainBundle]).andReturn(mockBundle);
  43. OCMStub([mockBundle bundleIdentifier]).andReturn(nil);
  44. _mockSettings =
  45. [[RCNConfigSettings alloc] initWithDatabaseManager:[[RCNConfigDBManager alloc] init]];
  46. [mockBundle stopMocking];
  47. }
  48. #ifdef FIX_OR_DELETE
  49. - (void)testThrottlingFresh {
  50. NSTimeInterval endTimestamp = [_mockSettings cachedDataThrottledEndTimestamp];
  51. NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
  52. XCTAssertTrue(endTimestamp <= now);
  53. // Fetch failed once.
  54. [_mockSettings updateFetchTimeWithSuccessFetch:NO];
  55. endTimestamp = [_mockSettings cachedDataThrottledEndTimestamp];
  56. now = [[NSDate date] timeIntervalSince1970];
  57. XCTAssertTrue(endTimestamp <= now);
  58. // Fetch succeeded once.
  59. [_mockSettings updateFetchTimeWithSuccessFetch:YES];
  60. endTimestamp = [_mockSettings cachedDataThrottledEndTimestamp];
  61. now = [[NSDate date] timeIntervalSince1970];
  62. XCTAssertTrue(endTimestamp <= now);
  63. // The failure fetch rate is 5. Try another 3 times, and do not go over the limit.
  64. for (int i = 0; i < 3; i++) {
  65. [_mockSettings updateFetchTimeWithSuccessFetch:NO];
  66. }
  67. endTimestamp = [_mockSettings cachedDataThrottledEndTimestamp];
  68. now = [[NSDate date] timeIntervalSince1970];
  69. XCTAssertTrue(endTimestamp <= now);
  70. // The success fetch rate is 5. Try another 4 times, which should go over the limit afterwards.
  71. for (int i = 0; i < 4; i++) {
  72. [_mockSettings updateFetchTimeWithSuccessFetch:YES];
  73. }
  74. endTimestamp = [_mockSettings cachedDataThrottledEndTimestamp];
  75. now = [[NSDate date] timeIntervalSince1970];
  76. // Now it should go over the limit.
  77. XCTAssertFalse(endTimestamp <= now);
  78. XCTAssertTrue([_mockSettings hasCachedData]);
  79. }
  80. #endif
  81. - (void)testResetDigestInNextRequest {
  82. NSDictionary *digestPerNamespace = @{@"firebase" : @"1234", @"p4" : @"5678"};
  83. [_mockSettings setNamespaceToDigest:digestPerNamespace];
  84. RCNNamedValue *firebaseDigest = [[RCNNamedValue alloc] init];
  85. firebaseDigest.name = @"firebase";
  86. firebaseDigest.value = @"1234";
  87. RCNNamedValue *p4Digest = [[RCNNamedValue alloc] init];
  88. p4Digest.name = @"p4";
  89. p4Digest.value = @"5678";
  90. // Test where each namespace's fetched config is a non-empty dictionary, request should include
  91. // the namespace's digest.
  92. NSDictionary *fetchedConfig =
  93. @{@"firebase" : @{@"a" : @"b", @"c" : @"d"}, @"p4" : @{@"p4key" : @"p4value"}};
  94. RCNConfigFetchRequest *request = [_mockSettings nextRequestWithUserProperties:nil
  95. fetchedConfig:fetchedConfig];
  96. XCTAssertEqual(request.packageDataArray.count, 1);
  97. NSArray *expectedArray = @[ firebaseDigest, p4Digest ];
  98. XCTAssertEqualObjects(request.packageDataArray[0].namespaceDigestArray, expectedArray);
  99. // Test when the namespace's fetched config doesn't exist, reset the digest by not included
  100. // in the request.
  101. fetchedConfig = @{@"firebase" : @{@"a" : @"b", @"c" : @"d"}};
  102. request = [_mockSettings nextRequestWithUserProperties:nil fetchedConfig:fetchedConfig];
  103. XCTAssertEqual(request.packageDataArray.count, 1);
  104. XCTAssertEqualObjects(request.packageDataArray[0].namespaceDigestArray, @[ firebaseDigest ]);
  105. // Test when a namespace's fetched config is empty, reset the digest.
  106. fetchedConfig = @{@"firebase" : @{@"a" : @"b", @"c" : @"d"}, @"p4" : @{}};
  107. request = [_mockSettings nextRequestWithUserProperties:nil fetchedConfig:fetchedConfig];
  108. XCTAssertEqual(request.packageDataArray.count, 1);
  109. XCTAssertEqualObjects(request.packageDataArray[0].namespaceDigestArray, @[ firebaseDigest ]);
  110. // Test when a namespace's fetched config is a invalid format (non-dictionary), reset the digest.
  111. fetchedConfig = @{@"firebase" : @{@"a" : @"b", @"c" : @"d"}, @"p4" : @[]};
  112. request = [_mockSettings nextRequestWithUserProperties:nil fetchedConfig:fetchedConfig];
  113. XCTAssertEqual(request.packageDataArray.count, 1);
  114. XCTAssertEqualObjects(request.packageDataArray[0].namespaceDigestArray, @[ firebaseDigest ]);
  115. // Test when a namespace's fetched config is a invalid format (non-dictionary), reset the digest.
  116. fetchedConfig = @{@"firebase" : @{@"a" : @"b", @"c" : @"d"}, @"p4" : @"wrong format of config"};
  117. request = [_mockSettings nextRequestWithUserProperties:nil fetchedConfig:fetchedConfig];
  118. XCTAssertEqual(request.packageDataArray.count, 1);
  119. XCTAssertEqualObjects(request.packageDataArray[0].namespaceDigestArray, @[ firebaseDigest ]);
  120. }
  121. @end