blob: bd3a61b5551155418be00a1d6b40aafe199f0783 [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
Yves Gerey3e707812018-11-28 16:47:49 +010011#include <string.h>
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020012#include <string>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/file.h"
Yves Gerey3e707812018-11-28 16:47:49 +010015#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "test/testsupport/file_utils.h"
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020017
18#if defined(WEBRTC_WIN)
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/win32.h"
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020021
22#else // if defined(WEBRTC_WIN)
23
24#include <errno.h>
25
26#endif
27
28namespace rtc {
29
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020030int LastError() {
31#if defined(WEBRTC_WIN)
32 return ::GetLastError();
33#else
34 return errno;
35#endif
36}
37
38bool VerifyBuffer(uint8_t* buffer, size_t length, uint8_t start_value) {
39 for (size_t i = 0; i < length; ++i) {
40 uint8_t val = start_value++;
41 EXPECT_EQ(val, buffer[i]);
42 if (buffer[i] != val)
43 return false;
44 }
45 // Prevent the same buffer from being verified multiple times simply
46 // because some operation that should have written to it failed
47 memset(buffer, 0, length);
48 return true;
49}
50
51class FileTest : public ::testing::Test {
52 protected:
53 std::string path_;
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080054 void SetUp() override {
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020055 path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file");
56 ASSERT_FALSE(path_.empty());
57 }
ehmaldonadoda8dcfb2017-01-04 07:11:23 -080058 void TearDown() override { RemoveFile(path_); }
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020059};
60
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +020061TEST_F(FileTest, DefaultConstructor) {
62 File file;
63 uint8_t buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
64
65 EXPECT_FALSE(file.IsOpen());
66 EXPECT_EQ(0u, file.Write(buffer, 10));
67 EXPECT_FALSE(file.Seek(0));
68 EXPECT_EQ(0u, file.Read(buffer, 10));
69 EXPECT_EQ(0u, file.WriteAt(buffer, 10, 0));
70 EXPECT_EQ(0u, file.ReadAt(buffer, 10, 0));
71 EXPECT_FALSE(file.Close());
72}
73
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020074TEST_F(FileTest, DoubleClose) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +020075 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020076 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
77
78 EXPECT_TRUE(file.Close());
79 EXPECT_FALSE(file.Close());
80}
81
82TEST_F(FileTest, SimpleReadWrite) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +020083 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +020084 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
85
86 uint8_t data[100] = {0};
87 uint8_t out[100] = {0};
88 for (int i = 0; i < 100; ++i) {
89 data[i] = i;
90 }
91
92 EXPECT_EQ(10u, file.Write(data, 10));
93
94 EXPECT_TRUE(file.Seek(0));
95 EXPECT_EQ(10u, file.Read(out, 10));
96 EXPECT_TRUE(VerifyBuffer(out, 10, 0));
97
98 EXPECT_TRUE(file.Seek(0));
99 EXPECT_EQ(100u, file.Write(data, 100));
100
101 EXPECT_TRUE(file.Seek(0));
102 EXPECT_EQ(100u, file.Read(out, 100));
103 EXPECT_TRUE(VerifyBuffer(out, 100, 0));
104
105 EXPECT_TRUE(file.Seek(1));
106 EXPECT_EQ(50u, file.Write(data, 50));
107 EXPECT_EQ(50u, file.Write(data + 50, 50));
108
109 EXPECT_TRUE(file.Seek(1));
110 EXPECT_EQ(100u, file.Read(out, 100));
111 EXPECT_TRUE(VerifyBuffer(out, 100, 0));
112}
113
114TEST_F(FileTest, ReadWriteClose) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +0200115 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200116 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
117
118 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
119 uint8_t out[10] = {0};
120 EXPECT_EQ(10u, file.Write(data, 10));
121 EXPECT_TRUE(file.Close());
122
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +0200123 File file2 = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200124 ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError();
125 EXPECT_EQ(10u, file2.Read(out, 10));
126 EXPECT_TRUE(VerifyBuffer(out, 10, 0));
127}
128
129TEST_F(FileTest, RandomAccessRead) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +0200130 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200131 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
132
133 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
134 uint8_t out[10] = {0};
135 EXPECT_EQ(10u, file.Write(data, 10));
136
137 EXPECT_EQ(4u, file.ReadAt(out, 4, 0));
138 EXPECT_TRUE(VerifyBuffer(out, 4, 0));
139
140 EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
141 EXPECT_TRUE(VerifyBuffer(out, 4, 4));
142
143 EXPECT_EQ(5u, file.ReadAt(out, 5, 5));
144 EXPECT_TRUE(VerifyBuffer(out, 5, 5));
145}
146
147TEST_F(FileTest, RandomAccessReadWrite) {
Viktor Palmkvist1d477ea2016-10-03 13:16:02 +0200148 File file = File::Open(path_);
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200149 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
150
151 uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
152 uint8_t out[10] = {0};
153 EXPECT_EQ(10u, file.Write(data, 10));
154 EXPECT_TRUE(file.Seek(4));
155
156 EXPECT_EQ(4u, file.WriteAt(data, 4, 4));
157 EXPECT_EQ(4u, file.ReadAt(out, 4, 4));
158 EXPECT_TRUE(VerifyBuffer(out, 4, 0));
159
160 EXPECT_EQ(2u, file.WriteAt(data, 2, 8));
161 EXPECT_EQ(2u, file.ReadAt(out, 2, 8));
162 EXPECT_TRUE(VerifyBuffer(out, 2, 0));
163}
164
zijiehe2769ec62016-12-14 15:03:03 -0800165TEST_F(FileTest, ShouldBeAbleToRemoveFile) {
166 {
Niels Möller392f8d02018-06-01 10:14:44 +0200167 File file = File::Open(path_);
zijiehe2769ec62016-12-14 15:03:03 -0800168 ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError();
169 }
170
Niels Möller392f8d02018-06-01 10:14:44 +0200171 ASSERT_TRUE(File::Remove(path_)) << "Error: " << LastError();
zijiehe2769ec62016-12-14 15:03:03 -0800172}
173
Viktor Palmkvist4ec6a0c2016-09-02 13:38:32 +0200174} // namespace rtc