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