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