test.c 131 KB
Newer Older
1
2
/* Copyright (c) 2001-2004, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3
 * Copyright (c) 2007-2008, The Tor Project, Inc. */
4
5
/* See LICENSE for licensing information */
/* $Id$ */
6
7
const char test_c_id[] =
  "$Id$";
8

9
10
const char tor_svn_revision[] = "";

11
12
13
14
15
/**
 * \file test.c
 * \brief Unit tests for many pieces of the lower level Tor modules.
 **/

Nick Mathewson's avatar
Nick Mathewson committed
16
#include "orconfig.h"
17
#include <stdio.h>
18
#ifdef HAVE_FCNTL_H
19
#include <fcntl.h>
20
21
#endif

22
#ifdef MS_WINDOWS
23
24
/* For mkdir() */
#include <direct.h>
Nick Mathewson's avatar
Nick Mathewson committed
25
#else
26
#include <dirent.h>
Nick Mathewson's avatar
Nick Mathewson committed
27
28
#endif

29
30
/* These macros pull in declarations for some functions and structures that
 * are typically file-private. */
31
#define BUFFERS_PRIVATE
32
33
#define CONFIG_PRIVATE
#define CONTROL_PRIVATE
34
35
#define CRYPTO_PRIVATE
#define DIRSERV_PRIVATE
36
#define DIRVOTE_PRIVATE
37
#define GEOIP_PRIVATE
38
#define MEMPOOL_PRIVATE
39
#define ROUTER_PRIVATE
40

Nick Mathewson's avatar
Nick Mathewson committed
41
#include "or.h"
42
43
44
#include "test.h"
#include "torgzip.h"
#include "mempool.h"
45
#include "memarea.h"
Nick Mathewson's avatar
Nick Mathewson committed
46

47
48
int have_failed = 0;

49
50
static char temp_dir[256];

51
static void
Nick Mathewson's avatar
Nick Mathewson committed
52
setup_directory(void)
53
54
{
  static int is_setup = 0;
55
  int r;
56
57
  if (is_setup) return;

58
#ifdef MS_WINDOWS
Nick Mathewson's avatar
Nick Mathewson committed
59
  // XXXX
60
61
  tor_snprintf(temp_dir, sizeof(temp_dir),
               "c:\\windows\\temp\\tor_test_%d", (int)getpid());
62
  r = mkdir(temp_dir);
63
#else
Nick Mathewson's avatar
Nick Mathewson committed
64
  tor_snprintf(temp_dir, sizeof(temp_dir), "/tmp/tor_test_%d", (int) getpid());
65
66
67
68
69
70
71
72
73
74
  r = mkdir(temp_dir, 0700);
#endif
  if (r) {
    fprintf(stderr, "Can't create directory %s:", temp_dir);
    perror("");
    exit(1);
  }
  is_setup = 1;
}

75
static const char *
76
77
78
79
get_fname(const char *name)
{
  static char buf[1024];
  setup_directory();
80
  tor_snprintf(buf,sizeof(buf),"%s/%s",temp_dir,name);
81
82
83
  return buf;
}

84
static void
Nick Mathewson's avatar
Nick Mathewson committed
85
remove_directory(void)
86
{
87
88
89
90
91
92
93
94
95
96
97
98
  smartlist_t *elements = tor_listdir(temp_dir);
  if (elements) {
    SMARTLIST_FOREACH(elements, const char *, cp,
       {
         size_t len = strlen(cp)+strlen(temp_dir)+16;
         char *tmp = tor_malloc(len);
         tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
         unlink(tmp);
         tor_free(tmp);
       });
    SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
    smartlist_free(elements);
Nick Mathewson's avatar
Nick Mathewson committed
99
  }
100
  rmdir(temp_dir);
101
102
}

103
104
105
static crypto_pk_env_t *
pk_generate(int idx)
{
106
  static crypto_pk_env_t *pregen[5] = {NULL, NULL, NULL, NULL, NULL};
107
108
109
110
111
112
113
114
  tor_assert(idx < (int)(sizeof(pregen)/sizeof(pregen[0])));
  if (! pregen[idx]) {
    pregen[idx] = crypto_new_pk_env();
    tor_assert(!crypto_pk_generate_key(pregen[idx]));
  }
  return crypto_pk_dup_key(pregen[idx]);
}

