OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 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 <string> | |
16 | |
17 #include "webrtc/base/gunit.h" | |
18 #include "webrtc/base/file.h" | |
19 #include "webrtc/test/testsupport/fileutils.h" | |
20 | |
21 namespace rtc { | |
22 | |
23 rtc::PlatformFile OpenFile(const std::string& path) { | |
sprang_webrtc
2016/08/05 14:45:14
Could this be a static function in the File class
palmkvist
2016/08/08 11:15:35
Definitely, but it apparently won't work in chrome
| |
24 #if defined(WEBRTC_WIN) | |
25 HANDLE handle = | |
26 ::CreateFile(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, | |
27 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); | |
28 return handle; | |
29 #else | |
30 int fd = ::open(path.c_str(), O_RDWR); | |
31 return fd; | |
32 #endif | |
33 } | |
34 | |
35 void RemoveFile(const std::string& path) { | |
36 #if defined(WEBRTC_WIN) | |
37 ::DeleteFile(path.c_str()); | |
38 #else | |
39 ::unlink(path.c_str()); | |
40 #endif | |
41 } | |
42 | |
43 bool VerifyBuffer(char* buffer, size_t length, char start_value) { | |
44 for (size_t i = 0; i < length; ++i) { | |
45 if (buffer[i] != start_value++) | |
46 return false; | |
47 } | |
48 // Prevent the same buffer from being verified multiple times simply | |
49 // because some operation that should have written to it failed | |
50 memset(buffer, 0, length); | |
51 return true; | |
52 } | |
53 | |
54 class FileTest : public ::testing::Test { | |
55 protected: | |
56 std::string path_; | |
57 void SetUp() { | |
58 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file"); | |
59 } | |
60 rtc::PlatformFile OpenTempFile() { return OpenFile(path_); } | |
61 void TearDown() { RemoveFile(path_); } | |
62 }; | |
63 | |
64 TEST_F(FileTest, SimpleReadWrite) { | |
65 rtc::PlatformFile p_file = OpenTempFile(); | |
66 ASSERT_NE(rtc::kInvalidPlatformFileValue, p_file); | |
67 File file(p_file); | |
68 | |
69 char data[100] = {0}; | |
70 char out[100] = {0}; | |
71 for (int i = 0; i < 100; ++i) { | |
72 data[i] = i; | |
73 } | |
74 | |
75 EXPECT_EQ(size_t{10}, file.Write(data, 10)); | |
sprang_webrtc
2016/08/05 14:45:14
I think you can just use 10u instead of a cast.
palmkvist
2016/08/08 11:15:35
Done.
| |
76 | |
77 EXPECT_TRUE(file.Seek(0)); | |
78 EXPECT_EQ(size_t{10}, file.Read(out, 10)); | |
79 EXPECT_TRUE(VerifyBuffer(out, 10, 0)); | |
80 | |
81 EXPECT_TRUE(file.Seek(0)); | |
82 EXPECT_EQ(size_t{100}, file.Write(data, 100)); | |
83 | |
84 EXPECT_TRUE(file.Seek(0)); | |
85 EXPECT_EQ(size_t{100}, file.Read(out, 100)); | |
86 EXPECT_TRUE(VerifyBuffer(out, 100, 0)); | |
87 | |
88 EXPECT_TRUE(file.Seek(1)); | |
89 EXPECT_EQ(size_t{50}, file.Write(data, 50)); | |
90 EXPECT_EQ(size_t{50}, file.Write(data + 50, 50)); | |
91 | |
92 EXPECT_TRUE(file.Seek(1)); | |
93 EXPECT_EQ(size_t{100}, file.Read(out, 100)); | |
94 EXPECT_TRUE(VerifyBuffer(out, 100, 0)); | |
95 } | |
96 | |
97 TEST_F(FileTest, ReadWriteClose) { | |
98 File file(OpenTempFile()); | |
99 | |
100 char data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
101 char out[10] = {0}; | |
102 EXPECT_EQ(size_t{10}, file.Write(data, 10)); | |
103 EXPECT_TRUE(file.Close()); | |
104 | |
105 File file2(OpenTempFile()); | |
106 EXPECT_EQ(size_t{10}, file2.Read(out, 10)); | |
107 EXPECT_TRUE(VerifyBuffer(out, 10, 0)); | |
108 } | |
109 | |
110 TEST_F(FileTest, RandomAccessRead) { | |
111 File file(OpenTempFile()); | |
112 | |
113 char data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
114 char out[10] = {0}; | |
115 EXPECT_EQ(size_t{10}, file.Write(data, 10)); | |
116 | |
117 EXPECT_EQ(size_t{4}, file.ReadAt(out, 4, 0)); | |
118 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | |
119 | |
120 EXPECT_EQ(size_t{4}, file.ReadAt(out, 4, 4)); | |
121 EXPECT_TRUE(VerifyBuffer(out, 4, 4)); | |
122 | |
123 EXPECT_EQ(size_t{5}, file.ReadAt(out, 5, 5)); | |
124 EXPECT_TRUE(VerifyBuffer(out, 5, 5)); | |
125 } | |
126 | |
127 TEST_F(FileTest, RandomAccessReadWrite) { | |
128 File file(OpenTempFile()); | |
129 | |
130 char data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
131 char out[10] = {0}; | |
132 EXPECT_EQ(size_t{10}, file.Write(data, 10)); | |
133 EXPECT_TRUE(file.Seek(4)); | |
134 | |
135 EXPECT_EQ(size_t{4}, file.WriteAt(data, 4, 4)); | |
136 EXPECT_EQ(size_t{4}, file.Read(out, 4)); | |
137 EXPECT_TRUE(VerifyBuffer(out, 4, 0)); | |
138 | |
139 EXPECT_EQ(size_t{2}, file.WriteAt(data, 2, 8)); | |
140 EXPECT_EQ(size_t{2}, file.ReadAt(out, 2, 8)); | |
141 EXPECT_TRUE(VerifyBuffer(out, 2, 0)); | |
142 } | |
143 | |
144 } // namespace rtc | |
OLD | NEW |