blob: cb8c77c586109a0cdbb869fa8e270a6531134ce8 [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 Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/flags.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 Bonadei2dfa9982018-10-18 11:35:32 +020022WEBRTC_DECLARE_string(test_artifacts_dir);
zijiehe2769ec62016-12-14 15:03:03 -080023
24namespace webrtc {
25namespace test {
26
27TEST(IsolatedOutputTest, ShouldRejectInvalidIsolatedOutDir) {
Edward Lemuraf8659a2017-09-27 14:46:24 +020028 const char* backup = FLAG_test_artifacts_dir;
29 FLAG_test_artifacts_dir = "";
30 ASSERT_FALSE(WriteToTestArtifactsDir("a-file", "some-contents"));
31 FLAG_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)) {
Niels Möller392f8d02018-06-01 10:14:44 +020044 std::string out_file = JoinFilename(FLAG_test_artifacts_dir, filename);
Niels Möllerb7edf692019-02-08 16:40:53 +010045 FileWrapper input = FileWrapper::OpenReadOnly(out_file);
46 EXPECT_TRUE(input.is_open());
47 EXPECT_TRUE(input.Rewind());
zijiehe2769ec62016-12-14 15:03:03 -080048 uint8_t buffer[32];
49 EXPECT_EQ(input.Read(buffer, strlen(content)), strlen(content));
50 buffer[strlen(content)] = 0;
51 EXPECT_EQ(std::string(content),
52 std::string(reinterpret_cast<char*>(buffer)));
53 input.Close();
54
Niels Möllerb7edf692019-02-08 16:40:53 +010055 EXPECT_TRUE(RemoveFile(out_file));
zijiehe2769ec62016-12-14 15:03:03 -080056 }
57}
58
59} // namespace test
60} // namespace webrtc