LCOV - code coverage report
Current view: top level - test - test-io-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 31 31 100.0 %
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 <fcntl.h>
       4             : #include <stdlib.h>
       5             : #include <unistd.h>
       6             : 
       7             : #include "alloc-util.h"
       8             : #include "fd-util.h"
       9             : #include "io-util.h"
      10             : #include "macro.h"
      11             : 
      12           5 : static void test_sparse_write_one(int fd, const char *buffer, size_t n) {
      13           5 :         char check[n];
      14             : 
      15           5 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
      16           5 :         assert_se(ftruncate(fd, 0) >= 0);
      17           5 :         assert_se(sparse_write(fd, buffer, n, 4) == (ssize_t) n);
      18             : 
      19           5 :         assert_se(lseek(fd, 0, SEEK_CUR) == (off_t) n);
      20           5 :         assert_se(ftruncate(fd, n) >= 0);
      21             : 
      22           5 :         assert_se(lseek(fd, 0, SEEK_SET) == 0);
      23           5 :         assert_se(read(fd, check, n) == (ssize_t) n);
      24             : 
      25           5 :         assert_se(memcmp(buffer, check, n) == 0);
      26           5 : }
      27             : 
      28           1 : static void test_sparse_write(void) {
      29           1 :         const char test_a[] = "test";
      30           1 :         const char test_b[] = "\0\0\0\0test\0\0\0\0";
      31           1 :         const char test_c[] = "\0\0test\0\0\0\0";
      32           1 :         const char test_d[] = "\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0test\0\0\0test\0\0\0\0test\0\0\0\0\0\0\0\0";
      33           1 :         const char test_e[] = "test\0\0\0\0test";
      34           1 :         _cleanup_close_ int fd = -1;
      35           1 :         char fn[] = "/tmp/sparseXXXXXX";
      36             : 
      37           1 :         fd = mkostemp(fn, O_CLOEXEC);
      38           1 :         assert_se(fd >= 0);
      39           1 :         unlink(fn);
      40             : 
      41           1 :         test_sparse_write_one(fd, test_a, sizeof(test_a));
      42           1 :         test_sparse_write_one(fd, test_b, sizeof(test_b));
      43           1 :         test_sparse_write_one(fd, test_c, sizeof(test_c));
      44           1 :         test_sparse_write_one(fd, test_d, sizeof(test_d));
      45           1 :         test_sparse_write_one(fd, test_e, sizeof(test_e));
      46           1 : }
      47             : 
      48           1 : int main(void) {
      49           1 :         test_sparse_write();
      50             : 
      51           1 :         return 0;
      52             : }

Generated by: LCOV version 1.14