-
Notifications
You must be signed in to change notification settings - Fork 306
/
GTLRBase64Test.m
98 lines (79 loc) · 3.31 KB
/
GTLRBase64Test.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Copyright (c) 2012 Google Inc.
*
* 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 <GoogleAPIClientForREST/GTLRBase64.h>
@interface GTLRBase64Test : XCTestCase
@end
@implementation GTLRBase64Test
- (void)testBase64Conversion {
NSMutableString *testStr = [NSMutableString string];
for (unichar idx = 1; idx < 500; idx = 15) {
[testStr appendFormat:@"%C", idx];
}
NSString *testEncodedStd = @"ARAfLj1MW2p5wojCl8KmwrXDhMOTw6LDscSAxI/EnsStxLzF"
@"i8WaxanFuMaHxpbGpca0x4PHksehx7A=";
NSString *testEncodedWeb = @"ARAfLj1MW2p5wojCl8KmwrXDhMOTw6LDscSAxI_EnsStxLzF"
@"i8WaxanFuMaHxpbGpca0x4PHksehx7A=";
NSString *testEncodedWebNoPadding =
[testEncodedWeb stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"="]];
XCTAssertNotEqualObjects(testEncodedWeb, testEncodedWebNoPadding);
// Encoding
NSData *data = nil;
NSString *result = GTLREncodeBase64(data);
XCTAssertNil(result, @"nil base64 data");
data = [NSData data];
result = GTLREncodeBase64(data);
NSString *expected = @"";
XCTAssertEqualObjects(result, expected, @"empty base64 data");
NSString *str = testStr;
data = [testStr dataUsingEncoding:NSUTF8StringEncoding];
result = GTLREncodeBase64(data);
expected = testEncodedStd;
XCTAssertEqualObjects(result, expected, @"test data");
result = GTLREncodeWebSafeBase64(data);
expected = testEncodedWeb;
XCTAssertEqualObjects(result, expected, @"test data ws");
// Decoding
str = nil;
data = GTLRDecodeBase64(str);
XCTAssertNil(data, @"nil string");
str = @"kjh"; // not valid base64 (not padded correctly)
data = GTLRDecodeBase64(str);
XCTAssertNil(data, @"invalid string");
str = @"";
data = GTLRDecodeBase64(str);
XCTAssertEqualObjects(data, [NSData data], @"empty string");
str = testEncodedStd;
data = GTLRDecodeBase64(str);
NSData *expectedData = [testStr dataUsingEncoding:NSUTF8StringEncoding];
XCTAssertEqualObjects(data, expectedData, @"test string");
str = testEncodedWeb;
data = GTLRDecodeWebSafeBase64(str);
expectedData = [testStr dataUsingEncoding:NSUTF8StringEncoding];
XCTAssertEqualObjects(data, expectedData, @"test string ws");
str = testEncodedWebNoPadding;
data = GTLRDecodeWebSafeBase64(str);
expectedData = [testStr dataUsingEncoding:NSUTF8StringEncoding];
XCTAssertEqualObjects(data, expectedData);
str = @"YQ=="; // A string with two equals to ensure that is counted as valid.
data = GTLRDecodeBase64(str);
expectedData = [@"a" dataUsingEncoding:NSUTF8StringEncoding];
XCTAssertEqualObjects(data, expectedData, @"two equals");
str = @"a==="; // invalid base64 encoding, no valid encoding can have 3 '='s.
data = GTLRDecodeBase64(str);
XCTAssertNil(data, @"invalid base64 encoding");
}
@end