115
static void
116
117
test_buffers(void)
{
118
119
120
  char str[256];
  char str2[256];

121
  buf_t *buf, *buf2;
122
  const char *cp;
Nick Mathewson's avatar
Nick Mathewson committed
123

Nick Mathewson's avatar
Nick Mathewson committed
124
  int j;
125
  size_t r;
126
127
128
129

  /****
   * buf_new
   ****/
130
  if (!(buf = buf_new()))
Nick Mathewson's avatar
Nick Mathewson committed
131
    test_fail();
132

133
  //test_eq(buf_capacity(buf), 4096);
134
  test_eq(buf_datalen(buf), 0);
135
136

  /****
137
138
   * General pointer frobbing
   */
139
140
  for (j=0;j<256;++j) {
    str[j] = (char)j;
Nick Mathewson's avatar
Nick Mathewson committed
141
  }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  write_to_buf(str, 256, buf);
  write_to_buf(str, 256, buf);
  test_eq(buf_datalen(buf), 512);
  fetch_from_buf(str2, 200, buf);
  test_memeq(str, str2, 200);
  test_eq(buf_datalen(buf), 312);
  memset(str2, 0, sizeof(str2));

  fetch_from_buf(str2, 256, buf);
  test_memeq(str+200, str2, 56);
  test_memeq(str, str2+56, 200);
  test_eq(buf_datalen(buf), 56);
  memset(str2, 0, sizeof(str2));
  /* Okay, now we should be 512 bytes into the 4096-byte buffer.  If we add
   * another 3584 bytes, we hit the end. */
Nick Mathewson's avatar
Nick Mathewson committed
157
  for (j=0;j<15;++j) {
158
159
160
161
162
163
164
    write_to_buf(str, 256, buf);
  }
  assert_buf_ok(buf);
  test_eq(buf_datalen(buf), 3896);
  fetch_from_buf(str2, 56, buf);
  test_eq(buf_datalen(buf), 3840);
  test_memeq(str+200, str2, 56);
Nick Mathewson's avatar
Nick Mathewson committed
165
  for (j=0;j<15;++j) {
166
167
168
169
170
171
172
173
174
    memset(str2, 0, sizeof(str2));
    fetch_from_buf(str2, 256, buf);
    test_memeq(str, str2, 256);
  }
  test_eq(buf_datalen(buf), 0);
  buf_free(buf);

  /* Okay, now make sure growing can work. */
  buf = buf_new_with_capacity(16);
175
  //test_eq(buf_capacity(buf), 16);
176
  write_to_buf(str+1, 255, buf);
177
  //test_eq(buf_capacity(buf), 256);
178
179
  fetch_from_buf(str2, 254, buf);
  test_memeq(str+1, str2, 254);
180
  //test_eq(buf_capacity(buf), 256);
181
182
  assert_buf_ok(buf);
  write_to_buf(str, 32, buf);
183
  //test_eq(buf_capacity(buf), 256);
184
185
186
  assert_buf_ok(buf);
  write_to_buf(str, 256, buf);
  assert_buf_ok(buf);
187
  //test_eq(buf_capacity(buf), 512);
188
189
190
191
192
  test_eq(buf_datalen(buf), 33+256);
  fetch_from_buf(str2, 33, buf);
  test_eq(*str2, str[255]);

  test_memeq(str2+1, str, 32);
193
  //test_eq(buf_capacity(buf), 512);
194
195
196
197
198
199
200
201
202
203
  test_eq(buf_datalen(buf), 256);
  fetch_from_buf(str2, 256, buf);
  test_memeq(str, str2, 256);

  /* now try shrinking: case 1. */
  buf_free(buf);
  buf = buf_new_with_capacity(33668);
  for (j=0;j<67;++j) {
    write_to_buf(str,255, buf);
  }
204
  //test_eq(buf_capacity(buf), 33668);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  test_eq(buf_datalen(buf), 17085);
  for (j=0; j < 40; ++j) {
    fetch_from_buf(str2, 255,buf);
    test_memeq(str2, str, 255);
  }

  /* now try shrinking: case 2. */
  buf_free(buf);
  buf = buf_new_with_capacity(33668);
  for (j=0;j<67;++j) {
    write_to_buf(str,255, buf);
  }
  for (j=0; j < 20; ++j) {
    fetch_from_buf(str2, 255,buf);
    test_memeq(str2, str, 255);
  }
  for (j=0;j<80;++j) {
    write_to_buf(str,255, buf);
  }
224
  //test_eq(buf_capacity(buf),33668);
225
226
227
228
229
  for (j=0; j < 120; ++j) {
    fetch_from_buf(str2, 255,buf);
    test_memeq(str2, str, 255);
  }

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
  /* Move from buf to buf. */
  buf_free(buf);
  buf = buf_new_with_capacity(4096);
  buf2 = buf_new_with_capacity(4096);
  for (j=0;j<100;++j)
    write_to_buf(str, 255, buf);
  test_eq(buf_datalen(buf), 25500);
  for (j=0;j<100;++j) {
    r = 10;
    move_buf_to_buf(buf2, buf, &r);
    test_eq(r, 0);
  }
  test_eq(buf_datalen(buf), 24500);
  test_eq(buf_datalen(buf2), 1000);
  for (j=0;j<3;++j) {
    fetch_from_buf(str2, 255, buf2);
    test_memeq(str2, str, 255);
  }
  r = 8192; /*big move*/
  move_buf_to_buf(buf2, buf, &r);
  test_eq(r, 0);
  r = 30000; /* incomplete move */
  move_buf_to_buf(buf2, buf, &r);
  test_eq(r, 13692);
  for (j=0;j<97;++j) {
    fetch_from_buf(str2, 255, buf2);
    test_memeq(str2, str, 255);
  }
  buf_free(buf);
  buf_free(buf2);

261
262
263
264
265
266
267
268
269
270
  buf = buf_new_with_capacity(5);
  cp = "Testing. This is a moderately long Testing string.";
  for (j = 0; cp[j]; j++)
    write_to_buf(cp+j, 1, buf);
  test_eq(0, buf_find_string_offset(buf, "Testing", 7));
  test_eq(1, buf_find_string_offset(buf, "esting", 6));
  test_eq(1, buf_find_string_offset(buf, "est", 3));
  test_eq(39, buf_find_string_offset(buf, "ing str", 7));
  test_eq(35, buf_find_string_offset(buf, "Testing str", 11));
  test_eq(32, buf_find_string_offset(buf, "ng ", 3));
271
  test_eq(43, buf_find_string_offset(buf, "string.", 7));
272
273
274
275
  test_eq(-1, buf_find_string_offset(buf, "shrdlu", 6));
  test_eq(-1, buf_find_string_offset(buf, "Testing thing", 13));
  test_eq(-1, buf_find_string_offset(buf, "ngx", 3));
  buf_free(buf);
276

Nick Mathewson's avatar
Nick Mathewson committed
277
#if 0
278
279
280
281
282
  {
  int s;
  int eof;
  int i;
  buf_t *buf2;
283
284
285
286
  /****
   * read_to_buf
   ****/
  s = open(get_fname("data"), O_WRONLY|O_CREAT|O_TRUNC, 0600);
287
288
  write(s, str, 256);
  close(s);
Roger Dingledine's avatar
Roger Dingledine committed
289

290
  s = open(get_fname("data"), O_RDONLY, 0);
291
  eof = 0;
292
  errno = 0; /* XXXX */
293
  i = read_to_buf(s, 10, buf, &eof);
294
  printf("%s\n", strerror(errno));
295
  test_eq(i, 10);
296
  test_eq(eof, 0);
297
  //test_eq(buf_capacity(buf), 4096);
298
299
  test_eq(buf_datalen(buf), 10);

300
  test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10);
301
302

  /* Test reading 0 bytes. */
303
  i = read_to_buf(s, 0, buf, &eof);
304
  //test_eq(buf_capacity(buf), 512*1024);
305
  test_eq(buf_datalen(buf), 10);
306
307
  test_eq(eof, 0);
  test_eq(i, 0);
Nick Mathewson's avatar
Nick Mathewson committed
308

309
  /* Now test when buffer is filled exactly. */
310
311
  buf2 = buf_new_with_capacity(6);
  i = read_to_buf(s, 6, buf2, &eof);
312
  //test_eq(buf_capacity(buf2), 6);
313
  test_eq(buf_datalen(buf2), 6);
314
315
  test_eq(eof, 0);
  test_eq(i, 6);
316
317
  test_memeq(str+10, (char*)_buf_peek_raw_buffer(buf2), 6);
  buf_free(buf2);
Roger Dingledine's avatar
Roger Dingledine committed
318

319
  /* Now test when buffer is filled with more data to read. */
320
321
  buf2 = buf_new_with_capacity(32);
  i = read_to_buf(s, 128, buf2, &eof);
322
  //test_eq(buf_capacity(buf2), 128);
323
  test_eq(buf_datalen(buf2), 32);
324
  test_eq(eof, 0);
325
326
  test_eq(i, 32);
  buf_free(buf2);
327
328

  /* Now read to eof. */
329
330
331
332
333
334
  test_assert(buf_capacity(buf) > 256);
  i = read_to_buf(s, 1024, buf, &eof);
  test_eq(i, (256-32-10-6));
  test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  test_eq(buf_datalen(buf), 256-6-32);
  test_memeq(str, (char*)_buf_peek_raw_buffer(buf), 10); /* XXX Check rest. */
335
336
  test_eq(eof, 0);

337
  i = read_to_buf(s, 1024, buf, &eof);
338
  test_eq(i, 0);
339
340
  test_eq(buf_capacity(buf), MAX_BUF_SIZE);
  test_eq(buf_datalen(buf), 256-6-32);
341
  test_eq(eof, 1);
342
  }
