Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "rtc_base/platform_file.h" |
| 12 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 13 | #include "test/testsupport/file_utils.h" |
Artem Titov | e62f600 | 2018-03-19 15:40:00 +0100 | [diff] [blame] | 14 | |
| 15 | namespace rtc { |
| 16 | |
| 17 | void FillWithDummyDataAndClose(FILE* const file, const std::string& filename) { |
| 18 | EXPECT_GT(fprintf(file, "%s", "Dummy data"), 0) |
| 19 | << "Failed to write to file: " << filename; |
| 20 | fclose(file); |
| 21 | } |
| 22 | |
| 23 | TEST(PlatformFileTest, CreateWriteAndDelete) { |
| 24 | const std::string filename = webrtc::test::GenerateTempFilename( |
| 25 | webrtc::test::OutputPath(), ".testfile"); |
| 26 | const PlatformFile fd = rtc::CreatePlatformFile(filename); |
| 27 | ASSERT_NE(fd, rtc::kInvalidPlatformFileValue) |
| 28 | << "Failed to create file descriptor for file: " << filename; |
| 29 | FILE* const file = rtc::FdopenPlatformFile(fd, "w"); |
| 30 | ASSERT_TRUE(file != nullptr) << "Failed to open file: " << filename; |
| 31 | FillWithDummyDataAndClose(file, filename); |
| 32 | webrtc::test::RemoveFile(filename); |
| 33 | } |
| 34 | |
| 35 | TEST(PlatformFileTest, OpenExistingWriteAndDelete) { |
| 36 | const std::string filename = webrtc::test::GenerateTempFilename( |
| 37 | webrtc::test::OutputPath(), ".testfile"); |
| 38 | |
| 39 | // Create file with dummy data. |
| 40 | FILE* file = fopen(filename.c_str(), "wb"); |
| 41 | ASSERT_TRUE(file != nullptr) << "Failed to open file: " << filename; |
| 42 | FillWithDummyDataAndClose(file, filename); |
| 43 | |
| 44 | // Open it for write, write and delete. |
| 45 | const PlatformFile fd = rtc::OpenPlatformFile(filename); |
| 46 | ASSERT_NE(fd, rtc::kInvalidPlatformFileValue) |
| 47 | << "Failed to open file descriptor for file: " << filename; |
| 48 | file = rtc::FdopenPlatformFile(fd, "w"); |
| 49 | ASSERT_TRUE(file != nullptr) << "Failed to open file: " << filename; |
| 50 | FillWithDummyDataAndClose(file, filename); |
| 51 | webrtc::test::RemoveFile(filename); |
| 52 | } |
| 53 | |
| 54 | TEST(PlatformFileTest, OpenExistingReadOnlyAndDelete) { |
| 55 | const std::string filename = webrtc::test::GenerateTempFilename( |
| 56 | webrtc::test::OutputPath(), ".testfile"); |
| 57 | |
| 58 | // Create file with dummy data. |
| 59 | FILE* file = fopen(filename.c_str(), "wb"); |
| 60 | ASSERT_TRUE(file != nullptr) << "Failed to open file: " << filename; |
| 61 | FillWithDummyDataAndClose(file, filename); |
| 62 | |
| 63 | // Open it for read, read and delete. |
| 64 | const PlatformFile fd = rtc::OpenPlatformFileReadOnly(filename); |
| 65 | ASSERT_NE(fd, rtc::kInvalidPlatformFileValue) |
| 66 | << "Failed to open file descriptor for file: " << filename; |
| 67 | file = rtc::FdopenPlatformFile(fd, "r"); |
| 68 | ASSERT_TRUE(file != nullptr) << "Failed to open file: " << filename; |
| 69 | |
| 70 | int buf[]{0}; |
| 71 | ASSERT_GT(fread(&buf, 1, 1, file), 0u) |
| 72 | << "Failed to read from file: " << filename; |
| 73 | fclose(file); |
| 74 | webrtc::test::RemoveFile(filename); |
| 75 | } |
| 76 | |
| 77 | } // namespace rtc |