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