[M108 Migration][HBBTV] Implement ewk_context_register_jsplugin_mime_types API
[platform/framework/web/chromium-efl.git] / courgette / encode_decode_unittest.cc
1 // Copyright 2011 The Chromium Authors
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 <stddef.h>
6
7 #include <memory>
8
9 #include "courgette/base_test_unittest.h"
10 #include "courgette/courgette.h"
11 #include "courgette/courgette_flow.h"
12 #include "courgette/streams.h"
13
14 namespace courgette {
15
16 class EncodeDecodeTest : public BaseTest {
17  public:
18   void TestAssembleToStreamDisassemble(const std::string& file,
19                                        size_t expected_encoded_length) const;
20 };
21
22 void EncodeDecodeTest::TestAssembleToStreamDisassemble(
23     const std::string& file,
24     size_t expected_encoded_length) const {
25   const uint8_t* original_data = reinterpret_cast<const uint8_t*>(file.data());
26   size_t original_length = file.length();
27   CourgetteFlow flow;
28
29   // Convert executable to encoded assembly.
30   RegionBuffer original_buffer(Region(original_data, original_length));
31   flow.ReadDisassemblerFromBuffer(flow.ONLY, original_buffer);
32   EXPECT_EQ(C_OK, flow.status());
33   EXPECT_TRUE(nullptr != flow.data(flow.ONLY)->disassembler.get());
34
35   flow.CreateAssemblyProgramFromDisassembler(flow.ONLY, false);
36   EXPECT_EQ(C_OK, flow.status());
37   EXPECT_TRUE(nullptr != flow.data(flow.ONLY)->program.get());
38
39   flow.CreateEncodedProgramFromDisassemblerAndAssemblyProgram(flow.ONLY);
40   EXPECT_EQ(C_OK, flow.status());
41   EXPECT_TRUE(nullptr != flow.data(flow.ONLY)->encoded.get());
42
43   flow.DestroyAssemblyProgram(flow.ONLY);
44   EXPECT_EQ(C_OK, flow.status());
45   EXPECT_TRUE(nullptr == flow.data(flow.ONLY)->program.get());
46
47   flow.DestroyDisassembler(flow.ONLY);
48   EXPECT_EQ(C_OK, flow.status());
49   EXPECT_TRUE(nullptr == flow.data(flow.ONLY)->disassembler.get());
50
51   flow.WriteSinkStreamSetFromEncodedProgram(flow.ONLY);
52   EXPECT_EQ(C_OK, flow.status());
53
54   flow.DestroyEncodedProgram(flow.ONLY);
55   EXPECT_EQ(C_OK, flow.status());
56   EXPECT_TRUE(nullptr == flow.data(flow.ONLY)->encoded.get());
57
58   SinkStream sink;
59   flow.WriteSinkStreamFromSinkStreamSet(flow.ONLY, &sink);
60   EXPECT_EQ(C_OK, flow.status());
61
62   const void* encoded_data = sink.Buffer();
63   size_t encoded_length = sink.Length();
64   EXPECT_EQ(expected_encoded_length, encoded_length);
65
66   // Convert encoded assembly back to executable.
67   RegionBuffer encoded_buffer(Region(encoded_data, encoded_length));
68   flow.ReadSourceStreamSetFromBuffer(flow.ONLY, encoded_buffer);
69   EXPECT_EQ(C_OK, flow.status());
70
71   flow.ReadEncodedProgramFromSourceStreamSet(flow.ONLY);
72   EXPECT_EQ(C_OK, flow.status());
73   EXPECT_TRUE(nullptr != flow.data(flow.ONLY)->encoded.get());
74
75   SinkStream executable;
76   flow.WriteExecutableFromEncodedProgram(flow.ONLY, &executable);
77   EXPECT_EQ(C_OK, flow.status());
78
79   flow.DestroyEncodedProgram(flow.ONLY);
80   EXPECT_EQ(C_OK, flow.status());
81   EXPECT_TRUE(nullptr == flow.data(flow.ONLY)->encoded.get());
82   EXPECT_TRUE(flow.ok());
83   EXPECT_FALSE(flow.failed());
84
85   const void* executable_data = executable.Buffer();
86   size_t executable_length = executable.Length();
87   EXPECT_EQ(original_length, executable_length);
88
89   EXPECT_EQ(0, memcmp(original_data, executable_data, original_length));
90 }
91
92 TEST_F(EncodeDecodeTest, PE) {
93   std::string file = FileContents("setup1.exe");
94   TestAssembleToStreamDisassemble(file, 972845);
95 }
96
97 TEST_F(EncodeDecodeTest, PE64) {
98   std::string file = FileContents("chrome64_1.exe");
99   TestAssembleToStreamDisassemble(file, 810090);
100 }
101
102 TEST_F(EncodeDecodeTest, Elf_Small) {
103   std::string file = FileContents("elf-32-1");
104   TestAssembleToStreamDisassemble(file, 136201);
105 }
106
107 TEST_F(EncodeDecodeTest, Elf_HighBSS) {
108   std::string file = FileContents("elf-32-high-bss");
109   TestAssembleToStreamDisassemble(file, 7308);
110 }
111
112 }  // namespace courgette