xref: /aosp_15_r20/tools/dexter/slicer/tests/src/slicer_test.cpp (revision f0dffb02cdb5c647d21204e89a92a1ffae2dad87)
1 /*
2  * Copyright (C) 2021 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 <iostream>
18 #include <string>
19 
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 
23 #include "slicer/common.h"
24 #include "slicer/dex_bytecode.h"
25 
26 static std::string customLoggerMsg;
testCustomLogger(const std::string & msg)27 static void testCustomLogger(const std::string& msg) {
28   customLoggerMsg = msg;
29 }
30 
TEST(Slicer,CustomLogger)31 TEST(Slicer, CustomLogger) {
32   slicer::set_logger(testCustomLogger);
33   slicer::_weakCheckFailed("expr1", 1, "file");
34   std::string expected("\nSLICER_WEAK_CHECK failed [expr1] at file:1\n\n");
35   ASSERT_EQ(customLoggerMsg, expected);
36 }
37 
TEST(Slicer,OpcodeToStringStream)38 TEST(Slicer, OpcodeToStringStream) {
39   std::stringstream ss;
40   ss << dex::Opcode::OP_IF_GTZ;
41   ASSERT_EQ("[0x3c] if-gtz", ss.str());
42 }
43 
TEST(Slicer,KnownInstructionFormatToStringStream)44 TEST(Slicer, KnownInstructionFormatToStringStream) {
45   std::stringstream ss;
46   ss << dex::InstructionFormat::k20bc;
47   ASSERT_EQ("20bc", ss.str());
48 }
49 
TEST(Slicer,UnknownInstructionFormatToStringStream)50 TEST(Slicer, UnknownInstructionFormatToStringStream) {
51   std::stringstream ss;
52   ss << static_cast<dex::InstructionFormat>(0xfe);
53   ASSERT_EQ("[0xfe] Unknown", ss.str());
54 }