Nick Mathewson's avatar
Nick Mathewson committed
343
#endif
Nick Mathewson's avatar
Nick Mathewson committed
344
345
}

346
static void
Nick Mathewson's avatar
Nick Mathewson committed
347
test_crypto_dh(void)
348
349
{
  crypto_dh_env_t *dh1, *dh2;
350
351
352
353
  char p1[DH_BYTES];
  char p2[DH_BYTES];
  char s1[DH_BYTES];
  char s2[DH_BYTES];
354
  int s1len, s2len;
355
356
357

  dh1 = crypto_dh_new();
  dh2 = crypto_dh_new();
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);

  memset(p1, 0, DH_BYTES);
  memset(p2, 0, DH_BYTES);
  test_memeq(p1, p2, DH_BYTES);
  test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  test_memneq(p1, p2, DH_BYTES);
  test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  test_memneq(p1, p2, DH_BYTES);

  memset(s1, 0, DH_BYTES);
  memset(s2, 0xFF, DH_BYTES);
  s1len = crypto_dh_compute_secret(dh1, p2, DH_BYTES, s1, 50);
  s2len = crypto_dh_compute_secret(dh2, p1, DH_BYTES, s2, 50);
373
374
375
  test_assert(s1len > 0);
  test_eq(s1len, s2len);
  test_memeq(s1, s2, s1len);
Roger Dingledine's avatar
Roger Dingledine committed
376

377
378
379
380
381
382
  {
    /* XXXX Now fabricate some bad values and make sure they get caught,
     * Check 0, 1, N-1, >= N, etc.
     */
  }

