bloaty_test_pe.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "test.h"
  15. struct BloatyTestEntry
  16. {
  17. std::string name;
  18. std::vector<std::string> commandline;
  19. std::string input_file;
  20. std::string result_file;
  21. };
  22. std::string TestEntryName(const testing::TestParamInfo<struct BloatyTestEntry>& entry) {
  23. return entry.param.name;
  24. }
  25. std::ostream& operator<<(std::ostream& os, const BloatyTestEntry& entry) {
  26. os << "{ ";
  27. for (const auto& str: entry.commandline) {
  28. os << str << ", ";
  29. }
  30. os << entry.input_file << ", " << entry.result_file << " }";
  31. return os;
  32. }
  33. // Strip all trailing whitespace (including \r)
  34. void Normalize(std::string& contents) {
  35. std::stringstream buffer(contents);
  36. contents.clear();
  37. std::string tmp;
  38. while (std::getline(buffer, tmp)) {
  39. auto end = tmp.find_last_not_of("\t \r");
  40. if (end != std::string::npos) {
  41. tmp = tmp.substr(0, end + 1);
  42. }
  43. else {
  44. tmp.clear();
  45. }
  46. if (!contents.empty()) {
  47. contents += "\n";
  48. }
  49. contents += tmp;
  50. }
  51. }
  52. inline bool GetFileContents(const std::string& filename, std::string& contents) {
  53. FILE* file = fopen(filename.c_str(), "rb");
  54. if (!file) {
  55. std::cerr << "Couldn't get file size for: " << filename << "\n";
  56. return false;
  57. }
  58. fseek(file, 0L, SEEK_END);
  59. size_t size = ftell(file);
  60. fseek(file, 0L, SEEK_SET);
  61. contents.resize(size);
  62. size_t result = fread(&contents[0], 1, size, file);
  63. fclose(file);
  64. contents.resize(result);
  65. Normalize(contents);
  66. return result == size;
  67. }
  68. class BloatyOutputTest: public BloatyTest,
  69. public testing::WithParamInterface<BloatyTestEntry>
  70. {
  71. public:
  72. BloatyOutputTest()
  73. : commandline(GetParam().commandline)
  74. , input_file(GetParam().input_file)
  75. , result_file(GetParam().result_file)
  76. {
  77. }
  78. const std::vector<std::string>& commandline;
  79. const std::string& input_file;
  80. const std::string& result_file;
  81. };
  82. TEST_P(BloatyOutputTest, CheckOutput) {
  83. uint64_t size;
  84. ASSERT_TRUE(GetFileSize(input_file, &size));
  85. std::string expect_result;
  86. ASSERT_TRUE(GetFileContents(result_file, expect_result));
  87. std::vector<std::string> cmdline = { "bloaty" };
  88. cmdline.insert(cmdline.end(), commandline.begin(), commandline.end());
  89. cmdline.push_back(input_file);
  90. RunBloaty(cmdline);
  91. bloaty::OutputOptions output_options;
  92. std::stringstream output_stream;
  93. output_options.output_format = bloaty::OutputFormat::kTSV;
  94. output_->Print(output_options, &output_stream);
  95. std::string tmp = output_stream.str();
  96. Normalize(tmp);
  97. EXPECT_EQ(tmp, expect_result);
  98. }
  99. static BloatyTestEntry tests[] = {
  100. { "MSVCR15DLL", {}, "msvc-15.0-foo-bar.dll", "msvc-15.0-foo-bar.dll.txt" },
  101. { "MSVCR15DLLSEG", {"-d", "segments"}, "msvc-15.0-foo-bar.dll", "msvc-15.0-foo-bar.dll.seg.txt" },
  102. { "MSVC15EXE", {}, "msvc-15.0-foo-bar-main-cv.bin", "msvc-15.0-foo-bar-main-cv.bin.txt" },
  103. { "MSVC15EXESEG", {"-d", "segments"}, "msvc-15.0-foo-bar-main-cv.bin", "msvc-15.0-foo-bar-main-cv.bin.seg.txt" },
  104. { "MSVCR16DLL", {}, "msvc-16.0-foo-bar.dll", "msvc-16.0-foo-bar.dll.txt" },
  105. { "MSVCR16DLLSEG", {"-d", "segments"}, "msvc-16.0-foo-bar.dll", "msvc-16.0-foo-bar.dll.seg.txt" },
  106. { "MSVC16EXE", {}, "msvc-16.0-foo-bar-main-cv.bin", "msvc-16.0-foo-bar-main-cv.bin.txt" },
  107. { "MSVC16EXESEG", {"-d", "segments"}, "msvc-16.0-foo-bar-main-cv.bin", "msvc-16.0-foo-bar-main-cv.bin.seg.txt" },
  108. };
  109. INSTANTIATE_TEST_SUITE_P(BloatyTest,
  110. BloatyOutputTest,
  111. testing::ValuesIn(tests),
  112. TestEntryName);