Inital Packaging on 0.10
[platform/upstream/libsecret.git] / egg / test-hex.c
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    see <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 test_encode (void)
37 {
38         gchar *hex;
39
40         hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA));
41         g_assert (hex);
42         g_assert_cmpstr (hex, ==, TEST_HEX);
43
44         g_free (hex);
45 }
46
47 static void
48 test_encode_spaces (void)
49 {
50         gchar *hex;
51
52         /* Encode without spaces */
53         hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, 0, 0);
54         g_assert (hex);
55         g_assert_cmpstr (hex, ==, TEST_HEX);
56
57         g_free (hex);
58
59         /* Encode with spaces */
60         hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, "  ", 1);
61         g_assert (hex);
62         g_assert_cmpstr (hex, ==, TEST_HEX_DELIM);
63
64         g_free (hex);
65 }
66
67 static void
68 test_decode (void)
69 {
70         guchar *data;
71         gsize n_data;
72
73         data = egg_hex_decode (TEST_HEX, -1, &n_data);
74         g_assert (data);
75         g_assert (n_data == sizeof (TEST_DATA));
76         g_assert (memcmp (data, TEST_DATA, n_data) == 0);
77         g_free (data);
78
79         /* Nothing in, empty out */
80         data = egg_hex_decode ("AB", 0, &n_data);
81         g_assert (data);
82         g_assert (n_data == 0);
83         g_free (data);
84
85         /* Delimited*/
86         data = egg_hex_decode_full (TEST_HEX_DELIM, -1, "  ", 1, &n_data);
87         g_assert (data);
88         g_assert (n_data == sizeof (TEST_DATA));
89         g_assert (memcmp (data, TEST_DATA, n_data) == 0);
90         g_free (data);
91 }
92
93 static void
94 test_decode_fail (void)
95 {
96         guchar *data;
97         gsize n_data;
98
99         /* Invalid input, null out */
100         data = egg_hex_decode ("AB", 1, &n_data);
101         g_assert (!data);
102
103         /* Bad characters, null out */
104         data = egg_hex_decode ("ABXX", -1, &n_data);
105         g_assert (!data);
106
107         /* Not Delimited, null out*/
108         data = egg_hex_decode_full ("ABABAB", -1, ":", 1, &n_data);
109         g_assert (!data);
110 }
111
112 int
113 main (int argc, char **argv)
114 {
115         g_test_init (&argc, &argv, NULL);
116
117         g_test_add_func ("/hex/encode", test_encode);
118         g_test_add_func ("/hex/encode_spaces", test_encode_spaces);
119         g_test_add_func ("/hex/decode", test_decode);
120         g_test_add_func ("/hex/decode_fail", test_decode_fail);
121
122         return g_test_run ();
123 }