383
384
385
386
  crypto_dh_free(dh1);
  crypto_dh_free(dh2);
}

387
static void
Nick Mathewson's avatar
Nick Mathewson committed
388
test_crypto(void)
389
{
390
391
392
  crypto_cipher_env_t *env1, *env2;
  crypto_pk_env_t *pk1, *pk2;
  char *data1, *data2, *data3, *cp;
393
  int i, j, p, len, idx, allok;
Roger Dingledine's avatar
Roger Dingledine committed
394
  size_t size;
Nick Mathewson's avatar
Nick Mathewson committed
395

396
397
398
  data1 = tor_malloc(1024);
  data2 = tor_malloc(1024);
  data3 = tor_malloc(1024);
399
  test_assert(data1 && data2 && data3);
400

401
  /* Try out RNG. */
Roger Dingledine's avatar
Roger Dingledine committed
402
  test_assert(! crypto_seed_rng(0));
403
404
  crypto_rand(data1, 100);
  crypto_rand(data2, 100);
405
  test_memneq(data1,data2,100);
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
  allok = 1;
  for (i = 0; i < 100; ++i) {
    uint64_t big;
    char *host;
    j = crypto_rand_int(100);
    if (i < 0 || i >= 100)
      allok = 0;
    big = crypto_rand_uint64(U64_LITERAL(1)<<40);
    if (big >= (U64_LITERAL(1)<<40))
      allok = 0;
    big = crypto_rand_uint64(U64_LITERAL(5));
    if (big >= 5)
      allok = 0;
    host = crypto_random_hostname(3,8,"www.",".onion");
    if (strcmpstart(host,"www.") ||
        strcmpend(host,".onion") ||
        strlen(host) < 13 ||
        strlen(host) > 18)
      allok = 0;
    tor_free(host);
  }
  test_assert(allok);
Roger Dingledine's avatar
Roger Dingledine committed
428

429
  /* Now, test encryption and decryption with stream cipher. */
430
  data1[0]='\0';
431
  for (i = 1023; i>0; i -= 35)
432
    strncat(data1, "Now is the time for all good onions", i);
433
434
435
436
437
438
439
440
441
442
443
444
445

  memset(data2, 0, 1024);
  memset(data3, 0, 1024);
  env1 = crypto_new_cipher_env();
  test_neq(env1, 0);
  env2 = crypto_new_cipher_env();
  test_neq(env2, 0);
  j = crypto_cipher_generate_key(env1);
  crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  crypto_cipher_encrypt_init_cipher(env1);
  crypto_cipher_decrypt_init_cipher(env2);

  /* Try encrypting 512 chars. */
446
447
  crypto_cipher_encrypt(env1, data2, data1, 512);
  crypto_cipher_decrypt(env2, data3, data2, 512);
448
449
450
451
452
  test_memeq(data1, data3, 512);
  test_memneq(data1, data2, 512);

  /* Now encrypt 1 at a time, and get 1 at a time. */
  for (j = 512; j < 560; ++j) {
453
    crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
454
455
  }
  for (j = 512; j < 560; ++j) {
456
    crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
457
458
459
460
  }
  test_memeq(data1, data3, 560);
  /* Now encrypt 3 at a time, and get 5 at a time. */
  for (j = 560; j < 1024-5; j += 3) {
461
    crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
462
463
  }
  for (j = 560; j < 1024-5; j += 5) {
464
    crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
465
466
467
468
469
470
471
472
473
474
475
476
  }
  test_memeq(data1, data3, 1024-5);
  /* Now make sure that when we encrypt with different chunk sizes, we get
     the same results. */
  crypto_free_cipher_env(env2);

  memset(data3, 0, 1024);
  env2 = crypto_new_cipher_env();
  test_neq(env2, 0);
  crypto_cipher_set_key(env2, crypto_cipher_get_key(env1));
  crypto_cipher_encrypt_init_cipher(env2);
  for (j = 0; j < 1024-16; j += 17) {
477
    crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
478
479
480
481
  }
  for (j= 0; j < 1024-16; ++j) {
    if (data2[j] != data3[j]) {
      printf("%d:  %d\t%d\n", j, (int) data2[j], (int) data3[j]);
482
483
    }
  }
484
485
486
  test_memeq(data2, data3, 1024-16);
  crypto_free_cipher_env(env1);
  crypto_free_cipher_env(env2);
Roger Dingledine's avatar
Roger Dingledine committed
487

488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  /* NIST test vector for aes. */
  env1 = crypto_new_cipher_env(); /* IV starts at 0 */
  crypto_cipher_set_key(env1, "\x80\x00\x00\x00\x00\x00\x00\x00"
                              "\x00\x00\x00\x00\x00\x00\x00\x00");
  crypto_cipher_encrypt_init_cipher(env1);
  crypto_cipher_encrypt(env1, data1,
                        "\x00\x00\x00\x00\x00\x00\x00\x00"
                        "\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  test_memeq_hex(data1, "0EDD33D3C621E546455BD8BA1418BEC8");

  /* Now test rollover.  All these values are originally from a python
   * script. */
  crypto_cipher_set_iv(env1, "\x00\x00\x00\x00\x00\x00\x00\x00"
                             "\xff\xff\xff\xff\xff\xff\xff\xff");
  memset(data2, 0,  1024);
  crypto_cipher_encrypt(env1, data1, data2, 32);
  test_memeq_hex(data1, "335fe6da56f843199066c14a00a40231"
                        "cdd0b917dbc7186908a6bfb5ffd574d3");

  crypto_cipher_set_iv(env1, "\x00\x00\x00\x00\xff\xff\xff\xff"
                             "\xff\xff\xff\xff\xff\xff\xff\xff");
  memset(data2, 0,  1024);
  crypto_cipher_encrypt(env1, data1, data2, 32);
  test_memeq_hex(data1, "e627c6423fa2d77832a02b2794094b73"
                        "3e63c721df790d2c6469cc1953a3ffac");

  crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
                             "\xff\xff\xff\xff\xff\xff\xff\xff");
  memset(data2, 0,  1024);
  crypto_cipher_encrypt(env1, data1, data2, 32);
  test_memeq_hex(data1, "2aed2bff0de54f9328efd070bf48f70a"
                        "0EDD33D3C621E546455BD8BA1418BEC8");

  /* Now check rollover on inplace cipher. */
  crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
                             "\xff\xff\xff\xff\xff\xff\xff\xff");
  crypto_cipher_crypt_inplace(env1, data2, 64);
  test_memeq_hex(data2, "2aed2bff0de54f9328efd070bf48f70a"
                        "0EDD33D3C621E546455BD8BA1418BEC8"
                        "93e2c5243d6839eac58503919192f7ae"
                        "1908e67cafa08d508816659c2e693191");
  crypto_cipher_set_iv(env1, "\xff\xff\xff\xff\xff\xff\xff\xff"
                             "\xff\xff\xff\xff\xff\xff\xff\xff");
  crypto_cipher_crypt_inplace(env1, data2, 64);
  test_assert(tor_mem_is_zero(data2, 64));
  crypto_free_cipher_env(env1);
