libpurple/tests/test_des.c

changeset 38413
e35c6b6ca094
parent 38412
e25c73450414
child 38414
3892b306732f
equal deleted inserted replaced
38412:e25c73450414 38413:e35c6b6ca094
1 /*
2 * purple
3 *
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 *
22 */
23 #include <glib.h>
24
25 #include <purple.h>
26
27 #include "ciphers/descipher.h"
28
29 static void
30 test_des_cipher(const gchar *in, const gchar *key, const gchar *out, size_t len) {
31 PurpleCipher *cipher = NULL;
32 guchar *decrypt = NULL, *encrypt = NULL, *answer = NULL;
33 size_t ret = 0;
34
35 decrypt = g_memdup(in, len + 1);
36 encrypt = g_memdup(out, len + 1);
37
38 cipher = purple_des_cipher_new();
39
40 purple_cipher_set_key(cipher, (const guchar *)key, 8);
41
42 answer = g_new0(guchar, len + 1);
43 ret = purple_cipher_encrypt(cipher, decrypt, len, answer, len);
44 g_assert(ret == len);
45 g_assert_cmpmem(encrypt, len, answer, len);
46 g_free(answer);
47
48 answer = g_new0(guchar, len + 1);
49 ret = purple_cipher_decrypt(cipher, encrypt, len, answer, len);
50 g_assert(ret == len);
51 g_assert_cmpmem(decrypt, len, answer, len);
52 g_free(answer);
53
54 g_free(encrypt);
55 g_free(decrypt);
56
57 g_object_unref(G_OBJECT(cipher));
58 }
59
60 static void
61 test_des_cipher_12345678(void) {
62 test_des_cipher(
63 "12345678",
64 "\x3b\x38\x98\x37\x15\x20\xf7\x5e",
65 "\x06\x22\x05\xac\x6a\x0d\x55\xdd",
66 8
67 );
68 }
69
70 static void
71 test_des_cipher_abcdefgh(void) {
72 test_des_cipher(
73 "abcdefgh",
74 "\x3b\x38\x98\x37\x15\x20\xf7\x5e",
75 "\x62\xe0\xc6\x8c\x48\xe4\x75\xed",
76 8
77 );
78 }
79
80 gint
81 main(gint argc, gchar **argv) {
82 g_test_init(&argc, &argv, NULL);
83
84 g_test_add_func("/cipher/des/12345678",
85 test_des_cipher_12345678);
86
87 g_test_add_func("/cipher/des/abcdefgh",
88 test_des_cipher_abcdefgh);
89
90 return g_test_run();
91 }

mercurial