OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <sys/types.h> | |
12 #include <sys/stat.h> | |
13 #include <fcntl.h> | |
14 | |
15 #include <limits> | 11 #include <limits> |
16 #include <memory> | 12 #include <memory> |
17 #include <string> | 13 #include <string> |
18 | 14 |
19 #include "webrtc/base/gunit.h" | 15 #include "webrtc/base/gunit.h" |
20 #include "webrtc/base/file.h" | 16 #include "webrtc/base/file.h" |
21 #include "webrtc/test/testsupport/fileutils.h" | 17 #include "webrtc/test/testsupport/fileutils.h" |
22 | 18 |
23 #if defined(WEBRTC_WIN) | 19 #if defined(WEBRTC_WIN) |
24 | 20 |
25 #include "webrtc/base/win32.h" | 21 #include "webrtc/base/win32.h" |
26 | 22 |
27 #else // if defined(WEBRTC_WIN) | 23 #else // if defined(WEBRTC_WIN) |
28 | 24 |
29 #include <errno.h> | 25 #include <errno.h> |
30 | 26 |
31 #endif | 27 #endif |
32 | 28 |
33 namespace rtc { | 29 namespace rtc { |
34 | 30 |
35 void RemoveFile(const std::string& path) { | |
36 #if defined(WEBRTC_WIN) | |
37 ::DeleteFile(ToUtf16(path).c_str()); | |
38 #else | |
39 ::unlink(path.c_str()); | |
40 #endif | |
41 } | |
42 | |
43 int LastError() { | 31 int LastError() { |
44 #if defined(WEBRTC_WIN) | 32 #if defined(WEBRTC_WIN) |
45 return ::GetLastError(); | 33 return ::GetLastError(); |
46 #else | 34 #else |
47 return errno; | 35 return errno; |
48 #endif | 36 #endif |
49 } | 37 } |
50 | 38 |
51 bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) { | 39 bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) { |
52 for (size_t i = 0; i < length; ++i) { | 40 for (size_t i = 0; i < length; ++i) { |
53 uint8_t val = start_value++; | 41 uint8_t val = start_value++; |
54 EXPECT_EQ(val, buffer[i]); | 42 EXPECT_EQ(val, buffer[i]); |
55 if (buffer[i] != val) | 43 if (buffer[i] != val) |
56 return false; | 44 return false; |
57 } | 45 } |
58 // Prevent the same buffer from being verified multiple times simply | 46 // Prevent the same buffer from being verified multiple times simply |
59 // because some operation that should have written to it failed | 47 // because some operation that should have written to it failed |
60 memset(buffer, 0, length); | 48 memset(buffer, 0, length); |
61 return true; | 49 return true; |
62 } | 50 } |
63 | 51 |
64 class FileTest : public ::testing::Test { | 52 class FileTest : public ::testing::Test { |
65 protected: | 53 protected: |
66 std::string path_; | 54 std::string path_; |
67 void SetUp() { | 55 void SetUp() { |
68 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file"); | 56 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file"); |
69 ASSERT_FALSE(path_.empty()); | 57 ASSERT_FALSE(path_.empty()); |
70 } | 58 } |
71 rtc::File OpenTempFile() { return rtc::File::Open(path_); } | 59 rtc::File OpenTempFile() { return rtc::File::Open(path_); } |
72 void TearDown() { RemoveFile(path_); } | 60 void TearDown() { rtc::RemoveFile(path_); } |
73 }; | 61 }; |
74 | 62 |
75 TEST_F(FileTest, DoubleClose) { | 63 TEST_F(FileTest, DoubleClose) { |
76 File file = OpenTempFile(); | 64 File file = OpenTempFile(); |
77 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); | 65 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); |
78 | 66 |
79 EXPECT_TRUE(file.Close()); | 67 EXPECT_TRUE(file.Close()); |
80 EXPECT_FALSE(file.Close()); | 68 EXPECT_FALSE(file.Close()); |
81 } | 69 } |
82 | 70 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 EXPECT_EQ(4u, file.WriteAt(data, 4, 4)); | 145 EXPECT_EQ(4u, file.WriteAt(data, 4, 4)); |
158 EXPECT_EQ(4u, file.ReadAt(out, 4, 4)); | 146 EXPECT_EQ(4u, file.ReadAt(out, 4, 4)); |
159 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | 147 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); |
160 | 148 |
161 EXPECT_EQ(2u, file.WriteAt(data, 2, 8)); | 149 EXPECT_EQ(2u, file.WriteAt(data, 2, 8)); |
162 EXPECT_EQ(2u, file.ReadAt(out, 2, 8)); | 150 EXPECT_EQ(2u, file.ReadAt(out, 2, 8)); |
163 EXPECT_TRUE(VerifyBuffer(out, 2, 0)); | 151 EXPECT_TRUE(VerifyBuffer(out, 2, 0)); |
164 } | 152 } |
165 | 153 |
166 } // namespace rtc | 154 } // namespace rtc |
OLD | NEW |