1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
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.
14 *
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.
27 */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/mman.h>
32
33 #include <gtest/gtest.h>
34
35 #include "linker_utils.h"
36 #include "platform/bionic/page.h"
37
TEST(linker_utils,format_string)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
TEST(linker_utils,normalize_path_smoke)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
62 ASSERT_TRUE(normalize_path("/..", &output));
63 ASSERT_EQ("/", output);
64
65 output = "unchanged";
66 ASSERT_FALSE(normalize_path("root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output));
67 ASSERT_EQ("unchanged", output);
68 }
69
TEST(linker_utils,file_is_in_dir_smoke)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
TEST(linker_utils,file_is_under_dir_smoke)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
TEST(linker_utils,parse_zip_path_smoke)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
TEST(linker_utils,page_start)107 TEST(linker_utils, page_start) {
108 const size_t kPageSize = page_size();
109
110 if (kPageSize == 4096) {
111 ASSERT_EQ(0x0001000U, page_start(0x0001000));
112 ASSERT_EQ(0x3002000U, page_start(0x300222f));
113 ASSERT_EQ(0x6001000U, page_start(0x6001fff));
114 } else if (kPageSize == 16384) {
115 ASSERT_EQ(0x0004000U, page_start(0x0004000));
116 ASSERT_EQ(0x3008000U, page_start(0x300822f));
117 ASSERT_EQ(0x6004000U, page_start(0x6004fff));
118 } else {
119 FAIL() << "Page size not supported " << kPageSize;
120 }
121 }
122
TEST(linker_utils,page_offset)123 TEST(linker_utils, page_offset) {
124 const size_t kPageSize = page_size();
125
126 if (kPageSize == 4096) {
127 ASSERT_EQ(0x0U, page_offset(0x0001000));
128 ASSERT_EQ(0x22fU, page_offset(0x30222f));
129 ASSERT_EQ(0xfffU, page_offset(0x6001fff));
130 } else if (kPageSize == 16384) {
131 ASSERT_EQ(0x0U, page_offset(0x0004000));
132 ASSERT_EQ(0x322fU, page_offset(0x30322f));
133 ASSERT_EQ(0x3fffU, page_offset(0x6003fff));
134 } else {
135 FAIL() << "Page size not supported " << kPageSize;
136 }
137 }
138
TEST(linker_utils,safe_add)139 TEST(linker_utils, safe_add) {
140 int64_t val = 42;
141 ASSERT_FALSE(safe_add(&val, INT64_MAX-20, 21U));
142 ASSERT_EQ(42, val);
143 ASSERT_TRUE(safe_add(&val, INT64_MAX-42, 42U));
144 ASSERT_EQ(INT64_MAX, val);
145 ASSERT_TRUE(safe_add(&val, 2000, 42U));
146 ASSERT_EQ(2042, val);
147 }
148