LCOV - code coverage report
Current view: top level - test - test-xattr-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 39 43 90.7 %
Date: 2019-08-22 15:41:25 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <fcntl.h>
       5             : #include <sys/types.h>
       6             : #include <sys/xattr.h>
       7             : #include <unistd.h>
       8             : 
       9             : #include "alloc-util.h"
      10             : #include "fd-util.h"
      11             : #include "fs-util.h"
      12             : #include "macro.h"
      13             : #include "string-util.h"
      14             : #include "tests.h"
      15             : #include "tmpfile-util.h"
      16             : #include "xattr-util.h"
      17             : 
      18           1 : static void test_fgetxattrat_fake(void) {
      19           1 :         char t[] = "/var/tmp/xattrtestXXXXXX";
      20           1 :         _cleanup_close_ int fd = -1;
      21             :         const char *x;
      22             :         char v[3];
      23             :         int r;
      24             :         size_t size;
      25             : 
      26           1 :         assert_se(mkdtemp(t));
      27           5 :         x = strjoina(t, "/test");
      28           1 :         assert_se(touch(x) >= 0);
      29             : 
      30           1 :         r = setxattr(x, "user.foo", "bar", 3, 0);
      31           1 :         if (r < 0 && errno == EOPNOTSUPP) /* no xattrs supported on /var/tmp... */
      32           0 :                 goto cleanup;
      33           1 :         assert_se(r >= 0);
      34             : 
      35           1 :         fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
      36           1 :         assert_se(fd >= 0);
      37             : 
      38           1 :         assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0, &size) >= 0);
      39           1 :         assert_se(size == 3);
      40           1 :         assert_se(memcmp(v, "bar", 3) == 0);
      41             : 
      42           1 :         safe_close(fd);
      43           1 :         fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
      44           1 :         assert_se(fd >= 0);
      45           1 :         assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0, &size) == -ENODATA);
      46             : 
      47           1 : cleanup:
      48           1 :         assert_se(unlink(x) >= 0);
      49           1 :         assert_se(rmdir(t) >= 0);
      50           1 : }
      51             : 
      52           1 : static void test_getcrtime(void) {
      53             : 
      54           1 :         _cleanup_close_ int fd = -1;
      55             :         char ts[FORMAT_TIMESTAMP_MAX];
      56             :         const char *vt;
      57             :         usec_t usec, k;
      58             :         int r;
      59             : 
      60           1 :         assert_se(tmp_dir(&vt) >= 0);
      61             : 
      62           1 :         fd = open_tmpfile_unlinkable(vt, O_RDWR);
      63           1 :         assert_se(fd >= 0);
      64             : 
      65           1 :         r = fd_getcrtime(fd, &usec);
      66           1 :         if (r < 0)
      67           1 :                 log_debug_errno(r, "btime: %m");
      68             :         else
      69           0 :                 log_debug("btime: %s", format_timestamp(ts, sizeof(ts), usec));
      70             : 
      71           1 :         k = now(CLOCK_REALTIME);
      72             : 
      73           1 :         r = fd_setcrtime(fd, 1519126446UL * USEC_PER_SEC);
      74           1 :         if (!IN_SET(r, -EOPNOTSUPP, -ENOTTY)) {
      75           0 :                 assert_se(fd_getcrtime(fd, &usec) >= 0);
      76           0 :                 assert_se(k < 1519126446UL * USEC_PER_SEC ||
      77             :                           usec == 1519126446UL * USEC_PER_SEC);
      78             :         }
      79           1 : }
      80             : 
      81           1 : int main(void) {
      82           1 :         test_setup_logging(LOG_DEBUG);
      83             : 
      84           1 :         test_fgetxattrat_fake();
      85           1 :         test_getcrtime();
      86             : 
      87           1 :         return 0;
      88             : }

Generated by: LCOV version 1.14