Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* unit-test-util.c: Test gck-util.c
3 :
4 : Copyright (C) 2007 Stefan Walter
5 :
6 : The Gnome Keyring Library is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Library General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : The Gnome Keyring Library is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Library General Public License for more details.
15 :
16 : You should have received a copy of the GNU Library General Public
17 : License along with the Gnome Library; see the file COPYING.LIB. If not,
18 : <http://www.gnu.org/licenses/>.
19 :
20 : Author: Stef Walter <stef@memberwebs.com>
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include "egg/egg-hex.h"
26 :
27 : #include <stdlib.h>
28 : #include <stdio.h>
29 : #include <string.h>
30 :
31 : static const guchar TEST_DATA[] = { 0x05, 0xD6, 0x95, 0x96, 0x10, 0x12, 0xAE, 0x35 };
32 : static const gchar *TEST_HEX = "05D695961012AE35";
33 : static const gchar *TEST_HEX_DELIM = "05 D6 95 96 10 12 AE 35";
34 :
35 : static void
36 1 : test_encode (void)
37 : {
38 : gchar *hex;
39 :
40 1 : hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA));
41 1 : g_assert (hex);
42 1 : g_assert_cmpstr (hex, ==, TEST_HEX);
43 :
44 1 : g_free (hex);
45 1 : }
46 :
47 : static void
48 1 : test_encode_spaces (void)
49 : {
50 : gchar *hex;
51 :
52 : /* Encode without spaces */
53 1 : hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, 0, 0);
54 1 : g_assert (hex);
55 1 : g_assert_cmpstr (hex, ==, TEST_HEX);
56 :
57 1 : g_free (hex);
58 :
59 : /* Encode with spaces */
60 1 : hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, " ", 1);
61 1 : g_assert (hex);
62 1 : g_assert_cmpstr (hex, ==, TEST_HEX_DELIM);
63 :
64 1 : g_free (hex);
65 1 : }
66 :
67 : static void
68 1 : test_decode (void)
69 : {
70 : guchar *data;
71 : gsize n_data;
72 :
73 1 : data = egg_hex_decode (TEST_HEX, -1, &n_data);
74 1 : g_assert (data);
75 1 : g_assert (n_data == sizeof (TEST_DATA));
76 1 : g_assert (memcmp (data, TEST_DATA, n_data) == 0);
77 1 : g_free (data);
78 :
79 : /* Nothing in, empty out */
80 1 : data = egg_hex_decode ("AB", 0, &n_data);
81 1 : g_assert (data);
82 1 : g_assert (n_data == 0);
83 1 : g_free (data);
84 :
85 : /* Delimited*/
86 1 : data = egg_hex_decode_full (TEST_HEX_DELIM, -1, " ", 1, &n_data);
87 1 : g_assert (data);
88 1 : g_assert (n_data == sizeof (TEST_DATA));
89 1 : g_assert (memcmp (data, TEST_DATA, n_data) == 0);
90 1 : g_free (data);
91 1 : }
92 :
93 : static void
94 1 : test_decode_fail (void)
95 : {
96 : guchar *data;
97 : gsize n_data;
98 :
99 : /* Invalid input, null out */
100 1 : data = egg_hex_decode ("AB", 1, &n_data);
101 1 : g_assert (!data);
102 :
103 : /* Bad characters, null out */
104 1 : data = egg_hex_decode ("ABXX", -1, &n_data);
105 1 : g_assert (!data);
106 :
107 : /* Not Delimited, null out*/
108 1 : data = egg_hex_decode_full ("ABABAB", -1, ":", 1, &n_data);
109 1 : g_assert (!data);
110 1 : }
111 :
112 : int
113 1 : main (int argc, char **argv)
114 : {
115 1 : g_test_init (&argc, &argv, NULL);
116 :
117 1 : g_test_add_func ("/hex/encode", test_encode);
118 1 : g_test_add_func ("/hex/encode_spaces", test_encode_spaces);
119 1 : g_test_add_func ("/hex/decode", test_decode);
120 1 : g_test_add_func ("/hex/decode_fail", test_decode_fail);
121 :
122 1 : return g_test_run ();
123 : }
|