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