433625e7620f678e89410b6b2b3ad28f825ccbdd
[debian/amanda] / common-src / hexencode-test.c
1 /*
2  * Copyright (c) Zmanda Inc.  All Rights Reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1 as
6  * published by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
11  * License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19  */
20
21 #include "amanda.h"
22 #include "util.h"
23 #include "testutils.h"
24 #include "simpleprng.h"
25
26 static int test_encode(void);
27 static int test_decode(void);
28 static int test_roundtrip(void);
29 static int test_roundtrip_rand(void);
30
31 typedef struct {char *in; char *out;} enc_vec;
32 static int
33 test_encode(void)
34 {
35     static const enc_vec test_strs[] = {
36         {"hi", "hi"},
37         {"hi!", "hi%21"},
38         {"%", "%25"},
39         {"*", "%2a"},
40         {"\n", "%0a"},
41         {"\nhi\n", "%0ahi%0a"}
42     };
43     static const int num = sizeof(test_strs)/sizeof(enc_vec);
44     int i, ret;
45     char *tmp;
46     
47     ret = TRUE;
48     for (i = 0; i < num; i++) {
49         tmp = hexencode_string(test_strs[i].in);
50         if (!tmp || strcmp(test_strs[i].out, tmp)) {
51             ret = FALSE;
52             tu_dbg("encode failure:\n")
53             tu_dbg("input:    \"%s\"\n", test_strs[i].in);
54             tu_dbg("output:   \"%s\"\n", tmp? tmp : "(null)");
55             tu_dbg("expected: \"%s\"\n", test_strs[i].out);
56         }
57         g_free(tmp);
58     }
59     return ret;
60 }
61
62 typedef struct {char *in; char *out; gboolean expect_err; } dec_vec;
63 static int
64 test_decode(void)
65 {
66     static const dec_vec test_strs[] = {
67         {"hi", "hi", FALSE},
68         {"hi%21", "hi!", FALSE},
69         {"%25", "%", FALSE},
70         {"%2a", "*", FALSE},
71         {"%2A", "*", FALSE},
72         {"%0a", "\n", FALSE},
73         {"%0A", "\n", FALSE},
74         {"%0ahi%0a", "\nhi\n", FALSE},
75         {"%", "", TRUE},
76         {"%2", "", TRUE},
77         {"h%", "", TRUE},
78         {"%0h", "", TRUE},
79         {"%h0", "", TRUE},
80         {"%00", "", TRUE}
81     };
82     static const int num = sizeof(test_strs)/sizeof(dec_vec);
83     int i, ret;
84     char *tmp;
85     GError *err = NULL;
86     
87     ret = TRUE;
88     for (i = 0; i < num; i++) {
89         tmp = hexdecode_string(test_strs[i].in, &err);
90         if (!tmp || strcmp(test_strs[i].out, tmp) ||
91             (!!err != test_strs[i].expect_err)) {
92             ret = FALSE;
93             tu_dbg("decode failure:\n")
94             tu_dbg("input:     \"%s\"\n", test_strs[i].in);
95             tu_dbg("output:    \"%s\"\n", tmp? tmp : "(null)");
96             tu_dbg("expected:  \"%s\"\n", test_strs[i].out);
97             tu_dbg("error msg: %s\n", err? err->message : "(none)");
98         }
99         g_clear_error(&err);
100         g_free(tmp);
101             
102     }
103     return ret;
104 }
105
106 typedef char* round_vec;
107 static int
108 test_roundtrip(void)
109 {
110     static const round_vec test_strs[] = {
111         "hi",
112         "hi!",
113         "hi%21",
114         "%",
115         "*",
116         "\n",
117         "h%"
118     };
119     static const int num = sizeof(test_strs)/sizeof(round_vec);
120     int i, ret;
121     char *tmp_enc = NULL, *tmp_dec = NULL;
122     GError *err = NULL;
123     
124     ret = TRUE;
125     for (i = 0; i < num; i++) {
126         tmp_enc = hexencode_string(test_strs[i]);
127         tmp_dec = tmp_enc? hexdecode_string(tmp_enc, &err) : NULL;
128         if (!tmp_enc || !tmp_dec || strcmp(test_strs[i], tmp_dec) || err) {
129             ret = FALSE;
130             tu_dbg("roundtrip failure:\n")
131             tu_dbg("input:      \"%s\"\n", test_strs[i]);
132             tu_dbg("enc output: \"%s\"\n", tmp_enc? tmp_enc : "(null)");
133             tu_dbg("dec output: \"%s\"\n", tmp_dec? tmp_dec : "(null)");
134             tu_dbg("error msg:  %s\n", err? err->message : "(none)");
135         }
136         g_clear_error(&err);
137         amfree(tmp_enc);
138         amfree(tmp_dec);
139     }
140     return ret;
141 }
142
143 static int
144 test_roundtrip_rand(void)
145 {
146     int i, ret;
147     simpleprng_state_t state;
148     char *in, *tmp_enc = NULL, *tmp_dec = NULL;
149     size_t size;
150     GError *err = NULL;
151     
152     simpleprng_seed(&state, 0xface);
153     ret = TRUE;
154     for (i = 0; i < 100; i++) {
155         size = simpleprng_rand_byte(&state);
156         in = g_malloc0(size+1);
157         simpleprng_fill_buffer(&state, in, size);
158         tmp_enc = hexencode_string(in);
159         tmp_dec = tmp_enc? hexdecode_string(tmp_enc, &err) : NULL;
160         if (!tmp_enc || !tmp_dec || strcmp(in, tmp_dec) || err) {
161             ret = FALSE;
162             tu_dbg("roundtrip failure:\n")
163             tu_dbg("input:      \"%s\"\n", in);
164             tu_dbg("enc output: \"%s\"\n", tmp_enc? tmp_enc : "(null)");
165             tu_dbg("dec output: \"%s\"\n", tmp_dec? tmp_dec : "(null)");
166             tu_dbg("error msg:  %s\n", err? err->message : "(none)");
167         }
168         g_clear_error(&err);
169         amfree(tmp_enc);
170         amfree(tmp_dec);
171         g_free(in);
172     }
173     return ret;
174 }
175
176 int
177 main(int argc, char **argv)
178 {
179     static TestUtilsTest tests[] = {
180         TU_TEST(test_encode, 90),
181         TU_TEST(test_decode, 90),
182         TU_TEST(test_roundtrip, 90),
183         TU_TEST(test_roundtrip_rand, 90),
184         TU_END()
185     };
186     return testutils_run_tests(argc, argv, tests);
187 }