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> |
| 14 | |
| 15 | #include <string> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/file.h" |
| 18 | #include "rtc_base/flags.h" |
| 19 | #include "rtc_base/pathutils.h" |
| 20 | #include "rtc_base/platform_file.h" |
| 21 | #include "test/gtest.h" |
Niels Möller | 392f8d0 | 2018-06-01 10:14:44 +0200 | [diff] [blame] | 22 | #include "test/testsupport/fileutils.h" |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 23 | |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame^] | 24 | WEBRTC_DECLARE_string(test_artifacts_dir); |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | namespace test { |
| 28 | |
| 29 | TEST(IsolatedOutputTest, ShouldRejectInvalidIsolatedOutDir) { |
Edward Lemur | af8659a | 2017-09-27 14:46:24 +0200 | [diff] [blame] | 30 | const char* backup = FLAG_test_artifacts_dir; |
| 31 | FLAG_test_artifacts_dir = ""; |
| 32 | ASSERT_FALSE(WriteToTestArtifactsDir("a-file", "some-contents")); |
| 33 | FLAG_test_artifacts_dir = backup; |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | TEST(IsolatedOutputTest, ShouldRejectInvalidFileName) { |
Edward Lemur | af8659a | 2017-09-27 14:46:24 +0200 | [diff] [blame] | 37 | ASSERT_FALSE(WriteToTestArtifactsDir(nullptr, "some-contents")); |
| 38 | ASSERT_FALSE(WriteToTestArtifactsDir("", "some-contents")); |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Sets isolated_out_dir=<a-writable-path> to execute this test. |
| 42 | TEST(IsolatedOutputTest, ShouldBeAbleToWriteContent) { |
| 43 | const char* filename = "a-file"; |
| 44 | const char* content = "some-contents"; |
Edward Lemur | af8659a | 2017-09-27 14:46:24 +0200 | [diff] [blame] | 45 | if (WriteToTestArtifactsDir(filename, content)) { |
Niels Möller | 392f8d0 | 2018-06-01 10:14:44 +0200 | [diff] [blame] | 46 | std::string out_file = JoinFilename(FLAG_test_artifacts_dir, filename); |
zijiehe | 2769ec6 | 2016-12-14 15:03:03 -0800 | [diff] [blame] | 47 | rtc::File input = rtc::File::Open(out_file); |
| 48 | EXPECT_TRUE(input.IsOpen()); |
| 49 | EXPECT_TRUE(input.Seek(0)); |
| 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 | |
| 57 | EXPECT_TRUE(rtc::File::Remove(out_file)); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | } // namespace test |
| 62 | } // namespace webrtc |