aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMBase64Test.m
blob: 084ea1e079c0813bf98375bb4f6da3808e552f24 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//
//  GTMBase64Test.m
//
//  Copyright 2006-2008 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 "GTMSenTestCase.h"
#import "GTMBase64.h"
#include <stdlib.h> // for randiom/srandomdev

static void FillWithRandom(char *data, NSUInteger len) {
  char *max = data + len;
  for ( ; data < max ; ++data) {
    *data = random() & 0xFF;
  }
}

static BOOL NoEqualChar(NSData *data) {
  const char *scan = [data bytes];
  const char *max = scan + [data length];
  for ( ; scan < max ; ++scan) {
    if (*scan == '=') {
      return NO;
    }
  }
  return YES;
}

@interface GTMBase64Test : GTMTestCase 
@end

@implementation GTMBase64Test

- (void)setUp {
  // seed random from /dev/random
  srandomdev();
}

- (void)testBase64 {
  // generate a range of sizes w/ random content
  for (int x = 1 ; x < 1024 ; ++x) {
    NSMutableData *data = [NSMutableData data];
    STAssertNotNil(data, @"failed to alloc data block");
    
    [data setLength:x];
    FillWithRandom([data mutableBytes], [data length]);
    
    // w/ *Bytes apis
    NSData *encoded = [GTMBase64 encodeBytes:[data bytes] length:[data length]];
    STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                   @"encoded size via *Bytes apis should be a multiple of 4");
    NSData *dataPrime = [GTMBase64 decodeBytes:[encoded bytes]
                                        length:[encoded length]];
    STAssertEqualObjects(data, dataPrime,
                         @"failed to round trip via *Bytes apis");

    // w/ *Data apis
    encoded = [GTMBase64 encodeData:data];
    STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                   @"encoded size via *Data apis should be a multiple of 4");
    dataPrime = [GTMBase64 decodeData:encoded];
    STAssertEqualObjects(data, dataPrime,
                         @"failed to round trip via *Data apis");
    
    // Bytes to String and back
    NSString *encodedString = [GTMBase64 stringByEncodingBytes:[data bytes]
                                                        length:[data length]];
    STAssertEquals(([encodedString length] % 4), (NSUInteger)0,
                   @"encoded size for Bytes to Strings should be a multiple of 4");
    dataPrime = [GTMBase64 decodeString:encodedString];
    STAssertEqualObjects(data, dataPrime,
                         @"failed to round trip for Bytes to Strings");

    // Data to String and back
    encodedString = [GTMBase64 stringByEncodingData:data];
    STAssertEquals(([encodedString length] % 4), (NSUInteger)0,
                   @"encoded size for Data to Strings should be a multiple of 4");
    dataPrime = [GTMBase64 decodeString:encodedString];
    STAssertEqualObjects(data, dataPrime,
                         @"failed to round trip for Bytes to Strings");
  }
  
  {
    // now test all byte values
    NSMutableData *data = [NSMutableData data];
    STAssertNotNil(data, @"failed to alloc data block");
    
    [data setLength:256];
    unsigned char *scan = (unsigned char*)[data mutableBytes];
    for (int x = 0 ; x <= 255 ; ++x) {
      *scan++ = x;
    }
    
    // w/ *Bytes apis
    NSData *encoded = [GTMBase64 encodeBytes:[data bytes] length:[data length]];
    STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                   @"encoded size via *Bytes apis should be a multiple of 4");
    NSData *dataPrime = [GTMBase64 decodeBytes:[encoded bytes]
                                        length:[encoded length]];
    STAssertEqualObjects(data, dataPrime,
                         @"failed to round trip via *Bytes apis");

    // w/ *Data apis
    encoded = [GTMBase64 encodeData:data];
    STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                   @"encoded size via *Data apis should be a multiple of 4");
    dataPrime = [GTMBase64 decodeData:encoded];
    STAssertEqualObjects(data, dataPrime,
                         @"failed to round trip via *Data apis");

    // Bytes to String and back
    NSString *encodedString = [GTMBase64 stringByEncodingBytes:[data bytes]
                                                        length:[data length]];
    STAssertEquals(([encodedString length] % 4), (NSUInteger)0,
                   @"encoded size for Bytes to Strings should be a multiple of 4");
    dataPrime = [GTMBase64 decodeString:encodedString];
    STAssertEqualObjects(data, dataPrime,
                         @"failed to round trip for Bytes to Strings");

    // Data to String and back
    encodedString = [GTMBase64 stringByEncodingData:data];
    STAssertEquals(([encodedString length] % 4), (NSUInteger)0,
                   @"encoded size for Data to Strings should be a multiple of 4");
    dataPrime = [GTMBase64 decodeString:encodedString];
    STAssertEqualObjects(data, dataPrime,
                         @"failed to round trip for Data to Strings");
  }
  
  {
    // test w/ a mix of spacing characters
    
    // generate some data, encode it, and add spaces
    NSMutableData *data = [NSMutableData data];
    STAssertNotNil(data, @"failed to alloc data block");
    
    [data setLength:253]; // should get some padding chars on the end
    FillWithRandom([data mutableBytes], [data length]);

    NSString *encodedString = [GTMBase64 stringByEncodingData:data];
    NSMutableString *encodedAndSpaced =
      [[encodedString mutableCopy] autorelease];

    NSString *spaces[] = { @"\t", @"\n", @"\r", @" " };
    const NSUInteger numSpaces = sizeof(spaces) / sizeof(NSString*);
    for (int x = 0 ; x < 512 ; ++x) {
      NSUInteger offset = random() % ([encodedAndSpaced length] + 1);
      [encodedAndSpaced insertString:spaces[random() % numSpaces]
                             atIndex:offset];
    }

    // we'll need it as data for apis
    NSData *encodedAsData =
      [encodedAndSpaced dataUsingEncoding:NSASCIIStringEncoding];
    STAssertNotNil(encodedAsData, @"failed to extract from string");
    STAssertEquals([encodedAsData length], [encodedAndSpaced length],
                   @"lengths for encoded string and data didn't match?");
    
    // all the decode modes
    NSData *dataPrime = [GTMBase64 decodeData:encodedAsData];
    STAssertEqualObjects(data, dataPrime,
                         @"failed Data decode w/ spaces");
    dataPrime = [GTMBase64 decodeBytes:[encodedAsData bytes]
                                length:[encodedAsData length]];
    STAssertEqualObjects(data, dataPrime,
                         @"failed Bytes decode w/ spaces");
    dataPrime = [GTMBase64 decodeString:encodedAndSpaced];
    STAssertEqualObjects(data, dataPrime,
                         @"failed String decode w/ spaces");
  }
}