534
535

  /* Test SHA-1 with a test vector from the specification. */
536
  i = crypto_digest(data1, "abc", 3);
537
  test_memeq_hex(data1, "A9993E364706816ABA3E25717850C26C9CD0D89D");
538

539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  /* Test HMAC-SHA-1 with test cases from RFC2202. */
  {
    char key[80];
    char digest[20];
    char data[50];

    /* Case 1. */
    memset(key, 0x0b, 20);
    crypto_hmac_sha1(digest, key, 20, "Hi There", 8);
    test_streq(hex_str(digest, 20),
               "B617318655057264E28BC0B6FB378C8EF146BE00");

    /* Case 2. */
    crypto_hmac_sha1(digest, "Jefe", 4, "what do ya want for nothing?", 28);
    test_streq(hex_str(digest, 20),
               "EFFCDF6AE5EB2FA2D27416D5F184DF9C259A7C79");

    /* Case 4. */
    base16_decode(key, 25,
                  "0102030405060708090a0b0c0d0e0f10111213141516171819", 50);
    memset(data, 0xcd, 50);
    crypto_hmac_sha1(digest, key, 25, data, 50);
    test_streq(hex_str(digest, 20),
               "4C9007F4026250C6BC8414F9BF50C86C2D7235DA");

    /* Case . */
    memset(key, 0xaa, 80);
    crypto_hmac_sha1(digest, key, 80,
                     "Test Using Larger Than Block-Size Key - Hash Key First",
                     54);
    test_streq(hex_str(digest, 20),
               "AA4AE5E15272D00E95705637CE8A3B55ED402112");

  }

574
  /* Public-key ciphers */
575
  pk1 = pk_generate(0);
576
  pk2 = crypto_new_pk_env();
577
  test_assert(pk1 && pk2);
578
579
  test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &size));
  test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, size));
580
  test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
581
582
  tor_free(cp);

583
584
  test_eq(128, crypto_pk_keysize(pk1));
  test_eq(128, crypto_pk_keysize(pk2));
Roger Dingledine's avatar
Roger Dingledine committed
585

586
  test_eq(128, crypto_pk_public_encrypt(pk2, data1, "Hello whirled.", 15,
587
                                        PK_PKCS1_OAEP_PADDING));
588
  test_eq(128, crypto_pk_public_encrypt(pk1, data2, "Hello whirled.", 15,
589
                                        PK_PKCS1_OAEP_PADDING));
590
591
  /* oaep padding should make encryption not match */
  test_memneq(data1, data2, 128);
592
  test_eq(15, crypto_pk_private_decrypt(pk1, data3, data1, 128,
593
                                        PK_PKCS1_OAEP_PADDING,1));
594
595
  test_streq(data3, "Hello whirled.");
  memset(data3, 0, 1024);
596
  test_eq(15, crypto_pk_private_decrypt(pk1, data3, data2, 128,
597
                                        PK_PKCS1_OAEP_PADDING,1));
598
599
  test_streq(data3, "Hello whirled.");
  /* Can't decrypt with public key. */
600
  test_eq(-1, crypto_pk_private_decrypt(pk2, data3, data2, 128,
601
                                        PK_PKCS1_OAEP_PADDING,1));
602
603
  /* Try again with bad padding */
  memcpy(data2+1, "XYZZY", 5);  /* This has fails ~ once-in-2^40 */
