OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <sys/types.h> | |
12 #include <sys/stat.h> | |
13 #include <fcntl.h> | |
14 | |
15 #include <limits> | |
16 #include <memory> | |
17 #include <string> | |
18 | |
19 #include "webrtc/base/gunit.h" | |
20 #include "webrtc/base/file.h" | |
21 #include "webrtc/test/testsupport/fileutils.h" | |
22 | |
23 #if defined(WEBRTC_WIN) | |
24 | |
25 #include "webrtc/base/win32.h" | |
26 | |
27 #else // if defined(WEBRTC_WIN) | |
28 | |
29 #include <errno.h> | |
30 | |
31 #endif | |
32 | |
33 namespace rtc { | |
34 | |
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() { | |
44 #if defined(WEBRTC_WIN) | |
45 return ::GetLastError(); | |
46 #else | |
47 return errno; | |
48 #endif | |
49 } | |
50 | |
51 bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) { | |
52 for (size_t i = 0; i < length; ++i) { | |
53 uint8_t val = start_value++; | |
54 EXPECT_EQ(val, buffer[i]); | |
55 if (buffer[i] != val) | |
56 return false; | |
57 } | |
58 // Prevent the same buffer from being verified multiple times simply | |
59 // because some operation that should have written to it failed | |
60 memset(buffer, 0, length); | |
61 return true; | |
62 } | |
63 | |
64 class FileTest : public ::testing::Test { | |
65 protected: | |
66 std::string path_; | |
67 void SetUp() { | |
68 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file"); | |
69 ASSERT_FALSE(path_.empty()); | |
70 } | |
71 rtc::File OpenTempFile() { return rtc::File::Open(path_); } | |
72 void TearDown() { RemoveFile(path_); } | |
73 }; | |
74 | |
75 TEST_F(FileTest, DoubleClose) { | |
76 File file = OpenTempFile(); | |
77 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); | |
78 | |
79 EXPECT_TRUE(file.Close()); | |
80 EXPECT_FALSE(file.Close()); | |
81 } | |
82 | |
83 TEST_F(FileTest, SimpleReadWrite) { | |
84 File file = OpenTempFile(); | |
85 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); | |
86 | |
87 uint8_t data[100] = {0}; | |
88 uint8_t out[100] = {0}; | |
89 for (int i = 0; i < 100; ++i) { | |
90 data[i] = i; | |
91 } | |
92 | |
93 EXPECT_EQ(10u, file.Write(data, 10)); | |
94 | |
95 EXPECT_TRUE(file.Seek(0)); | |
96 EXPECT_EQ(10u, file.Read(out, 10)); | |
97 EXPECT_TRUE(VerifyBuffer(out, 10, 0)); | |
98 | |
99 EXPECT_TRUE(file.Seek(0)); | |
100 EXPECT_EQ(100u, file.Write(data, 100)); | |
101 | |
102 EXPECT_TRUE(file.Seek(0)); | |
103 EXPECT_EQ(100u, file.Read(out, 100)); | |
104 EXPECT_TRUE(VerifyBuffer(out, 100, 0)); | |
105 | |
106 EXPECT_TRUE(file.Seek(1)); | |
107 EXPECT_EQ(50u, file.Write(data, 50)); | |
108 EXPECT_EQ(50u, file.Write(data + 50, 50)); | |
109 | |
110 EXPECT_TRUE(file.Seek(1)); | |
111 EXPECT_EQ(100u, file.Read(out, 100)); | |
112 EXPECT_TRUE(VerifyBuffer(out, 100, 0)); | |
113 } | |
114 | |
115 TEST_F(FileTest, ReadWriteClose) { | |
116 File file = OpenTempFile(); | |
117 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); | |
118 | |
119 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
120 uint8_t out[10] = {0}; | |
121 EXPECT_EQ(10u, file.Write(data, 10)); | |
122 EXPECT_TRUE(file.Close()); | |
123 | |
124 File file2 = OpenTempFile(); | |
125 ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError(); | |
126 EXPECT_EQ(10u, file2.Read(out, 10)); | |
127 EXPECT_TRUE(VerifyBuffer(out, 10, 0)); | |
128 } | |
129 | |
130 TEST_F(FileTest, RandomAccessRead) { | |
131 File file = OpenTempFile(); | |
132 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); | |
133 | |
134 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
135 uint8_t out[10] = {0}; | |
136 EXPECT_EQ(10u, file.Write(data, 10)); | |
137 | |
138 EXPECT_EQ(4u, file.ReadAt(out, 4, 0)); | |
139 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | |
140 | |
141 EXPECT_EQ(4u, file.ReadAt(out, 4, 4)); | |
142 EXPECT_TRUE(VerifyBuffer(out, 4, 4)); | |
143 | |
144 EXPECT_EQ(5u, file.ReadAt(out, 5, 5)); | |
145 EXPECT_TRUE(VerifyBuffer(out, 5, 5)); | |
146 } | |
147 | |
148 TEST_F(FileTest, RandomAccessReadWrite) { | |
149 File file = OpenTempFile(); | |
150 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); | |
151 | |
152 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
153 uint8_t out[10] = {0}; | |
154 EXPECT_EQ(10u, file.Write(data, 10)); | |
155 EXPECT_TRUE(file.Seek(4)); | |
156 | |
157 EXPECT_EQ(4u, file.WriteAt(data, 4, 4)); | |
158 EXPECT_EQ(4u, file.ReadAt(out, 4, 4)); | |
159 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | |
160 | |
161 EXPECT_EQ(2u, file.WriteAt(data, 2, 8)); | |
162 EXPECT_EQ(2u, file.ReadAt(out, 2, 8)); | |
163 EXPECT_TRUE(VerifyBuffer(out, 2, 0)); | |
164 } | |
165 | |
166 } // namespace rtc | |
OLD | NEW |