1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/linux/tests/scoped_temporary_file.h"
11 #include <sys/types.h>
16 #include "base/files/scoped_file.h"
17 #include "base/logging.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 bool FullWrite(int fd
, const char* buffer
, size_t count
) {
27 const ssize_t transfered
= HANDLE_EINTR(write(fd
, buffer
, count
));
28 if (transfered
<= 0 || static_cast<size_t>(transfered
) > count
) {
37 bool FullRead(int fd
, char* buffer
, size_t count
) {
39 const ssize_t transfered
= HANDLE_EINTR(read(fd
, buffer
, count
));
40 if (transfered
<= 0 || static_cast<size_t>(transfered
) > count
) {
49 TEST(ScopedTemporaryFile
, Basics
) {
50 std::string temp_file_name
;
52 ScopedTemporaryFile temp_file_1
;
53 const char kTestString
[] = "This is a test";
54 ASSERT_LE(0, temp_file_1
.fd());
56 temp_file_name
= temp_file_1
.full_file_name();
57 base::ScopedFD
temp_file_2(open(temp_file_1
.full_file_name(), O_RDONLY
));
58 ASSERT_TRUE(temp_file_2
.is_valid());
60 ASSERT_TRUE(FullWrite(temp_file_1
.fd(), kTestString
, sizeof(kTestString
)));
62 char test_string_read
[sizeof(kTestString
)] = {0};
64 temp_file_2
.get(), test_string_read
, sizeof(test_string_read
)));
65 ASSERT_EQ(0, memcmp(kTestString
, test_string_read
, sizeof(kTestString
)));
70 ASSERT_EQ(-1, stat(temp_file_name
.c_str(), &buf
));
71 ASSERT_EQ(ENOENT
, errno
);
76 } // namespace sandbox