Files
firebase-ios-sdk/Example/Database/Tests/Unit/FUtilitiesTest.m
T

152 lines
6.0 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <XCTest/XCTest.h>
#import "FClock.h"
#import "FConstants.h"
2019-08-05 13:27:47 -07:00
#import "FIRDatabaseConfig_Private.h"
#import "FIRDatabaseReference_Private.h"
#import "FIRDatabase_Private.h"
2018-11-22 06:44:42 -05:00
#import "FTestHelpers.h"
2019-08-05 13:27:47 -07:00
#import "FUtilities.h"
#import "FWebSocketConnection.h"
2017-05-15 12:27:07 -07:00
@interface FWebSocketConnection (Tests)
2019-08-05 13:27:47 -07:00
- (NSString *)userAgent;
2017-05-15 12:27:07 -07:00
@end
@interface FUtilitiesTest : XCTestCase
@end
@implementation FUtilitiesTest
- (void)testUrlWithSchema {
2019-08-05 13:27:47 -07:00
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"https://repo.firebaseio.com"];
XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"repo.firebaseio.com");
XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"repo");
XCTAssertTrue(parsedUrl.repoInfo.secure);
XCTAssertEqualObjects(parsedUrl.path, [FPath empty]);
2019-08-05 16:54:37 -07:00
parsedUrl = [FUtilities parseUrl:@"wss://repo.firebaseio.com"];
XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"repo.firebaseio.com");
XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"repo");
XCTAssertTrue(parsedUrl.repoInfo.secure);
XCTAssertEqualObjects(parsedUrl.path, [FPath empty]);
2017-05-15 12:27:07 -07:00
}
- (void)testUrlParsedWithoutSchema {
2019-08-05 13:27:47 -07:00
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com"];
XCTAssertEqualObjects(parsedUrl.repoInfo.host, @"repo.firebaseio.com");
XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"repo");
XCTAssertTrue(parsedUrl.repoInfo.secure);
XCTAssertEqualObjects(parsedUrl.path, [FPath empty]);
2017-05-15 12:27:07 -07:00
}
2019-08-05 16:54:37 -07:00
- (void)testUrlParsedUsesSpaceInsteadOfPlus {
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com/+"];
XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@"/ "]);
}
- (void)testUrlParsedWithSpecialCharacters {
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com/a%b&c@d/space: /non-ascii:ø"];
XCTAssertEqualObjects(parsedUrl.path, [FPath pathWithString:@"/a%b&c@d/space: /non-ascii:ø"]);
}
- (void)testUrlParsedWithNamespace {
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"localhost/?ns=mrschmidt"];
XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"mrschmidt");
parsedUrl = [FUtilities parseUrl:@"127.0.0.1:9000/?ns=mrschmidt"];
XCTAssertEqualObjects(parsedUrl.repoInfo.namespace, @"mrschmidt");
}
- (void)testUrlParsedWithSslDetection {
// Hosts with custom ports are considered non-secure
FParsedUrl *parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com:9000"];
XCTAssertFalse(parsedUrl.repoInfo.secure);
// Hosts with omitted ports are considered secure
parsedUrl = [FUtilities parseUrl:@"repo.firebaseio.com"];
XCTAssertTrue(parsedUrl.repoInfo.secure);
}
2017-05-15 12:27:07 -07:00
- (void)testDefaultCacheSizeIs10MB {
2019-08-05 13:27:47 -07:00
XCTAssertEqual([FTestHelpers defaultConfig].persistenceCacheSizeBytes,
(NSUInteger)10 * 1024 * 1024);
XCTAssertEqual([FTestHelpers configForName:@"test-config"].persistenceCacheSizeBytes,
(NSUInteger)10 * 1024 * 1024);
2017-05-15 12:27:07 -07:00
}
- (void)testSettingCacheSizeToHighOrToLowThrows {
2019-08-05 13:27:47 -07:00
FIRDatabaseConfig *config = [FTestHelpers configForName:@"config-tests-config"];
config.persistenceCacheSizeBytes = 5 * 1024 * 1024; // Works fine
XCTAssertThrows(config.persistenceCacheSizeBytes = (1024 * 1024 - 1));
XCTAssertThrows(config.persistenceCacheSizeBytes = 100 * 1024 * 1024 + 1);
2017-05-15 12:27:07 -07:00
}
- (void)testSystemClockMatchesCurrentTime {
2019-08-05 13:27:47 -07:00
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
// Accuracy within 10ms
XCTAssertEqualWithAccuracy(currentTime, [[FSystemClock clock] currentTime], 0.010);
2017-05-15 12:27:07 -07:00
}
// This test is here for a lack of a better place to put it
- (void)testUserAgentString {
2019-08-05 13:27:47 -07:00
FWebSocketConnection *conn = [[FWebSocketConnection alloc] init];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
NSString *agent = [conn performSelector:@selector(userAgent) withObject:nil];
2017-05-15 12:27:07 -07:00
2019-08-05 13:27:47 -07:00
NSArray *parts = [agent componentsSeparatedByString:@"/"];
XCTAssertEqual(parts.count, (NSUInteger)5);
XCTAssertEqualObjects(parts[0], @"Firebase");
XCTAssertEqualObjects(parts[1], kWebsocketProtocolVersion); // Wire protocol version
XCTAssertEqualObjects(parts[2], [FIRDatabase buildVersion]); // Build version
2017-05-15 12:27:07 -07:00
#if TARGET_OS_IPHONE
2019-08-05 13:27:47 -07:00
XCTAssertEqualObjects(parts[3], [[UIDevice currentDevice] systemVersion]); // iOS Version
NSString *deviceName = [UIDevice currentDevice].model;
XCTAssertEqualObjects([parts[4] componentsSeparatedByString:@"_"][0], deviceName);
2017-05-15 12:27:07 -07:00
#endif
}
- (void)testKeyComparison {
2019-08-05 13:27:47 -07:00
NSArray *order = @[
@"-2147483648", @"0", @"1", @"2", @"10", @"2147483647", // Treated as integers
@"-2147483649", @"-2147483650", @"-a", @"2147483648", @"21474836480", @"2147483649",
@"a" // treated as strings
];
for (NSInteger i = 0; i < order.count; i++) {
for (NSInteger j = i + 1; j < order.count; j++) {
NSString *first = order[i];
NSString *second = order[j];
XCTAssertEqual([FUtilities compareKey:first toKey:second], NSOrderedAscending,
@"Expected %@ < %@", first, second);
XCTAssertEqual([FUtilities compareKey:first toKey:first], NSOrderedSame, @"Expected %@ == %@",
first, first);
XCTAssertEqual([FUtilities compareKey:second toKey:first], NSOrderedDescending,
@"Expected %@ > %@", second, first);
2017-05-15 12:27:07 -07:00
}
2019-08-05 13:27:47 -07:00
}
2017-05-15 12:27:07 -07:00
}
// Enforce a > b, b < a, a != b, because this is apparently something that happens semi-regularly
- (void)testUnicodeKeyComparison {
XCTAssertEqual([FUtilities compareKey:@"유주연" toKey:@"윤규완오빠"], NSOrderedAscending);
XCTAssertEqual([FUtilities compareKey:@"윤규완오빠" toKey:@"유주연"], NSOrderedDescending);
XCTAssertNotEqual([FUtilities compareKey:@"윤규완오빠" toKey:@"유주연"], NSOrderedSame);
}
@end