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 "absl/flags/flag.h"
16 #include "absl/flags/parse.h"
17 #include "rtc_base/logging.h"
18 #include "rtc_base/system/file_wrapper.h"
19 #include "test/testsupport/file_utils.h"
20 
21 namespace {
DefaultArtifactPath()22 const std::string& DefaultArtifactPath() {
23   static const std::string path = webrtc::test::OutputPath();
24   return path;
25 }
26 }  // namespace
27 
28 ABSL_FLAG(std::string,
29           test_artifacts_dir,
30           DefaultArtifactPath().c_str(),
31           "The output folder where test output should be saved.");
32 
33 namespace webrtc {
34 namespace test {
35 
GetTestArtifactsDir(std::string * out_dir)36 bool GetTestArtifactsDir(std::string* out_dir) {
37   if (absl::GetFlag(FLAGS_test_artifacts_dir).empty()) {
38     RTC_LOG(LS_WARNING) << "No test_out_dir defined.";
39     return false;
40   }
41   *out_dir = absl::GetFlag(FLAGS_test_artifacts_dir);
42   return true;
43 }
44 
WriteToTestArtifactsDir(const char * filename,const uint8_t * buffer,size_t length)45 bool WriteToTestArtifactsDir(const char* filename,
46                              const uint8_t* buffer,
47                              size_t length) {
48   if (absl::GetFlag(FLAGS_test_artifacts_dir).empty()) {
49     RTC_LOG(LS_WARNING) << "No test_out_dir defined.";
50     return false;
51   }
52 
53   if (filename == nullptr || strlen(filename) == 0) {
54     RTC_LOG(LS_WARNING) << "filename must be provided.";
55     return false;
56   }
57 
58   FileWrapper output = FileWrapper::OpenWriteOnly(
59       JoinFilename(absl::GetFlag(FLAGS_test_artifacts_dir), filename));
60 
61   return output.is_open() && output.Write(buffer, length);
62 }
63 
WriteToTestArtifactsDir(const char * filename,const std::string & content)64 bool WriteToTestArtifactsDir(const char* filename, const std::string& content) {
65   return WriteToTestArtifactsDir(
66       filename, reinterpret_cast<const uint8_t*>(content.c_str()),
67       content.length());
68 }
69 
70 }  // namespace test
71 }  // namespace webrtc
72