1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "crazy_linker_search_path_list.h"
6
7#include <minitest/minitest.h>
8#include "crazy_linker_system_mock.h"
9
10namespace crazy {
11
12class TestSystem {
13 public:
14  TestSystem() : sys_() {
15    sys_.AddRegularFile("/tmp/foo/bar", "BARBARBAR", 9);
16    sys_.AddRegularFile("/tmp/zoo", "ZOO", 3);
17    sys_.AddRegularFile("/foo", "Foo", 3);
18    sys_.AddEnvVariable("TEST_LIBRARY_PATH", "/tmp:/");
19  }
20
21  ~TestSystem() {}
22
23  void AddFile(const char* path, const char* data, size_t len) {
24    sys_.AddRegularFile(path, data, len);
25  }
26
27 private:
28  SystemMock sys_;
29};
30
31TEST(SearchPathList, Empty) {
32  TestSystem sys;
33  SearchPathList list;
34  EXPECT_FALSE(list.FindFile("/foo"));
35  EXPECT_FALSE(list.FindFile("/tmp/zoo"));
36  EXPECT_FALSE(list.FindFile("/tmp/foo/bar"));
37}
38
39TEST(SearchPathList, OneItem) {
40  TestSystem sys;
41  SearchPathList list;
42  list.AddPaths("/tmp/foo");
43  EXPECT_STREQ("/tmp/foo/bar", list.FindFile("bar"));
44  EXPECT_FALSE(list.FindFile("zoo"));
45  EXPECT_FALSE(list.FindFile("foo"));
46}
47
48TEST(SearchPathList, Reset) {
49  TestSystem sys;
50  SearchPathList list;
51  list.AddPaths("/tmp/foo");
52  EXPECT_STREQ("/tmp/foo/bar", list.FindFile("bar"));
53
54  list.Reset();
55  EXPECT_FALSE(list.FindFile("bar"));
56}
57
58TEST(SearchPathList, ResetFromEnv) {
59  TestSystem sys;
60  SearchPathList list;
61  list.ResetFromEnv("TEST_LIBRARY_PATH");
62  EXPECT_STREQ("/tmp/foo/bar", list.FindFile("foo/bar"));
63  EXPECT_STREQ("/foo", list.FindFile("foo"));
64}
65
66TEST(SearchPathList, ThreeItems) {
67  TestSystem sys;
68  SearchPathList list;
69  list.AddPaths("/tmp/foo");
70  list.AddPaths("/tmp/");
71
72  EXPECT_STREQ("/tmp/foo/bar", list.FindFile("bar"));
73  EXPECT_STREQ("/tmp/zoo", list.FindFile("zoo"));
74  EXPECT_FALSE(list.FindFile("foo"));
75}
76
77TEST(SearchPathList, EnvPathsAfterAddedOnes) {
78  TestSystem sys;
79  sys.AddFile("/opt/foo", "FOO", 3);
80  SearchPathList list;
81  list.ResetFromEnv("TEST_LIBRARY_PATH");
82  list.AddPaths("/opt");
83
84  // This checks that paths added with AddPaths() have priority over
85  // paths added with ResetFromEnv(). An invalid implementation would
86  // find '/tmp/foo' instead.
87  EXPECT_STREQ("/opt/foo", list.FindFile("foo"));
88}
89
90}  // namespace crazy