From fa4aeed2c60fb97a06e2550481b9ef4d0e100b7e Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Tue, 5 Apr 2016 13:29:50 -0700 Subject: [PATCH] linker: Add unit-test for sleb128_decoder Add missing test for sleb128_decoder. Also remove dependency to static library because it exposes malloc/free which interfere with libc.so malloc/free causing test to crash. Change-Id: Iedd3268011df9f67ed58c0b58c71f34e30370b23 --- linker/linker_sleb128.h | 2 + linker/tests/Android.mk | 8 +-- linker/tests/linker_sleb128_test.cpp | 98 ++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 linker/tests/linker_sleb128_test.cpp diff --git a/linker/linker_sleb128.h b/linker/linker_sleb128.h index a34916f87..c4df2597b 100644 --- a/linker/linker_sleb128.h +++ b/linker/linker_sleb128.h @@ -19,6 +19,8 @@ #include +#include "linker_debug.h" + // Helper classes for decoding LEB128, used in packed relocation data. // http://en.wikipedia.org/wiki/LEB128 diff --git a/linker/tests/Android.mk b/linker/tests/Android.mk index e9f43e9f3..48c637478 100644 --- a/linker/tests/Android.mk +++ b/linker/tests/Android.mk @@ -27,17 +27,17 @@ LOCAL_CFLAGS += -g -Wall -Wextra -Wunused -Werror LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../libc/ LOCAL_SRC_FILES := \ + linker_block_allocator_test.cpp \ linker_globals.cpp \ linked_list_test.cpp \ - linker_block_allocator_test.cpp \ - ../linker_block_allocator.cpp \ linker_memory_allocator_test.cpp \ + linker_sleb128_test.cpp \ linker_utils_test.cpp \ + ../linker_allocator.cpp \ + ../linker_block_allocator.cpp \ ../linker_utils.cpp # for __libc_fatal LOCAL_SRC_FILES += ../../libc/bionic/libc_logging.cpp -LOCAL_STATIC_LIBRARIES := liblinker_malloc - include $(BUILD_NATIVE_TEST) diff --git a/linker/tests/linker_sleb128_test.cpp b/linker/tests/linker_sleb128_test.cpp new file mode 100644 index 000000000..4e29bca1b --- /dev/null +++ b/linker/tests/linker_sleb128_test.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include + +#include "../linker_sleb128.h" + +TEST(linker_sleb128, smoke) { + std::vector encoding; + // 624485 + encoding.push_back(0xe5); + encoding.push_back(0x8e); + encoding.push_back(0x26); + // 0 + encoding.push_back(0x00); + // 1 + encoding.push_back(0x01); + // 63 + encoding.push_back(0x3f); + // 64 + encoding.push_back(0xc0); + encoding.push_back(0x00); + // -1 + encoding.push_back(0x7f); + // -624485 + encoding.push_back(0x9b); + encoding.push_back(0xf1); + encoding.push_back(0x59); + // 2147483647 + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0x07); + // -2147483648 + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x78); +#if defined(__LP64__) + // 9223372036854775807 + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0xff); + encoding.push_back(0x00); + // -9223372036854775808 + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x80); + encoding.push_back(0x7f); +#endif + sleb128_decoder decoder(&encoding[0], encoding.size()); + + EXPECT_EQ(624485U, decoder.pop_front()); + + EXPECT_EQ(0U, decoder.pop_front()); + EXPECT_EQ(1U, decoder.pop_front()); + EXPECT_EQ(63U, decoder.pop_front()); + EXPECT_EQ(64U, decoder.pop_front()); + EXPECT_EQ(static_cast(-1), decoder.pop_front()); + EXPECT_EQ(static_cast(-624485), decoder.pop_front()); + EXPECT_EQ(2147483647U, decoder.pop_front()); + EXPECT_EQ(static_cast(-2147483648), decoder.pop_front()); +#if defined(__LP64__) + EXPECT_EQ(9223372036854775807ULL, decoder.pop_front()); + EXPECT_EQ(static_cast(-9223372036854775807LL - 1), decoder.pop_front()); +#endif +}