blob: 98de9e4bb832e95f06d96bf748727595d9531748 [file] [log] [blame]
zijiehe2769ec62016-12-14 15:03:03 -08001/*
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 Lemuraf8659a2017-09-27 14:46:24 +020011#include "test/testsupport/test_artifacts.h"
zijiehe2769ec62016-12-14 15:03:03 -080012
13#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
zijiehe2769ec62016-12-14 15:03:03 -080015#include <string>
16
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020017#include "absl/flags/flag.h"
Niels Möllerb7edf692019-02-08 16:40:53 +010018#include "rtc_base/system/file_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "test/testsupport/file_utils.h"
zijiehe2769ec62016-12-14 15:03:03 -080021
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020022ABSL_DECLARE_FLAG(std::string, test_artifacts_dir);
zijiehe2769ec62016-12-14 15:03:03 -080023
24namespace webrtc {
25namespace test {
26
27TEST(IsolatedOutputTest, ShouldRejectInvalidIsolatedOutDir) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020028 const std::string backup = absl::GetFlag(FLAGS_test_artifacts_dir);
29 absl::SetFlag(&FLAGS_test_artifacts_dir, "");
Edward Lemuraf8659a2017-09-27 14:46:24 +020030 ASSERT_FALSE(WriteToTestArtifactsDir("a-file", "some-contents"));
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020031 absl::SetFlag(&FLAGS_test_artifacts_dir, backup);
zijiehe2769ec62016-12-14 15:03:03 -080032}
33
34TEST(IsolatedOutputTest, ShouldRejectInvalidFileName) {
Edward Lemuraf8659a2017-09-27 14:46:24 +020035 ASSERT_FALSE(WriteToTestArtifactsDir(nullptr, "some-contents"));
36 ASSERT_FALSE(WriteToTestArtifactsDir("", "some-contents"));
zijiehe2769ec62016-12-14 15:03:03 -080037}
38
39// Sets isolated_out_dir=<a-writable-path> to execute this test.
40TEST(IsolatedOutputTest, ShouldBeAbleToWriteContent) {
41 const char* filename = "a-file";
42 const char* content = "some-contents";
Edward Lemuraf8659a2017-09-27 14:46:24 +020043 if (WriteToTestArtifactsDir(filename, content)) {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020044 std::string out_file =
45 JoinFilename(absl::GetFlag(FLAGS_test_artifacts_dir), filename);
Niels Möllerb7edf692019-02-08 16:40:53 +010046 FileWrapper input = FileWrapper::OpenReadOnly(out_file);
47 EXPECT_TRUE(input.is_open());
48 EXPECT_TRUE(input.Rewind());
zijiehe2769ec62016-12-14 15:03:03 -080049 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öllerb7edf692019-02-08 16:40:53 +010056 EXPECT_TRUE(RemoveFile(out_file));
zijiehe2769ec62016-12-14 15:03:03 -080057 }
58}
59
60} // namespace test
61} // namespace webrtc