Roll libvpx 861f35:1fff3e
[chromium-blink-merge.git] / sandbox / linux / tests / scoped_temporary_file_unittest.cc
blob44a2ecb1ae6fb7fa594f037277441733f232dea4
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"
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>
14 #include <string>
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"
21 namespace sandbox {
23 namespace {
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;
31 count -= transfered;
32 buffer += transfered;
34 return true;
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;
43 count -= transfered;
44 buffer += transfered;
46 return true;
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};
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)));
68 errno = 0;
69 struct stat buf;
70 ASSERT_EQ(-1, stat(temp_file_name.c_str(), &buf));
71 ASSERT_EQ(ENOENT, errno);
74 } // namespace
76 } // namespace sandbox