test.c 14.1 KB
Newer Older
1
2
3
4
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* See LICENSE for licensing information */
/* $Id$ */

5
6
7
#include <stdio.h>
#include <fcntl.h>

Nick Mathewson's avatar
Nick Mathewson committed
8
9
10
#include "or.h"
#include "../common/test.h"

11
12
13
14
15
16
17
18
void
setup_directory() {
  char buf[256];
  sprintf(buf, "/tmp/tor_test");
  if (mkdir(buf, 0700) && errno != EEXIST)
    fprintf(stderr, "Can't create directory %s", buf);
}

Nick Mathewson's avatar
Nick Mathewson committed
19
20
void
test_buffers() {
21
22
23
  char str[256];
  char str2[256];

Nick Mathewson's avatar
Nick Mathewson committed
24
25
26
  char *buf;
  int buflen, buf_datalen;

27
28
29
30
31
32
33
34
35
36
37
  char *buf2;
  int buf2len, buf2_datalen;

  int s, i, j, eof;
  z_compression *comp;
  z_decompression *decomp;

  /****
   * buf_new
   ****/
  if (buf_new(&buf, &buflen, &buf_datalen))
Nick Mathewson's avatar
Nick Mathewson committed
38
    test_fail();
39
40
41
42
43
44
45
46
47
48

  test_eq(buflen, MAX_BUF_SIZE);
  test_eq(buf_datalen, 0);

  /****
   * read_to_buf
   ****/
  s = open("/tmp/tor_test/data", O_WRONLY|O_CREAT|O_TRUNC, 0600);
  for (j=0;j<256;++j) {
    str[j] = (char)j;
Nick Mathewson's avatar
Nick Mathewson committed
49
  }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  write(s, str, 256);
  close(s);
  
  s = open("/tmp/tor_test/data", O_RDONLY, 0);
  eof = 0;
  i = read_to_buf(s, 10, &buf, &buflen, &buf_datalen, &eof);
  test_eq(buflen, MAX_BUF_SIZE);
  test_eq(buf_datalen, 10);
  test_eq(eof, 0);
  test_eq(i, 10);
  test_memeq(str, buf, 10);

  /* Test reading 0 bytes. */
  i = read_to_buf(s, 0, &buf, &buflen, &buf_datalen, &eof);
  test_eq(buflen, MAX_BUF_SIZE);
  test_eq(buf_datalen, 10);
  test_eq(eof, 0);
  test_eq(i, 0);
Nick Mathewson's avatar
Nick Mathewson committed
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  /* Now test when buffer is filled exactly. */
  buflen = 16;
  i = read_to_buf(s, 6, &buf, &buflen, &buf_datalen, &eof);
  test_eq(buflen, 16);
  test_eq(buf_datalen, 16);
  test_eq(eof, 0);
  test_eq(i, 6);
  test_memeq(str, buf, 16);
  
  /* Now test when buffer is filled with more data to read. */
  buflen = 32;
  i = read_to_buf(s, 128, &buf, &buflen, &buf_datalen, &eof);
  test_eq(buflen, 32);
  test_eq(buf_datalen, 32);
  test_eq(eof, 0);
  test_eq(i, 16);
  test_memeq(str, buf, 32);

  /* Now read to eof. */
  buflen = MAX_BUF_SIZE;
  test_assert(buflen > 256);
  i = read_to_buf(s, 1024, &buf, &buflen, &buf_datalen, &eof);
  test_eq(i, (256-32));
Nick Mathewson's avatar
Nick Mathewson committed
92
  test_eq(buflen, MAX_BUF_SIZE);
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
  test_eq(buf_datalen, 256);
  test_memeq(str, buf, 256);
  test_eq(eof, 0);

  i = read_to_buf(s, 1024, &buf, &buflen, &buf_datalen, &eof);
  test_eq(i, 0);
  test_eq(buflen, MAX_BUF_SIZE);
  test_eq(buf_datalen, 256);
  test_eq(eof, 1);

  close(s);

  /**** 
   * find_on_inbuf
   ****/

  test_eq(((int)'d') + 1, find_on_inbuf("abcd", 4, buf, buf_datalen));
  test_eq(-1, find_on_inbuf("xyzzy", 5, buf, buf_datalen));
  /* Make sure we don't look off the end of the buffef */
  buf[256] = 'A';
  buf[257] = 'X';
  test_eq(-1, find_on_inbuf("\xff" "A", 2, buf, buf_datalen));
  test_eq(-1, find_on_inbuf("AX", 2, buf, buf_datalen));
  /* Make sure we use the string length */
  test_eq(((int)'d')+1, find_on_inbuf("abcdX", 4, buf, buf_datalen));

  /****
   * fetch_from_buf
   ****/
  memset(str2, 255, 256);
  test_eq(246, fetch_from_buf(str2, 10, &buf, &buflen, &buf_datalen));
  test_memeq(str2, str, 10);
  test_memeq(str+10,buf,246);
  test_eq(buf_datalen,246);

  test_eq(-1, fetch_from_buf(str2, 247, &buf, &buflen, &buf_datalen));
  test_memeq(str+10,buf,246);
  test_eq(buf_datalen, 246);
  
  test_eq(0, fetch_from_buf(str2, 246, &buf, &buflen, &buf_datalen));
  test_memeq(str2, str+10, 246);
  test_eq(buflen,MAX_BUF_SIZE);
  test_eq(buf_datalen,0);

  /****
   * write_to_buf
   ****/
  memset(buf, (int)'-', 256);
  i = write_to_buf("Hello world", 11, &buf, &buflen, &buf_datalen);
  test_eq(i, 11);
  test_eq(buf_datalen, 11);
  test_memeq(buf, "Hello world", 11);
  i = write_to_buf("XYZZY", 5, &buf, &buflen, &buf_datalen);
  test_eq(i, 16);
  test_eq(buf_datalen, 16);
  test_memeq(buf, "Hello worldXYZZY", 16);
  /* Test when buffer is overfull. */
  buflen = 18;
  test_eq(-1, write_to_buf("This string will not fit.", 25, 
                           &buf, &buflen, &buf_datalen));
  test_eq(buf_datalen, 16);
  test_memeq(buf, "Hello worldXYZZY--", 18);
  buflen = MAX_BUF_SIZE;

  /****
   * flush_buf
   ****/
160
  /* XXXX Needs tests. */
161

162
  
163
164
165
166
167
168
169
170
  /***
   * compress_from_buf (simple)
   ***/
  buf_datalen = 0;
  comp = compression_new();
  for (i = 0; i < 20; ++i) {
    write_to_buf("Hello world.  ", 14, &buf, &buflen, &buf_datalen);
  }
171
172
  i = compress_from_buf(str, 256, &buf, &buflen, &buf_datalen, comp, 
                        Z_SYNC_FLUSH);
Nick Mathewson's avatar
Nick Mathewson committed
173
  test_eq(buf_datalen, 0);
174
175
176
177
178
179
180
181
182
183
184
185
186
  /*
  for (j = 0; j <i ; ++j) {
    printf("%x '%c'\n", ((int) str[j])&0xff, str[j]);
  }
  */
  /* Now try decompressing. */
  decomp = decompression_new();
  if (buf_new(&buf2, &buf2len, &buf2_datalen))
    test_fail();
  buf_datalen = 0;
  test_eq(i, write_to_buf(str, i, &buf, &buflen, &buf_datalen));
  j = decompress_buf_to_buf(&buf, &buflen, &buf_datalen,
                            &buf2, &buf2len, &buf2_datalen,
187
188
189
190
191
                            decomp, Z_SYNC_FLUSH);
  test_eq(buf2_datalen, 14*20);
  for (i = 0; i < 20; ++i) {
    test_memeq(buf2+(14*i), "Hello world.  ", 14);
  }
192
193
194
195
196
  
  /* Now compress more, into less room. */
  for (i = 0; i < 20; ++i) {
    write_to_buf("Hello wxrlx.  ", 14, &buf, &buflen, &buf_datalen);
  }
197
198
  i = compress_from_buf(str, 8, &buf, &buflen, &buf_datalen, comp, 
                        Z_SYNC_FLUSH);
199
  test_eq(buf_datalen, 0);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  test_eq(i, 8);
  memset(str+8,0,248);
  j = compress_from_buf(str+8, 248, &buf, &buflen, &buf_datalen, comp, 
                        Z_SYNC_FLUSH);
  /* test_eq(j, 2); XXXX This breaks, see below. */ 

  buf2_datalen=buf_datalen=0;
  write_to_buf(str, i+j, &buf, &buflen, &buf_datalen);
  memset(buf2, 0, buf2len);
  j = decompress_buf_to_buf(&buf, &buflen, &buf_datalen,
                            &buf2, &buf2len, &buf2_datalen,
                            decomp, Z_SYNC_FLUSH);
  test_eq(buf2_datalen, 14*20);
  for (i = 0; i < 20; ++i) {
    test_memeq(buf2+(14*i), "Hello wxrlx.  ", 14);
  }

  /* This situation is a bit messy.  We need to refactor our use of
   * zlib until the above code works.  Here's the problem: The zlib
   * documentation claims that we should reinvoke deflate immediately 
   * when the outbuf buffer is full and we get Z_OK, without adjusting
   * the input at all.  This implies that we need to tie a buffer to a
   * compression or decompression object.
   */
Nick Mathewson's avatar
Nick Mathewson committed
224

225
226
  compression_free(comp);
  decompression_free(decomp);
Nick Mathewson's avatar
Nick Mathewson committed
227
228

  buf_free(buf);
229
  buf_free(buf2);
Nick Mathewson's avatar
Nick Mathewson committed
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
void
test_crypto_dh()
{
  crypto_dh_env_t *dh1, *dh2;
  char p1[CRYPTO_DH_SIZE];
  char p2[CRYPTO_DH_SIZE];
  char s1[CRYPTO_DH_SIZE];
  char s2[CRYPTO_DH_SIZE];

  dh1 = crypto_dh_new();
  dh2 = crypto_dh_new();
  test_eq(crypto_dh_get_bytes(dh1), CRYPTO_DH_SIZE);
  test_eq(crypto_dh_get_bytes(dh2), CRYPTO_DH_SIZE);

  memset(p1, 0, CRYPTO_DH_SIZE);
  memset(p2, 0, CRYPTO_DH_SIZE);
  test_memeq(p1, p2, CRYPTO_DH_SIZE);
  test_assert(! crypto_dh_get_public(dh1, p1, CRYPTO_DH_SIZE));
  test_memneq(p1, p2, CRYPTO_DH_SIZE);
  test_assert(! crypto_dh_get_public(dh2, p2, CRYPTO_DH_SIZE));
  test_memneq(p1, p2, CRYPTO_DH_SIZE);
  
  memset(s1, 0, CRYPTO_DH_SIZE);
  memset(s2, 0, CRYPTO_DH_SIZE);
  test_assert(! crypto_dh_compute_secret(dh1, p2, CRYPTO_DH_SIZE, s1));
  test_assert(! crypto_dh_compute_secret(dh2, p1, CRYPTO_DH_SIZE, s2));
  test_memeq(s1, s2, CRYPTO_DH_SIZE);
  
  crypto_dh_free(dh1);
  crypto_dh_free(dh2);
}

264
void 
265
266
test_crypto() 
{
267
268
269
270
271
272
273
274
275
276
  crypto_cipher_env_t *env1, *env2;
  crypto_pk_env_t *pk1, *pk2;
  char *data1, *data2, *data3, *cp;
  FILE *f;
  int i, j;
  int str_ciphers[] = { CRYPTO_CIPHER_IDENTITY, 
                        CRYPTO_CIPHER_DES,
                        CRYPTO_CIPHER_RC4,
                        CRYPTO_CIPHER_3DES,
                        -1 };
Nick Mathewson's avatar
Nick Mathewson committed
277

278
279
280
281
  data1 = malloc(1024);
  data2 = malloc(1024);
  data3 = malloc(1024);
  test_assert(data1 && data2 && data3);
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
  /* Try out identity ciphers. */
  env1 = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  test_neq(env1, 0);
  test_eq(crypto_cipher_generate_key(env1), 0);
  test_eq(crypto_cipher_set_iv(env1, ""), 0);
  test_eq(crypto_cipher_encrypt_init_cipher(env1), 0);
  for(i = 0; i < 1024; ++i) {
    data1[i] = (char) i*73;
  }
  crypto_cipher_encrypt(env1, data1, 1024, data2); 
  test_memeq(data1, data2, 1024);
  crypto_free_cipher_env(env1);
  
  /* Now, test encryption and decryption with stream ciphers. */
  data1[0]='\0';
  for(i = 1023; i>0; i -= 35)
    strncat(data1, "Now is the time for all good onions", i);
  for(i=0; str_ciphers[i] >= 0; ++i) {
    /* For each cipher... */
    memset(data2, 0, 1024);
    memset(data3, 0, 1024);
    env1 = crypto_new_cipher_env(str_ciphers[i]);
    test_neq(env1, 0);
    env2 = crypto_new_cipher_env(str_ciphers[i]);
    test_neq(env2, 0);
    j = crypto_cipher_generate_key(env1);
    if (str_ciphers[i] != CRYPTO_CIPHER_IDENTITY) {
      crypto_cipher_set_key(env2, env1->key);
    }
    crypto_cipher_set_iv(env1, "12345678901234567890");
    crypto_cipher_set_iv(env2, "12345678901234567890");
    crypto_cipher_encrypt_init_cipher(env1);
    crypto_cipher_decrypt_init_cipher(env2);
316

317
318
319
320
    /* Try encrypting 512 chars. */
    crypto_cipher_encrypt(env1, data1, 512, data2);
    crypto_cipher_decrypt(env2, data2, 512, data3);
    test_memeq(data1, data3, 512);
321
    if (str_ciphers[i] == CRYPTO_CIPHER_IDENTITY) {
322
      test_memeq(data1, data2, 512);
323
324
    } else {
      test_memneq(data1, data2, 512);
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
    }
    /* Now encrypt 1 at a time, and get 1 at a time. */
    for (j = 512; j < 560; ++j) {
      crypto_cipher_encrypt(env1, data1+j, 1, data2+j);
    }
    for (j = 512; j < 560; ++j) {
      crypto_cipher_decrypt(env2, data2+j, 1, data3+j);
    }
    test_memeq(data1, data3, 560);
    /* Now encrypt 3 at a time, and get 5 at a time. */
    for (j = 560; j < 1024; j += 3) {
      crypto_cipher_encrypt(env1, data1+j, 3, data2+j);
    }
    for (j = 560; j < 1024; j += 5) {
      crypto_cipher_decrypt(env2, data2+j, 5, data3+j);
    }
    test_memeq(data1, data3, 1024-4);
    /* 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(str_ciphers[i]);
    test_neq(env2, 0);
    if (str_ciphers[i] != CRYPTO_CIPHER_IDENTITY) {
      crypto_cipher_set_key(env2, env1->key);
    }
    crypto_cipher_set_iv(env2, "12345678901234567890");
    crypto_cipher_encrypt_init_cipher(env2);
354
    for (j = 0; j < 1024-16; j += 17) {
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
      crypto_cipher_encrypt(env2, data1+j, 17, data3+j);
    }
    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]);
      }
    }
    test_memeq(data2, data3, 1024-16);
    crypto_free_cipher_env(env1);
    crypto_free_cipher_env(env2);
  }
  
  /* Test vectors for stream ciphers. */
  /* XXXX Look up some test vectors for the ciphers and make sure we match. */

  /* Test SHA-1 with a test vector from the specification. */
  i = crypto_SHA_digest("abc", 3, data1);
  test_memeq(data1,
             "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78"
             "\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);

  /* Public-key ciphers */
  pk1 = crypto_new_pk_env(CRYPTO_PK_RSA);
  pk2 = crypto_new_pk_env(CRYPTO_PK_RSA);
  test_assert(pk1 && pk2);
  test_assert(! crypto_pk_generate_key(pk1));
  test_assert(! crypto_pk_write_public_key_to_string(pk1, &cp, &i));
  test_assert(! crypto_pk_read_public_key_from_string(pk2, cp, i));
  test_eq(0, crypto_pk_cmp_keys(pk1, pk2));

  test_eq(128, crypto_pk_keysize(pk1));
  test_eq(128, crypto_pk_keysize(pk2));
  
  test_eq(128, crypto_pk_public_encrypt(pk2, "Hello whirled.", 15, data1,
                                        RSA_PKCS1_OAEP_PADDING));
  test_eq(128, crypto_pk_public_encrypt(pk1, "Hello whirled.", 15, data2,
                                        RSA_PKCS1_OAEP_PADDING));
  /* oaep padding should make encryption not match */
  test_memneq(data1, data2, 128);
  test_eq(15, crypto_pk_private_decrypt(pk1, data1, 128, data3,
                                        RSA_PKCS1_OAEP_PADDING));
  test_streq(data3, "Hello whirled.");
  memset(data3, 0, 1024);
  test_eq(15, crypto_pk_private_decrypt(pk1, data2, 128, data3,
                                        RSA_PKCS1_OAEP_PADDING));
  test_streq(data3, "Hello whirled.");
  /* Can't decrypt with public key. */
  test_eq(-1, crypto_pk_private_decrypt(pk2, data2, 128, data3,
                                        RSA_PKCS1_OAEP_PADDING));
  /* Try again with bad padding */
  memcpy(data2+1, "XYZZY", 5);  /* This has fails ~ once-in-2^40 */
  test_eq(-1, crypto_pk_private_decrypt(pk1, data2, 128, data3,
                                        RSA_PKCS1_OAEP_PADDING));

  /* File operations: save and load private key */
  f = fopen("/tmp/tor_test/pkey1", "wb");
  test_assert(! crypto_pk_write_private_key_to_file(pk1, f));
  fclose(f);
  f = fopen("/tmp/tor_test/pkey1", "rb");
  test_assert(! crypto_pk_read_private_key_from_file(pk2, f));
  fclose(f);
  test_eq(15, crypto_pk_private_decrypt(pk2, data1, 128, data3,
                                        RSA_PKCS1_OAEP_PADDING));
  test_assert(! crypto_pk_read_private_key_from_filename(pk2, 
                                               "/tmp/tor_test/pkey1"));
  test_eq(15, crypto_pk_private_decrypt(pk2, data1, 128, data3,
                                        RSA_PKCS1_OAEP_PADDING));
    

  crypto_free_pk_env(pk1);  
  crypto_free_pk_env(pk2);  

  free(data1);
  free(data2);
  free(data3);
Nick Mathewson's avatar
Nick Mathewson committed
430

431
432
}

433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
void 
test_util() {
  struct timeval start, end;

  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;

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

}

463
464
int 
main(int c, char**v) {
465
466
467
468
469
470
471
472
#if 0
  or_options_t options; /* command-line and config-file options */

  if(getconfig(c,v,&options))
    exit(1);
#endif
  log(LOG_ERR,NULL);         /* make logging quieter */

473
  setup_directory();
474
  puts("========================== Buffers =========================");
475
476
  test_buffers();
  puts("========================== Crypto ==========================");
477
  test_crypto_dh();
478
  test_crypto();
479
  puts("\n========================== Util ============================");
480
  test_util();
481
  puts("");
482
483
484
485
486
487
488
489
490
491
  return 0;
}

/*
  Local Variables:
  mode:c
  indent-tabs-mode:nil
  c-basic-offset:2
  End:
*/