Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <string.h> |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/file.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "test/testsupport/file_utils.h" |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 17 | |
| 18 | #if defined(WEBRTC_WIN) |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/win32.h" |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 21 | |
| 22 | #else // if defined(WEBRTC_WIN) |
| 23 | |
| 24 | #include <errno.h> |
| 25 | |
| 26 | #endif |
| 27 | |
| 28 | namespace rtc { |
| 29 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 30 | int LastError() { |
| 31 | #if defined(WEBRTC_WIN) |
| 32 | return ::GetLastError(); |
| 33 | #else |
| 34 | return errno; |
| 35 | #endif |
| 36 | } |
| 37 | |
| 38 | bool 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 | |
| 51 | class FileTest : public ::testing::Test { |
| 52 | protected: |
| 53 | std::string path_; |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 54 | void SetUp() override { |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 55 | path_ = webrtc::test::TempFilename(webrtc::test::OutputPath(), "test_file"); |
| 56 | ASSERT_FALSE(path_.empty()); |
| 57 | } |
ehmaldonado | da8dcfb | 2017-01-04 07:11:23 -0800 | [diff] [blame] | 58 | void TearDown() override { RemoveFile(path_); } |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
Viktor Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 61 | TEST_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 Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 74 | TEST_F(FileTest, DoubleClose) { |
Viktor Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 75 | File file = File::Open(path_); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 76 | ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); |
| 77 | |
| 78 | EXPECT_TRUE(file.Close()); |
| 79 | EXPECT_FALSE(file.Close()); |
| 80 | } |
| 81 | |
| 82 | TEST_F(FileTest, SimpleReadWrite) { |
Viktor Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 83 | File file = File::Open(path_); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 84 | 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 | |
| 114 | TEST_F(FileTest, ReadWriteClose) { |
Viktor Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 115 | File file = File::Open(path_); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 116 | 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 Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 123 | File file2 = File::Open(path_); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 124 | ASSERT_TRUE(file2.IsOpen()) << "Error: " << LastError(); |
| 125 | EXPECT_EQ(10u, file2.Read(out, 10)); |
| 126 | EXPECT_TRUE(VerifyBuffer(out, 10, 0)); |
| 127 | } |
| 128 | |
| 129 | TEST_F(FileTest, RandomAccessRead) { |
Viktor Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 130 | File file = File::Open(path_); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 131 | 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 | |
| 147 | TEST_F(FileTest, RandomAccessReadWrite) { |
Viktor Palmkvist | 1d477ea | 2016-10-03 13:16:02 +0200 | [diff] [blame] | 148 | File file = File::Open(path_); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 149 | 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 | |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 165 | TEST_F(FileTest, ShouldBeAbleToRemoveFile) { |
| 166 | { |
Niels Möller | 392f8d0 | 2018-06-01 10:14:44 +0200 | [diff] [blame] | 167 | File file = File::Open(path_); |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 168 | ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); |
| 169 | } |
| 170 | |
Niels Möller | 392f8d0 | 2018-06-01 10:14:44 +0200 | [diff] [blame] | 171 | ASSERT_TRUE(File::Remove(path_)) << "Error: " << LastError(); |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 174 | } // namespace rtc |