]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_safe_io.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / test / common / test_safe_io.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <algorithm>
5 #include <cstring>
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include "common/safe_io.h"
10
11 #include "gtest/gtest.h"
12
13
14 TEST(SafeIO, safe_read_file) {
15 const char *fname = "safe_read_testfile";
16 ::unlink(fname);
17 int fd = ::open(fname, O_RDWR|O_CREAT|O_TRUNC, 0600);
18 ASSERT_NE(fd, -1);
19 const char buf[] = "0123456789";
20 for (int i = 0; i < 8; i++) {
21 ASSERT_EQ((ssize_t)sizeof(buf), write(fd, buf, sizeof(buf)));
22 }
23 ::close(fd);
24 char rdata[80];
25 ASSERT_EQ((int)sizeof(rdata),
26 safe_read_file(".", fname, rdata, sizeof(rdata)));
27 for (char *p = rdata, *end = rdata+sizeof(rdata); p < end; p+=sizeof(buf)) {
28 ASSERT_EQ(0, std::memcmp(p, buf, std::min(size_t(end-p), sizeof(buf))));
29 }
30 ::unlink(fname);
31 }
32
33 // Local Variables:
34 // compile-command: "cd ../.. ;
35 // make unittest_safe_io &&
36 // ./unittest_safe_io"
37 // End: