]> granicus.if.org Git - vnstat/commitdiff
increase test coverage
authorTeemu Toivola <git@humdi.net>
Tue, 19 Aug 2014 19:22:35 +0000 (22:22 +0300)
committerTeemu Toivola <git@humdi.net>
Tue, 19 Aug 2014 19:22:35 +0000 (22:22 +0300)
tests/daemon_tests.c
tests/vnstat_tests.c

index dfc1f6a1ec40f0cc583a22700762fac65fbde22f..4f06c8e34a837a3358c2a2406ffa53cb9b7ca4bc 100644 (file)
@@ -785,6 +785,58 @@ START_TEST(preparedirs_with_dir)
 }
 END_TEST
 
+START_TEST(preparevnstatdir_with_no_vnstat)
+{
+       char testdir[512], testpath[512];
+       defaultcfg();
+       cfg.updatefileowner = 0;
+
+       ck_assert_int_eq(remove_directory(TESTDIR), 1);
+       ck_assert_int_eq(direxists(TESTDIR), 0);
+       snprintf(testdir, 512, "%s/here/be/dragons", TESTDIR);
+       snprintf(testpath, 512, "%s/or_something.txt", testdir);
+       preparevnstatdir(testpath, "user", "group");
+       ck_assert_int_eq(direxists(TESTDIR), 0);
+       ck_assert_int_eq(direxists(testdir), 0);
+
+       snprintf(testdir, 512, "%s/here/be/vnstat/dragons", TESTDIR);
+       snprintf(testpath, 512, "%s/or_something.txt", testdir);
+       preparevnstatdir(testpath, "user", "group");
+       ck_assert_int_eq(direxists(TESTDIR), 0);
+       ck_assert_int_eq(direxists(testdir), 0);
+
+       snprintf(testdir, 512, "%s/here/be/vnstati", TESTDIR);
+       snprintf(testpath, 512, "%s/or_something.txt", testdir);
+       preparevnstatdir(testpath, "user", "group");
+       ck_assert_int_eq(direxists(TESTDIR), 0);
+       ck_assert_int_eq(direxists(testdir), 0);
+}
+END_TEST
+
+START_TEST(preparevnstatdir_with_vnstat)
+{
+       char testdir[512], testpath[512];
+       defaultcfg();
+       cfg.updatefileowner = 0;
+
+       ck_assert_int_eq(remove_directory(TESTDIR), 1);
+       ck_assert_int_eq(direxists(TESTDIR), 0);
+       snprintf(testdir, 512, "%s/here/be/vnstat", TESTDIR);
+       snprintf(testpath, 512, "%s/or_something.txt", testdir);
+       preparevnstatdir(testpath, "user", "group");
+       ck_assert_int_eq(direxists(TESTDIR), 1);
+       ck_assert_int_eq(direxists(testdir), 1);
+
+       ck_assert_int_eq(remove_directory(TESTDIR), 1);
+       ck_assert_int_eq(direxists(TESTDIR), 0);
+       snprintf(testdir, 512, "%s/here/be/vnstatd", TESTDIR);
+       snprintf(testpath, 512, "%s/or_something.txt", testdir);
+       preparevnstatdir(testpath, "user", "group");
+       ck_assert_int_eq(direxists(TESTDIR), 1);
+       ck_assert_int_eq(direxists(testdir), 1);
+}
+END_TEST
+
 void add_daemon_tests(Suite *s)
 {
        /* Config test cases */
@@ -834,6 +886,8 @@ void add_daemon_tests(Suite *s)
        tcase_add_test(tc_daemon, mkpath_with_dir);
        tcase_add_test(tc_daemon, preparedirs_with_no_dir);
        tcase_add_test(tc_daemon, preparedirs_with_dir);
+       tcase_add_test(tc_daemon, preparevnstatdir_with_no_vnstat);
+       tcase_add_test(tc_daemon, preparevnstatdir_with_vnstat);
        suite_add_tcase(s, tc_daemon);
 }
 
index e07026a3909f87b64f43adaa67034289cae7f2ad..a136d0908a5a79d71fbcfbbc956c31553bc3e66d 100644 (file)
@@ -110,22 +110,26 @@ int remove_directory(const char *directory)
 
        while ((di=readdir(dir))) {
                switch (di->d_type) {
+                       case DT_LNK:
                        case DT_REG:
                                snprintf(entryname, 512, "%s/%s", directory, di->d_name);
                                if (unlink(entryname)!=0) {
+                                       closedir(dir);
                                        return 0;
                                }
                                break;
                        case DT_DIR:
-                               if (strlen(di->d_name)>2) {
-                                       snprintf(entryname, 512, "%s/%s", directory, di->d_name);
-                                       if (!remove_directory(entryname)) {
-                                               return 0;
-                                       }
+                               if (strcmp(di->d_name, ".")==0 || strcmp(di->d_name, "..")==0) {
+                                       continue;
+                               }
+                               snprintf(entryname, 512, "%s/%s", directory, di->d_name);
+                               if (!remove_directory(entryname)) {
+                                       closedir(dir);
+                                       return 0;
                                }
                                break;
                        default:
-                               return 0;
+                               continue;
                }
        }
        closedir(dir);