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