blob: d5a7d6f155f8dc39fcb3cd1f18cbbf22534189dd [file] [log] [blame]
Steve Muckle18b981e2019-04-15 17:43:02 -07001/*
2 * Copyright (C) 2019 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 <functional>
18
19#include <android-base/file.h>
20#include <android-base/macros.h>
21#include <android-base/unique_fd.h>
22#include <gtest/gtest.h>
23
24#include <modprobe/modprobe.h>
25
26#include "libmodprobe_test.h"
27
28// Used by libmodprobe_ext_test to check if requested modules are present.
29std::vector<std::string> test_modules;
30
31// Used by libmodprobe_ext_test to report which modules would have been loaded.
32std::vector<std::string> modules_loaded;
33
34TEST(libmodprobe, Test) {
35 test_modules = {
36 "/test1.ko", "/test2.ko", "/test3.ko", "/test4.ko", "/test5.ko",
37 "/test6.ko", "/test7.ko", "/test8.ko", "/test9.ko", "/test10.ko",
38 "/test11.ko", "/test12.ko", "/test13.ko", "/test14.ko", "/test15.ko",
39 };
40
41 std::vector<std::string> expected_modules_loaded = {
42 "/test14.ko",
43 "/test15.ko",
44 "/test3.ko",
45 "/test4.ko",
46 "/test1.ko",
47 "/test6.ko",
48 "/test2.ko",
49 "/test5.ko",
50 "/test8.ko",
51 "/test7.ko param1=4",
52 "/test9.ko param_x=1 param_y=2 param_z=3",
53 "/test10.ko",
54 "/test12.ko",
55 "/test11.ko",
56 "/test13.ko",
57 };
58
Steve Mucklebb58b012019-07-30 11:58:11 -070059 std::vector<std::string> expected_after_remove = {
60 "/test14.ko", "/test15.ko", "/test1.ko",
61 "/test6.ko", "/test2.ko", "/test5.ko",
62 "/test8.ko", "/test7.ko param1=4", "/test9.ko param_x=1 param_y=2 param_z=3",
63 "/test10.ko", "/test12.ko", "/test11.ko",
64 "/test13.ko",
65 };
66
Steve Muckle18b981e2019-04-15 17:43:02 -070067 const std::string modules_dep =
68 "test1.ko:\n"
69 "test2.ko:\n"
70 "test3.ko:\n"
71 "test4.ko: test3.ko\n"
72 "test5.ko: test2.ko test6.ko\n"
73 "test6.ko:\n"
74 "test7.ko:\n"
75 "test8.ko:\n"
76 "test9.ko:\n"
77 "test10.ko:\n"
78 "test11.ko:\n"
79 "test12.ko:\n"
80 "test13.ko:\n"
81 "test14.ko:\n"
82 "test15.ko:\n";
83
84 const std::string modules_softdep =
85 "softdep test7 pre: test8\n"
86 "softdep test9 post: test10\n"
87 "softdep test11 pre: test12 post: test13\n"
88 "softdep test3 pre: test141516\n";
89
90 const std::string modules_alias =
91 "# Aliases extracted from modules themselves.\n"
92 "\n"
93 "alias test141516 test14\n"
94 "alias test141516 test15\n"
95 "alias test141516 test16\n";
96
97 const std::string modules_options =
98 "options test7.ko param1=4\n"
99 "options test9.ko param_x=1 param_y=2 param_z=3\n"
100 "options test100.ko param_1=1\n";
101
102 const std::string modules_load =
103 "test4.ko\n"
104 "test1.ko\n"
105 "test3.ko\n"
106 "test5.ko\n"
107 "test7.ko\n"
108 "test9.ko\n"
109 "test11.ko\n";
110
111 TemporaryDir dir;
112 ASSERT_TRUE(android::base::WriteStringToFile(
113 modules_alias, std::string(dir.path) + "/modules.alias", 0600, getuid(), getgid()));
114
115 ASSERT_TRUE(android::base::WriteStringToFile(
116 modules_dep, std::string(dir.path) + "/modules.dep", 0600, getuid(), getgid()));
117 ASSERT_TRUE(android::base::WriteStringToFile(
118 modules_softdep, std::string(dir.path) + "/modules.softdep", 0600, getuid(), getgid()));
119 ASSERT_TRUE(android::base::WriteStringToFile(
120 modules_options, std::string(dir.path) + "/modules.options", 0600, getuid(), getgid()));
121 ASSERT_TRUE(android::base::WriteStringToFile(
122 modules_load, std::string(dir.path) + "/modules.load", 0600, getuid(), getgid()));
123
124 for (auto i = test_modules.begin(); i != test_modules.end(); ++i) {
125 *i = dir.path + *i;
126 }
127
128 Modprobe m({dir.path});
129 EXPECT_TRUE(m.LoadListedModules());
130
131 GTEST_LOG_(INFO) << "Expected modules loaded (in order):";
132 for (auto i = expected_modules_loaded.begin(); i != expected_modules_loaded.end(); ++i) {
133 *i = dir.path + *i;
134 GTEST_LOG_(INFO) << "\"" << *i << "\"";
135 }
136 GTEST_LOG_(INFO) << "Actual modules loaded (in order):";
137 for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) {
138 GTEST_LOG_(INFO) << "\"" << *i << "\"";
139 }
140
141 EXPECT_TRUE(modules_loaded == expected_modules_loaded);
Steve Mucklebb58b012019-07-30 11:58:11 -0700142
143 EXPECT_TRUE(m.Remove("test4"));
144
145 GTEST_LOG_(INFO) << "Expected modules loaded after removing test4 (in order):";
146 for (auto i = expected_after_remove.begin(); i != expected_after_remove.end(); ++i) {
147 *i = dir.path + *i;
148 GTEST_LOG_(INFO) << "\"" << *i << "\"";
149 }
150 GTEST_LOG_(INFO) << "Actual modules loaded after removing test4 (in order):";
151 for (auto i = modules_loaded.begin(); i != modules_loaded.end(); ++i) {
152 GTEST_LOG_(INFO) << "\"" << *i << "\"";
153 }
154
155 EXPECT_TRUE(modules_loaded == expected_after_remove);
Steve Muckle18b981e2019-04-15 17:43:02 -0700156}