OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "sandbox/linux/tests/scoped_temporary_file.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 9 #include <stdlib.h> |
| 10 #include <sys/stat.h> |
| 11 #include <sys/types.h> |
| 12 #include <unistd.h> |
| 13 |
| 14 #include <string> |
| 15 |
| 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" |
| 20 |
| 21 namespace sandbox { |
| 22 |
| 23 namespace { |
| 24 |
| 25 bool FullWrite(int fd, const char* buffer, size_t count) { |
| 26 while (count > 0) { |
| 27 const ssize_t transfered = HANDLE_EINTR(write(fd, buffer, count)); |
| 28 if (transfered <= 0 || static_cast<size_t>(transfered) > count) { |
| 29 return false; |
| 30 } |
| 31 count -= transfered; |
| 32 buffer += transfered; |
| 33 } |
| 34 return true; |
| 35 } |
| 36 |
| 37 bool FullRead(int fd, char* buffer, size_t count) { |
| 38 while (count > 0) { |
| 39 const ssize_t transfered = HANDLE_EINTR(read(fd, buffer, count)); |
| 40 if (transfered <= 0 || static_cast<size_t>(transfered) > count) { |
| 41 return false; |
| 42 } |
| 43 count -= transfered; |
| 44 buffer += transfered; |
| 45 } |
| 46 return true; |
| 47 } |
| 48 |
| 49 TEST(ScopedTemporaryFile, Basics) { |
| 50 std::string temp_file_name; |
| 51 { |
| 52 ScopedTemporaryFile temp_file_1; |
| 53 const char kTestString[] = "This is a test"; |
| 54 ASSERT_LE(0, temp_file_1.fd()); |
| 55 |
| 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()); |
| 59 |
| 60 ASSERT_TRUE(FullWrite(temp_file_1.fd(), kTestString, sizeof(kTestString))); |
| 61 |
| 62 char test_string_read[sizeof(kTestString)] = {0}; |
| 63 ASSERT_TRUE(FullRead( |
| 64 temp_file_2.get(), test_string_read, sizeof(test_string_read))); |
| 65 ASSERT_EQ(0, memcmp(kTestString, test_string_read, sizeof(kTestString))); |
| 66 } |
| 67 |
| 68 errno = 0; |
| 69 struct stat buf; |
| 70 ASSERT_EQ(-1, stat(temp_file_name.c_str(), &buf)); |
| 71 ASSERT_EQ(ENOENT, errno); |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 } // namespace sandbox |
OLD | NEW |