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.
7 #include "base/path_service.h"
8 #include "base/file_util.h"
9 #include "base/string_util.h"
11 #include "courgette/courgette.h"
12 #include "courgette/streams.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 class EncodeDecodeTest
: public testing::Test
{
18 void TestExe(const char *) const;
22 PathService::Get(base::DIR_SOURCE_ROOT
, &testdata_dir_
);
23 testdata_dir_
= testdata_dir_
.AppendASCII("courgette");
24 testdata_dir_
= testdata_dir_
.AppendASCII("testdata");
29 // Returns contents of |file_name| as uninterprested bytes stored in a string.
30 std::string
FileContents(const char* file_name
) const;
32 FilePath testdata_dir_
; // Full path name of testdata directory
35 // Reads a test file into a string.
36 std::string
EncodeDecodeTest::FileContents(const char* file_name
) const {
37 FilePath file_path
= testdata_dir_
;
38 file_path
= file_path
.AppendASCII(file_name
);
39 std::string file_contents
;
40 if (!file_util::ReadFileToString(file_path
, &file_contents
)) {
41 EXPECT_TRUE(!"Could not read test data");
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
);
51 const void* original_buffer
= file1
.c_str();
52 size_t original_length
= file1
.size();
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
);
59 courgette::EncodedProgram
* encoded
= NULL
;
61 const courgette::Status encode_status
= Encode(program
, &encoded
);
62 EXPECT_EQ(courgette::C_OK
, encode_status
);
64 DeleteAssemblyProgram(program
);
66 courgette::SinkStreamSet sinks
;
67 const courgette::Status write_status
= WriteEncodedProgram(encoded
, &sinks
);
68 EXPECT_EQ(courgette::C_OK
, write_status
);
70 DeleteEncodedProgram(encoded
);
72 courgette::SinkStream sink
;
73 bool can_collect
= sinks
.CopyTo(&sink
);
74 EXPECT_TRUE(can_collect
);
76 const void* buffer
= sink
.Buffer();
77 size_t length
= sink
.Length();
79 EXPECT_EQ(971850U, length
);
81 courgette::SourceStreamSet sources
;
82 bool can_get_source_streams
= sources
.Init(buffer
, length
);
83 EXPECT_TRUE(can_get_source_streams
);
85 courgette::EncodedProgram
*encoded2
= NULL
;
86 const courgette::Status read_status
= ReadEncodedProgram(&sources
, &encoded2
);
87 EXPECT_EQ(courgette::C_OK
, read_status
);
89 courgette::SinkStream assembled
;
90 const courgette::Status assemble_status
= Assemble(encoded2
, &assembled
);
91 EXPECT_EQ(courgette::C_OK
, assemble_status
);
93 const void* assembled_buffer
= assembled
.Buffer();
94 size_t assembled_length
= assembled
.Length();
96 EXPECT_EQ(original_length
, assembled_length
);
97 EXPECT_EQ(0, memcmp(original_buffer
, assembled_buffer
, original_length
));
99 DeleteEncodedProgram(encoded2
);
103 TEST_F(EncodeDecodeTest
, All
) {
104 TestExe("setup1.exe");