OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/path_service.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/string_util.h" |
| 10 |
| 11 #include "courgette/courgette.h" |
| 12 #include "courgette/streams.h" |
| 13 |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 class EncodeDecodeTest : public testing::Test { |
| 17 public: |
| 18 void TestExe(const char *) const; |
| 19 |
| 20 private: |
| 21 void SetUp() { |
| 22 PathService::Get(base::DIR_SOURCE_ROOT, &testdata_dir_); |
| 23 file_util::AppendToPath(&testdata_dir_, L"courgette"); |
| 24 file_util::AppendToPath(&testdata_dir_, L"testdata"); |
| 25 } |
| 26 |
| 27 void TearDown() { } |
| 28 |
| 29 // Returns contents of |file_name| as uninterprested bytes stored in a string. |
| 30 std::string FileContents(const char* file_name) const; |
| 31 |
| 32 std::wstring testdata_dir_; // Full path name of testdata directory |
| 33 }; |
| 34 |
| 35 // Reads a test file into a string. |
| 36 std::string EncodeDecodeTest::FileContents(const char* file_name) const { |
| 37 std::wstring file_path = testdata_dir_; |
| 38 file_util::AppendToPath(&file_path, UTF8ToWide(file_name)); |
| 39 std::string file_contents; |
| 40 if (!file_util::ReadFileToString(file_path, &file_contents)) { |
| 41 EXPECT_TRUE(!"Could not read test data"); |
| 42 } |
| 43 return file_contents; |
| 44 } |
| 45 |
| 46 void EncodeDecodeTest::TestExe(const char* file_name) const { |
| 47 // Test top-level Courgette API for converting an a file to a binary |
| 48 // assembly representation and back. |
| 49 std::string file1 = FileContents(file_name); |
| 50 |
| 51 const void* original_buffer = file1.c_str(); |
| 52 size_t original_length = file1.size(); |
| 53 |
| 54 courgette::AssemblyProgram* program = NULL; |
| 55 const courgette::Status parse_status = |
| 56 courgette::ParseWin32X86PE(original_buffer, original_length, &program); |
| 57 EXPECT_EQ(courgette::C_OK, parse_status); |
| 58 |
| 59 courgette::EncodedProgram* encoded = NULL; |
| 60 |
| 61 const courgette::Status encode_status = Encode(program, &encoded); |
| 62 EXPECT_EQ(courgette::C_OK, encode_status); |
| 63 |
| 64 DeleteAssemblyProgram(program); |
| 65 |
| 66 courgette::SinkStreamSet sinks; |
| 67 const courgette::Status write_status = WriteEncodedProgram(encoded, &sinks); |
| 68 EXPECT_EQ(courgette::C_OK, write_status); |
| 69 |
| 70 DeleteEncodedProgram(encoded); |
| 71 |
| 72 courgette::SinkStream sink; |
| 73 bool can_collect = sinks.CopyTo(&sink); |
| 74 EXPECT_TRUE(can_collect); |
| 75 |
| 76 const void* buffer = sink.Buffer(); |
| 77 size_t length = sink.Length(); |
| 78 |
| 79 EXPECT_EQ(971850, length); |
| 80 |
| 81 courgette::SourceStreamSet sources; |
| 82 bool can_get_source_streams = sources.Init(buffer, length); |
| 83 EXPECT_TRUE(can_get_source_streams); |
| 84 |
| 85 courgette::EncodedProgram *encoded2 = NULL; |
| 86 const courgette::Status read_status = ReadEncodedProgram(&sources, &encoded2); |
| 87 EXPECT_EQ(courgette::C_OK, read_status); |
| 88 |
| 89 courgette::SinkStream assembled; |
| 90 const courgette::Status assemble_status = Assemble(encoded2, &assembled); |
| 91 EXPECT_EQ(courgette::C_OK, assemble_status); |
| 92 |
| 93 const void* assembled_buffer = assembled.Buffer(); |
| 94 size_t assembled_length = assembled.Length(); |
| 95 |
| 96 EXPECT_EQ(original_length, assembled_length); |
| 97 EXPECT_EQ(0, memcmp(original_buffer, assembled_buffer, original_length)); |
| 98 |
| 99 DeleteEncodedProgram(encoded2); |
| 100 } |
| 101 |
| 102 |
| 103 TEST_F(EncodeDecodeTest, All) { |
| 104 TestExe("setup1.exe"); |
| 105 } |
OLD | NEW |