bloaty_test_pe.cc
Go to the documentation of this file.
1 // Copyright 2021 Google Inc. All Rights Reserved.
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 #include "test.h"
16 
18 {
20  std::vector<std::string> commandline;
23 };
24 
26  return entry.param.name;
27 }
28 
29 std::ostream& operator<<(std::ostream& os, const BloatyTestEntry& entry) {
30  os << "{ ";
31  for (const auto& str: entry.commandline) {
32  os << str << ", ";
33  }
34  os << entry.input_file << ", " << entry.result_file << " }";
35  return os;
36 }
37 
38 // Strip all trailing whitespace (including \r)
40  std::stringstream buffer(contents);
41  contents.clear();
43  while (std::getline(buffer, tmp)) {
44  auto end = tmp.find_last_not_of("\t \r");
45  if (end != std::string::npos) {
46  tmp = tmp.substr(0, end + 1);
47  }
48  else {
49  tmp.clear();
50  }
51  if (!contents.empty()) {
52  contents += "\n";
53  }
54  contents += tmp;
55  }
56 }
57 
59  FILE* file = fopen(filename.c_str(), "rb");
60  if (!file) {
61  std::cerr << "Couldn't get file size for: " << filename << "\n";
62  return false;
63  }
64  fseek(file, 0L, SEEK_END);
65  size_t size = ftell(file);
66  fseek(file, 0L, SEEK_SET);
67  contents.resize(size);
68  size_t result = fread(&contents[0], 1, size, file);
69  fclose(file);
70  contents.resize(result);
72  return result == size;
73 }
74 
76  public testing::WithParamInterface<BloatyTestEntry>
77 {
78 public:
83  {
84  }
85 
86  const std::vector<std::string>& commandline;
89 };
90 
91 
92 TEST_P(BloatyOutputTest, CheckOutput) {
93  uint64_t size;
94  ASSERT_TRUE(GetFileSize(input_file, &size));
95  std::string expect_result;
96  ASSERT_TRUE(GetFileContents(result_file, expect_result));
97 
98  std::vector<std::string> cmdline = { "bloaty" };
99  cmdline.insert(cmdline.end(), commandline.begin(), commandline.end());
100  cmdline.push_back(input_file);
102 
103  bloaty::OutputOptions output_options;
104  std::stringstream output_stream;
105  output_options.output_format = bloaty::OutputFormat::kTSV;
106  output_->Print(output_options, &output_stream);
107  std::string tmp = output_stream.str();
108  Normalize(tmp);
109  EXPECT_EQ(tmp, expect_result);
110 }
111 
112 static BloatyTestEntry tests[] = {
113  { "MSVCR15DLL", {}, "msvc-15.0-foo-bar.dll", "msvc-15.0-foo-bar.dll.txt" },
114  { "MSVCR15DLLSEG", {"-d", "segments"}, "msvc-15.0-foo-bar.dll", "msvc-15.0-foo-bar.dll.seg.txt" },
115  { "MSVC15EXE", {}, "msvc-15.0-foo-bar-main-cv.bin", "msvc-15.0-foo-bar-main-cv.bin.txt" },
116  { "MSVC15EXESEG", {"-d", "segments"}, "msvc-15.0-foo-bar-main-cv.bin", "msvc-15.0-foo-bar-main-cv.bin.seg.txt" },
117 
118  { "MSVCR16DLL", {}, "msvc-16.0-foo-bar.dll", "msvc-16.0-foo-bar.dll.txt" },
119  { "MSVCR16DLLSEG", {"-d", "segments"}, "msvc-16.0-foo-bar.dll", "msvc-16.0-foo-bar.dll.seg.txt" },
120  { "MSVC16EXE", {}, "msvc-16.0-foo-bar-main-cv.bin", "msvc-16.0-foo-bar-main-cv.bin.txt" },
121  { "MSVC16EXESEG", {"-d", "segments"}, "msvc-16.0-foo-bar-main-cv.bin", "msvc-16.0-foo-bar-main-cv.bin.seg.txt" },
122 };
123 
127  TestEntryName);
testing::TestParamInfo::param
ParamType param
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-param-util.h:60
xds_interop_client.str
str
Definition: xds_interop_client.py:487
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
SEEK_END
#define SEEK_END
Definition: bloaty/third_party/zlib/contrib/minizip/zip.c:84
BloatyOutputTest::input_file
const std::string & input_file
Definition: bloaty_test_pe.cc:87
testing::WithParamInterface
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1848
bloaty::OutputFormat::kTSV
@ kTSV
bloaty::RunBloaty
void RunBloaty(const InputFileFactory &factory, const std::string &data_source)
Definition: fuzz_target.cc:44
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
testing::TestParamInfo
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-param-util.h:56
BloatyTestEntry::input_file
std::string input_file
Definition: bloaty_test_pe.cc:21
BloatyTestEntry::commandline
std::vector< std::string > commandline
Definition: bloaty_test_pe.cc:20
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
Normalize
void Normalize(std::string &contents)
Definition: bloaty_test_pe.cc:39
BloatyOutputTest::commandline
const std::vector< std::string > & commandline
Definition: bloaty_test_pe.cc:86
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
bloaty::OutputOptions
Definition: bloaty.h:370
BloatyTest
Definition: bloaty/tests/test.h:72
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
bloaty::OutputOptions::output_format
OutputFormat output_format
Definition: bloaty.h:371
BloatyOutputTest::BloatyOutputTest
BloatyOutputTest()
Definition: bloaty_test_pe.cc:79
BloatyTestEntry::result_file
std::string result_file
Definition: bloaty_test_pe.cc:22
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
GetFileContents
bool GetFileContents(const std::string &filename, std::string &contents)
Definition: bloaty_test_pe.cc:58
output_
std::string output_
Definition: json_writer.cc:76
contents
string_view contents
Definition: elf.cc:597
TEST_P
TEST_P(BloatyOutputTest, CheckOutput)
Definition: bloaty_test_pe.cc:92
benchmark.FILE
FILE
Definition: benchmark.py:21
GetFileSize
bool GetFileSize(const std::string &filename, uint64_t *size)
Definition: bloaty/tests/test.h:38
tests
Definition: src/python/grpcio_tests/tests/__init__.py:1
grpc::fclose
fclose(creds_file)
BloatyOutputTest
Definition: bloaty_test_pe.cc:75
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
BloatyTestEntry::name
std::string name
Definition: bloaty_test_pe.cc:19
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
testing::WithParamInterface< BloatyTestEntry >::GetParam
static const ParamType & GetParam()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1855
BloatyTestEntry
Definition: bloaty_test_pe.cc:17
absl::string_view::empty
constexpr bool empty() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:292
INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(BloatyTest, BloatyOutputTest, testing::ValuesIn(tests), TestEntryName)
operator<<
std::ostream & operator<<(std::ostream &os, const BloatyTestEntry &entry)
Definition: bloaty_test_pe.cc:29
testing::ValuesIn
internal::ParamGenerator< typename std::iterator_traits< ForwardIterator >::value_type > ValuesIn(ForwardIterator begin, ForwardIterator end)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:297
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
BloatyOutputTest::result_file
const std::string & result_file
Definition: bloaty_test_pe.cc:88
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
TestEntryName
std::string TestEntryName(const testing::TestParamInfo< struct BloatyTestEntry > &entry)
Definition: bloaty_test_pe.cc:25
run_clang_tidy.cmdline
list cmdline
Definition: run_clang_tidy.py:47
SEEK_SET
#define SEEK_SET
Definition: bloaty/third_party/zlib/contrib/minizip/zip.c:88
test.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48