blob: d9b290cd650d263b0bd67f63acdf8a867e9d5e2a [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