Imported Upstream version 3.1.0
[debian/amanda] / device-src / vfs-test.c
1 /*
2  * Copyright (c) 2008,2009 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program 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 General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 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 94085, USA, or: http://www.zmanda.com
19  */
20
21 #include "glib-util.h"
22 #include "amanda.h"
23 #include "device.h"
24 #include "conffile.h"
25 #include "testutils.h"
26
27 /* Global state set up for the tests */
28 static char *device_path = NULL;
29
30 /*
31  * Utilities
32  */
33
34 static void
35 cleanup_vtape_dir(char *device_path)
36 {
37     char *quoted = g_shell_quote(device_path);
38     char *cmd = vstralloc("rm -rf ", quoted, NULL);
39
40     /* would you rather write 'rm -rf' here? */
41     if (system(cmd) == -1) {
42         exit(1);
43     }
44
45     amfree(cmd);
46     amfree(quoted);
47 }
48
49 static char *
50 setup_vtape_dir(void)
51 {
52     char *cwd = g_get_current_dir();
53     char *device_path = NULL;
54     char *data_dir = NULL;
55
56     device_path = vstralloc(cwd, "/vfs-test-XXXXXX", NULL);
57     amfree(cwd);
58
59     if (mkdtemp(device_path) == NULL) {
60         fprintf(stderr, "Could not create temporary directory in %s\n", cwd);
61         return NULL;
62     }
63
64     /* append "/data/" to that for the VFS device*/
65     data_dir = vstralloc(device_path, "/data/", NULL);
66     if (mkdir(data_dir, 0777) == -1) {
67         fprintf(stderr, "Could not create %s: %s\n", cwd, strerror(errno));
68         amfree(data_dir);
69         return NULL;
70     }
71
72     amfree(data_dir);
73     return device_path;
74 }
75
76 static Device *
77 setup_device(void)
78 {
79     Device *device;
80     char *device_name = NULL;
81
82     device_name = vstralloc("file:", device_path, NULL);
83     device = device_open(device_name);
84     if (device->status != DEVICE_STATUS_SUCCESS) {
85         g_critical("Could not open device %s: %s\n", device_name, device_error(device));
86     }
87
88     amfree(device_name);
89     return device;
90 }
91
92 /*
93  * Tests
94  */
95
96 static int
97 test_vfs_free_space(void)
98 {
99     Device *device = NULL;
100     GValue value;
101     QualifiedSize qsize;
102
103     device = setup_device();
104     if (!device)
105         return FALSE;
106
107     bzero(&value, sizeof(value));
108     if (!device_property_get(device, PROPERTY_FREE_SPACE, &value)) {
109         g_debug("Could not get property_free_space\n");
110         return FALSE;
111     }
112
113     qsize = *(QualifiedSize*)g_value_get_boxed(&value);
114     g_value_unset(&value);
115
116     if (qsize.accuracy != SIZE_ACCURACY_REAL) {
117         g_debug("property_free_space accuracy is not SIZE_ACCURACY_REAL\n");
118         return FALSE;
119     }
120
121     if (qsize.bytes == 0) {
122         g_debug("property_free_space returned bytes=0\n");
123         return FALSE;
124     }
125
126     g_object_unref(device);
127
128     return TRUE;
129 }
130
131 /*
132  * Main driver
133  */
134
135 int
136 main(int argc, char **argv)
137 {
138     int result;
139     static TestUtilsTest tests[] = {
140         TU_TEST(test_vfs_free_space, 90),
141         TU_END()
142     };
143
144     glib_init();
145     config_init(0, NULL);
146     device_api_init();
147
148     /* TODO: if more tests are added, we'll need a setup/cleanup hook
149      * for testutils */
150     device_path = setup_vtape_dir();
151
152     result = testutils_run_tests(argc, argv, tests);
153
154     cleanup_vtape_dir(device_path);
155     amfree(device_path);
156
157     return result;
158 }