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 
11 #include "test/testsupport/test_artifacts.h"
12 
13 #include <string.h>
14 
15 #include <string>
16 
17 #include "absl/flags/declare.h"
18 #include "absl/flags/flag.h"
19 #include "rtc_base/system/file_wrapper.h"
20 #include "test/gtest.h"
21 #include "test/testsupport/file_utils.h"
22 
23 ABSL_DECLARE_FLAG(std::string, test_artifacts_dir);
24 
25 namespace webrtc {
26 namespace test {
27 
TEST(IsolatedOutputTest,ShouldRejectInvalidIsolatedOutDir)28 TEST(IsolatedOutputTest, ShouldRejectInvalidIsolatedOutDir) {
29   const std::string backup = absl::GetFlag(FLAGS_test_artifacts_dir);
30   absl::SetFlag(&FLAGS_test_artifacts_dir, "");
31   ASSERT_FALSE(WriteToTestArtifactsDir("a-file", "some-contents"));
32   absl::SetFlag(&FLAGS_test_artifacts_dir, backup);
33 }
34 
TEST(IsolatedOutputTest,ShouldRejectInvalidFileName)35 TEST(IsolatedOutputTest, ShouldRejectInvalidFileName) {
36   ASSERT_FALSE(WriteToTestArtifactsDir(nullptr, "some-contents"));
37   ASSERT_FALSE(WriteToTestArtifactsDir("", "some-contents"));
38 }
39 
40 // Sets isolated_out_dir=<a-writable-path> to execute this test.
TEST(IsolatedOutputTest,ShouldBeAbleToWriteContent)41 TEST(IsolatedOutputTest, ShouldBeAbleToWriteContent) {
42   const char* filename = "a-file";
43   const char* content = "some-contents";
44   if (WriteToTestArtifactsDir(filename, content)) {
45     std::string out_file =
46         JoinFilename(absl::GetFlag(FLAGS_test_artifacts_dir), filename);
47     FileWrapper input = FileWrapper::OpenReadOnly(out_file);
48     EXPECT_TRUE(input.is_open());
49     EXPECT_TRUE(input.Rewind());
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(RemoveFile(out_file));
58   }
59 }
60 
61 }  // namespace test
62 }  // namespace webrtc
63