blob: 3be9b3f37b92da1a24a9742592aac2a2129a0ab8 [file] [log] [blame]
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001/*
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
25TEST(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}
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070046
47TEST(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
57TEST(linker_utils, parse_zip_path_smoke) {
58 std::string zip_path;
59 std::string entry_path;
60
61 ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip", &zip_path, &entry_path));
62 ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip!path/in/zip", &zip_path, &entry_path));
63 ASSERT_TRUE(parse_zip_path("/zip/path/file.zip!/path/in/zip", &zip_path, &entry_path));
64 ASSERT_EQ("/zip/path/file.zip", zip_path);
65 ASSERT_EQ("path/in/zip", entry_path);
66
67 ASSERT_TRUE(parse_zip_path("/zip/path/file2.zip!/", &zip_path, &entry_path));
68 ASSERT_EQ("/zip/path/file2.zip", zip_path);
69 ASSERT_EQ("", entry_path);
70}
71
Dmitriy Ivanov84bab5a2015-11-20 13:34:11 -080072TEST(linker_utils, page_start) {
73 ASSERT_EQ(0x0001000, page_start(0x0001000));
74 ASSERT_EQ(0x3002000, page_start(0x300222f));
75 ASSERT_EQ(0x6001000, page_start(0x6001fff));
76}
77
78TEST(linker_utils, page_offset) {
79 ASSERT_EQ(0x0U, page_offset(0x0001000));
80 ASSERT_EQ(0x22fU, page_offset(0x300222f));
81 ASSERT_EQ(0xfffU, page_offset(0x6001fff));
82}
83
84TEST(linker_utils, safe_add) {
85 int64_t val = 42;
86 ASSERT_FALSE(safe_add(&val, INT64_MAX-20, 21U));
87 ASSERT_EQ(42, val);
88 ASSERT_TRUE(safe_add(&val, INT64_MAX-42, 42U));
89 ASSERT_EQ(INT64_MAX, val);
90 ASSERT_TRUE(safe_add(&val, 2000, 42U));
91 ASSERT_EQ(2042, val);
92}