1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdint.h>
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include <gtest/gtest.h>
24 
25 #include <unwindstack/DwarfError.h>
26 #include <unwindstack/DwarfMemory.h>
27 #include <unwindstack/Log.h>
28 #include <unwindstack/Regs.h>
29 
30 #include "DwarfOp.h"
31 
32 #include "utils/MemoryFake.h"
33 
34 namespace unwindstack {
35 
36 template <typename TypeParam>
37 class DwarfOpLogTest : public ::testing::Test {
38  protected:
SetUp()39   void SetUp() override {
40     op_memory_ = new MemoryFake;
41     std::shared_ptr<Memory> memory(op_memory_);
42     mem_.reset(new DwarfMemory(memory));
43     regular_memory_.Clear();
44     op_.reset(new DwarfOp<TypeParam>(mem_.get(), &regular_memory_));
45   }
46 
47   MemoryFake* op_memory_;
48   MemoryFake regular_memory_;
49 
50   std::unique_ptr<DwarfMemory> mem_;
51   std::unique_ptr<DwarfOp<TypeParam>> op_;
52 };
53 TYPED_TEST_SUITE_P(DwarfOpLogTest);
54 
TYPED_TEST_P(DwarfOpLogTest,multiple_ops)55 TYPED_TEST_P(DwarfOpLogTest, multiple_ops) {
56   // Multi operation opcodes.
57   std::vector<uint8_t> opcode_buffer = {
58       0x0a, 0x20, 0x10, 0x08, 0x03, 0x12, 0x27,
59   };
60   this->op_memory_->SetMemory(0, opcode_buffer);
61 
62   std::vector<std::string> lines;
63   this->op_->GetLogInfo(0, opcode_buffer.size(), &lines);
64   std::vector<std::string> expected{
65       "DW_OP_const2u 4128", "Raw Data: 0x0a 0x20 0x10", "DW_OP_const1u 3", "Raw Data: 0x08 0x03",
66       "DW_OP_dup",          "Raw Data: 0x12",           "DW_OP_xor",       "Raw Data: 0x27"};
67   ASSERT_EQ(expected, lines);
68 }
69 
70 REGISTER_TYPED_TEST_SUITE_P(DwarfOpLogTest, multiple_ops);
71 
72 typedef ::testing::Types<uint32_t, uint64_t> DwarfOpLogTestTypes;
73 INSTANTIATE_TYPED_TEST_SUITE_P(Libunwindstack, DwarfOpLogTest, DwarfOpLogTestTypes);
74 
75 }  // namespace unwindstack
76