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/>.
22 #include "testutils.h"
25 #include "virfilecache.h"
28 #define VIR_FROM_THIS VIR_FROM_NONE
31 struct _testFileCacheObj
{
35 typedef struct _testFileCacheObj testFileCacheObj
;
36 typedef testFileCacheObj
*testFileCacheObjPtr
;
39 static virClassPtr testFileCacheObjClass
;
43 testFileCacheObjDispose(void *opaque
)
45 testFileCacheObjPtr obj
= opaque
;
51 testFileCacheObjOnceInit(void)
53 if (!VIR_CLASS_NEW(testFileCacheObj
, virClassForObject()))
60 VIR_ONCE_GLOBAL_INIT(testFileCacheObj
);
63 static testFileCacheObjPtr
64 testFileCacheObjNew(const char *data
)
66 testFileCacheObjPtr obj
;
68 if (testFileCacheObjInitialize() < 0)
71 if (!(obj
= virObjectNew(testFileCacheObjClass
)))
74 if (VIR_STRDUP(obj
->data
, data
) < 0)
85 struct _testFileCachePriv
{
88 const char *expectData
;
90 typedef struct _testFileCachePriv testFileCachePriv
;
91 typedef testFileCachePriv
*testFileCachePrivPtr
;
95 testFileCacheIsValid(void *data
,
98 testFileCachePrivPtr testPriv
= priv
;
99 testFileCacheObjPtr obj
= data
;
101 return STREQ(testPriv
->expectData
, obj
->data
);
106 testFileCacheNewData(const char *name ATTRIBUTE_UNUSED
,
109 testFileCachePrivPtr testPriv
= priv
;
111 return testFileCacheObjNew(testPriv
->newData
);
116 testFileCacheLoadFile(const char *filename
,
117 const char *name ATTRIBUTE_UNUSED
,
118 void *priv ATTRIBUTE_UNUSED
)
120 testFileCacheObjPtr obj
;
123 if (virFileReadAll(filename
, 20, &data
) < 0)
126 obj
= testFileCacheObjNew(data
);
134 testFileCacheSaveFile(void *data ATTRIBUTE_UNUSED
,
135 const char *filename ATTRIBUTE_UNUSED
,
138 testFileCachePrivPtr testPriv
= priv
;
140 testPriv
->dataSaved
= true;
146 virFileCacheHandlers testFileCacheHandlers
= {
147 .isValid
= testFileCacheIsValid
,
148 .newData
= testFileCacheNewData
,
149 .loadFile
= testFileCacheLoadFile
,
150 .saveFile
= testFileCacheSaveFile
154 struct _testFileCacheData
{
155 virFileCachePtr cache
;
158 const char *expectData
;
161 typedef struct _testFileCacheData testFileCacheData
;
162 typedef testFileCacheData
*testFileCacheDataPtr
;
166 testFileCache(const void *opaque
)
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");
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
));
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");
207 testFileCachePriv testPriv
= {0};
208 virFileCachePtr cache
= NULL
;
210 if (!(cache
= virFileCacheNew(abs_srcdir
"/virfilecachedata",
211 "cache", &testFileCacheHandlers
)))
214 virFileCacheSetPriv(cache
, &testPriv
);
216 #define TEST_RUN(name, newData, expectData, expectSave) \
218 testFileCacheData data = { \
219 cache, name, newData, expectData, expectSave \
221 if (virTestRun(name, testFileCache, &data) < 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")