• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  
TEST(linker_utils,format_string)37  TEST(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  
TEST(linker_utils,normalize_path_smoke)44  TEST(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    ASSERT_TRUE(normalize_path("/..", &output));
62    ASSERT_EQ("/", output);
63  
64    output = "unchanged";
65    ASSERT_FALSE(normalize_path("root///dir/.///dir2/somedir/../zipfile!/dir/dir9//..///afile", &output));
66    ASSERT_EQ("unchanged", output);
67  }
68  
TEST(linker_utils,file_is_in_dir_smoke)69  TEST(linker_utils, file_is_in_dir_smoke) {
70    ASSERT_TRUE(file_is_in_dir("/foo/bar/file", "/foo/bar"));
71    ASSERT_FALSE(file_is_in_dir("/foo/bar/file", "/foo"));
72  
73    ASSERT_FALSE(file_is_in_dir("/foo/bar/file", "/bar/foo"));
74  
75    ASSERT_TRUE(file_is_in_dir("/file", ""));
76    ASSERT_FALSE(file_is_in_dir("/file", "/"));
77  }
78  
TEST(linker_utils,file_is_under_dir_smoke)79  TEST(linker_utils, file_is_under_dir_smoke) {
80    ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "/foo/bar"));
81    ASSERT_TRUE(file_is_under_dir("/foo/bar/file", "/foo"));
82  
83    ASSERT_FALSE(file_is_under_dir("/foo/bar/file", "/bar/foo"));
84  
85    ASSERT_TRUE(file_is_under_dir("/file", ""));
86    ASSERT_TRUE(file_is_under_dir("/foo/bar/file", ""));
87    ASSERT_FALSE(file_is_under_dir("/file", "/"));
88    ASSERT_FALSE(file_is_under_dir("/foo/bar/file", "/"));
89  }
90  
TEST(linker_utils,parse_zip_path_smoke)91  TEST(linker_utils, parse_zip_path_smoke) {
92    std::string zip_path;
93    std::string entry_path;
94  
95    ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip", &zip_path, &entry_path));
96    ASSERT_FALSE(parse_zip_path("/not/a/zip/path/file.zip!path/in/zip", &zip_path, &entry_path));
97    ASSERT_TRUE(parse_zip_path("/zip/path/file.zip!/path/in/zip", &zip_path, &entry_path));
98    ASSERT_EQ("/zip/path/file.zip", zip_path);
99    ASSERT_EQ("path/in/zip", entry_path);
100  
101    ASSERT_TRUE(parse_zip_path("/zip/path/file2.zip!/", &zip_path, &entry_path));
102    ASSERT_EQ("/zip/path/file2.zip", zip_path);
103    ASSERT_EQ("", entry_path);
104  }
105  
TEST(linker_utils,page_start)106  TEST(linker_utils, page_start) {
107    ASSERT_EQ(0x0001000, page_start(0x0001000));
108    ASSERT_EQ(0x3002000, page_start(0x300222f));
109    ASSERT_EQ(0x6001000, page_start(0x6001fff));
110  }
111  
TEST(linker_utils,page_offset)112  TEST(linker_utils, page_offset) {
113    ASSERT_EQ(0x0U, page_offset(0x0001000));
114    ASSERT_EQ(0x22fU, page_offset(0x300222f));
115    ASSERT_EQ(0xfffU, page_offset(0x6001fff));
116  }
117  
TEST(linker_utils,safe_add)118  TEST(linker_utils, safe_add) {
119    int64_t val = 42;
120    ASSERT_FALSE(safe_add(&val, INT64_MAX-20, 21U));
121    ASSERT_EQ(42, val);
122    ASSERT_TRUE(safe_add(&val, INT64_MAX-42, 42U));
123    ASSERT_EQ(INT64_MAX, val);
124    ASSERT_TRUE(safe_add(&val, 2000, 42U));
125    ASSERT_EQ(2042, val);
126  }
127