Imported Upstream version 3.2.0
[debian/amanda] / common-src / fileheader-test.c
1 /*
2  * Copyright (c) 2010 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 "testutils.h"
23 #include "fileheader.h"
24
25 #define TAPE_HEADER(hdr) ((hdr)->type == F_TAPESTART || (hdr)->type == F_TAPEEND)
26
27 static int n_round_trips = 0;
28
29 /* actually test the round trip */
30 static int
31 try_rt(dumpfile_t *hdr)
32 {
33     char *strval;
34     size_t size;
35     dumpfile_t hdr2;
36
37     size = 0;
38     strval = build_header(hdr, &size, 32768);
39     g_assert(strval != NULL);
40     fh_init(&hdr2);
41     parse_file_header(strval, &hdr2, size);
42
43     if (!headers_are_equal(hdr, &hdr2)) {
44         tu_dbg("*** build_header of:\n");
45         dump_dumpfile_t(hdr);
46         tu_dbg("*** gives:\n");
47         tu_dbg("%s", strval);
48         tu_dbg("*** which becomes:\n");
49         dump_dumpfile_t(&hdr2);
50         return 0;
51     }
52
53     n_round_trips++;
54
55     dumpfile_free_data(&hdr2);
56     return 1;
57 #define PREV_RT try_rt
58 }
59
60 static int
61 rt_partnum(dumpfile_t *hdr)
62 {
63     if (hdr->type == F_SPLIT_DUMPFILE) {
64         hdr->partnum = 1;
65         hdr->totalparts = 2;
66         if (!PREV_RT(hdr)) return 0;
67         hdr->partnum = 2;
68         hdr->totalparts = 2;
69         if (!PREV_RT(hdr)) return 0;
70         hdr->partnum = 2;
71         hdr->totalparts = -1;
72         if (!PREV_RT(hdr)) return 0;
73     } else if (hdr->type == F_DUMPFILE) {
74         hdr->partnum = 1;
75         hdr->totalparts = 1;
76         if (!PREV_RT(hdr)) return 0;
77     } else {
78         hdr->partnum = 0;
79         hdr->totalparts = 0;
80         if (!PREV_RT(hdr)) return 0;
81     }
82     return 1;
83 #undef PREV_RT
84 #define PREV_RT rt_partnum
85 }
86
87 static int
88 rt_compress(dumpfile_t *hdr)
89 {
90     if (TAPE_HEADER(hdr)) {
91         hdr->compressed = 0;
92         strcpy(hdr->comp_suffix, "");
93         if (!PREV_RT(hdr)) return 0;
94     } else {
95         hdr->compressed = 0;
96         strcpy(hdr->comp_suffix, "");
97         if (!PREV_RT(hdr)) return 0;
98         hdr->compressed = 1;
99         strcpy(hdr->comp_suffix, ".gz");
100         if (!PREV_RT(hdr)) return 0;
101     }
102     return 1;
103 #undef PREV_RT
104 #define PREV_RT rt_compress
105 }
106
107 static int
108 rt_encrypt(dumpfile_t *hdr)
109 {
110     if (TAPE_HEADER(hdr)) {
111         hdr->encrypted = 0;
112         strcpy(hdr->encrypt_suffix, "");
113         if (!PREV_RT(hdr)) return 0;
114     } else {
115         hdr->encrypted = 0;
116         strcpy(hdr->encrypt_suffix, "");
117         if (!PREV_RT(hdr)) return 0;
118
119         /* (note: Amanda seems to use 'enc' as the only suffix, and it doesn't
120          * really use it as a suffix; using .aes here ensures that the fileheader
121          * code can handle real suffixes, too */
122         hdr->encrypted = 1;
123         strcpy(hdr->encrypt_suffix, ".aes");
124         if (!PREV_RT(hdr)) return 0;
125     }
126     return 1;
127 #undef PREV_RT
128 #define PREV_RT rt_encrypt
129 }
130
131 static int
132 rt_disk(dumpfile_t *hdr)
133 {
134     if (TAPE_HEADER(hdr)) {
135         strcpy(hdr->disk, "");
136         if (!PREV_RT(hdr)) return 0;
137     } else {
138         strcpy(hdr->disk, "/usr");
139         if (!PREV_RT(hdr)) return 0;
140         strcpy(hdr->disk, ""); /* should be quoted */
141         if (!PREV_RT(hdr)) return 0;
142     }
143     return 1;
144 #undef PREV_RT
145 #define PREV_RT rt_disk
146 }
147
148 static int
149 rt_partial(dumpfile_t *hdr)
150 {
151     if (TAPE_HEADER(hdr)) {
152         hdr->is_partial = 0;
153         if (!PREV_RT(hdr)) return 0;
154     } else {
155         hdr->is_partial = 1;
156         if (!PREV_RT(hdr)) return 0;
157         hdr->is_partial = 0;
158         if (!PREV_RT(hdr)) return 0;
159     }
160     return 1;
161 #undef PREV_RT
162 #define PREV_RT rt_partial
163 }
164
165 static int
166 rt_application(dumpfile_t *hdr)
167 {
168     if (TAPE_HEADER(hdr)) {
169         strcpy(hdr->application, "");
170         if (!PREV_RT(hdr)) return 0;
171     } else {
172         strcpy(hdr->application, "MS-PAINT");
173         if (!PREV_RT(hdr)) return 0;
174     }
175     return 1;
176 #undef PREV_RT
177 #define PREV_RT rt_application
178 }
179
180 static int
181 rt_dle_str(dumpfile_t *hdr)
182 {
183     if (TAPE_HEADER(hdr)) {
184         strcpy(hdr->dle_str, "");
185         if (!PREV_RT(hdr)) return 0;
186     } else {
187         hdr->dle_str = g_strdup("");
188         if (!PREV_RT(hdr)) return 0;
189         hdr->dle_str = g_strdup("no-newline");
190         if (!PREV_RT(hdr)) return 0;
191         hdr->dle_str = g_strdup("ENDDLE\nmy dle\nENDDLE3");
192         if (!PREV_RT(hdr)) return 0;
193     }
194     return 1;
195 #undef PREV_RT
196 #define PREV_RT rt_dle_str
197 }
198
199 static int
200 rt_program(dumpfile_t *hdr)
201 {
202     if (TAPE_HEADER(hdr)) {
203         strcpy(hdr->program, "");
204         if (!PREV_RT(hdr)) return 0;
205     } else {
206         strcpy(hdr->program, "CHECK");
207         if (!PREV_RT(hdr)) return 0;
208     }
209     return 1;
210 #undef PREV_RT
211 #define PREV_RT rt_program
212 }
213
214 static int
215 rt_encrypt_prog(dumpfile_t *hdr)
216 {
217     /* only do this for F_DUMPFILE, just to spare some repetitive testing */
218     if (hdr->type == F_DUMPFILE) {
219         strcpy(hdr->srv_encrypt, "");
220         strcpy(hdr->clnt_encrypt, "");
221         strcpy(hdr->srv_decrypt_opt, "");
222         strcpy(hdr->clnt_decrypt_opt, "");
223         if (!PREV_RT(hdr)) return 0;
224         strcpy(hdr->srv_encrypt, "my-enc");
225         strcpy(hdr->clnt_encrypt, "");
226         strcpy(hdr->srv_decrypt_opt, "");
227         strcpy(hdr->clnt_decrypt_opt, "");
228         if (!PREV_RT(hdr)) return 0;
229         strcpy(hdr->srv_encrypt, "my-enc");
230         strcpy(hdr->clnt_encrypt, "");
231         strcpy(hdr->srv_decrypt_opt, "-foo");
232         strcpy(hdr->clnt_decrypt_opt, "");
233         if (!PREV_RT(hdr)) return 0;
234         strcpy(hdr->srv_encrypt, "");
235         strcpy(hdr->clnt_encrypt, "its-enc");
236         strcpy(hdr->srv_decrypt_opt, "");
237         strcpy(hdr->clnt_decrypt_opt, "");
238         if (!PREV_RT(hdr)) return 0;
239         strcpy(hdr->srv_encrypt, "");
240         strcpy(hdr->clnt_encrypt, "its-enc");
241         strcpy(hdr->srv_decrypt_opt, "");
242         strcpy(hdr->clnt_decrypt_opt, "-foo");
243         if (!PREV_RT(hdr)) return 0;
244     } else {
245         strcpy(hdr->srv_encrypt, "");
246         strcpy(hdr->clnt_encrypt, "");
247         strcpy(hdr->srv_decrypt_opt, "");
248         strcpy(hdr->clnt_decrypt_opt, "");
249         if (!PREV_RT(hdr)) return 0;
250     }
251     return 1;
252 #undef PREV_RT
253 #define PREV_RT rt_encrypt_prog
254 }
255
256 static int
257 rt_compprog(dumpfile_t *hdr)
258 {
259     /* only do this for F_DUMPFILE, just to spare some repetitive testing */
260     if (hdr->type == F_DUMPFILE) {
261         strcpy(hdr->srvcompprog, "");
262         strcpy(hdr->clntcompprog, "");
263         if (!PREV_RT(hdr)) return 0;
264         strcpy(hdr->srvcompprog, "my-comp-prog");
265         strcpy(hdr->clntcompprog, "");
266         if (!PREV_RT(hdr)) return 0;
267         strcpy(hdr->srvcompprog, "");
268         strcpy(hdr->clntcompprog, "its-comp-prog");
269         if (!PREV_RT(hdr)) return 0;
270     } else {
271         strcpy(hdr->srvcompprog, "");
272         strcpy(hdr->clntcompprog, "");
273         if (!PREV_RT(hdr)) return 0;
274     }
275     return 1;
276 #undef PREV_RT
277 #define PREV_RT rt_compprog
278 }
279
280 static int
281 rt_cmds(dumpfile_t *hdr)
282 {
283     /* only do this for F_SPLIT_DUMPFILE, just to spare some repetitive testing */
284     if (hdr->type == F_SPLIT_DUMPFILE) {
285         strcpy(hdr->recover_cmd, "");
286         strcpy(hdr->uncompress_cmd, "");
287         strcpy(hdr->decrypt_cmd, "");
288         if (!PREV_RT(hdr)) return 0;
289         strcpy(hdr->recover_cmd, "get my data back");
290         strcpy(hdr->uncompress_cmd, "");
291         strcpy(hdr->decrypt_cmd, "");
292         if (!PREV_RT(hdr)) return 0;
293         strcpy(hdr->recover_cmd, "get my data back");
294         strcpy(hdr->uncompress_cmd, "make my data bigger |");
295         strcpy(hdr->decrypt_cmd, "");
296         if (!PREV_RT(hdr)) return 0;
297         strcpy(hdr->recover_cmd, "get my data back");
298         strcpy(hdr->uncompress_cmd, "make my data bigger |");
299         strcpy(hdr->decrypt_cmd, "unscramble it too |");
300         if (!PREV_RT(hdr)) return 0;
301     } else {
302         strcpy(hdr->recover_cmd, "");
303         strcpy(hdr->uncompress_cmd, "");
304         strcpy(hdr->decrypt_cmd, "");
305         if (!PREV_RT(hdr)) return 0;
306     }
307     return 1;
308 #undef PREV_RT
309 #define PREV_RT rt_cmds
310 }
311
312 static int
313 rt_cont_filename(dumpfile_t *hdr)
314 {
315     if (hdr->type == F_DUMPFILE || hdr->type == F_CONT_DUMPFILE) {
316         strcpy(hdr->cont_filename, "/next/file");
317         if (!PREV_RT(hdr)) return 0;
318     } else {
319         strcpy(hdr->cont_filename, "");
320         if (!PREV_RT(hdr)) return 0;
321     }
322     return 1;
323 #undef PREV_RT
324 #define PREV_RT rt_cont_filename
325 }
326
327 static int
328 rt_dumplevel(dumpfile_t *hdr)
329 {
330     if (TAPE_HEADER(hdr)) {
331         hdr->dumplevel = 0;
332         if (!PREV_RT(hdr)) return 0;
333     } else {
334         hdr->dumplevel = 0;
335         if (!PREV_RT(hdr)) return 0;
336         hdr->dumplevel = 1;
337         if (!PREV_RT(hdr)) return 0;
338     }
339     return 1;
340 #undef PREV_RT
341 #define PREV_RT rt_dumplevel
342 }
343
344 static int
345 rt_name(dumpfile_t *hdr)
346 {
347     if (hdr->type == F_TAPEEND)
348         strcpy(hdr->name, "");
349     else if (TAPE_HEADER(hdr))
350         strcpy(hdr->name, "TEST-LABEL");
351     else 
352         strcpy(hdr->name, "batcave-web");
353     if (!PREV_RT(hdr)) return 0;
354     return 1;
355 #undef PREV_RT
356 #define PREV_RT rt_name
357 }
358
359 static int
360 rt_type(dumpfile_t *hdr)
361 {
362     hdr->type = F_DUMPFILE;
363     if (!PREV_RT(hdr)) return 0;
364     hdr->type = F_CONT_DUMPFILE;
365     if (!PREV_RT(hdr)) return 0;
366     hdr->type = F_SPLIT_DUMPFILE;
367     if (!PREV_RT(hdr)) return 0;
368     hdr->type = F_TAPESTART;
369     if (!PREV_RT(hdr)) return 0;
370     hdr->type = F_TAPEEND;
371     if (!PREV_RT(hdr)) return 0;
372     return 1;
373 #undef PREV_RT
374 #define PREV_RT rt_type
375 }
376
377 /* one function for each field; each fn calls the one above */
378 static int
379 test_roundtrip(void)
380 {
381     int rv;
382     dumpfile_t hdr;
383     fh_init(&hdr);
384
385     /* set up some basic, constant values */
386     strcpy(hdr.datestamp, "20100102030405");
387     strcpy(hdr.name, "localhost");
388
389     rv = PREV_RT(&hdr);
390     tu_dbg("%d round-trips run\n", n_round_trips);
391     return rv;
392 }
393
394 /* doc
395
396 encrypted + encrypt_suffix (N special)
397 compressed + comp_suffix (N special)
398 blocksize not parsed
399 partnum/totalparts interaction
400 {srv,clnt}{compprog,_encrypt} pairs
401 uncompress/decrypt_cmd invalid without recover_cmd
402 uncompress/decrypt_cmd require trailing |
403 default to uncompress if only 2 cmds
404
405 */
406
407 int
408 main(int argc, char **argv)
409 {
410     static TestUtilsTest tests[] = {
411         TU_TEST(test_roundtrip, 90),
412         TU_END()
413     };
414     return testutils_run_tests(argc, argv, tests);
415 }