source: trunk/libtransmission/crypto-test.c @ 14241

Last change on this file since 14241 was 14241, checked in by jordan, 8 years ago

Copyedit the license's revised text: (1) remove unnecessary repitition use of the word 'license' from the top of the header and source files (2) add the standard 'we hope it's useful, but no warranty' clause to COPYING (3) make explicit that linking OpenSSL is allowed (see https://people.gnome.org/~markmc/openssl-and-the-gpl.html for background) (4) sync the Qt and GTK+ clients' license popups with COPYING's revised text

  • Property svn:keywords set to Date Rev Author Id
File size: 4.3 KB
Line 
1/*
2 * This file Copyright (C) 2013-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 * $Id: crypto-test.c 14241 2014-01-21 03:10:30Z jordan $
8 */
9
10#include <string.h>
11
12#include "transmission.h"
13#include "crypto.h"
14
15#include "libtransmission-test.h"
16
17#define SHA_DIGEST_LENGTH 20
18
19static int
20test_torrent_hash (void)
21{
22  tr_crypto a;
23  uint8_t hash[SHA_DIGEST_LENGTH];
24  int i;
25
26  for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
27    hash[i] = i;
28
29  tr_cryptoConstruct (&a, NULL, true);
30
31  check (!tr_cryptoHasTorrentHash (&a));
32#ifdef NDEBUG
33  check (tr_cryptoGetTorrentHash (&a) == NULL);
34#endif
35
36  tr_cryptoSetTorrentHash (&a, hash);
37  check (tr_cryptoHasTorrentHash (&a));
38  check (tr_cryptoGetTorrentHash (&a) != NULL);
39  check (memcmp (tr_cryptoGetTorrentHash (&a), hash, SHA_DIGEST_LENGTH) == 0);
40
41  tr_cryptoDestruct (&a);
42
43  for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
44    hash[i] = i + 1;
45
46  tr_cryptoConstruct (&a, hash, false);
47
48  check (tr_cryptoHasTorrentHash (&a));
49  check (tr_cryptoGetTorrentHash (&a) != NULL);
50  check (memcmp (tr_cryptoGetTorrentHash (&a), hash, SHA_DIGEST_LENGTH) == 0);
51
52  tr_cryptoSetTorrentHash (&a, NULL);
53  check (!tr_cryptoHasTorrentHash (&a));
54#ifdef NDEBUG
55  check (tr_cryptoGetTorrentHash (&a) == NULL);
56#endif
57
58  tr_cryptoDestruct (&a);
59
60  return 0;
61}
62
63static int
64test_encrypt_decrypt (void)
65{
66  tr_crypto a, b;
67  uint8_t hash[SHA_DIGEST_LENGTH];
68  const char test1[] = { "test1" };
69  char buf11[sizeof (test1)], buf12[sizeof (test1)];
70  const char test2[] = { "@#)C$@)#(*%bvkdjfhwbc039bc4603756VB3)" };
71  char buf21[sizeof (test2)], buf22[sizeof (test2)];
72  int i;
73
74  for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
75    hash[i] = i;
76
77  tr_cryptoConstruct (&a, hash, false);
78  tr_cryptoConstruct (&b, hash, true);
79  tr_cryptoComputeSecret (&a, tr_cryptoGetMyPublicKey (&b, &i));
80  tr_cryptoComputeSecret (&b, tr_cryptoGetMyPublicKey (&a, &i));
81
82  tr_cryptoEncryptInit (&a);
83  tr_cryptoEncrypt (&a, sizeof (test1), test1, buf11);
84  tr_cryptoDecryptInit (&b);
85  tr_cryptoDecrypt (&b, sizeof (test1), buf11, buf12);
86  check_streq (test1, buf12);
87
88  tr_cryptoEncryptInit (&b);
89  tr_cryptoEncrypt (&b, sizeof (test2), test2, buf21);
90  tr_cryptoDecryptInit (&a);
91  tr_cryptoDecrypt (&a, sizeof (test2), buf21, buf22);
92  check_streq (test2, buf22);
93
94  tr_cryptoDestruct (&b);
95  tr_cryptoDestruct (&a);
96
97  return 0;
98}
99
100static int
101test_sha1 (void)
102{
103  uint8_t hash[SHA_DIGEST_LENGTH];
104
105  tr_sha1 (hash, "test", 4, NULL);
106  check (memcmp (hash, "\xa9\x4a\x8f\xe5\xcc\xb1\x9b\xa6\x1c\x4c\x08\x73\xd3\x91\xe9\x87\x98\x2f\xbb\xd3", SHA_DIGEST_LENGTH) == 0);
107
108  tr_sha1 (hash, "1", 1, "22", 2, "333", 3, NULL);
109  check (memcmp (hash, "\x1f\x74\x64\x8e\x50\xa6\xa6\x70\x8e\xc5\x4a\xb3\x27\xa1\x63\xd5\x53\x6b\x7c\xed", SHA_DIGEST_LENGTH) == 0);
110
111  return 0;
112}
113
114static int
115test_ssha1 (void)
116{
117  const char * const test_data[] =
118    {
119      "test",
120      "QNY)(*#$B)!_X$B !_B#($^!)*&$%CV!#)&$C!@$(P*)"
121    };
122
123  size_t i;
124
125#define HASH_COUNT (16 * 1024)
126
127  for (i = 0; i < sizeof (test_data) / sizeof (*test_data); ++i)
128    {
129      char * const phrase = tr_strdup (test_data[i]);
130      char ** hashes = tr_new (char *, HASH_COUNT);
131      size_t j;
132
133      for (j = 0; j < HASH_COUNT; ++j)
134        {
135          hashes[j] = tr_ssha1 (phrase);
136
137          check (hashes[j] != NULL);
138
139          /* phrase matches each of generated hashes */
140          check (tr_ssha1_matches (hashes[j], phrase));
141        }
142
143      for (j = 0; j < HASH_COUNT; ++j)
144        {
145          size_t k;
146
147          /* all hashes are different */
148          for (k = 0; k < HASH_COUNT; ++k)
149            check (k == j || strcmp (hashes[j], hashes[k]) != 0);
150        }
151
152      /* exchange two first chars */
153      phrase[0] ^= phrase[1];
154      phrase[1] ^= phrase[0];
155      phrase[0] ^= phrase[1];
156
157      for (j = 0; j < HASH_COUNT; ++j)
158        /* changed phrase doesn't match the hashes */
159        check (!tr_ssha1_matches (hashes[j], phrase));
160
161      for (j = 0; j < HASH_COUNT; ++j)
162        tr_free (hashes[j]);
163
164      tr_free (hashes);
165      tr_free (phrase);
166    }
167
168#undef HASH_COUNT
169
170  return 0;
171}
172
173int
174main (void)
175{
176  const testFunc tests[] = { test_torrent_hash,
177                             test_encrypt_decrypt,
178                             test_sha1,
179                             test_ssha1 };
180
181  return runTests (tests, NUM_TESTS (tests));
182}
Note: See TracBrowser for help on using the repository browser.