604
  test_eq(-1, crypto_pk_private_decrypt(pk1, data3, data2, 128,
605
                                        PK_PKCS1_OAEP_PADDING,1));
Roger Dingledine's avatar
Roger Dingledine committed
606

607
  /* File operations: save and load private key */
608
  test_assert(! crypto_pk_write_private_key_to_filename(pk1,
609
                                                        get_fname("pkey1")));
610
611
612
613
614
615
616
  /* failing case for read: can't read. */
  test_assert(crypto_pk_read_private_key_from_filename(pk2,
                                                   get_fname("xyzzy")) < 0);
  write_str_to_file(get_fname("xyzzy"), "foobar", 6);
  /* Failing case for read: no key. */
  test_assert(crypto_pk_read_private_key_from_filename(pk2,
                                                   get_fname("xyzzy")) < 0);
Roger Dingledine's avatar
Roger Dingledine committed
617
  test_assert(! crypto_pk_read_private_key_from_filename(pk2,
618
                                                         get_fname("pkey1")));
619
  test_eq(15, crypto_pk_private_decrypt(pk2, data3, data1, 128,
620
                                        PK_PKCS1_OAEP_PADDING,1));
621

622
  /* Now try signing. */
623
  strlcpy(data1, "Ossifrage", 1024);
624
625
  test_eq(128, crypto_pk_private_sign(pk1, data2, data1, 10));
  test_eq(10, crypto_pk_public_checksig(pk1, data3, data2, 128));
626
  test_streq(data3, "Ossifrage");
627
  /* Try signing digests. */
628
629
  test_eq(128, crypto_pk_private_sign_digest(pk1, data2, data1, 10));
  test_eq(20, crypto_pk_public_checksig(pk1, data3, data2, 128));
630
631
  test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
632
  /*XXXX test failed signing*/
Roger Dingledine's avatar
Roger Dingledine committed
633

634
635
636
637
638
639
640
641
  /* Try encoding */
  crypto_free_pk_env(pk2);
  pk2 = NULL;
  i = crypto_pk_asn1_encode(pk1, data1, 1024);
  test_assert(i>0);
  pk2 = crypto_pk_asn1_decode(data1, i);
  test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);

642
  /* Try with hybrid encryption wrappers. */
643
  crypto_rand(data1, 1024);
644
645
646
647
648
649
  for (i = 0; i < 3; ++i) {
    for (j = 85; j < 140; ++j) {
      memset(data2,0,1024);
      memset(data3,0,1024);
      if (i == 0 && j < 129)
        continue;
650
651
      p = (i==0)?PK_NO_PADDING:
        (i==1)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
652
      len = crypto_pk_public_hybrid_encrypt(pk1,data2,data1,j,p,0);
653
      test_assert(len>=0);
654
      len = crypto_pk_private_hybrid_decrypt(pk1,data3,data2,len,p,1);
655
656
657
658
      test_eq(len,j);
      test_memeq(data1,data3,j);
    }
  }
Roger Dingledine's avatar
Roger Dingledine committed
659
660
  crypto_free_pk_env(pk1);
  crypto_free_pk_env(pk2);
661

662
  /* Base64 tests */
663
664
665
666
667
668
669
670
  memset(data1, 6, 1024);
  for (idx = 0; idx < 10; ++idx) {
    i = base64_encode(data2, 1024, data1, idx);
    j = base64_decode(data3, 1024, data2, i);
    test_eq(j,idx);
    test_memeq(data3, data1, idx);
  }

671
672
  strlcpy(data1, "Test string that contains 35 chars.", 1024);
  strlcat(data1, " 2nd string that contains 35 chars.", 1024);
673
674
675
676

  i = base64_encode(data2, 1024, data1, 71);
  j = base64_decode(data3, 1024, data2, i);
  test_eq(j, 71);
677
  test_streq(data3, data1);
678
679
  test_assert(data2[i] == '\0');

680
681
682
683
684
685
  crypto_rand(data1, DIGEST_LEN);
  memset(data2, 100, 1024);
  digest_to_base64(data2, data1);
  test_eq(BASE64_DIGEST_LEN, strlen(data2));
  test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  memset(data3, 99, 1024);
686
  test_eq(digest_from_base64(data3, data2), 0);
687
688
689
  test_memeq(data1, data3, DIGEST_LEN);
  test_eq(99, data3[DIGEST_LEN+1]);

690
691
  test_assert(digest_from_base64(data3, "###") < 0);

692
  /* Base32 tests */
693
  strlcpy(data1, "5chrs", 1024);
694
695
696
697
  /* bit pattern is:  [35 63 68 72 73] ->
   *        [00110101 01100011 01101000 01110010 01110011]
   * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
   */
698
  base32_encode(data2, 9, data1, 5);
699
700
  test_streq(data2, "gvrwq4tt");

701
  strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
702
  base32_encode(data2, 30, data1, 10);
703
  test_streq(data2, "772w2rfobvomsywe");
704

Nick Mathewson's avatar
Nick Mathewson committed
705
  /* Base16 tests */
706
  strlcpy(data1, "6chrs\xff", 1024);
707
  base16_encode(data2, 13, data1, 6);
