blob: edf2b29127a5ede58a160afb94efcde5a36d9a5a [file] [log] [blame]
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +02001/*
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
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020011#include <limits>
12#include <memory>
13#include <string>
14
15#include "webrtc/base/gunit.h"
16#include "webrtc/base/file.h"
17#include "webrtc/test/testsupport/fileutils.h"
18
19#if defined(WEBRTC_WIN)
20
21#include "webrtc/base/win32.h"
22
23#else // if defined(WEBRTC_WIN)
24
25#include <errno.h>
26
27#endif
28
29namespace rtc {
30
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020031int LastError() {
32#if defined(WEBRTC_WIN)
33 return ::GetLastError();
34#else
35 return errno;
36#endif
37}
38
39bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) {
40 for (size_t i = 0; i < length; ++i) {
41 uint8_t val = start_value++;
42 EXPECT_EQ(val, buffer[i]);
43 if (buffer[i] != val)
44 return false;
45 }
46 // Prevent the same buffer from being verified multiple times simply
47 // because some operation that should have written to it failed
48 memset(buffer, 0, length);
49 return true;
50}
51
52class FileTest : public ::testing::Test {
53 protected:
54 std::string path_;
55 void SetUp() {
56 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file");
57 ASSERT_FALSE(path_.empty());
58 }
59 rtc::File OpenTempFile() { return rtc::File::Open(path_); }
Viktor Palmkvist971eb272016-09-16 10:19:23 +020060 void TearDown() { rtc::RemoveFile(path_); }
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020061};
62
63TEST_F(FileTest, DoubleClose) {
64 File file = OpenTempFile();
65 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
66
67 EXPECT_TRUE(file.Close());
68 EXPECT_FALSE(file.Close());
69}
70
71TEST_F(FileTest, SimpleReadWrite) {
72 File file = OpenTempFile();
73 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
74
75 uint8_t data[100] = {0};
76 uint8_t out[100] = {0};
77 for (int i = 0; i < 100; ++i) {
78 data[i] = i;
79 }
80
81 EXPECT_EQ(10u, file.Write(data, 10));
82
83 EXPECT_TRUE(file.Seek(0));
84 EXPECT_EQ(10u, file.Read(out, 10));
85 EXPECT_TRUE(VerifyBuffer(out, 10, 0));
86
87 EXPECT_TRUE(file.Seek(0));
88 EXPECT_EQ(100u, file.Write(data, 100));
89
90 EXPECT_TRUE(file.Seek(0));
91 EXPECT_EQ(100u, file.Read(out, 100));
92 EXPECT_TRUE(VerifyBuffer(out, 100, 0));
93
94 EXPECT_TRUE(file.Seek(1));
95 EXPECT_EQ(50u, file.Write(data, 50));
96 EXPECT_EQ(50u, file.Write(data + 50, 50));
97
98 EXPECT_TRUE(file.Seek(1));
99 EXPECT_EQ(100u, file.Read(out, 100));
100 EXPECT_TRUE(VerifyBuffer(out, 100, 0));
101}
102
103TEST_F(FileTest, ReadWriteClose) {
104 File file = OpenTempFile();
105 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
106
107 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
108 uint8_t out[10] = {0};
109 EXPECT_EQ(10u, file.Write(data, 10));
110 EXPECT_TRUE(file.Close());
111
112 File file2 = OpenTempFile();
113 ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError();
114 EXPECT_EQ(10u, file2.Read(out, 10));
115 EXPECT_TRUE(VerifyBuffer(out, 10, 0));
116}
117
118TEST_F(FileTest, RandomAccessRead) {
119 File file = OpenTempFile();
120 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
121
122 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
123 uint8_t out[10] = {0};
124 EXPECT_EQ(10u, file.Write(data, 10));
125
126 EXPECT_EQ(4u, file.ReadAt(out, 4, 0));
127 EXPECT_TRUE(VerifyBuffer(out, 4, 0));
128
129 EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
130 EXPECT_TRUE(VerifyBuffer(out, 4, 4));
131
132 EXPECT_EQ(5u, file.ReadAt(out, 5, 5));
133 EXPECT_TRUE(VerifyBuffer(out, 5, 5));
134}
135
136TEST_F(FileTest, RandomAccessReadWrite) {
137 File file = OpenTempFile();
138 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
139
140 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
141 uint8_t out[10] = {0};
142 EXPECT_EQ(10u, file.Write(data, 10));
143 EXPECT_TRUE(file.Seek(4));
144
145 EXPECT_EQ(4u, file.WriteAt(data, 4, 4));
146 EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
147 EXPECT_TRUE(VerifyBuffer(out, 4, 0));
148
149 EXPECT_EQ(2u, file.WriteAt(data, 2, 8));
150 EXPECT_EQ(2u, file.ReadAt(out, 2, 8));
151 EXPECT_TRUE(VerifyBuffer(out, 2, 0));
152}
153
154} // namespace rtc