backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virfilecachetest.c
blob08062d66e38cf56408ca96b0e6b932d913dd5f6d
1 /*
2 * Copyright (C) 2017 Red Hat, Inc.
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.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
20 #include <config.h>
22 #include "testutils.h"
24 #include "virfile.h"
25 #include "virfilecache.h"
28 #define VIR_FROM_THIS VIR_FROM_NONE
31 struct _testFileCacheObj {
32 virObject parent;
33 char *data;
35 typedef struct _testFileCacheObj testFileCacheObj;
36 typedef testFileCacheObj *testFileCacheObjPtr;
39 static virClassPtr testFileCacheObjClass;
42 static void
43 testFileCacheObjDispose(void *opaque)
45 testFileCacheObjPtr obj = opaque;
46 VIR_FREE(obj->data);
50 static int
51 testFileCacheObjOnceInit(void)
53 if (!VIR_CLASS_NEW(testFileCacheObj, virClassForObject()))
54 return -1;
56 return 0;
60 VIR_ONCE_GLOBAL_INIT(testFileCacheObj);
63 static testFileCacheObjPtr
64 testFileCacheObjNew(const char *data)
66 testFileCacheObjPtr obj;
68 if (testFileCacheObjInitialize() < 0)
69 return NULL;
71 if (!(obj = virObjectNew(testFileCacheObjClass)))
72 return NULL;
74 if (VIR_STRDUP(obj->data, data) < 0)
75 goto error;
77 return obj;
79 error:
80 virObjectUnref(obj);
81 return NULL;
85 struct _testFileCachePriv {
86 bool dataSaved;
87 const char *newData;
88 const char *expectData;
90 typedef struct _testFileCachePriv testFileCachePriv;
91 typedef testFileCachePriv *testFileCachePrivPtr;
94 static bool
95 testFileCacheIsValid(void *data,
96 void *priv)
98 testFileCachePrivPtr testPriv = priv;
99 testFileCacheObjPtr obj = data;
101 return STREQ(testPriv->expectData, obj->data);
105 static void *
106 testFileCacheNewData(const char *name ATTRIBUTE_UNUSED,
107 void *priv)
109 testFileCachePrivPtr testPriv = priv;
111 return testFileCacheObjNew(testPriv->newData);
115 static void *
116 testFileCacheLoadFile(const char *filename,
117 const char *name ATTRIBUTE_UNUSED,
118 void *priv ATTRIBUTE_UNUSED)
120 testFileCacheObjPtr obj;
121 char *data;
123 if (virFileReadAll(filename, 20, &data) < 0)
124 return NULL;
126 obj = testFileCacheObjNew(data);
128 VIR_FREE(data);
129 return obj;
133 static int
134 testFileCacheSaveFile(void *data ATTRIBUTE_UNUSED,
135 const char *filename ATTRIBUTE_UNUSED,
136 void *priv)
138 testFileCachePrivPtr testPriv = priv;
140 testPriv->dataSaved = true;
142 return 0;
146 virFileCacheHandlers testFileCacheHandlers = {
147 .isValid = testFileCacheIsValid,
148 .newData = testFileCacheNewData,
149 .loadFile = testFileCacheLoadFile,
150 .saveFile = testFileCacheSaveFile
154 struct _testFileCacheData {
155 virFileCachePtr cache;
156 const char *name;
157 const char *newData;
158 const char *expectData;
159 bool expectSave;
161 typedef struct _testFileCacheData testFileCacheData;
162 typedef testFileCacheData *testFileCacheDataPtr;
165 static int
166 testFileCache(const void *opaque)
168 int ret = -1;
169 const testFileCacheData *data = opaque;
170 testFileCacheObjPtr obj = NULL;
171 testFileCachePrivPtr testPriv = virFileCacheGetPriv(data->cache);
173 testPriv->dataSaved = false;
174 testPriv->newData = data->newData;
175 testPriv->expectData = data->expectData;
177 if (!(obj = virFileCacheLookup(data->cache, data->name))) {
178 fprintf(stderr, "Getting cached data failed.\n");
179 goto cleanup;
182 if (!obj->data || STRNEQ(data->expectData, obj->data)) {
183 fprintf(stderr, "Expect data '%s', loaded data '%s'.\n",
184 data->expectData, NULLSTR(obj->data));
185 goto cleanup;
188 if (data->expectSave != testPriv->dataSaved) {
189 fprintf(stderr, "Expect data to be saved '%s', data saved '%s'.\n",
190 data->expectSave ? "yes" : "no",
191 testPriv->dataSaved ? "yes" : "no");
192 goto cleanup;
195 ret = 0;
197 cleanup:
198 virObjectUnref(obj);
199 return ret;
203 static int
204 mymain(void)
206 int ret = 0;
207 testFileCachePriv testPriv = {0};
208 virFileCachePtr cache = NULL;
210 if (!(cache = virFileCacheNew(abs_srcdir "/virfilecachedata",
211 "cache", &testFileCacheHandlers)))
212 return EXIT_FAILURE;
214 virFileCacheSetPriv(cache, &testPriv);
216 #define TEST_RUN(name, newData, expectData, expectSave) \
217 do { \
218 testFileCacheData data = { \
219 cache, name, newData, expectData, expectSave \
220 }; \
221 if (virTestRun(name, testFileCache, &data) < 0) \
222 ret = -1; \
223 } while (0)
225 /* The cache file name is created using:
226 * '$ echo -n $TEST_NAME | sha256sum' */
227 TEST_RUN("cacheValid", NULL, "aaa\n", false);
228 TEST_RUN("cacheInvalid", "bbb\n", "bbb\n", true);
229 TEST_RUN("cacheMissing", "ccc\n", "ccc\n", true);
231 virObjectUnref(cache);
233 return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
236 VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virfilecachemock.so")