diff options
-rw-r--r-- | src/libsystemd-bus/test-bus-memfd.c | 81 |
1 files changed, 75 insertions, 6 deletions
diff --git a/src/libsystemd-bus/test-bus-memfd.c b/src/libsystemd-bus/test-bus-memfd.c index 37e150a397..3300470e0f 100644 --- a/src/libsystemd-bus/test-bus-memfd.c +++ b/src/libsystemd-bus/test-bus-memfd.c @@ -33,6 +33,9 @@ int main(int argc, char *argv[]) { uint64_t sz; int r, fd; FILE *f; + char buf[3] = {}; + struct iovec iov[3] = {}; + char bufv[3][3] = {}; log_set_max_level(LOG_DEBUG); @@ -49,12 +52,20 @@ int main(int argc, char *argv[]) { assert_se(r == -EPERM); assert_se(write(sd_memfd_get_fd(m), "he", 2) == 2); + assert_se(write(sd_memfd_get_fd(m), "XXX", 3) == 3); + assert_se(streq(s, "heXXX world")); + + /* fix "hello" */ + assert_se(lseek(sd_memfd_get_fd(m), 2, SEEK_SET) == 2); assert_se(write(sd_memfd_get_fd(m), "ll", 2) == 2); assert_se(sd_memfd_get_file(m, &f) >= 0); fputc('o', f); fflush(f); + /* check content */ + assert_se(streq(s, "hello world")); + assert_se(munmap(s, 12) == 0); r = sd_memfd_get_sealed(m); @@ -64,9 +75,15 @@ int main(int argc, char *argv[]) { assert_se(r >= 0); assert_se(sz = page_size()); - r = sd_memfd_set_size(m, 12); + /* truncate it */ + r = sd_memfd_set_size(m, 6); assert_se(r >= 0); + /* get back new value */ + r = sd_memfd_get_size(m, &sz); + assert_se(r >= 0); + assert_se(sz == 6); + r = sd_memfd_set_sealed(m, 1); assert_se(r >= 0); @@ -78,27 +95,79 @@ int main(int argc, char *argv[]) { sd_memfd_free(m); + /* new sd_memfd, same underlying memfd */ r = sd_memfd_make(fd, &m); assert_se(r >= 0); + /* we did truncate it to 6 */ r = sd_memfd_get_size(m, &sz); - assert_se(r >= 0); - assert_se(sz = 12); + assert_se(sz == 6); - r = sd_memfd_map(m, 0, 12, (void**) &s); + /* map it, check content */ + r = sd_memfd_map(m, 0, 12, (void **)&s); assert_se(r >= 0); + /* we only see the truncated size */ + assert_se(streq(s, "hello ")); + + /* it was already sealed */ r = sd_memfd_set_sealed(m, 1); assert_se(r == -EALREADY); + /* we cannot break the seal, it is mapped */ r = sd_memfd_set_sealed(m, 0); assert_se(r == -EPERM); - assert_se(streq(s, "hello world")); + /* unmap it; become the single owner */ assert_se(munmap(s, 12) == 0); + /* now we can do flip the sealing */ r = sd_memfd_set_sealed(m, 0); - assert_se(r >= 0); + assert_se(r == 0); + r = sd_memfd_get_sealed(m); + assert_se(r == 0); + + r = sd_memfd_set_sealed(m, 1); + assert_se(r == 0); + r = sd_memfd_get_sealed(m); + assert_se(r == 1); + + r = sd_memfd_set_sealed(m, 0); + assert_se(r == 0); + r = sd_memfd_get_sealed(m); + assert_se(r == 0); + + /* seek at 2, read() 2 bytes */ + assert_se(lseek(fd, 2, SEEK_SET) == 2); + assert_se(read(fd, buf, 2) == 2); + + /* check content */ + assert_se(memcmp(buf, "ll", 2) == 0); + + /* writev it out*/ + iov[0].iov_base = (char *)"ABC"; + iov[0].iov_len = 3; + iov[1].iov_base = (char *)"DEF"; + iov[1].iov_len = 3; + iov[2].iov_base = (char *)"GHI"; + iov[2].iov_len = 3; + assert_se(lseek(fd, 0, SEEK_SET) == 0); + assert_se(writev(fd, iov, 3) == 9); + + /* readv it back */ + iov[0].iov_base = bufv[0]; + iov[0].iov_len = 3; + iov[1].iov_base = bufv[1]; + iov[1].iov_len = 3; + iov[2].iov_base = bufv[2]; + iov[2].iov_len = 3; + assert_se(lseek(fd, 0, SEEK_SET) == 0); + assert_se(readv(fd, iov, 3) == 9); + + /* check content */ + assert_se(memcmp(bufv[0], "ABC", 3) == 0); + assert_se(memcmp(bufv[1], "DEF", 3) == 0); + assert_se(memcmp(bufv[2], "GHI", 3) == 0); sd_memfd_free(m); |