]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
test: extend util unit test
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 24 Oct 2019 08:54:37 +0000 (10:54 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 24 Oct 2019 10:48:45 +0000 (12:48 +0200)
test/unit/util.c

index 870d9e4b64fa6653d66e022d57ccb7d8a27e6573..f4d9f3309b62e94aba8a999526b00ef4d9d269dd 100644 (file)
@@ -33,6 +33,8 @@ void test_unit(void) {
   char buf[16], *s;
   uid_t uid;
   gid_t gid;
+  struct stat st;
+  FILE *file;
 
   for (i = -31; i < 31; i++) {
     x = pow(2.0, i);
@@ -269,4 +271,43 @@ void test_unit(void) {
   TEST_CHECK(!UTI_CheckDirPermissions("testdir", 0700, uid + 1, gid));
   TEST_CHECK(!UTI_CheckDirPermissions("testdir", 0700, uid, gid + 1));
   TST_ResumeLogging();
+
+  umask(0);
+
+  unlink("testfile");
+  file = UTI_OpenFile(NULL, "testfile", NULL, 'r', 0);
+  TEST_CHECK(!file);
+  TEST_CHECK(stat("testfile", &st) < 0);
+
+  file = UTI_OpenFile(NULL, "testfile", NULL, 'w', 0644);
+  TEST_CHECK(file);
+  TEST_CHECK(stat("testfile", &st) == 0);
+  TEST_CHECK((st.st_mode & 0777) == 0644);
+  fclose(file);
+
+  file = UTI_OpenFile(".", "test", "file", 'W', 0640);
+  TEST_CHECK(file);
+  TEST_CHECK(stat("testfile", &st) == 0);
+  TEST_CHECK((st.st_mode & 0777) == 0640);
+  fclose(file);
+
+  file = UTI_OpenFile(NULL, "test", "file", 'r', 0);
+  TEST_CHECK(file);
+  fclose(file);
+
+  TEST_CHECK(UTI_RenameTempFile(NULL, "testfil", "e", NULL));
+  TEST_CHECK(stat("testfil", &st) == 0);
+  file = UTI_OpenFile(NULL, "testfil", NULL, 'R', 0);
+  TEST_CHECK(file);
+  fclose(file);
+
+  TEST_CHECK(UTI_RenameTempFile(NULL, "test", "fil", "file"));
+  TEST_CHECK(stat("testfile", &st) == 0);
+  file = UTI_OpenFile(NULL, "testfile", NULL, 'R', 0);
+  TEST_CHECK(file);
+  fclose(file);
+
+  TEST_CHECK(UTI_RemoveFile(NULL, "testfile", NULL));
+  TEST_CHECK(stat("testfile", &st) < 0);
+  TEST_CHECK(!UTI_RemoveFile(NULL, "testfile", NULL));
 }