2 * test-save.c: test various aspects of saving
4 * Copyright (C) 2009-2011 Red Hat Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Author: David Lutterkort <lutter@redhat.com>
30 #include <sys/types.h>
34 const char *abs_top_srcdir;
35 const char *abs_top_builddir;
36 char *root = NULL, *src_root = NULL;
37 struct augeas *aug = NULL;
41 fprintf(stderr, "%s:%d: Fatal error: %s\n", __FILE__, __LINE__, msg); \
45 static void setup(CuTest *tc) {
48 if (asprintf(&root, "%s/build/test-save/%s",
49 abs_top_builddir, tc->name) < 0) {
50 CuFail(tc, "failed to set root");
53 if (asprintf(&lensdir, "%s/lenses", abs_top_srcdir) < 0)
54 CuFail(tc, "asprintf lensdir failed");
57 run(tc, "test -d %s && chmod -R u+w %s || :", root, root);
58 run(tc, "rm -rf %s", root);
59 run(tc, "mkdir -p %s", root);
60 run(tc, "cp -pr %s/* %s", src_root, root);
61 run(tc, "chmod -R u+w %s", root);
63 aug = aug_init(root, lensdir, AUG_NO_STDINC);
64 CuAssertPtrNotNull(tc, aug);
67 static void teardown(ATTRIBUTE_UNUSED CuTest *tc) {
68 /* testRemovePermission makes <root>/etc nonwritable. That leads
69 to an error from 'make distcheck' make sure that directory is
70 writable by the user after the test */
71 run(tc, "chmod u+w %s/etc", root);
79 static void testRemoveNoPermission(CuTest *tc) {
81 puts("pending (testRemoveNoPermission): can't test permissions under root account");
88 // Prevent deletion of files
89 run(tc, "chmod 0500 %s/etc", root);
91 r = aug_rm(aug, "/files/etc/hosts");
92 CuAssertTrue(tc, r > 0);
95 CuAssertIntEquals(tc, -1, r);
97 r = aug_get(aug, "/augeas/files/etc/hosts/error", &errmsg);
98 CuAssertIntEquals(tc, 1, r);
99 CuAssertPtrNotNull(tc, errmsg);
100 CuAssertStrEquals(tc, "unlink_orig", errmsg);
103 static void testSaveNewFile(CuTest *tc) {
106 r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
107 CuAssertIntEquals(tc, 0, r);
109 r = aug_set(aug, "/files/etc/yum.repos.d/new.repo/newrepo/baseurl",
111 CuAssertIntEquals(tc, 0, r);
114 CuAssertIntEquals(tc, 0, r);
116 r = aug_match(aug, "/augeas/files/etc/yum.repos.d/new.repo/path", NULL);
117 CuAssertIntEquals(tc, 1, r);
120 static void testNonExistentLens(CuTest *tc) {
123 r = aug_rm(aug, "/augeas/load/*");
124 CuAssertTrue(tc, r >= 0);
126 r = aug_set(aug, "/augeas/load/Fake/lens", "Fake.lns");
127 CuAssertIntEquals(tc, 0, r);
128 r = aug_set(aug, "/augeas/load/Fake/incl", "/fake");
129 CuAssertIntEquals(tc, 0, r);
130 r = aug_set(aug, "/files/fake/entry", "value");
131 CuAssertIntEquals(tc, 0, r);
134 CuAssertIntEquals(tc, -1, r);
136 CuAssertIntEquals(tc, AUG_ENOLENS, r);
139 static void testMultipleXfm(CuTest *tc) {
142 r = aug_set(aug, "/augeas/load/Yum2/lens", "Yum.lns");
143 CuAssertIntEquals(tc, 0, r);
144 r = aug_set(aug, "/augeas/load/Yum2/incl", "/etc/yum.repos.d/*");
145 CuAssertIntEquals(tc, 0, r);
147 r = aug_set(aug, "/files/etc/yum.repos.d/fedora.repo/fedora/enabled", "0");
148 CuAssertIntEquals(tc, 0, r);
151 CuAssertIntEquals(tc, -1, r);
153 CuAssertIntEquals(tc, AUG_EMXFM, r);
156 static void testMtime(CuTest *tc) {
157 const char *s, *mtime2;
161 r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
162 CuAssertIntEquals(tc, 0, r);
164 r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &s);
165 CuAssertIntEquals(tc, 1, r);
167 CuAssertPtrNotNull(tc, mtime1);
171 CuAssertIntEquals(tc, 0, r);
173 r = aug_get(aug, "/augeas/files/etc/hosts/mtime", &mtime2);
174 CuAssertIntEquals(tc, 1, r);
176 CuAssertStrNotEqual(tc, mtime1, mtime2);
177 CuAssertStrNotEqual(tc, "0", mtime2);
180 /* Check that loading and saving a file given with a relative path
183 static void testRelPath(CuTest *tc) {
186 r = aug_rm(aug, "/augeas/load/*");
187 CuAssertPositive(tc, r);
189 r = aug_set(aug, "/augeas/load/Hosts/lens", "Hosts.lns");
190 CuAssertRetSuccess(tc, r);
191 r = aug_set(aug, "/augeas/load/Hosts/incl", "etc/hosts");
192 CuAssertRetSuccess(tc, r);
194 CuAssertRetSuccess(tc, r);
196 r = aug_match(aug, "/files/etc/hosts/1/alias[ . = 'new']", NULL);
197 CuAssertIntEquals(tc, 0, r);
199 r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
200 CuAssertRetSuccess(tc, r);
203 CuAssertRetSuccess(tc, r);
204 r = aug_match(aug, "/augeas//error", NULL);
205 CuAssertIntEquals(tc, 0, r);
207 /* Force reloading the file */
208 r = aug_rm(aug, "/augeas/files//mtime");
209 CuAssertPositive(tc, r);
212 CuAssertRetSuccess(tc, r);
214 r = aug_match(aug, "/files/etc/hosts/1/alias[. = 'new']", NULL);
215 CuAssertIntEquals(tc, 1, r);
218 /* Check that loading and saving a file with // in the incl pattern works.
221 static void testDoubleSlashPath(CuTest *tc) {
224 r = aug_rm(aug, "/augeas/load/*");
225 CuAssertPositive(tc, r);
227 r = aug_set(aug, "/augeas/load/Hosts/lens", "Hosts.lns");
228 CuAssertRetSuccess(tc, r);
229 r = aug_set(aug, "/augeas/load/Hosts/incl", "/etc//hosts");
230 CuAssertRetSuccess(tc, r);
232 CuAssertRetSuccess(tc, r);
234 r = aug_match(aug, "/files/etc/hosts/1/alias[ . = 'new']", NULL);
235 CuAssertIntEquals(tc, 0, r);
237 r = aug_set(aug, "/files/etc/hosts/1/alias[last() + 1]", "new");
238 CuAssertRetSuccess(tc, r);
241 CuAssertRetSuccess(tc, r);
242 r = aug_match(aug, "/augeas//error", NULL);
243 CuAssertIntEquals(tc, 0, r);
245 /* Force reloading the file */
246 r = aug_rm(aug, "/augeas/files//mtime");
247 CuAssertPositive(tc, r);
250 CuAssertRetSuccess(tc, r);
252 r = aug_match(aug, "/files/etc/hosts/1/alias[. = 'new']", NULL);
253 CuAssertIntEquals(tc, 1, r);
256 /* Check the umask is followed when creating files
258 static void testUmask(CuTest *tc, int tumask, mode_t expected_mode) {
263 if (asprintf(&fpath, "%s/etc/test", root) < 0) {
264 CuFail(tc, "failed to set root");
269 r = aug_rm(aug, "/augeas/load/*");
270 CuAssertPositive(tc, r);
272 r = aug_set(aug, "/augeas/load/Test/lens", "Simplelines.lns");
273 CuAssertRetSuccess(tc, r);
274 r = aug_set(aug, "/augeas/load/Test/incl", "/etc/test");
275 CuAssertRetSuccess(tc, r);
277 CuAssertRetSuccess(tc, r);
278 r = aug_set(aug, "/files/etc/test/1", "test");
279 CuAssertRetSuccess(tc, r);
282 CuAssertRetSuccess(tc, r);
283 r = aug_match(aug, "/augeas//error", NULL);
284 CuAssertIntEquals(tc, 0, r);
286 CuAssertIntEquals(tc, 0, stat(fpath, &buf));
287 CuAssertIntEquals(tc, expected_mode, buf.st_mode & 0777);
289 static void testUmask077(CuTest *tc) {
290 testUmask(tc, 0077, 0600);
292 static void testUmask027(CuTest *tc) {
293 testUmask(tc, 0027, 0640);
295 static void testUmask022(CuTest *tc) {
296 testUmask(tc, 0022, 0644);
299 /* Test that handling of 'strange' characters in path names works as
300 * expected. In particular, that paths with characters that have special
301 * meaning in path expressions are escaped properly.
303 * This test isn't all that specific to save, but since these tests set up
304 * a copy of tests/root/ that is modifiable, it was convenient to put this
307 static void testPathEscaping(CuTest *tc) {
308 /* Path expression with characters escaped */
309 static const char *const weird =
310 "/files/etc/sysconfig/network-scripts/ifcfg-weird\\ \\[\\!\\]\\ \\(used\\ to\\ fail\\)";
311 /* Path without any escaping */
312 static const char *const weird_no_escape =
313 "/files/etc/sysconfig/network-scripts/ifcfg-weird [!] (used to fail)";
315 char *fname = NULL, *s = NULL;
319 /* Construct the file name in the file system and check the file is there */
320 r = asprintf(&fname, "%s%s", root, weird_no_escape + strlen("/files"));
321 CuAssertPositive(tc, r);
323 r = access(fname, R_OK);
324 CuAssertIntEquals(tc, 0, r);
326 /* Make sure weird is in the tree */
327 r = aug_match(aug, weird, NULL);
328 CuAssertIntEquals(tc, 1, r);
330 /* Make sure we can get to the metadata about weird */
331 r = asprintf(&s, "/augeas%s/path", weird);
332 CuAssertPositive(tc, r);
334 r = aug_get(aug, s, &v);
335 CuAssertIntEquals(tc, 1, r);
336 CuAssertStrEquals(tc, weird_no_escape, v);
338 /* Delete it from the tree and save it; make sure it gets removed
339 from the file system */
340 r = aug_rm(aug, weird);
341 CuAssertPositive(tc, r);
344 CuAssertRetSuccess(tc, r);
346 r = access(fname, R_OK);
347 CuAssertIntEquals(tc, -1, r);
348 CuAssertIntEquals(tc, ENOENT, errno);
351 /* Test that we handle failure to save a file because we lack permission on
352 * the target file is handled gracefully.
354 * As reported in https://github.com/hercules-team/augeas/issues/178, this
355 * used to lead to a SEGV
357 static void testSaveNoPermission(CuTest *tc) {
362 r = asprintf(&path, "%s/etc/hosts", root);
363 CuAssertPositive(tc, r);
365 r = aug_set(aug, "/files/etc/hosts/1/alias[1]", "othername");
366 CuAssertRetSuccess(tc, r);
369 CuAssertRetSuccess(tc, r);
372 CuAssertIntEquals(tc, -1, r);
374 r = aug_get(aug, "/augeas/files/etc/hosts/error", &v);
375 CuAssertIntEquals(tc, 1, r);
376 CuAssertStrEquals(tc, "replace_from_missing", v);
381 CuSuite* suite = CuSuiteNew();
383 abs_top_srcdir = getenv("abs_top_srcdir");
384 if (abs_top_srcdir == NULL)
385 die("env var abs_top_srcdir must be set");
387 abs_top_builddir = getenv("abs_top_builddir");
388 if (abs_top_builddir == NULL)
389 die("env var abs_top_builddir must be set");
391 if (asprintf(&src_root, "%s/tests/root", abs_top_srcdir) < 0) {
392 die("failed to set src_root");
395 CuSuiteSetup(suite, setup, teardown);
397 SUITE_ADD_TEST(suite, testSaveNoPermission);
398 SUITE_ADD_TEST(suite, testSaveNewFile);
399 SUITE_ADD_TEST(suite, testRemoveNoPermission);
400 SUITE_ADD_TEST(suite, testNonExistentLens);
401 SUITE_ADD_TEST(suite, testMultipleXfm);
402 SUITE_ADD_TEST(suite, testMtime);
403 SUITE_ADD_TEST(suite, testRelPath);
404 SUITE_ADD_TEST(suite, testDoubleSlashPath);
405 SUITE_ADD_TEST(suite, testUmask077);
406 SUITE_ADD_TEST(suite, testUmask027);
407 SUITE_ADD_TEST(suite, testUmask022);
408 SUITE_ADD_TEST(suite, testPathEscaping);
411 CuSuiteSummary(suite, &output);
412 CuSuiteDetails(suite, &output);
413 printf("%s\n", output);
415 return suite->failCount;
420 * indent-tabs-mode: nil