PageRenderTime 92ms CodeModel.GetById 9ms app.highlight 77ms RepoModel.GetById 1ms app.codeStats 1ms

/core/externals/google-toolbox-for-mac/Foundation/GTMStringEncodingTest.m

http://macfuse.googlecode.com/
Objective C | 309 lines | 227 code | 40 blank | 42 comment | 7 complexity | 9e37f439e8357fdfcbb02bb0788b0bd4 MD5 | raw file
  1//
  2//  GTMStringEncodingTest.m
  3//
  4//  Copyright 2010 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMSenTestCase.h"
 20#import "GTMStringEncoding.h"
 21#import "GTMUnitTestDevLog.h"
 22
 23@interface GTMStringEncodingTest : GTMTestCase
 24@end
 25
 26@implementation GTMStringEncodingTest
 27
 28// Empty inputs should result in empty outputs.
 29- (void)testEmptyInputs {
 30  GTMStringEncoding *coder = [GTMStringEncoding stringEncodingWithString:@"01"];
 31
 32  [GTMUnitTestDevLog expectString:@"Empty input"];
 33  STAssertEqualStrings([coder encode:[NSData data]], @"", nil);
 34  [GTMUnitTestDevLog expectString:@"Empty input"];
 35  STAssertEqualObjects([coder encodeString:@""], @"", nil);
 36  STAssertEqualObjects([coder decode:@""], [NSData data], nil);
 37  STAssertEqualStrings([coder stringByDecoding:@""], @"", nil);
 38}
 39
 40// Invalid inputs should result in nil outputs.
 41- (void)testInvalidInputs {
 42  GTMStringEncoding *coder = [GTMStringEncoding stringEncodingWithString:@"01"];
 43
 44  [GTMUnitTestDevLog expectString:@"unable to convert buffer to ASCII"];
 45  STAssertNil([coder decode:nil], nil);
 46  [GTMUnitTestDevLog expectString:@"Unexpected data in input pos 0"];
 47  STAssertNil([coder decode:@"banana"], nil);
 48}
 49
 50// Ignored inputs should be silently ignored.
 51- (void)testIgnoreChars {
 52  GTMStringEncoding *coder = [GTMStringEncoding stringEncodingWithString:@"01"];
 53  [coder ignoreCharacters:@" \n-"];
 54
 55  char aa = 0xaa;
 56  NSData *aaData = [NSData dataWithBytes:&aa length:sizeof(aa)];
 57  STAssertEqualObjects([coder decode:@"10101010"], aaData, nil);
 58
 59  // Inputs with ignored characters
 60  STAssertEqualObjects([coder decode:@"1010 1010"], aaData, nil);
 61  STAssertEqualObjects([coder decode:@"1010-1010"], aaData, nil);
 62  STAssertEqualObjects([coder decode:@"1010\n1010"], aaData, nil);
 63
 64  // Invalid inputs
 65  [GTMUnitTestDevLog expectString:@"Unexpected data in input pos 4"];
 66  STAssertNil([coder decode:@"1010+1010"], nil);
 67}
 68
 69#define ASSERT_ENCODE_DECODE_STRING(coder, decoded, encoded) do { \
 70  STAssertEqualStrings([coder encodeString:decoded], encoded, nil); \
 71  STAssertEqualStrings([coder stringByDecoding:encoded], decoded, nil); \
 72} while (0)
 73
 74- (void)testBinary {
 75  GTMStringEncoding *coder = [GTMStringEncoding binaryStringEncoding];
 76
 77  [GTMUnitTestDevLog expectString:@"Empty input"];
 78  ASSERT_ENCODE_DECODE_STRING(coder, @"", @"");
 79  ASSERT_ENCODE_DECODE_STRING(coder, @"f", @"01100110");
 80  ASSERT_ENCODE_DECODE_STRING(coder, @"fo", @"0110011001101111");
 81  ASSERT_ENCODE_DECODE_STRING(coder, @"foo", @"011001100110111101101111");
 82  ASSERT_ENCODE_DECODE_STRING(coder, @"foob", @"011001100110111101101111"
 83                                               "01100010");
 84  ASSERT_ENCODE_DECODE_STRING(coder, @"fooba", @"011001100110111101101111"
 85                                                "0110001001100001");
 86  ASSERT_ENCODE_DECODE_STRING(coder, @"foobar", @"011001100110111101101111"
 87                                                 "011000100110000101110010");
 88
 89  // All values, generated with:
 90  // perl -le 'print unpack "B*", join("", map chr, 0..255)'
 91  NSString *allValues = @""
 92  "0000000000000001000000100000001100000100000001010000011000000111000010000000"
 93  "1001000010100000101100001100000011010000111000001111000100000001000100010010"
 94  "0001001100010100000101010001011000010111000110000001100100011010000110110001"
 95  "1100000111010001111000011111001000000010000100100010001000110010010000100101"
 96  "0010011000100111001010000010100100101010001010110010110000101101001011100010"
 97  "1111001100000011000100110010001100110011010000110101001101100011011100111000"
 98  "0011100100111010001110110011110000111101001111100011111101000000010000010100"
 99  "0010010000110100010001000101010001100100011101001000010010010100101001001011"
100  "0100110001001101010011100100111101010000010100010101001001010011010101000101"
101  "0101010101100101011101011000010110010101101001011011010111000101110101011110"
102  "0101111101100000011000010110001001100011011001000110010101100110011001110110"
103  "1000011010010110101001101011011011000110110101101110011011110111000001110001"
104  "0111001001110011011101000111010101110110011101110111100001111001011110100111"
105  "1011011111000111110101111110011111111000000010000001100000101000001110000100"
106  "1000010110000110100001111000100010001001100010101000101110001100100011011000"
107  "1110100011111001000010010001100100101001001110010100100101011001011010010111"
108  "1001100010011001100110101001101110011100100111011001111010011111101000001010"
109  "0001101000101010001110100100101001011010011010100111101010001010100110101010"
110  "1010101110101100101011011010111010101111101100001011000110110010101100111011"
111  "0100101101011011011010110111101110001011100110111010101110111011110010111101"
112  "1011111010111111110000001100000111000010110000111100010011000101110001101100"
113  "0111110010001100100111001010110010111100110011001101110011101100111111010000"
114  "1101000111010010110100111101010011010101110101101101011111011000110110011101"
115  "1010110110111101110011011101110111101101111111100000111000011110001011100011"
116  "1110010011100101111001101110011111101000111010011110101011101011111011001110"
117  "1101111011101110111111110000111100011111001011110011111101001111010111110110"
118  "111101111111100011111001111110101111101111111100111111011111111011111111";
119  char allValuesBytes[256];
120  for (NSUInteger i = 0; i < sizeof(allValuesBytes); i++)
121    allValuesBytes[i] = i;
122  NSData *allValuesData = [NSData dataWithBytes:&allValuesBytes
123                                         length:sizeof(allValuesBytes)];
124
125  STAssertEqualObjects([coder decode:allValues], allValuesData, nil);
126  STAssertEqualStrings([coder encode:allValuesData], allValues, nil);
127}
128
129- (void)testBase64 {
130  // RFC4648 test vectors
131  GTMStringEncoding *coder = [GTMStringEncoding rfc4648Base64StringEncoding];
132
133  [GTMUnitTestDevLog expectString:@"Empty input"];
134  ASSERT_ENCODE_DECODE_STRING(coder, @"", @"");
135  ASSERT_ENCODE_DECODE_STRING(coder, @"f", @"Zg==");
136  ASSERT_ENCODE_DECODE_STRING(coder, @"fo", @"Zm8=");
137  ASSERT_ENCODE_DECODE_STRING(coder, @"foo", @"Zm9v");
138  ASSERT_ENCODE_DECODE_STRING(coder, @"foob", @"Zm9vYg==");
139  ASSERT_ENCODE_DECODE_STRING(coder, @"fooba", @"Zm9vYmE=");
140  ASSERT_ENCODE_DECODE_STRING(coder, @"foobar", @"Zm9vYmFy");
141
142  // All values, generated with:
143  // python -c 'import base64; print base64.b64encode("".join([chr(x) for x in range(0, 256)]))'
144  NSString *allValues = @""
145  "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4"
146  "OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx"
147  "cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmq"
148  "q6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj"
149  "5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==";
150  char allValuesBytes[256];
151  for (NSUInteger i = 0; i < sizeof(allValuesBytes); i++)
152    allValuesBytes[i] = i;
153  NSData *allValuesData = [NSData dataWithBytes:&allValuesBytes
154                                         length:sizeof(allValuesBytes)];
155
156  STAssertEqualObjects([coder decode:allValues], allValuesData, nil);
157  STAssertEqualStrings([coder encode:allValuesData], allValues, nil);
158}
159
160- (void)testBase64Websafe {
161  // RFC4648 test vectors
162  GTMStringEncoding *coder =
163      [GTMStringEncoding rfc4648Base64WebsafeStringEncoding];
164
165  [GTMUnitTestDevLog expectString:@"Empty input"];
166  ASSERT_ENCODE_DECODE_STRING(coder, @"", @"");
167  ASSERT_ENCODE_DECODE_STRING(coder, @"f", @"Zg==");
168  ASSERT_ENCODE_DECODE_STRING(coder, @"fo", @"Zm8=");
169  ASSERT_ENCODE_DECODE_STRING(coder, @"foo", @"Zm9v");
170  ASSERT_ENCODE_DECODE_STRING(coder, @"foob", @"Zm9vYg==");
171  ASSERT_ENCODE_DECODE_STRING(coder, @"fooba", @"Zm9vYmE=");
172  ASSERT_ENCODE_DECODE_STRING(coder, @"foobar", @"Zm9vYmFy");
173
174  // All values, generated with:
175  // python -c 'import base64; print base64.urlsafe_b64encode("".join([chr(x) for x in range(0, 256)]))'
176  NSString *allValues = @""
177  "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4"
178  "OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx"
179  "cnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmq"
180  "q6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj"
181  "5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w==";
182  char allValuesBytes[256];
183  for (NSUInteger i = 0; i < sizeof(allValuesBytes); i++)
184    allValuesBytes[i] = i;
185  NSData *allValuesData = [NSData dataWithBytes:&allValuesBytes
186                                         length:sizeof(allValuesBytes)];
187
188  STAssertEqualObjects([coder decode:allValues], allValuesData, nil);
189  STAssertEqualStrings([coder encode:allValuesData], allValues, nil);
190}
191
192- (void)testBase32 {
193  // RFC4648 test vectors
194  GTMStringEncoding *coder = [GTMStringEncoding rfc4648Base32StringEncoding];
195
196  [GTMUnitTestDevLog expectString:@"Empty input"];
197  ASSERT_ENCODE_DECODE_STRING(coder, @"", @"");
198  ASSERT_ENCODE_DECODE_STRING(coder, @"f", @"MY======");
199  ASSERT_ENCODE_DECODE_STRING(coder, @"fo", @"MZXQ====");
200  ASSERT_ENCODE_DECODE_STRING(coder, @"foo", @"MZXW6===");
201  ASSERT_ENCODE_DECODE_STRING(coder, @"foob", @"MZXW6YQ=");
202  ASSERT_ENCODE_DECODE_STRING(coder, @"fooba", @"MZXW6YTB");
203  ASSERT_ENCODE_DECODE_STRING(coder, @"foobar", @"MZXW6YTBOI======");
204
205  // All values, generated with:
206  // python -c 'import base64; print base64.b32encode("".join([chr(x) for x in range(0, 256)]))'
207  NSString *allValues = @""
208  "AAAQEAYEAUDAOCAJBIFQYDIOB4IBCEQTCQKRMFYYDENBWHA5DYPSAIJCEMSCKJRHFAUSUKZMFUXC"
209  "6MBRGIZTINJWG44DSOR3HQ6T4P2AIFBEGRCFIZDUQSKKJNGE2TSPKBIVEU2UKVLFOWCZLJNVYXK6"
210  "L5QGCYTDMRSWMZ3INFVGW3DNNZXXA4LSON2HK5TXPB4XU634PV7H7AEBQKBYJBMGQ6EITCULRSGY"
211  "5D4QSGJJHFEVS2LZRGM2TOOJ3HU7UCQ2FI5EUWTKPKFJVKV2ZLNOV6YLDMVTWS23NN5YXG5LXPF5"
212  "X274BQOCYPCMLRWHZDE4VS6MZXHM7UGR2LJ5JVOW27MNTWW33TO55X7A4HROHZHF43T6R2PK5PWO"
213  "33XP6DY7F47U6X3PP6HZ7L57Z7P674======";
214  char allValuesBytes[256];
215  for (NSUInteger i = 0; i < sizeof(allValuesBytes); i++)
216    allValuesBytes[i] = i;
217  NSData *allValuesData = [NSData dataWithBytes:&allValuesBytes
218                                         length:sizeof(allValuesBytes)];
219
220  STAssertEqualObjects([coder decode:allValues], allValuesData, nil);
221  STAssertEqualStrings([coder encode:allValuesData], allValues, nil);
222}
223
224- (void)testBase32Hex {
225  // RFC4648 test vectors
226  GTMStringEncoding *coder = [GTMStringEncoding rfc4648Base32HexStringEncoding];
227
228  [GTMUnitTestDevLog expectString:@"Empty input"];
229  ASSERT_ENCODE_DECODE_STRING(coder, @"", @"");
230  ASSERT_ENCODE_DECODE_STRING(coder, @"f", @"CO======");
231  ASSERT_ENCODE_DECODE_STRING(coder, @"fo", @"CPNG====");
232  ASSERT_ENCODE_DECODE_STRING(coder, @"foo", @"CPNMU===");
233  ASSERT_ENCODE_DECODE_STRING(coder, @"foob", @"CPNMUOG=");
234  ASSERT_ENCODE_DECODE_STRING(coder, @"fooba", @"CPNMUOJ1");
235  ASSERT_ENCODE_DECODE_STRING(coder, @"foobar", @"CPNMUOJ1E8======");
236
237  // All values, generated with:
238  // python -c 'import base64; print base64.b32encode("".join([chr(x) for x in range(0, 256)]))' | tr A-Z2-7 0-9A-V
239  NSString *allValues = @""
240  "000G40O40K30E209185GO38E1S8124GJ2GAHC5OO34D1M70T3OFI08924CI2A9H750KIKAPC5KN2"
241  "UC1H68PJ8D9M6SS3IEHR7GUJSFQ085146H258P3KGIAA9D64QJIFA18L4KQKALB5EM2PB9DLONAU"
242  "BTG62OJ3CHIMCPR8D5L6MR3DDPNN0SBIEDQ7ATJNF1SNKURSFLV7V041GA1O91C6GU48J2KBHI6O"
243  "T3SGI699754LIQBPH6CQJEE9R7KVK2GQ58T4KMJAFA59LALQPBDELUOB3CLJMIQRDDTON6TBNF5T"
244  "NQVS1GE2OF2CBHM7P34SLIUCPN7CVK6HQB9T9LEMQVCDJMMRRJETTNV0S7HE7P75SRJUHQFATFME"
245  "RRNFU3OV5SVKUNRFFU7PVBTVPVFUVS======";
246  char allValuesBytes[256];
247  for (NSUInteger i = 0; i < sizeof(allValuesBytes); i++)
248    allValuesBytes[i] = i;
249  NSData *allValuesData = [NSData dataWithBytes:&allValuesBytes
250                                         length:sizeof(allValuesBytes)];
251
252  STAssertEqualObjects([coder decode:allValues], allValuesData, nil);
253  STAssertEqualStrings([coder encode:allValuesData], allValues, nil);
254}
255
256- (void)testHex {
257  // RFC4648 test vectors
258  GTMStringEncoding *coder = [GTMStringEncoding hexStringEncoding];
259
260  [GTMUnitTestDevLog expectString:@"Empty input"];
261  ASSERT_ENCODE_DECODE_STRING(coder, @"", @"");
262  ASSERT_ENCODE_DECODE_STRING(coder, @"f", @"66");
263  ASSERT_ENCODE_DECODE_STRING(coder, @"fo", @"666F");
264  ASSERT_ENCODE_DECODE_STRING(coder, @"foo", @"666F6F");
265  ASSERT_ENCODE_DECODE_STRING(coder, @"foob", @"666F6F62");
266  ASSERT_ENCODE_DECODE_STRING(coder, @"fooba", @"666F6F6261");
267  ASSERT_ENCODE_DECODE_STRING(coder, @"foobar", @"666F6F626172");
268
269  // All Values, generated with:
270  // python -c 'import binascii; print binascii.b2a_hex("".join([chr(x) for x in range(0, 256)])).upper()'
271  NSString *allValues = @""
272  "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425"
273  "262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B"
274  "4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F7071"
275  "72737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F9091929394959697"
276  "98999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBD"
277  "BEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3"
278  "E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF";
279  char allValuesBytes[256];
280  for (NSUInteger i = 0; i < sizeof(allValuesBytes); i++)
281    allValuesBytes[i] = i;
282  NSData *allValuesData = [NSData dataWithBytes:&allValuesBytes
283                                         length:sizeof(allValuesBytes)];
284
285  STAssertEqualObjects([coder decode:allValues], allValuesData, nil);
286  STAssertEqualStrings([coder encode:allValuesData], allValues, nil);
287
288  // Lower case
289  STAssertEqualObjects([coder decode:[allValues lowercaseString]],
290                       allValuesData, nil);
291
292  // Extra tests from GTMNSData+HexTest.m
293  NSString *testString = @"1C2F0032F40123456789ABCDEF";
294  char testBytes[] = { 0x1c, 0x2f, 0x00, 0x32, 0xf4, 0x01, 0x23,
295                       0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
296  NSData *testData = [NSData dataWithBytes:testBytes length:sizeof(testBytes)];
297  STAssertEqualStrings([coder encode:testData], testString, nil);
298  STAssertEqualObjects([coder decode:testString], testData, nil);
299
300  // Invalid inputs
301  [GTMUnitTestDevLog expectString:@"Incomplete trailing data"];
302  STAssertNil([coder decode:@"1c2f003"], nil);
303  [GTMUnitTestDevLog expectString:@"Unexpected data in input pos 7"];
304  STAssertNil([coder decode:@"1c2f00ft"], nil);
305  [GTMUnitTestDevLog expectString:@"Unexpected data in input pos 4"];
306  STAssertNil([coder decode:@"abcd<C3><A9>f"], nil);
307}
308
309@end