Imported Upstream version 2.6.0
[debian/amanda] / device-src / tests / vfs_test.c
1 /*
2  * Copyright (c) 2005 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., 505 N Mathlida Ave, Suite 120
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 #include <device.h>
22 #include <amanda.h>
23
24 /* global so the 'atexit' handler can access it */
25
26 static void
27 cleanup_vtape_dir(char *device_path)
28 {
29     char *quoted = g_shell_quote(device_path);
30     char *cmd = vstralloc("rm -rf ", quoted, NULL);
31
32     /* would you rather write 'rm -rf' here? */
33     if (system(cmd) == -1) {
34         exit(1);
35     }
36
37     amfree(cmd);
38     amfree(quoted);
39 }
40
41 static char *
42 setup_vtape_dir(void)
43 {
44     char *cwd = g_get_current_dir();
45     char *device_path = NULL;
46     char *data_dir = NULL;
47
48     device_path = vstralloc(cwd, "/vfs-test-XXXXXX", NULL);
49     amfree(cwd);
50
51     if (mkdtemp(device_path) == NULL) {
52         fprintf(stderr, "Could not create temporary directory in %s\n", cwd);
53         return NULL;
54     }
55
56     /* append "/data/" to that for the VFS device*/
57     data_dir = vstralloc(device_path, "/data/", NULL);
58     if (mkdir(data_dir, 0777) == -1) {
59         fprintf(stderr, "Could not create %s: %s\n", cwd, strerror(errno));
60         amfree(data_dir);
61         return NULL;
62     }
63
64     amfree(data_dir);
65     return device_path;
66 }
67
68 static Device *
69 setup_device(char *device_path)
70 {
71     Device *device;
72     char *device_name = NULL;
73
74     device_name = vstralloc("file:", device_path, NULL);
75     device = device_open(device_name);
76     if (!device) {
77         fprintf(stderr, "Could not open device %s\n", device_name);
78     }
79
80     amfree(device_name);
81     return device;
82 }
83
84 static gboolean
85 check_free_space(Device *device)
86 {
87     GValue value;
88     QualifiedSize qsize;
89
90     bzero(&value, sizeof(value));
91     if (!device_property_get(device, PROPERTY_FREE_SPACE, &value)) {
92         fprintf(stderr, "Could not get property_free_space\n");
93         return FALSE;
94     }
95
96     qsize = *(QualifiedSize*)g_value_get_boxed(&value);
97     g_value_unset(&value);
98
99     if (qsize.accuracy != SIZE_ACCURACY_REAL) {
100         fprintf(stderr, "property_free_space accuracy is not SIZE_ACCURACY_REAL\n");
101         return FALSE;
102     }
103
104     if (qsize.bytes == 0) {
105         fprintf(stderr, "property_free_space returned bytes=0\n");
106         return FALSE;
107     }
108
109     return TRUE;
110 }
111
112 int
113 main(int argc G_GNUC_UNUSED, char **argv G_GNUC_UNUSED)
114 {
115     Device *device = NULL;
116     gboolean ok = TRUE;
117     char *device_path = NULL;
118     pid_t pid;
119     amwait_t status;
120
121     amanda_thread_init(NULL);
122
123     device_path = setup_vtape_dir();
124
125     /* run the tests in a subprocess so we can clean up even if they fail */
126     switch (pid = fork()) {
127         case -1: /* error */
128             perror("fork");
129             g_assert_not_reached();
130
131         case 0: /* child */
132             device_api_init();
133
134             device = setup_device(device_path);
135             if (!device)
136                 return 1;
137
138             ok = ok && check_free_space(device);
139
140             g_object_unref(device);
141
142             if (!ok) exit(1);
143             exit(0);
144             g_assert_not_reached();
145
146         default: /* parent */
147             if (waitpid(pid, &status, 0) == -1)
148                 perror("waitpid");
149
150             /* cleanup */
151             cleanup_vtape_dir(device_path);
152             amfree(device_path);
153
154             /* figure our own return status */
155             if (WIFEXITED(status))
156                 return WEXITSTATUS(status);
157             else if (WIFSIGNALED(status)) {
158                 fprintf(stderr, "Test failed with signal %d\n", (int)WTERMSIG(status));
159                 return 1;
160             } else {
161                 /* weird.. */
162                 return 1;
163             }
164             g_assert_not_reached();
165     }
166 }