xref: /aosp_15_r20/external/tink/cc/internal/test_file_util.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #include "tink/internal/test_file_util.h"
18 
19 #include <fstream>
20 #include <ios>
21 #include <string>
22 #include <vector>
23 
24 #include "gtest/gtest.h"
25 #include "absl/log/check.h"
26 #include "absl/status/status.h"
27 #include "absl/strings/escaping.h"
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/str_split.h"
30 #include "absl/strings/string_view.h"
31 #include "tink/subtle/random.h"
32 #include "tink/util/status.h"
33 #include "tink/util/test_util.h"
34 
35 namespace crypto {
36 namespace tink {
37 namespace internal {
38 
CreateTestFile(absl::string_view filename,absl::string_view file_content)39 util::Status CreateTestFile(absl::string_view filename,
40                             absl::string_view file_content) {
41   std::string full_filename = absl::StrCat(test::TmpDir(), "/", filename);
42   std::ofstream output_stream(full_filename, std::ios::binary);
43   if (!output_stream) {
44     return util::Status(absl::StatusCode::kInternal, "Cannot open file");
45   }
46   output_stream.write(file_content.data(), file_content.size());
47   return util::OkStatus();
48 }
49 
GetTestFileNamePrefix()50 std::string GetTestFileNamePrefix() {
51   const testing::TestInfo* const test_info =
52       testing::UnitTest::GetInstance()->current_test_info();
53   CHECK(test_info != nullptr);
54   std::string random_string = subtle::Random::GetRandomBytes(/*length=*/16);
55   std::string test_suite_name = test_info->test_suite_name();
56   std::string test_name = test_info->name();
57   // Parametrized tests return test_suite_name of the form <Prefix>/<Test Suite>
58   // and name of the form <Test Name>/<Suffix>.
59   // In this case, get only the prefix and test name. Keeping all of these may
60   // result in a file name that is too long.
61   if (test_info->value_param() != nullptr) {
62     std::vector<std::string> test_suite_parts =
63         absl::StrSplit(test_info->test_suite_name(), '/');
64     CHECK_GE(test_suite_parts.size(), 1);
65     test_suite_name = test_suite_parts[0];
66     std::vector<std::string> test_name_parts =
67         absl::StrSplit(test_info->name(), '/');
68     CHECK_GE(test_name_parts.size(), 1);
69     test_name = test_name_parts[0];
70   }
71   return absl::StrCat(test_suite_name, "_", test_name, "_",
72                       absl::BytesToHexString(random_string));
73 }
74 
75 }  // namespace internal
76 }  // namespace tink
77 }  // namespace crypto
78