FUtilitiesTest.m 4.7 KB

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