blob: dce223a10cf7ea8a6894b1a3ebd1827f5bb55c40 [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
35#include "../linker_utils.h"
36
Dimitry Ivanov2a6d9b22017-03-11 14:35:38 -080037TEST(linker_utils, format_string) {
38 std::vector<std::pair<std::string, std::string>> params = {{ "LIB", "lib32"}, { "SDKVER", "42"}};
39 std::string str_smoke = "LIB$LIB${LIB${SDKVER}SDKVER$TEST$";
40 format_string(&str_smoke, params);
41 ASSERT_EQ("LIBlib32${LIB42SDKVER$TEST$", str_smoke);
42}
43
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070044TEST(linker_utils, normalize_path_smoke) {
45 std::string output;
46 ASSERT_TRUE(normalize_path("/../root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output));
47 ASSERT_EQ("/root/dir/dir2/zipfile!/dir/afile", output);
48
49 ASSERT_TRUE(normalize_path("/../root///dir/.///dir2/somedir/.../zipfile!/.dir/dir9//..///afile", &output));
50 ASSERT_EQ("/root/dir/dir2/somedir/.../zipfile!/.dir/afile", output);
51
52 ASSERT_TRUE(normalize_path("/root/..", &output));
53 ASSERT_EQ("/", output);
54
55 ASSERT_TRUE(normalize_path("/root/notroot/..", &output));
56 ASSERT_EQ("/root/", output);
57
58 ASSERT_TRUE(normalize_path("/a/../../b", &output));
59 ASSERT_EQ("/b", output);
60
61 output = "unchanged";
62 ASSERT_FALSE(normalize_path("root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output));
63 ASSERT_EQ("unchanged", output);
64}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070065
66TEST(linker_utils, file_is_in_dir_smoke) {
67 ASSERT_TRUE(file_is_in_dir("/foo/bar/file", "/foo/bar"));
68 ASSERT_FALSE(file_is_in_dir("/foo/bar/file", "/foo"));
69
70 ASSERT_FALSE(file_is_in_dir("/foo/bar/file", "/bar/foo"));
71
72 ASSERT_TRUE(file_is_in_dir("/file", ""));
73 ASSERT_FALSE(file_is_in_dir("/file", "/"));
74}
75
Dimitry Ivanov284ae352015-12-08 10:47:13 -080076TEST(linker_utils, file_is_under_dir_smoke) {
77 ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "/foo/bar"));
78 ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "/foo"));
79
80 ASSERT_FALSE(file_is_under_dir("/foo/bar/file", "/bar/foo"));
81
82 ASSERT_TRUE(file_is_under_dir("/file", ""));
83 ASSERT_TRUE(file_is_under_dir("/foo/bar/file", ""));
84 ASSERT_FALSE(file_is_under_dir("/file", "/"));
85 ASSERT_FALSE(file_is_under_dir("/foo/bar/file", "/"));
86}
87
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070088TEST(linker_utils, parse_zip_path_smoke) {
89 std::string zip_path;
90 std::string entry_path;
91
92 ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip", &zip_path, &entry_path));
93 ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip!path/in/zip", &zip_path, &entry_path));
94 ASSERT_TRUE(parse_zip_path("/zip/path/file.zip!/path/in/zip", &zip_path, &entry_path));
95 ASSERT_EQ("/zip/path/file.zip", zip_path);
96 ASSERT_EQ("path/in/zip", entry_path);
97
98 ASSERT_TRUE(parse_zip_path("/zip/path/file2.zip!/", &zip_path, &entry_path));
99 ASSERT_EQ("/zip/path/file2.zip", zip_path);
100 ASSERT_EQ("", entry_path);
101}
102
Dmitriy Ivanov84bab5a2015-11-20 13:34:11 -0800103TEST(linker_utils, page_start) {
104 ASSERT_EQ(0x0001000, page_start(0x0001000));
105 ASSERT_EQ(0x3002000, page_start(0x300222f));
106 ASSERT_EQ(0x6001000, page_start(0x6001fff));
107}
108
109TEST(linker_utils, page_offset) {
110 ASSERT_EQ(0x0U, page_offset(0x0001000));
111 ASSERT_EQ(0x22fU, page_offset(0x300222f));
112 ASSERT_EQ(0xfffU, page_offset(0x6001fff));
113}
114
115TEST(linker_utils, safe_add) {
116 int64_t val = 42;
117 ASSERT_FALSE(safe_add(&val, INT64_MAX-20, 21U));
118 ASSERT_EQ(42, val);
119 ASSERT_TRUE(safe_add(&val, INT64_MAX-42, 42U));
120 ASSERT_EQ(INT64_MAX, val);
121 ASSERT_TRUE(safe_add(&val, 2000, 42U));
122 ASSERT_EQ(2042, val);
123}