Nick Mathewson's avatar
Nick Mathewson committed
708
709
  test_streq(data2, "3663687273FF");

710
  strlcpy(data1, "f0d678affc000100", 1024);
Nick Mathewson's avatar
Nick Mathewson committed
711
712
713
714
  i = base16_decode(data2, 8, data1, 16);
  test_eq(i,0);
  test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);

715
716
717
718
719
720
  /* now try some failing base16 decodes */
  test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */
  test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */
  strlcpy(data1, "f0dz!8affc000100", 1024);
  test_eq(-1, base16_decode(data2, 8, data1, 16));

721
722
723
  tor_free(data1);
  tor_free(data2);
  tor_free(data3);
724
725
726
727
728
729
730
731
732
733
734

  /* Add spaces to fingerprint */
  {
    data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000");
    test_eq(strlen(data1), 40);
    data2 = tor_malloc(FINGERPRINT_LEN+1);
    add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1);
    test_streq(data2, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000");
    tor_free(data1);
    tor_free(data2);
  }
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773

  /* Check fingerprint */
  {
    test_assert(crypto_pk_check_fingerprint_syntax(
                "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000"));
    test_assert(!crypto_pk_check_fingerprint_syntax(
                "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 000"));
    test_assert(!crypto_pk_check_fingerprint_syntax(
                "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 00000"));
    test_assert(!crypto_pk_check_fingerprint_syntax(
                "ABCD 1234 ABCD 5678 0000 ABCD1234 ABCD 5678 0000"));
    test_assert(!crypto_pk_check_fingerprint_syntax(
                "ABCD 1234 ABCD 5678 0000 ABCD1234 ABCD 5678 00000"));
    test_assert(!crypto_pk_check_fingerprint_syntax(
                "ACD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 00000"));
  }

  /* Incremental digest code. */
  {
    crypto_digest_env_t *d1, *d2;
    char d_out1[DIGEST_LEN], d_out2[DIGEST_LEN];
    d1 = crypto_new_digest_env();
    test_assert(d1);
    crypto_digest_add_bytes(d1, "abcdef", 6);
    d2 = crypto_digest_dup(d1);
    test_assert(d2);
    crypto_digest_add_bytes(d2, "ghijkl", 6);
    crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
    crypto_digest(d_out2, "abcdefghijkl", 12);
    test_memeq(d_out1, d_out2, DIGEST_LEN);
    crypto_digest_assign(d2, d1);
    crypto_digest_add_bytes(d2, "mno", 3);
    crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
    crypto_digest(d_out2, "abcdefmno", 9);
    test_memeq(d_out1, d_out2, DIGEST_LEN);
    crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
    crypto_digest(d_out2, "abcdef", 6);
    test_memeq(d_out1, d_out2, DIGEST_LEN);
  }
774
775
}

Nick Mathewson's avatar
Nick Mathewson committed
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
static void
test_crypto_s2k(void)
{
  char buf[29];
  char buf2[29];
  char *buf3;
  int i;

  memset(buf, 0, sizeof(buf));
  memset(buf2, 0, sizeof(buf2));
  buf3 = tor_malloc(65536);
  memset(buf3, 0, 65536);

  secret_to_key(buf+9, 20, "", 0, buf);
  crypto_digest(buf2+9, buf3, 1024);
  test_memeq(buf, buf2, 29);

  memcpy(buf,"vrbacrda",8);
  memcpy(buf2,"vrbacrda",8);
  buf[8] = 96;
  buf2[8] = 96;
  secret_to_key(buf+9, 20, "12345678", 8, buf);
  for (i = 0; i < 65536; i += 16) {
    memcpy(buf3+i, "vrbacrda12345678", 16);
  }
  crypto_digest(buf2+9, buf3, 65536);
  test_memeq(buf, buf2, 29);
803
  tor_free(buf3);
Nick Mathewson's avatar
Nick Mathewson committed
804
805
}

806
807
808
809
810
811
812
813
814
815
816
817
818
819
static int
_compare_strs(const void **a, const void **b)
{
  const char *s1 = *a, *s2 = *b;
  return strcmp(s1, s2);
}

static int
_compare_without_first_ch(const void *a, const void **b)
{
  const char *s1 = a, *s2 = *b;
  return strcasecmp(s1+1, s2);
}

820
static void
821
822
test_util(void)
{
823
  struct timeval start, end;
824
  struct tm a_time;
825
  char timestr[RFC1123_TIME_LEN+1];
826
  char buf[1024];
827
828
  time_t t_res;
  int i;
829
830
  uint32_t u32;
  uint16_t u16;
831
  char *cp, *k, *v;
832
  const char *str;
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855

  start.tv_sec = 5;
  start.tv_usec = 5000;

  end.tv_sec = 5;
  end.tv_usec = 5000;

  test_eq(0L, tv_udiff(&start, &end));

  end.tv_usec = 7000;

  test_eq(2000L, tv_udiff(&start, &end));

  end.tv_sec = 6;

  test_eq(1002000L, tv_udiff(&start, &end));

  end.tv_usec = 0;

  test_eq(995000L, tv_udiff(&start, &end));

  end.tv_sec = 4;

856
  test_eq(-1005000L, tv_udiff(&start, &end));
857

858
859
860
861
  end.tv_usec = 999990;
  start.tv_sec = 1;
  start.tv_usec = 500;

862
  /* The test values here are confirmed to be correct on a platform
863
   * with a working timegm. */
864
865
866
867
868
869
870
871
872
873
874
875
  a_time.tm_year = 2003-1900;
  a_time.tm_mon = 7;
  a_time.tm_mday = 30;
  a_time.tm_hour = 6;
  a_time.tm_min = 14;
  a_time.tm_sec = 55;
  test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
  a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
  test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
  a_time.tm_mon = 1;          /* Try a leap year, in feb. */
  a_time.tm_mday = 10;
  test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
876

877
  format_rfc1123_time(timestr, 0);
878
  test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
879
  format_rfc1123_time(timestr, (time_t)1091580502UL);
880
881
882
  test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);

  t_res = 0;
