1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/mman.h>
20 
21 #include <gtest/gtest.h>
22 
23 #include "../linker_utils.h"
24 
TEST(linker_utils,normalize_path_smoke)25 TEST(linker_utils, normalize_path_smoke) {
26   std::string output;
27   ASSERT_TRUE(normalize_path("/../root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output));
28   ASSERT_EQ("/root/dir/dir2/zipfile!/dir/afile", output);
29 
30   ASSERT_TRUE(normalize_path("/../root///dir/.///dir2/somedir/.../zipfile!/.dir/dir9//..///afile", &output));
31   ASSERT_EQ("/root/dir/dir2/somedir/.../zipfile!/.dir/afile", output);
32 
33   ASSERT_TRUE(normalize_path("/root/..", &output));
34   ASSERT_EQ("/", output);
35 
36   ASSERT_TRUE(normalize_path("/root/notroot/..", &output));
37   ASSERT_EQ("/root/", output);
38 
39   ASSERT_TRUE(normalize_path("/a/../../b", &output));
40   ASSERT_EQ("/b", output);
41 
42   output = "unchanged";
43   ASSERT_FALSE(normalize_path("root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output));
44   ASSERT_EQ("unchanged", output);
45 }
46 
TEST(linker_utils,file_is_in_dir_smoke)47 TEST(linker_utils, file_is_in_dir_smoke) {
48   ASSERT_TRUE(file_is_in_dir("/foo/bar/file", "/foo/bar"));
49   ASSERT_FALSE(file_is_in_dir("/foo/bar/file", "/foo"));
50 
51   ASSERT_FALSE(file_is_in_dir("/foo/bar/file", "/bar/foo"));
52 
53   ASSERT_TRUE(file_is_in_dir("/file", ""));
54   ASSERT_FALSE(file_is_in_dir("/file", "/"));
55 }
56 
TEST(linker_utils,file_is_under_dir_smoke)57 TEST(linker_utils, file_is_under_dir_smoke) {
58   ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "/foo/bar"));
59   ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "/foo"));
60 
61   ASSERT_FALSE(file_is_under_dir("/foo/bar/file", "/bar/foo"));
62 
63   ASSERT_TRUE(file_is_under_dir("/file", ""));
64   ASSERT_TRUE(file_is_under_dir("/foo/bar/file", ""));
65   ASSERT_FALSE(file_is_under_dir("/file", "/"));
66   ASSERT_FALSE(file_is_under_dir("/foo/bar/file", "/"));
67 }
68 
TEST(linker_utils,parse_zip_path_smoke)69 TEST(linker_utils, parse_zip_path_smoke) {
70   std::string zip_path;
71   std::string entry_path;
72 
73   ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip", &zip_path, &entry_path));
74   ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip!path/in/zip", &zip_path, &entry_path));
75   ASSERT_TRUE(parse_zip_path("/zip/path/file.zip!/path/in/zip", &zip_path, &entry_path));
76   ASSERT_EQ("/zip/path/file.zip", zip_path);
77   ASSERT_EQ("path/in/zip", entry_path);
78 
79   ASSERT_TRUE(parse_zip_path("/zip/path/file2.zip!/", &zip_path, &entry_path));
80   ASSERT_EQ("/zip/path/file2.zip", zip_path);
81   ASSERT_EQ("", entry_path);
82 }
83 
TEST(linker_utils,page_start)84 TEST(linker_utils, page_start) {
85   ASSERT_EQ(0x0001000, page_start(0x0001000));
86   ASSERT_EQ(0x3002000, page_start(0x300222f));
87   ASSERT_EQ(0x6001000, page_start(0x6001fff));
88 }
89 
TEST(linker_utils,page_offset)90 TEST(linker_utils, page_offset) {
91   ASSERT_EQ(0x0U, page_offset(0x0001000));
92   ASSERT_EQ(0x22fU, page_offset(0x300222f));
93   ASSERT_EQ(0xfffU, page_offset(0x6001fff));
94 }
95 
TEST(linker_utils,safe_add)96 TEST(linker_utils, safe_add) {
97   int64_t val = 42;
98   ASSERT_FALSE(safe_add(&val, INT64_MAX-20, 21U));
99   ASSERT_EQ(42, val);
100   ASSERT_TRUE(safe_add(&val, INT64_MAX-42, 42U));
101   ASSERT_EQ(INT64_MAX, val);
102   ASSERT_TRUE(safe_add(&val, 2000, 42U));
103   ASSERT_EQ(2042, val);
104 }
105