zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | |
Edward Lemur | af8659a | 2017-09-27 12:46:24 | [diff] [blame] | 11 | #include "test/testsupport/test_artifacts.h" |
zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 14 | |
zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Artem Titov | 506d4eb | 2020-06-05 15:04:26 | [diff] [blame] | 17 | #include "absl/flags/declare.h" |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 18 | #include "absl/flags/flag.h" |
Niels Möller | b7edf69 | 2019-02-08 15:40:53 | [diff] [blame] | 19 | #include "rtc_base/system/file_wrapper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "test/testsupport/file_utils.h" |
zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 22 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 23 | ABSL_DECLARE_FLAG(std::string, test_artifacts_dir); |
zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | |
| 28 | TEST(IsolatedOutputTest, ShouldRejectInvalidIsolatedOutDir) { |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 29 | const std::string backup = absl::GetFlag(FLAGS_test_artifacts_dir); |
| 30 | absl::SetFlag(&FLAGS_test_artifacts_dir, ""); |
Edward Lemur | af8659a | 2017-09-27 12:46:24 | [diff] [blame] | 31 | ASSERT_FALSE(WriteToTestArtifactsDir("a-file", "some-contents")); |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 32 | absl::SetFlag(&FLAGS_test_artifacts_dir, backup); |
zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST(IsolatedOutputTest, ShouldRejectInvalidFileName) { |
Edward Lemur | af8659a | 2017-09-27 12:46:24 | [diff] [blame] | 36 | ASSERT_FALSE(WriteToTestArtifactsDir(nullptr, "some-contents")); |
| 37 | ASSERT_FALSE(WriteToTestArtifactsDir("", "some-contents")); |
zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Sets isolated_out_dir=<a-writable-path> to execute this test. |
| 41 | TEST(IsolatedOutputTest, ShouldBeAbleToWriteContent) { |
| 42 | const char* filename = "a-file"; |
| 43 | const char* content = "some-contents"; |
Edward Lemur | af8659a | 2017-09-27 12:46:24 | [diff] [blame] | 44 | if (WriteToTestArtifactsDir(filename, content)) { |
Mirko Bonadei | 2ab97f6 | 2019-07-18 11:44:12 | [diff] [blame] | 45 | std::string out_file = |
| 46 | JoinFilename(absl::GetFlag(FLAGS_test_artifacts_dir), filename); |
Niels Möller | b7edf69 | 2019-02-08 15:40:53 | [diff] [blame] | 47 | FileWrapper input = FileWrapper::OpenReadOnly(out_file); |
| 48 | EXPECT_TRUE(input.is_open()); |
| 49 | EXPECT_TRUE(input.Rewind()); |
zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 50 | uint8_t buffer[32]; |
| 51 | EXPECT_EQ(input.Read(buffer, strlen(content)), strlen(content)); |
| 52 | buffer[strlen(content)] = 0; |
| 53 | EXPECT_EQ(std::string(content), |
| 54 | std::string(reinterpret_cast<char*>(buffer))); |
| 55 | input.Close(); |
| 56 | |
Niels Möller | b7edf69 | 2019-02-08 15:40:53 | [diff] [blame] | 57 | EXPECT_TRUE(RemoveFile(out_file)); |
zijiehe | 2769ec6 | 2016-12-14 23:03:03 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | } // namespace test |
| 62 | } // namespace webrtc |