From 2769ec62c089218394c1fe3a5b41b9331940b68c Mon Sep 17 00:00:00 2001 From: zijiehe Date: Wed, 14 Dec 2016 15:03:03 -0800 Subject: [PATCH] Add WriteIsolatedOutput() functions WriteIsolatedOutput() functions write large content into swarming isolated output folder, which are useful to log large test data for debugging purpose. BUG=webrtc:6732 TBR=holmer@webrtc.org Review-Url: https://codereview.webrtc.org/2558693002 Cr-Commit-Position: refs/heads/master@{#15616} --- webrtc/base/file.cc | 15 +++++ webrtc/base/file.h | 5 ++ webrtc/base/file_unittest.cc | 9 +++ webrtc/test/BUILD.gn | 5 ++ webrtc/test/testsupport/isolated_output.cc | 53 ++++++++++++++++ webrtc/test/testsupport/isolated_output.h | 35 +++++++++++ .../testsupport/isolated_output_unittest.cc | 61 +++++++++++++++++++ 7 files changed, 183 insertions(+) create mode 100644 webrtc/test/testsupport/isolated_output.cc create mode 100644 webrtc/test/testsupport/isolated_output.h create mode 100644 webrtc/test/testsupport/isolated_output_unittest.cc diff --git a/webrtc/base/file.cc b/webrtc/base/file.cc index 8b5df9e9be..983eb8b8da 100644 --- a/webrtc/base/file.cc +++ b/webrtc/base/file.cc @@ -61,6 +61,21 @@ File File::Create(const Pathname& path) { return Create(Pathname(path)); } +// static +bool File::Remove(const std::string& path) { + return RemoveFile(path); +} + +// static +bool File::Remove(Pathname&& path) { + return Remove(NormalizePathname(std::move(path))); +} + +// static +bool File::Remove(const Pathname& path) { + return Remove(Pathname(path)); +} + File::File(File&& other) : file_(other.file_) { other.file_ = kInvalidPlatformFileValue; } diff --git a/webrtc/base/file.h b/webrtc/base/file.h index 5e8449ccdf..f4806d1a02 100644 --- a/webrtc/base/file.h +++ b/webrtc/base/file.h @@ -47,6 +47,11 @@ class File { static File Create(Pathname&& path); static File Create(const Pathname& path); + // Remove a file in the file system. + static bool Remove(const std::string& path); + static bool Remove(Pathname&& path); + static bool Remove(const Pathname& path); + size_t Write(const uint8_t* data, size_t length); size_t Read(uint8_t* buffer, size_t length); diff --git a/webrtc/base/file_unittest.cc b/webrtc/base/file_unittest.cc index d8800a55f4..d7cefc8748 100644 --- a/webrtc/base/file_unittest.cc +++ b/webrtc/base/file_unittest.cc @@ -189,4 +189,13 @@ TEST_F(FileTest, CreateFromPathname) { } } +TEST_F(FileTest, ShouldBeAbleToRemoveFile) { + { + File file = File::Open(Pathname(path_)); + ASSERT_TRUE(file.IsOpen()) << "Error: " << LastError(); + } + + ASSERT_TRUE(File::Remove(Pathname(path_))) << "Error: " << LastError(); +} + } // namespace rtc diff --git a/webrtc/test/BUILD.gn b/webrtc/test/BUILD.gn index c476a12e8a..66220eb72b 100644 --- a/webrtc/test/BUILD.gn +++ b/webrtc/test/BUILD.gn @@ -128,6 +128,8 @@ rtc_source_set("test_support") { "testsupport/frame_reader.h", "testsupport/frame_writer.cc", "testsupport/frame_writer.h", + "testsupport/isolated_output.cc", + "testsupport/isolated_output.h", "testsupport/metrics/video_metrics.cc", "testsupport/metrics/video_metrics.h", "testsupport/mock/mock_frame_reader.h", @@ -148,6 +150,7 @@ rtc_source_set("test_support") { "../system_wrappers", "//testing/gmock", "//testing/gtest", + "//third_party/gflags", ] public_deps = [ @@ -213,6 +216,7 @@ rtc_test("test_support_unittests") { "testsupport/always_passing_unittest.cc", "testsupport/frame_reader_unittest.cc", "testsupport/frame_writer_unittest.cc", + "testsupport/isolated_output_unittest.cc", "testsupport/metrics/video_metrics_unittest.cc", "testsupport/packet_reader_unittest.cc", "testsupport/perf_test_unittest.cc", @@ -248,6 +252,7 @@ rtc_test("test_support_unittests") { "../modules/video_capture", "//testing/gmock", "//testing/gtest", + "//third_party/gflags", ] } rtc_source_set("fileutils_unittests") { diff --git a/webrtc/test/testsupport/isolated_output.cc b/webrtc/test/testsupport/isolated_output.cc new file mode 100644 index 0000000000..26e9f5577d --- /dev/null +++ b/webrtc/test/testsupport/isolated_output.cc @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/test/testsupport/isolated_output.h" + +#include + +#include "gflags/gflags.h" +#include "webrtc/base/file.h" +#include "webrtc/base/logging.h" +#include "webrtc/base/pathutils.h" +#include "webrtc/test/testsupport/fileutils.h" + +DEFINE_string(isolated_out_dir, webrtc::test::OutputPath(), + "The isolated output folder provided by swarming test framework."); + +namespace webrtc { +namespace test { + +bool WriteIsolatedOutput(const char* filename, + const uint8_t* buffer, + size_t length) { + if (FLAGS_isolated_out_dir.empty()) { + LOG(LS_WARNING) << "No isolated_out_dir defined."; + return false; + } + + if (filename == nullptr || strlen(filename) == 0) { + LOG(LS_WARNING) << "filename must be provided."; + return false; + } + + rtc::File output = + rtc::File::Create(rtc::Pathname(FLAGS_isolated_out_dir, filename)); + + return output.IsOpen() && output.Write(buffer, length) == length; +} + +bool WriteIsolatedOutput(const char* filename, const std::string& content) { + return WriteIsolatedOutput(filename, + reinterpret_cast(content.c_str()), + content.length()); +} + +} // namespace test +} // namespace webrtc diff --git a/webrtc/test/testsupport/isolated_output.h b/webrtc/test/testsupport/isolated_output.h new file mode 100644 index 0000000000..f4e9695157 --- /dev/null +++ b/webrtc/test/testsupport/isolated_output.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_TEST_TESTSUPPORT_ISOLATED_OUTPUT_H_ +#define WEBRTC_TEST_TESTSUPPORT_ISOLATED_OUTPUT_H_ + +#include + +#include + +namespace webrtc { +namespace test { + +// Writes a |length| bytes array |buffer| to |filename| in isolated output +// directory defined by swarming. If the file is existing, content will be +// appended. Otherwise a new file will be created. This function returns false +// if isolated output directory has not been defined, or |filename| indicates an +// invalid or non-writable file, or underlying file system errors. +bool WriteIsolatedOutput(const char* filename, + const uint8_t* buffer, + size_t length); + +bool WriteIsolatedOutput(const char* filename, const std::string& content); + +} // namespace test +} // namespace webrtc + +#endif // WEBRTC_TEST_TESTSUPPORT_ISOLATED_OUTPUT_H_ diff --git a/webrtc/test/testsupport/isolated_output_unittest.cc b/webrtc/test/testsupport/isolated_output_unittest.cc new file mode 100644 index 0000000000..0918554b8b --- /dev/null +++ b/webrtc/test/testsupport/isolated_output_unittest.cc @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/test/testsupport/isolated_output.h" + +#include + +#include + +#include "gflags/gflags.h" +#include "webrtc/base/file.h" +#include "webrtc/base/pathutils.h" +#include "webrtc/base/platform_file.h" +#include "webrtc/test/gtest.h" + +DECLARE_string(isolated_out_dir); + +namespace webrtc { +namespace test { + +TEST(IsolatedOutputTest, ShouldRejectInvalidIsolatedOutDir) { + std::string backup = FLAGS_isolated_out_dir; + FLAGS_isolated_out_dir = ""; + ASSERT_FALSE(WriteIsolatedOutput("a-file", "some-contents")); + FLAGS_isolated_out_dir = backup; +} + +TEST(IsolatedOutputTest, ShouldRejectInvalidFileName) { + ASSERT_FALSE(WriteIsolatedOutput(nullptr, "some-contents")); + ASSERT_FALSE(WriteIsolatedOutput("", "some-contents")); +} + +// Sets isolated_out_dir= to execute this test. +TEST(IsolatedOutputTest, ShouldBeAbleToWriteContent) { + const char* filename = "a-file"; + const char* content = "some-contents"; + if (WriteIsolatedOutput(filename, content)) { + rtc::Pathname out_file(FLAGS_isolated_out_dir, filename); + rtc::File input = rtc::File::Open(out_file); + EXPECT_TRUE(input.IsOpen()); + EXPECT_TRUE(input.Seek(0)); + uint8_t buffer[32]; + EXPECT_EQ(input.Read(buffer, strlen(content)), strlen(content)); + buffer[strlen(content)] = 0; + EXPECT_EQ(std::string(content), + std::string(reinterpret_cast(buffer))); + input.Close(); + + EXPECT_TRUE(rtc::File::Remove(out_file)); + } +} + +} // namespace test +} // namespace webrtc