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 "third_party/courgette/courgette.h" | |
12 #include "third_party/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"third_party"); | |
24 file_util::AppendToPath(&testdata_dir_, L"courgette"); | |
25 file_util::AppendToPath(&testdata_dir_, L"testdata"); | |
26 } | |
27 | |
28 void TearDown() { } | |
29 | |
30 // Returns contents of |file_name| as uninterprested bytes stored in a string. | |
31 std::string FileContents(const char* file_name) const; | |
32 | |
33 std::wstring testdata_dir_; // Full path name of testdata directory | |
34 }; | |
35 | |
36 // Reads a test file into a string. | |
37 std::string EncodeDecodeTest::FileContents(const char* file_name) const { | |
38 std::wstring file_path = testdata_dir_; | |
39 file_util::AppendToPath(&file_path, UTF8ToWide(file_name)); | |
40 std::string file_contents; | |
41 if (!file_util::ReadFileToString(file_path, &file_contents)) { | |
42 EXPECT_TRUE(!"Could not read test data"); | |
43 } | |
44 return file_contents; | |
45 } | |
46 | |
47 void EncodeDecodeTest::TestExe(const char* file_name) const { | |
48 // Test top-level Courgette API for converting an a file to a binary | |
49 // assembly representation and back. | |
50 std::string file1 = FileContents(file_name); | |
51 | |
52 const void* original_buffer = file1.c_str(); | |
53 size_t original_length = file1.size(); | |
54 | |
55 courgette::AssemblyProgram* program = NULL; | |
56 const courgette::Status parse_status = | |
57 courgette::ParseWin32X86PE(original_buffer, original_length, &program); | |
58 EXPECT_EQ(courgette::C_OK, parse_status); | |
59 | |
60 courgette::EncodedProgram* encoded = NULL; | |
61 | |
62 const courgette::Status encode_status = Encode(program, &encoded); | |
63 EXPECT_EQ(courgette::C_OK, encode_status); | |
64 | |
65 DeleteAssemblyProgram(program); | |
66 | |
67 courgette::SinkStreamSet sinks; | |
68 const courgette::Status write_status = WriteEncodedProgram(encoded, &sinks); | |
69 EXPECT_EQ(courgette::C_OK, write_status); | |
70 | |
71 DeleteEncodedProgram(encoded); | |
72 | |
73 courgette::SinkStream sink; | |
74 bool can_collect = sinks.CopyTo(&sink); | |
75 EXPECT_TRUE(can_collect); | |
76 | |
77 const void* buffer = sink.Buffer(); | |
78 size_t length = sink.Length(); | |
79 | |
80 EXPECT_EQ(971850, length); | |
81 | |
82 courgette::SourceStreamSet sources; | |
83 bool can_get_source_streams = sources.Init(buffer, length); | |
84 EXPECT_TRUE(can_get_source_streams); | |
85 | |
86 courgette::EncodedProgram *encoded2 = NULL; | |
87 const courgette::Status read_status = ReadEncodedProgram(&sources, &encoded2); | |
88 EXPECT_EQ(courgette::C_OK, read_status); | |
89 | |
90 courgette::SinkStream assembled; | |
91 const courgette::Status assemble_status = Assemble(encoded2, &assembled); | |
92 EXPECT_EQ(courgette::C_OK, assemble_status); | |
93 | |
94 const void* assembled_buffer = assembled.Buffer(); | |
95 size_t assembled_length = assembled.Length(); | |
96 | |
97 EXPECT_EQ(original_length, assembled_length); | |
98 EXPECT_EQ(0, memcmp(original_buffer, assembled_buffer, original_length)); | |
99 | |
100 DeleteEncodedProgram(encoded2); | |
101 } | |
102 | |
103 | |
104 TEST_F(EncodeDecodeTest, All) { | |
105 TestExe("setup1.exe"); | |
106 } | |
OLD | NEW |