blob: 0b881d43258b5962caf4af999ab497e85b91a78c [file] [log] [blame]
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * 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 Ivanova1feb112015-10-01 18:41:57 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * 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 Ivanova1feb112015-10-01 18:41:57 -070027 */
28
29#include <stdlib.h>
30#include <string.h>
31#include <sys/mman.h>
32
33#include <gtest/gtest.h>
34
Elliott Hughes15a2b7b2019-02-15 13:48:38 -080035#include "linker_utils.h"
Peter Collingbournebb11ee62022-05-02 12:26:16 -070036#include "platform/bionic/page.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070037
Dimitry Ivanov2a6d9b22017-03-11 14:35:38 -080038TEST(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 Ivanova1feb112015-10-01 18:41:57 -070045TEST(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 Prichard269bb492018-10-04 14:45:55 -070062 ASSERT_TRUE(normalize_path("/..", &output));
63 ASSERT_EQ("/", output);
64
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070065 output = "unchanged";
66 ASSERT_FALSE(normalize_path("root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output));
67 ASSERT_EQ("unchanged", output);
68}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070069
70TEST(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 Ivanov284ae352015-12-08 10:47:13 -080080TEST(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 Ivanov42d5fcb2015-10-29 17:01:24 -070092TEST(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 Ivanov84bab5a2015-11-20 13:34:11 -0800107TEST(linker_utils, page_start) {
Peter Collingbournebb11ee62022-05-02 12:26:16 -0700108 ASSERT_EQ(0x0001000U, page_start(0x0001000));
109 ASSERT_EQ(0x3002000U, page_start(0x300222f));
110 ASSERT_EQ(0x6001000U, page_start(0x6001fff));
Dmitriy Ivanov84bab5a2015-11-20 13:34:11 -0800111}
112
113TEST(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
119TEST(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}