Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 4 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 14 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/mman.h> |
| 32 | |
| 33 | #include <gtest/gtest.h> |
| 34 | |
Elliott Hughes | 15a2b7b | 2019-02-15 13:48:38 -0800 | [diff] [blame] | 35 | #include "linker_utils.h" |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 36 | #include "platform/bionic/page.h" |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 37 | |
Dimitry Ivanov | 2a6d9b2 | 2017-03-11 14:35:38 -0800 | [diff] [blame] | 38 | TEST(linker_utils, format_string) { |
| 39 | std::vector<std::pair<std::string, std::string>> params = {{ "LIB", "lib32"}, { "SDKVER", "42"}}; |
| 40 | std::string str_smoke = "LIB$LIB${LIB${SDKVER}SDKVER$TEST$"; |
| 41 | format_string(&str_smoke, params); |
| 42 | ASSERT_EQ("LIBlib32${LIB42SDKVER$TEST$", str_smoke); |
| 43 | } |
| 44 | |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 45 | TEST(linker_utils, normalize_path_smoke) { |
| 46 | std::string output; |
| 47 | ASSERT_TRUE(normalize_path("/../root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output)); |
| 48 | ASSERT_EQ("/root/dir/dir2/zipfile!/dir/afile", output); |
| 49 | |
| 50 | ASSERT_TRUE(normalize_path("/../root///dir/.///dir2/somedir/.../zipfile!/.dir/dir9//..///afile", &output)); |
| 51 | ASSERT_EQ("/root/dir/dir2/somedir/.../zipfile!/.dir/afile", output); |
| 52 | |
| 53 | ASSERT_TRUE(normalize_path("/root/..", &output)); |
| 54 | ASSERT_EQ("/", output); |
| 55 | |
| 56 | ASSERT_TRUE(normalize_path("/root/notroot/..", &output)); |
| 57 | ASSERT_EQ("/root/", output); |
| 58 | |
| 59 | ASSERT_TRUE(normalize_path("/a/../../b", &output)); |
| 60 | ASSERT_EQ("/b", output); |
| 61 | |
Ryan Prichard | 269bb49 | 2018-10-04 14:45:55 -0700 | [diff] [blame] | 62 | ASSERT_TRUE(normalize_path("/..", &output)); |
| 63 | ASSERT_EQ("/", output); |
| 64 | |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 65 | output = "unchanged"; |
| 66 | ASSERT_FALSE(normalize_path("root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output)); |
| 67 | ASSERT_EQ("unchanged", output); |
| 68 | } |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 69 | |
| 70 | TEST(linker_utils, file_is_in_dir_smoke) { |
| 71 | ASSERT_TRUE(file_is_in_dir("/foo/bar/file", "/foo/bar")); |
| 72 | ASSERT_FALSE(file_is_in_dir("/foo/bar/file", "/foo")); |
| 73 | |
| 74 | ASSERT_FALSE(file_is_in_dir("/foo/bar/file", "/bar/foo")); |
| 75 | |
| 76 | ASSERT_TRUE(file_is_in_dir("/file", "")); |
| 77 | ASSERT_FALSE(file_is_in_dir("/file", "/")); |
| 78 | } |
| 79 | |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 80 | TEST(linker_utils, file_is_under_dir_smoke) { |
| 81 | ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "/foo/bar")); |
| 82 | ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "/foo")); |
| 83 | |
| 84 | ASSERT_FALSE(file_is_under_dir("/foo/bar/file", "/bar/foo")); |
| 85 | |
| 86 | ASSERT_TRUE(file_is_under_dir("/file", "")); |
| 87 | ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "")); |
| 88 | ASSERT_FALSE(file_is_under_dir("/file", "/")); |
| 89 | ASSERT_FALSE(file_is_under_dir("/foo/bar/file", "/")); |
| 90 | } |
| 91 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 92 | TEST(linker_utils, parse_zip_path_smoke) { |
| 93 | std::string zip_path; |
| 94 | std::string entry_path; |
| 95 | |
| 96 | ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip", &zip_path, &entry_path)); |
| 97 | ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip!path/in/zip", &zip_path, &entry_path)); |
| 98 | ASSERT_TRUE(parse_zip_path("/zip/path/file.zip!/path/in/zip", &zip_path, &entry_path)); |
| 99 | ASSERT_EQ("/zip/path/file.zip", zip_path); |
| 100 | ASSERT_EQ("path/in/zip", entry_path); |
| 101 | |
| 102 | ASSERT_TRUE(parse_zip_path("/zip/path/file2.zip!/", &zip_path, &entry_path)); |
| 103 | ASSERT_EQ("/zip/path/file2.zip", zip_path); |
| 104 | ASSERT_EQ("", entry_path); |
| 105 | } |
| 106 | |
Dmitriy Ivanov | 84bab5a | 2015-11-20 13:34:11 -0800 | [diff] [blame] | 107 | TEST(linker_utils, page_start) { |
Peter Collingbourne | bb11ee6 | 2022-05-02 12:26:16 -0700 | [diff] [blame^] | 108 | ASSERT_EQ(0x0001000U, page_start(0x0001000)); |
| 109 | ASSERT_EQ(0x3002000U, page_start(0x300222f)); |
| 110 | ASSERT_EQ(0x6001000U, page_start(0x6001fff)); |
Dmitriy Ivanov | 84bab5a | 2015-11-20 13:34:11 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST(linker_utils, page_offset) { |
| 114 | ASSERT_EQ(0x0U, page_offset(0x0001000)); |
| 115 | ASSERT_EQ(0x22fU, page_offset(0x300222f)); |
| 116 | ASSERT_EQ(0xfffU, page_offset(0x6001fff)); |
| 117 | } |
| 118 | |
| 119 | TEST(linker_utils, safe_add) { |
| 120 | int64_t val = 42; |
| 121 | ASSERT_FALSE(safe_add(&val, INT64_MAX-20, 21U)); |
| 122 | ASSERT_EQ(42, val); |
| 123 | ASSERT_TRUE(safe_add(&val, INT64_MAX-42, 42U)); |
| 124 | ASSERT_EQ(INT64_MAX, val); |
| 125 | ASSERT_TRUE(safe_add(&val, 2000, 42U)); |
| 126 | ASSERT_EQ(2042, val); |
| 127 | } |