- (void)testWebSafeBase64 {
  // loop to test w/ and w/o padding
  for (int paddedLoop = 0; paddedLoop < 2 ; ++paddedLoop) {
    BOOL padded = (paddedLoop == 1);
  
    // generate a range of sizes w/ random content
    for (int x = 1 ; x < 1024 ; ++x) {
      NSMutableData *data = [NSMutableData data];
      STAssertNotNil(data, @"failed to alloc data block");

      [data setLength:x];
      FillWithRandom([data mutableBytes], [data length]);

      // w/ *Bytes apis
      NSData *encoded = [GTMBase64 webSafeEncodeBytes:[data bytes]
                                               length:[data length]
                                               padded:padded];
      if (padded) {
        STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                       @"encoded size via *Bytes apis should be a multiple of 4");
      } else {
        STAssertTrue(NoEqualChar(encoded),
                     @"encoded via *Bytes apis had a base64 padding char");
      }
      NSData *dataPrime = [GTMBase64 webSafeDecodeBytes:[encoded bytes]
                                                 length:[encoded length]];
      STAssertEqualObjects(data, dataPrime,
                           @"failed to round trip via *Bytes apis");
      
      // w/ *Data apis
      encoded = [GTMBase64 webSafeEncodeData:data padded:padded];
      if (padded) {
        STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                       @"encoded size via *Data apis should be a multiple of 4");
      } else {
        STAssertTrue(NoEqualChar(encoded),
                     @"encoded via *Data apis had a base64 padding char");
      }
      dataPrime = [GTMBase64 webSafeDecodeData:encoded];
      STAssertEqualObjects(data, dataPrime,
                           @"failed to round trip via *Data apis");
      
      // Bytes to String and back
      NSString *encodedString =
        [GTMBase64 stringByWebSafeEncodingBytes:[data bytes]
                                         length:[data length]
                                         padded:padded];
      if (padded) {
        STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                       @"encoded size via *Bytes apis should be a multiple of 4");
      } else {
        STAssertTrue(NoEqualChar(encoded),
                     @"encoded via Bytes to Strings had a base64 padding char");
      }
      dataPrime = [GTMBase64 webSafeDecodeString:encodedString];
      STAssertEqualObjects(data, dataPrime,
                           @"failed to round trip for Bytes to Strings");
      
      // Data to String and back
      encodedString =
        [GTMBase64 stringByWebSafeEncodingData:data padded:padded];
      if (padded) {
        STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                       @"encoded size via *Data apis should be a multiple of 4");
      } else {
        STAssertTrue(NoEqualChar(encoded),
                     @"encoded via Data to Strings had a base64 padding char");
      }
      dataPrime = [GTMBase64 webSafeDecodeString:encodedString];
      STAssertEqualObjects(data, dataPrime,
                           @"failed to round trip for Data to Strings");
    }

    {
      // now test all byte values
      NSMutableData *data = [NSMutableData data];
      STAssertNotNil(data, @"failed to alloc data block");
      
      [data setLength:256];
      unsigned char *scan = (unsigned char*)[data mutableBytes];
      for (int x = 0 ; x <= 255 ; ++x) {
        *scan++ = x;
      }
      
      // w/ *Bytes apis
      NSData *encoded =
        [GTMBase64 webSafeEncodeBytes:[data bytes]
                               length:[data length]
                               padded:padded];
      if (padded) {
        STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                       @"encoded size via *Bytes apis should be a multiple of 4");
      } else {
        STAssertTrue(NoEqualChar(encoded),
                   @"encoded via *Bytes apis had a base64 padding char");
      }
      NSData *dataPrime = [GTMBase64 webSafeDecodeBytes:[encoded bytes]
                                                 length:[encoded length]];
      STAssertEqualObjects(data, dataPrime,
                           @"failed to round trip via *Bytes apis");
      
      // w/ *Data apis
      encoded = [GTMBase64 webSafeEncodeData:data padded:padded];
      if (padded) {
        STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                       @"encoded size via *Data apis should be a multiple of 4");
      } else {
        STAssertTrue(NoEqualChar(encoded),
                   @"encoded via *Data apis had a base64 padding char");
      }
      dataPrime = [GTMBase64 webSafeDecodeData:encoded];
      STAssertEqualObjects(data, dataPrime,
                           @"failed to round trip via *Data apis");
      
      // Bytes to String and back
      NSString *encodedString =
        [GTMBase64 stringByWebSafeEncodingBytes:[data bytes]
                                         length:[data length]
                                         padded:padded];
      if (padded) {
        STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                       @"encoded size via *Bytes apis should be a multiple of 4");
      } else {
        STAssertTrue(NoEqualChar(encoded),
                   @"encoded via Bytes to Strings had a base64 padding char");
      }
      dataPrime = [GTMBase64 webSafeDecodeString:encodedString];
      STAssertEqualObjects(data, dataPrime,
                           @"failed to round trip for Bytes to Strings");
      
      // Data to String and back
      encodedString =
        [GTMBase64 stringByWebSafeEncodingData:data padded:padded];
      if (padded) {
        STAssertEquals(([encoded length] % 4), (NSUInteger)0,
                       @"encoded size via *Data apis should be a multiple of 4");
      } else {
        STAssertTrue(NoEqualChar(encoded),
                   @"encoded via Data to Strings had a base64 padding char");
      }
      dataPrime = [GTMBase64 webSafeDecodeString:encodedString];
      STAssertEqualObjects(data, dataPrime,
                           @"failed to round trip for Data to Strings");
    }

    {
      // test w/ a mix of spacing characters
      
      // generate some data, encode it, and add spaces
      NSMutableData *data = [NSMutableData data];
      STAssertNotNil(data, @"failed to alloc data block");
      
      [data setLength:253]; // should get some padding chars on the end
      FillWithRandom([data mutableBytes], [data length]);
      
      NSString *encodedString = [GTMBase64 stringByWebSafeEncodingData:data
                                                                padded:padded];
      NSMutableString *encodedAndSpaced =
        [[encodedString mutableCopy] autorelease];
      
      NSString *spaces[] = { @"\t", @"\n", @"\r", @" " };
      const NSUInteger numSpaces = sizeof(spaces) / sizeof(NSString*);
      for (int x = 0 ; x < 512 ; ++x) {
        NSUInteger offset = random() % ([encodedAndSpaced length] + 1);
        [encodedAndSpaced insertString:spaces[random() % numSpaces]
                               atIndex:offset];
      }
      
      // we'll need it as data for apis
      NSData *encodedAsData =
        [encodedAndSpaced dataUsingEncoding:NSASCIIStringEncoding];
      STAssertNotNil(encodedAsData, @"failed to extract from string");
      STAssertEquals([encodedAsData length], [encodedAndSpaced length],
                     @"lengths for encoded string and data didn't match?");
      
      // all the decode modes
      NSData *dataPrime = [GTMBase64 webSafeDecodeData:encodedAsData];
      STAssertEqualObjects(data, dataPrime,
                           @"failed Data decode w/ spaces");
      dataPrime = [GTMBase64 webSafeDecodeBytes:[encodedAsData bytes]
                                         length:[encodedAsData length]];
      STAssertEqualObjects(data, dataPrime,
                           @"failed Bytes decode w/ spaces");
      dataPrime = [GTMBase64 webSafeDecodeString:encodedAndSpaced];
      STAssertEqualObjects(data, dataPrime,
                           @"failed String decode w/ spaces");
    }
  } // paddedLoop
}

