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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* unit-test-util.c: Test hex routines
Copyright (C) 2007 Stefan Walter
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <stef@memberwebs.com>
*/
#include "config.h"
#include "egg/egg-hex.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static const guchar TEST_DATA[] = { 0x05, 0xD6, 0x95, 0x96, 0x10, 0x12, 0xAE, 0x35 };
static const gchar *TEST_HEX = "05D695961012AE35";
static const gchar *TEST_HEX_DELIM = "05 D6 95 96 10 12 AE 35";
static void
test_encode (void)
{
gchar *hex;
hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA));
g_assert (hex);
g_assert_cmpstr (hex, ==, TEST_HEX);
}
static void
test_encode_spaces (void)
{
gchar *hex;
/* Encode without spaces */
hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, 0, 0);
g_assert (hex);
g_assert_cmpstr (hex, ==, TEST_HEX);
/* Encode with spaces */
hex = egg_hex_encode_full (TEST_DATA, sizeof (TEST_DATA), TRUE, ' ', 1);
g_assert (hex);
g_assert_cmpstr (hex, ==, TEST_HEX_DELIM);
}
static void
test_decode (void)
{
guchar *data;
gsize n_data;
data = egg_hex_decode (TEST_HEX, -1, &n_data);
g_assert (data);
g_assert (n_data == sizeof (TEST_DATA));
g_assert (memcmp (data, TEST_DATA, n_data) == 0);
g_free (data);
/* Nothing in, empty out */
data = egg_hex_decode ("AB", 0, &n_data);
g_assert (data);
g_assert (n_data == 0);
g_free (data);
/* Delimited*/
data = egg_hex_decode_full (TEST_HEX_DELIM, -1, ' ', 1, &n_data);
g_assert (data);
g_assert (n_data == sizeof (TEST_DATA));
g_assert (memcmp (data, TEST_DATA, n_data) == 0);
g_free (data);
}
static void
test_decode_fail (void)
{
guchar *data;
gsize n_data;
/* Invalid input, null out */
data = egg_hex_decode ("AB", 1, &n_data);
g_assert (!data);
/* Bad characters, null out */
data = egg_hex_decode ("ABXX", -1, &n_data);
g_assert (!data);
/* Not Delimited, null out*/
data = egg_hex_decode_full ("ABABAB", -1, ':', 1, &n_data);
g_assert (!data);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/hex/encode", test_encode);
g_test_add_func ("/hex/encode_spaces", test_encode_spaces);
g_test_add_func ("/hex/decode", test_decode);
g_test_add_func ("/hex/decode_fail", test_decode_fail);
return g_test_run ();
}
|