aliasing
[platform/upstream/xdelta1.git] / libedsio / edsiotest.c
1 /* -*-Mode: C;-*-
2  * $Id: edsiotest.c 1.1 Sun, 28 Jan 2007 10:02:26 -0800 jmacd $
3  *
4  * Copyright (C) 1998, 1999, Josh MacDonald.
5  * All Rights Reserved.
6  *
7  * Author: Josh MacDonald <jmacd@CS.Berkeley.EDU>
8  */
9
10 #include "edsio.h"
11
12 void
13 test1 ()
14 {
15   guint32 x = 0;
16
17   PropTest *pt = g_new0 (PropTest, 1);
18
19   EdsioPropTestUintProperty prop;
20
21   g_assert (edsio_edsio_init ());
22
23   g_assert (edsio_new_proptest_uint_property ("Just testing (1)...", PF_Persistent, & prop));
24
25   g_assert (! proptest_isset_uint (pt, prop));
26
27   g_assert (proptest_set_uint (pt, prop, 42));
28
29   g_assert (proptest_isset_uint (pt, prop));
30
31   g_assert (proptest_get_uint (pt, prop, & x) && x == 42);
32
33   /* kill the cache, to test persistence. */
34   pt->_edsio_property_table = NULL;
35
36   g_assert (proptest_get_uint (pt, prop, & x) && x == 42);
37
38   g_assert (proptest_unset_uint (pt, prop));
39
40   g_assert (! proptest_isset_uint (pt, prop));
41
42   g_assert (! pt->_edsio_property_table);
43 }
44
45 void
46 test2 ()
47 {
48   const char* str = "hello there";
49   const char* str2;
50   guint32 str2_len;
51   const char ** str2_ptr = &str2;
52
53   PropTest *pt = g_new0 (PropTest, 1);
54
55   EdsioPropTestBytesProperty prop;
56
57   g_assert (edsio_edsio_init ());
58
59   g_assert (edsio_new_proptest_bytes_property ("Just testing (2)...", PF_Persistent, & prop));
60
61   g_assert (! proptest_isset_bytes (pt, prop));
62
63   g_assert (proptest_set_bytes (pt, prop, (guint8*) str, strlen (str) + 1));
64
65   g_assert (proptest_isset_bytes (pt, prop));
66
67   g_assert (proptest_get_bytes (pt, prop, (const guint8**) str2_ptr, & str2_len) && str2_len == (strlen (str) + 1) && strcmp (str, str2) == 0);
68
69   /* kill the cache, to test persistence. */
70   pt->_edsio_property_table = NULL;
71
72   g_assert (proptest_get_bytes (pt, prop, (const guint8**) str2_ptr, & str2_len) && str2_len == (strlen (str) + 1) && strcmp (str, str2) == 0);
73
74   g_assert (proptest_unset_bytes (pt, prop));
75
76   g_assert (! proptest_isset_bytes (pt, prop));
77
78   g_assert (! pt->_edsio_property_table);
79 }
80
81 void
82 test3 ()
83 {
84   SerialEdsioUint x;
85   SerialEdsioUint *p;
86   EdsioPropTestEdsioUintProperty prop;
87
88   PropTest *pt = g_new0 (PropTest, 1);
89
90   x.val = 42;
91
92   g_assert (edsio_edsio_init ());
93
94   g_assert (edsio_new_proptest_edsiouint_property ("Just testing (3)...", PF_Persistent, & prop));
95
96   g_assert (! proptest_isset_edsiouint (pt, prop));
97
98   g_assert (proptest_set_edsiouint (pt, prop, & x));
99
100   g_assert (proptest_isset_edsiouint (pt, prop));
101
102   g_assert (proptest_get_edsiouint (pt, prop, & p) && x.val == p->val);
103
104   /* kill the cache, to test persistence. */
105   pt->_edsio_property_table = NULL;
106
107   g_assert (proptest_get_edsiouint (pt, prop, & p) && x.val == p->val);
108
109   g_assert (proptest_unset_edsiouint (pt, prop));
110
111   g_assert (! proptest_isset_edsiouint (pt, prop));
112
113   g_assert (! pt->_edsio_property_table);
114 }
115
116 void
117 test4 ()
118 {
119   const char* str = "hello there";
120   const char* str2;
121
122   PropTest *pt = g_new0 (PropTest, 1);
123
124   EdsioPropTestStringProperty prop;
125
126   g_assert (edsio_edsio_init ());
127
128   g_assert (edsio_new_proptest_string_property ("Just testing (4)...", PF_Persistent, & prop));
129
130   g_assert (! proptest_isset_string (pt, prop));
131
132   g_assert (proptest_set_string (pt, prop, str));
133
134   g_assert (proptest_isset_string (pt, prop));
135
136   g_assert (proptest_get_string (pt, prop, & str2) && strcmp (str, str2) == 0);
137
138   /* kill the cache, to test persistence. */
139   pt->_edsio_property_table = NULL;
140
141   g_assert (proptest_get_string (pt, prop, & str2) && strcmp (str, str2) == 0);
142
143   g_assert (proptest_unset_string (pt, prop));
144
145   g_assert (! proptest_isset_string (pt, prop));
146
147   g_assert (! pt->_edsio_property_table);
148 }
149
150 void test5 ()
151 {
152   GByteArray* sink_result;
153   SerialSink* sink = edsio_simple_sink (NULL, SBF_Checksum | SBF_Base64 | SBF_Compress, FALSE, NULL, & sink_result);
154   SerialSource* src;
155   const char* input = "hello there!!!!!!!!";
156   SerialEdsioString *output;
157   guint8 zero[1];
158   zero[0] = 0;
159
160   g_assert (serialize_edsiostring (sink, input));
161
162   g_assert (sink->sink_close (sink));
163
164   g_byte_array_append (sink_result, zero, 1);
165
166   g_print ("%s -> %s\n", input, sink_result->data);
167
168   src = edsio_simple_source (sink_result->data, sink_result->len - 1, SBF_Checksum | SBF_Base64 | SBF_Compress);
169
170   g_assert (unserialize_edsiostring (src, & output));
171
172   g_assert (src->source_close (src));
173
174   g_assert (strcmp (output->val, input) == 0);
175 }
176
177 void
178 test6 ()
179 {
180   const char* md5str = "aed3918c4ccb89f2dcf74d5dcab22989";
181   const char* md5strbad1 = "aed3918c4cXb89f2dcf74d5dcab22989";
182   const char* md5strbad2 = "aed3918c4cab89f2dcf74d5dcab22989X";
183   const char* md5strbad3 = "aed3918c4cab89f2dcf74d5dcab2298";
184   char md5str2[33];
185   guint8 md5[16];
186
187   g_assert (! edsio_md5_from_string (md5, md5strbad1));
188   g_assert (! edsio_md5_from_string (md5, md5strbad2));
189   g_assert (! edsio_md5_from_string (md5, md5strbad3));
190
191   g_assert (edsio_md5_from_string (md5, md5str));
192
193   edsio_md5_to_string (md5, md5str2);
194
195   g_assert (strcmp (md5str, md5str2) == 0);
196 }
197
198 int
199 main ()
200 {
201   test1 ();
202
203   test2 ();
204
205   test3 ();
206
207   test4 ();
208
209   test5 ();
210
211   test6 ();
212
213   return 0;
214 }