- (void)testErrors {
  const int something = 0;
  NSString *nonAscString = [NSString stringWithUTF8String:"This test ©™®๒०᠐٧"];

  STAssertNil([GTMBase64 encodeData:nil], @"it worked?");
  STAssertNil([GTMBase64 decodeData:nil], @"it worked?");
  STAssertNil([GTMBase64 encodeBytes:NULL length:10], @"it worked?");
  STAssertNil([GTMBase64 encodeBytes:&something length:0], @"it worked?");
  STAssertNil([GTMBase64 decodeBytes:NULL length:10], @"it worked?");
  STAssertNil([GTMBase64 decodeBytes:&something length:0], @"it worked?");
  STAssertNil([GTMBase64 stringByEncodingData:nil], @"it worked?");
  STAssertNil([GTMBase64 stringByEncodingBytes:NULL length:10], @"it worked?");
  STAssertNil([GTMBase64 stringByEncodingBytes:&something length:0], @"it worked?");
  STAssertNil([GTMBase64 decodeString:nil], @"it worked?");
  // test some pads at the end that aren't right
  STAssertNil([GTMBase64 decodeString:@"=="], @"it worked?"); // just pads
  STAssertNil([GTMBase64 decodeString:@"vw="], @"it worked?"); // missing pad (in state 2)
  STAssertNil([GTMBase64 decodeString:@"vw"], @"it worked?"); // missing pad (in state 2)
  STAssertNil([GTMBase64 decodeString:@"NNw"], @"it worked?"); // missing pad (in state 3)
  STAssertNil([GTMBase64 decodeString:@"vw=v"], @"it worked?"); // missing pad, has something else
  STAssertNil([GTMBase64 decodeString:@"v="], @"it worked?"); // missing a needed char, has pad instead
  STAssertNil([GTMBase64 decodeString:@"v"], @"it worked?"); // missing a needed char
  STAssertNil([GTMBase64 decodeString:@"vw== vw"], @"it worked?");
  STAssertNil([GTMBase64 decodeString:nonAscString], @"it worked?");
  STAssertNil([GTMBase64 decodeString:@"@@@not valid###"], @"it worked?");
  // carefully crafted bad input to make sure we don't overwalk
  STAssertNil([GTMBase64 decodeString:@"WD=="], @"it worked?");
  
  STAssertNil([GTMBase64 webSafeEncodeData:nil padded:YES], @"it worked?");
  STAssertNil([GTMBase64 webSafeDecodeData:nil], @"it worked?");
  STAssertNil([GTMBase64 webSafeEncodeBytes:NULL length:10 padded:YES],
              @"it worked?");
  STAssertNil([GTMBase64 webSafeEncodeBytes:&something length:0 padded:YES],
              @"it worked?");
  STAssertNil([GTMBase64 webSafeDecodeBytes:NULL length:10], @"it worked?");
  STAssertNil([GTMBase64 webSafeDecodeBytes:&something length:0], @"it worked?");
  STAssertNil([GTMBase64 stringByWebSafeEncodingData:nil padded:YES],
              @"it worked?");
  STAssertNil([GTMBase64 stringByWebSafeEncodingBytes:NULL
                                               length:10
                                               padded:YES],
              @"it worked?");
  STAssertNil([GTMBase64 stringByWebSafeEncodingBytes:&something
                                               length:0
                                               padded:YES],
              @"it worked?");
  STAssertNil([GTMBase64 webSafeDecodeString:nil], @"it worked?");
  // test some pads at the end that aren't right
  STAssertNil([GTMBase64 webSafeDecodeString:@"=="], @"it worked?"); // just pad chars
  STAssertNil([GTMBase64 webSafeDecodeString:@"aw="], @"it worked?"); // missing pad
  STAssertNil([GTMBase64 webSafeDecodeString:@"aw=a"], @"it worked?"); // missing pad, has something else
  STAssertNil([GTMBase64 webSafeDecodeString:@"a"], @"it worked?"); // missing a needed char
  STAssertNil([GTMBase64 webSafeDecodeString:@"a="], @"it worked?"); // missing a needed char, has pad instead
  STAssertNil([GTMBase64 webSafeDecodeString:@"aw== a"], @"it worked?"); // missing pad
  STAssertNil([GTMBase64 webSafeDecodeString:nonAscString], @"it worked?");
  STAssertNil([GTMBase64 webSafeDecodeString:@"@@@not valid###"], @"it worked?");
  // carefully crafted bad input to make sure we don't overwalk
  STAssertNil([GTMBase64 webSafeDecodeString:@"WD=="], @"it worked?");

  // make sure our local helper is working right
  STAssertFalse(NoEqualChar([NSData dataWithBytes:"aa=zz" length:5]), @"");
}

@end