FUtilitiesTest.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2017 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 <XCTest/XCTest.h>
  17. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabaseReference_Private.h"
  18. #import "FirebaseDatabase/Sources/Api/Private/FIRDatabase_Private.h"
  19. #import "FirebaseDatabase/Sources/Constants/FConstants.h"
  20. #import "FirebaseDatabase/Sources/FClock.h"
  21. #import "FirebaseDatabase/Sources/FIRDatabaseConfig_Private.h"
  22. #import "FirebaseDatabase/Sources/Realtime/FWebSocketConnection.h"
  23. #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
  24. #import "FirebaseDatabase/Tests/Helpers/FTestHelpers.h"
  25. @interface FWebSocketConnection (Tests)
  26. - (NSString *)userAgent;
  27. @end
  28. @interface FUtilitiesTest : XCTestCase
  29. @end
  30. @implementation FUtilitiesTest
  31. - (void)testUrlWithSchema {
  32. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"https://repo.firebaseio.com"];
  33. XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"repo.firebaseio.com");
  34. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"repo");
  35. XCTAssertTrue(parsedUrl.repoInfo.secure);
  36. XCTAssertEqualObjects(parsedUrl.path, [FPath empty]);
  37. parsedUrl = [FUtilities parseUrl:@"wss://repo.firebaseio.com"];
  38. XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"repo.firebaseio.com");
  39. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"repo");
  40. XCTAssertTrue(parsedUrl.repoInfo.secure);
  41. XCTAssertEqualObjects(parsedUrl.path, [FPath empty]);
  42. }
  43. - (void)testUrlParsedWithoutSchema {
  44. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com"];
  45. XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"repo.firebaseio.com");
  46. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"repo");
  47. XCTAssertTrue(parsedUrl.repoInfo.secure);
  48. XCTAssertEqualObjects(parsedUrl.path, [FPath empty]);
  49. }
  50. - (void)testUrlParsedUsesSpaceInsteadOfPlus {
  51. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com/+"];
  52. XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@"/ "]);
  53. }
  54. - (void)testUrlParsedWithSpecialCharacters {
  55. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com/a%b&c@d/space: /non-ascii:ø"];
  56. XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@"/a%b&c@d/space: /non-ascii:ø"]);
  57. }
  58. - (void)testUrlParsedWithNamespace {
  59. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"localhost/?ns=mrschmidt"];
  60. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"mrschmidt");
  61. parsedUrl = [FUtilities parseUrl:@"127.0.0.1:9000/?ns=mrschmidt"];
  62. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"mrschmidt");
  63. }
  64. - (void)testUrlParsedWithSslDetection {
  65. // Hosts with custom ports are considered non-secure
  66. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com:9000"];
  67. XCTAssertFalse(parsedUrl.repoInfo.secure);
  68. // Hosts with omitted ports are considered secure
  69. parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com"];
  70. XCTAssertTrue(parsedUrl.repoInfo.secure);
  71. }
  72. - (void)testUrlParsedWithPathPartOfHost {
  73. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"https://sample.firebaseio.com/a"];
  74. XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"sample.firebaseio.com");
  75. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"sample");
  76. XCTAssertTrue(parsedUrl.repoInfo.secure);
  77. XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@"a"]);
  78. }
  79. - (void)testUrlParsedWithPathPartOfHost2 {
  80. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"https://sample.firebaseio.com/"];
  81. XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"sample.firebaseio.com");
  82. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"sample");
  83. XCTAssertTrue(parsedUrl.repoInfo.secure);
  84. XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@""]);
  85. }
  86. - (void)testUrlParsedWithPathPartOfHost3 {
  87. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"https://sample.firebaseio.com"];
  88. XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"sample.firebaseio.com");
  89. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"sample");
  90. XCTAssertTrue(parsedUrl.repoInfo.secure);
  91. XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@""]);
  92. }
  93. - (void)testDefaultCacheSizeIs10MB {
  94. XCTAssertEqual([FTestHelpers defaultConfig].persistenceCacheSizeBytes,
  95. (NSUInteger)10 * 1024 * 1024);
  96. XCTAssertEqual([FTestHelpers configForName:@"test-config"].persistenceCacheSizeBytes,
  97. (NSUInteger)10 * 1024 * 1024);
  98. }
  99. - (void)testSettingCacheSizeToHighOrToLowThrows {
  100. FIRDatabaseConfig *config = [FTestHelpers configForName:@"config-tests-config"];
  101. config.persistenceCacheSizeBytes = 5 * 1024 * 1024; // Works fine
  102. XCTAssertThrows(config.persistenceCacheSizeBytes = (1024 * 1024 - 1));
  103. XCTAssertThrows(config.persistenceCacheSizeBytes = 100 * 1024 * 1024 + 1);
  104. }
  105. - (void)testSystemClockMatchesCurrentTime {
  106. NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
  107. // Accuracy within 10ms
  108. XCTAssertEqualWithAccuracy(currentTime, [[FSystemClock clock] currentTime], 0.010);
  109. }
  110. // This test is here for a lack of a better place to put it
  111. - (void)testUserAgentString {
  112. FWebSocketConnection *conn = [[FWebSocketConnection alloc] init];
  113. NSString *agent = [conn performSelector:@selector(userAgent) withObject:nil];
  114. NSArray *parts = [agent componentsSeparatedByString:@"/"];
  115. XCTAssertEqual(parts.count, (NSUInteger)5);
  116. XCTAssertEqualObjects(parts[0], @"Firebase");
  117. XCTAssertEqualObjects(parts[1], kWebsocketProtocolVersion); // Wire protocol version
  118. XCTAssertEqualObjects(parts[2], [FIRDatabase buildVersion]); // Build version
  119. #if TARGET_OS_IPHONE && !TARGET_OS_WATCH
  120. XCTAssertEqualObjects(parts[3], [[UIDevice currentDevice] systemVersion]); // iOS Version
  121. NSString *deviceName = [UIDevice currentDevice].model;
  122. XCTAssertEqualObjects([parts[4] componentsSeparatedByString:@"_"][0], deviceName);
  123. #endif
  124. }
  125. - (void)testKeyComparison {
  126. NSArray *order = @[
  127. @"-2147483648", @"0", @"1", @"2", @"10", @"2147483647", // Treated as integers
  128. @"-2147483649", @"-2147483650", @"-a", @"2147483648", @"21474836480", @"2147483649",
  129. @"a" // treated as strings
  130. ];
  131. for (NSInteger i = 0; i < order.count; i++) {
  132. for (NSInteger j = i + 1; j < order.count; j++) {
  133. NSString *first = order[i];
  134. NSString *second = order[j];
  135. XCTAssertEqual([FUtilities compareKey:first toKey:second], NSOrderedAscending,
  136. @"Expected %@ < %@", first, second);
  137. XCTAssertEqual([FUtilities compareKey:first toKey:first], NSOrderedSame, @"Expected %@ == %@",
  138. first, first);
  139. XCTAssertEqual([FUtilities compareKey:second toKey:first], NSOrderedDescending,
  140. @"Expected %@ > %@", second, first);
  141. }
  142. }
  143. }
  144. // Enforce a > b, b < a, a != b, because this is apparently something that happens semi-regularly
  145. - (void)testUnicodeKeyComparison {
  146. XCTAssertEqual([FUtilities compareKey:@"유주연" toKey:@"윤규완오빠"], NSOrderedAscending);
  147. XCTAssertEqual([FUtilities compareKey:@"윤규완오빠" toKey:@"유주연"], NSOrderedDescending);
  148. XCTAssertNotEqual([FUtilities compareKey:@"윤규완오빠" toKey:@"유주연"], NSOrderedSame);
  149. }
  150. @end