FUtilitiesTest.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "FUtilities.h"
  18. #import "FIRDatabase_Private.h"
  19. #import "FIRDatabaseReference_Private.h"
  20. #import "FClock.h"
  21. #import "FIRDatabaseConfig_Private.h"
  22. #import "FWebSocketConnection.h"
  23. #import "FConstants.h"
  24. #import "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. }
  38. - (void)testUrlParsedWithoutSchema {
  39. FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com"];
  40. XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"repo.firebaseio.com");
  41. XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"repo");
  42. XCTAssertTrue(parsedUrl.repoInfo.secure);
  43. XCTAssertEqualObjects(parsedUrl.path, [FPath empty]);
  44. }
  45. - (void)testDefaultCacheSizeIs10MB {
  46. XCTAssertEqual([FTestHelpers defaultConfig].persistenceCacheSizeBytes, (NSUInteger)10*1024*1024);
  47. XCTAssertEqual([FTestHelpers configForName:@"test-config"].persistenceCacheSizeBytes, (NSUInteger)10*1024*1024);
  48. }
  49. - (void)testSettingCacheSizeToHighOrToLowThrows {
  50. FIRDatabaseConfig *config = [FTestHelpers configForName:@"config-tests-config"];
  51. config.persistenceCacheSizeBytes = 5*1024*1024; // Works fine
  52. XCTAssertThrows(config.persistenceCacheSizeBytes = (1024*1024-1));
  53. XCTAssertThrows(config.persistenceCacheSizeBytes = 100*1024*1024+1);
  54. }
  55. - (void)testSystemClockMatchesCurrentTime {
  56. NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
  57. // Accuracy within 10ms
  58. XCTAssertEqualWithAccuracy(currentTime, [[FSystemClock clock] currentTime], 0.010);
  59. }
  60. // This test is here for a lack of a better place to put it
  61. - (void)testUserAgentString {
  62. FWebSocketConnection *conn = [[FWebSocketConnection alloc] init];
  63. NSString *agent = [conn performSelector:@selector(userAgent) withObject:nil];
  64. NSArray *parts = [agent componentsSeparatedByString:@"/"];
  65. XCTAssertEqual(parts.count, (NSUInteger)5);
  66. XCTAssertEqualObjects(parts[0], @"Firebase");
  67. XCTAssertEqualObjects(parts[1], kWebsocketProtocolVersion); // Wire protocol version
  68. XCTAssertEqualObjects(parts[2], [FIRDatabase buildVersion]); // Build version
  69. #if TARGET_OS_IPHONE
  70. XCTAssertEqualObjects(parts[3], [[UIDevice currentDevice] systemVersion]); // iOS Version
  71. NSString *deviceName = [UIDevice currentDevice].model;
  72. XCTAssertEqualObjects([parts[4] componentsSeparatedByString:@"_"][0], deviceName);
  73. #endif
  74. }
  75. - (void)testKeyComparison {
  76. NSArray *order = @[
  77. @"-2147483648", @"0", @"1", @"2", @"10", @"2147483647", // Treated as integers
  78. @"-2147483649", @"-2147483650", @"-a", @"2147483648", @"21474836480", @"2147483649", @"a" // treated as strings
  79. ];
  80. for (NSInteger i = 0; i < order.count; i++) {
  81. for (NSInteger j = i + 1; j < order.count; j++) {
  82. NSString *first = order[i];
  83. NSString *second = order[j];
  84. XCTAssertEqual([FUtilities compareKey:first toKey:second], NSOrderedAscending,
  85. @"Expected %@ < %@", first, second);
  86. XCTAssertEqual([FUtilities compareKey:first toKey:first], NSOrderedSame,
  87. @"Expected %@ == %@", first, first);
  88. XCTAssertEqual([FUtilities compareKey:second toKey:first], NSOrderedDescending,
  89. @"Expected %@ > %@", second, first);
  90. }
  91. }
  92. }
  93. // Enforce a > b, b < a, a != b, because this is apparently something that happens semi-regularly
  94. - (void)testUnicodeKeyComparison {
  95. XCTAssertEqual([FUtilities compareKey:@"유주연" toKey:@"윤규완오빠"], NSOrderedAscending);
  96. XCTAssertEqual([FUtilities compareKey:@"윤규완오빠" toKey:@"유주연"], NSOrderedDescending);
  97. XCTAssertNotEqual([FUtilities compareKey:@"윤규완오빠" toKey:@"유주연"], NSOrderedSame);
  98. }
  99. @end