883
  i = parse_rfc1123_time(timestr, &t_res);
884
885
  test_eq(i,0);
  test_eq(t_res, (time_t)1091580502UL);
886
887
  test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
  tor_gettimeofday(&start);
888

889
890
891
892
893
  /* Tests for corner cases of strl operations */
  test_eq(5, strlcpy(buf, "Hello", 0));
  strlcpy(buf, "Hello", sizeof(buf));
  test_eq(10, strlcat(buf, "Hello", 5));

894
  /* Test tor_strstrip() */
895
  strlcpy(buf, "Testing 1 2 3", sizeof(buf));
896
  tor_strstrip(buf, ",!");
897
  test_streq(buf, "Testing 1 2 3");
898
  strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
899
  tor_strstrip(buf, "!? ");
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
  test_streq(buf, "Testing123");

  /* Test parse_addr_port */
  cp = NULL; u32 = 3; u16 = 3;
  test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
  test_streq(cp, "1.2.3.4");
  test_eq(u32, 0x01020304u);
  test_eq(u16, 0);
  tor_free(cp);
  test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
  test_streq(cp, "4.3.2.1");
  test_eq(u32, 0x04030201u);
  test_eq(u16, 99);
  tor_free(cp);
  test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
                               &cp, NULL, &u16));
  test_streq(cp, "nonexistent.address");
  test_eq(u16, 4040);
  tor_free(cp);
  test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
  test_streq(cp, "localhost");
  test_eq(u32, 0x7f000001u);
  test_eq(u16, 9999);
  tor_free(cp);
  u32 = 3;
  test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
  test_eq(cp, NULL);
  test_eq(u32, 0x7f000001u);
  test_eq(u16, 0);
  tor_free(cp);
  test_eq(0, addr_mask_get_bits(0x0u));
  test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
  test_eq(16, addr_mask_get_bits(0xFFFF0000u));
  test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
  test_eq(1, addr_mask_get_bits(0x80000000u));

  /* Test tor_parse_long. */
  test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
  test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
939
940
941
942
943
  test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL,NULL));

  /* Test tor_parse_ulong */
  test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
  test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
944

945
946
947
948
949
950
951
952
953
954
955
956
  /* Test tor_parse_uint64. */
  test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
  test_assert(i == 1);
  test_streq(cp, " x");
  test_assert(U64_LITERAL(12345678901) ==
              tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
  test_assert(i == 1);
  test_streq(cp, "");
  test_assert(U64_LITERAL(0) ==
              tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
  test_assert(i == 0);

957
958
959
960
  /* Test failing snprintf cases */
  test_eq(-1, tor_snprintf(buf, 0, "Foo"));
  test_eq(-1, tor_snprintf(buf, 2, "Foo"));

961
962
963
964
965
  /* Test printf with uint64 */
  tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
               U64_PRINTF_ARG(U64_LITERAL(12345678901)));
  test_streq(buf, "x!12345678901!x");

966
  /* Test parse_config_line_from_str */
967
968
969
  strlcpy(buf, "k v\n" " key    value with spaces   \n" "keykey val\n"
          "k2\n"
          "k3 \n" "\n" "   \n" "#comment\n"
970
971
972
973
          "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
          "kseven   \"a quoted 'string\"\n"
          "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
          , sizeof(buf));
974
  str = buf;
975

976
  str = parse_config_line_from_str(str, &k, &v);
977
978
  test_streq(k, "k");
  test_streq(v, "v");
979
  tor_free(k); tor_free(v);
980
  test_assert(!strcmpstart(str, "key    value with"));
981

982
  str = parse_config_line_from_str(str, &k, &v);
983
984
  test_streq(k, "key");
  test_streq(v, "value with spaces");
985
986
  tor_free(k); tor_free(v);
  test_assert(!strcmpstart(str, "keykey"));
987

988
  str = parse_config_line_from_str(str, &k, &v);
989
990
  test_streq(k, "keykey");
  test_streq(v, "val");
991
992
  tor_free(k); tor_free(v);
  test_assert(!strcmpstart(str, "k2\n"));
993

994
  str = parse_config_line_from_str(str, &k, &v);
995
996
  test_streq(k, "k2");
  test_streq(v, "");
997
998
  tor_free(k); tor_free(v);
  test_assert(!strcmpstart(str, "k3 \n"));
999

1000
  str = parse_config_line_from_str(str, &k, &v);
For faster browsing, not all history is shown. View entire blame