File: test_crypto.cc

package info (click to toggle)
astroidmail 0.17%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,160 kB
  • sloc: cpp: 21,133; ansic: 1,619; python: 93; sh: 73; makefile: 7
file content (376 lines) | stat: -rw-r--r-- 9,924 bytes parent folder | download | duplicates (6)
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
# define BOOST_TEST_DYN_LINK
# define BOOST_TEST_MODULE TestCrypto
# include <boost/test/unit_test.hpp>

# include "test_common.hh"
# include "compose_message.hh"
# include "crypto.hh"
# include "message_thread.hh"
# include "account_manager.hh"
# include "db.hh"
# include "utils/gmime/gmime-compat.h"


BOOST_AUTO_TEST_SUITE(GPGEncryption)

  BOOST_AUTO_TEST_CASE(crypto_compose_simple_test)
  {
    using Astroid::ComposeMessage;
    using Astroid::Account;
    setup ();

    Account a = astroid->accounts->accounts[0];
    LOG (trace) << "cy: account gpg: " << a.gpgkey;

    ComposeMessage * c = new ComposeMessage ();
    c->set_from (&a);

    delete c;

    teardown ();
  }

  BOOST_AUTO_TEST_CASE (crypto_compose_test_encrypt_body)
  {
    using Astroid::ComposeMessage;
    using Astroid::Account;
    using Astroid::Message;
    setup ();

    Account a = astroid->accounts->accounts[0];
    LOG (trace) << "cy: account gpg: " << a.gpgkey;
    a.email = "gaute@astroidmail.bar";

    ComposeMessage * c = new ComposeMessage ();
    c->set_from (&a);
    c->set_to ("astrid@astroidmail.bar");
    c->encrypt =  true;
    c->sign = false;

    ustring bdy = "This is test: æøå.\n > testing\ntesting\n...";

    LOG (trace) << "cm: writing utf-8 text to message body: " << bdy;
    c->body << bdy;

    LOG (trace) << "build:";
    c->build ();
    LOG (trace) << "finalize:";
    c->finalize ();
    ustring fn = c->write_tmp ();

    BOOST_CHECK_MESSAGE (c->encryption_success == true, "encryption should be successful");
    LOG (test) << "cm: encryption error: " << c->encryption_error;

    LOG (test) << "cm: encrypted content: ";
    LOG (test) << g_mime_object_to_string (GMIME_OBJECT(c->message), NULL);

    LOG (test) << "cm: deleting ComposeMessage..";

    delete c;

    Message m (fn);
    ustring rbdy = m.plain_text (false);

    BOOST_CHECK_MESSAGE (bdy == rbdy, "message reading produces the same output as compose message input");

    unlink (fn.c_str ());

    teardown ();
  }

  BOOST_AUTO_TEST_CASE (crypto_compose_test_sign_body)
  {
    using Astroid::ComposeMessage;
    using Astroid::Account;
    using Astroid::Message;
    setup ();

    Account a = astroid->accounts->accounts[0];
    LOG (test) << "cy: account gpg: " << a.gpgkey;
    a.email = "gaute@astroidmail.bar";

    ComposeMessage * c = new ComposeMessage ();
    c->set_from (&a);
    c->set_to ("who@astroidmail.bar"); // no pub key
    c->encrypt = false;
    c->sign = true;

    ustring bdy = "This is test: æøå.\n > testing\ntesting\n...";

    LOG (test) << "cm: writing utf-8 text to message body: " << bdy;
    c->body << bdy;

    c->build ();
    c->finalize ();
    ustring fn = c->write_tmp ();

    BOOST_CHECK_MESSAGE (c->encryption_success == true, "encryption should be successful");
    LOG (test) << "cm: encryption error: " << c->encryption_error;

    LOG (test) << "cm: encrypted content: ";
    LOG (test) << g_mime_object_to_string (GMIME_OBJECT(c->message), NULL);

    LOG (test) << "cm: deleting ComposeMessage..";

    delete c;

    Message m (fn);

    ustring rbdy = m.plain_text (false);

    BOOST_CHECK_MESSAGE (bdy == rbdy, "message reading produces the same output as compose message input");

    unlink (fn.c_str ());

    teardown ();
  }


  BOOST_AUTO_TEST_CASE (crypto_missing_pub)
  {
    using Astroid::ComposeMessage;
    using Astroid::Account;
    using Astroid::Message;
    setup ();

    Account a = astroid->accounts->accounts[0];
    LOG (test) << "cy: account gpg: " << a.gpgkey;
    a.email = "gaute@astroidmail.bar";

    ComposeMessage * c = new ComposeMessage ();
    c->set_from (&a);
    c->set_to ("err@astroidmail.bar"); // unknown
    c->encrypt = true;

    ustring bdy = "This is test: æøå.\n > testing\ntesting\n...";

    c->body << bdy;

    LOG (test) << "build:";
    c->build ();

    LOG (test) << "finalize:";
    c->finalize ();

    BOOST_CHECK_MESSAGE (c->encryption_success == false, "encryption should fail");
    LOG (test) << "cm: encryption error: " << c->encryption_error;

    LOG (test) << "cm: encrypted content: ";
    LOG (test) << g_mime_object_to_string (GMIME_OBJECT(c->message), NULL);

    delete c;


    teardown ();
  }

  BOOST_AUTO_TEST_CASE (crypto_too_many_open_files)
  {
    using Astroid::ComposeMessage;
    using Astroid::Account;
    using Astroid::Message;
    using Astroid::MessageThread;
    using Astroid::message_error;
    using Astroid::Db;
    using Astroid::NotmuchThread;
    setup ();

    Account a = astroid->accounts->accounts[0];
    LOG (trace) << "cy: account gpg: " << a.gpgkey;
    a.email = "gaute@astroidmail.bar";

    /* create a test message */
    ComposeMessage * c = new ComposeMessage ();
    c->set_from (&a);
    c->set_to ("astrid@astroidmail.bar");
    c->encrypt =  true;
    c->sign = true;

    ustring bdy = "This is test: æøå.\n > testing\ntesting\n...";

    c->body << bdy;
    c->set_subject ("test gpg e-mail");
    ustring mid = "test-gpg-1@astroid";
    c->set_id (mid);

    c->build ();
    c->finalize ();

    ustring fn = "tests/mail/test_mail/gpg-test-1.eml";
    c->write (fn);
    system ("notmuch new; notmuch search \"*\"");

    BOOST_CHECK_MESSAGE (c->encryption_success == true, "encryption should be successful");
    LOG (test) << "cm: deleting ComposeMessage..";

    delete c;

    Message *m;

    {
      Db db(Db::DATABASE_READ_ONLY);
      db.on_message (mid, [&](notmuch_message_t * msg) {
          LOG (test) << "trying to open encrypted file.";

          m = new Message (msg, 0);

          });
    }

    /* check that body matches */
    ustring rbdy = m->plain_text (false);
    BOOST_CHECK_MESSAGE (bdy == rbdy, "message reading produces the same output as compose message input");

    /* notmuch thread id */
    ustring tid = m->tid;
    delete m;

    refptr<NotmuchThread> nt;

    LOG (test) << "trying to open thread: " << tid;

    {
      Db db(Db::DATABASE_READ_ONLY);
      db.on_thread (tid, [&](notmuch_thread_t * nmt) {

          BOOST_CHECK (nmt != NULL);
          nt = refptr<NotmuchThread> (new NotmuchThread (nmt));

          });
    }


    /* Okay, let's see if we can provoke 'Too many open files' in GPG */
    int tries = 0;
    bool failed = false;

    try {
      for (int i = 0; i < 100; i ++) {
        LOG (info) << "loading message thread..";
        refptr<MessageThread> mthread = refptr<MessageThread>(new MessageThread (nt));
        Db db (Db::DATABASE_READ_ONLY);
        mthread->load_messages (&db);

        BOOST_CHECK_MESSAGE (bdy == mthread->messages[0]->plain_text (false), "message body matches composed message");

        tries++;
      }
    } catch (Astroid::message_error &ex) {
      LOG (error) << "caught too many files error: " << ex.what ();
      failed = true;
    }

    /* let's try without using refptr */
    /*
    LOG (info) << "loading without using refptr..";
    try {
      for (int i = 0; i < 1200; i ++) {
        LOG (info) << "loading message thread..";
        MessageThread mthread (nt);
        Db db (Db::DATABASE_READ_ONLY);
        mthread.load_messages (&db);
        tries++;
      }
    } catch (Astroid::message_error &ex) {
      LOG (error) << "caught too many files error: " << ex.what ();
      failed = true;
    }
    */

    LOG (warn) << "opened a GPG e-mail: " << tries << " times.";

    BOOST_CHECK_MESSAGE (failed == false, "Caught 'Too many open files error'");

    /* removing test encrypted file */
    unlink (fn.c_str ());
    system ("notmuch new");

    teardown ();
  }

  BOOST_AUTO_TEST_CASE (decrypt_entire_message)
  {
    using Astroid::ComposeMessage;
    using Astroid::Account;
    using Astroid::Message;
    setup ();

    Account a = astroid->accounts->accounts[0];
    LOG (trace) << "cy: account gpg: " << a.gpgkey;
    a.email = "gaute@astroidmail.bar";

    ComposeMessage * c = new ComposeMessage ();
    c->set_from (&a);
    c->set_to ("astrid@astroidmail.bar");
    c->encrypt =  true;
    c->sign = false;

    ustring bdy = "This is test: æøå.\n > testing\ntesting\n...";

    c->body << bdy;

    c->build ();
    c->finalize ();
    ustring fn = c->write_tmp ();

    BOOST_CHECK_MESSAGE (c->encryption_success == true, "encryption should be successful");

    LOG (test) << "cm: encrypted content: ";
    LOG (test) << g_mime_object_to_string (GMIME_OBJECT(c->message), NULL);

    delete c;

    Message m (fn);
    ustring rbdy = m.plain_text (false);

    BOOST_CHECK_MESSAGE (bdy == rbdy, "message reading produces the same output as compose message input");

    /* Try to get a decrypted version of the message */
    GMimeMessage * _dm = m.decrypt ();
    BOOST_CHECK (_dm != NULL);
    Message dm (_dm);

    LOG (test) << "cm: decrypted content: ";
    LOG (test) << g_mime_object_to_string (GMIME_OBJECT (_dm), NULL);

    ustring dec (g_mime_object_to_string (GMIME_OBJECT (_dm), NULL));
    BOOST_CHECK (dec.find ("This is test") != std::string::npos);


    unlink (fn.c_str ());

    teardown ();
  }

  BOOST_AUTO_TEST_CASE (crypto_md5)
  {
    using Astroid::Crypto;

    ustring data = "12345asdfasdfasdf";
    ustring expected = "983443541cfec1e77671d8dd8e0ee33e"; // echo -n ... | md5sum

    ustring chk;
    BOOST_CHECK_NO_THROW (chk = Crypto::get_md5_digest (data));
    BOOST_CHECK (chk == expected);
    LOG (test) << "digest: " << chk;

    /* testing md5_b */
    refptr<Glib::Bytes> d = Crypto::get_md5_digest_b (data);

    LOG (test) << "digest, length: " << d->get_size ();

    gsize len;
    guint8 * buffer = (guint8 *) d->get_data (len);

    std::stringstream ss;
    for (unsigned int i = 0; i < len; i++)
      ss << std::hex << std::setw(2) << std::setfill ('0') << static_cast<unsigned int>(buffer[i]);

    LOG (test) << "digest, bytes:  " << ss.str ();

    BOOST_CHECK (len == 16);
    BOOST_CHECK (ss.str () == expected);
  }

BOOST_AUTO_TEST_SUITE_END()