summaryrefslogtreecommitdiff
path: root/egg/tests/unit-test-hex.c
blob: f620991af4e1e88d74eaf6e5bb797f04bf006bd3 (plain)
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* unit-test-util.c: Test gck-util.c

   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 <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "run-auto-test.h"

#include "egg-hex.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 const gchar *TEST_HEX_SPACE = "\n05 D695 \r961012AE35\n\n";

DEFINE_TEST(hex_encode)
{
	gchar *hex;

	hex = egg_hex_encode (TEST_DATA, sizeof (TEST_DATA));
	g_assert (hex);
	g_assert_cmpstr (hex, ==, TEST_HEX);
}

DEFINE_TEST(hex_encode_spaces)
{
	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);
}

DEFINE_TEST(hex_decode)
{
	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);

	/* Spaces should be ignored */
	data = egg_hex_decode (TEST_HEX_SPACE, -1, &n_data);
	g_assert (data);
	g_assert (n_data == sizeof (TEST_DATA));
	g_assert (memcmp (data, TEST_DATA, n_data) == 0);

	/* Invalid input, null out */
	data = egg_hex_decode ("AB", 1, &n_data);
	g_assert (!data);

	/* Nothing in, empty out */
	data = egg_hex_decode ("AB", 0, &n_data);
	g_assert (data);
	g_assert (n_data == 0);
}