diff options
author | Treehugger Robot | 2018-05-30 12:27:35 -0500 |
---|---|---|
committer | Gerrit Code Review | 2018-05-30 12:27:35 -0500 |
commit | 680bc0b6b5fa17b36831da0b44ee19f0b43549e1 (patch) | |
tree | 5d4452073fe53af91b101660340400e8754752c8 /init | |
parent | e6a878644ce693873b7e7eafd208312b323fc91a (diff) | |
parent | 75b901d4a8d53baaccaec6af7fadae6a3ce939c1 (diff) | |
download | platform-system-core-680bc0b6b5fa17b36831da0b44ee19f0b43549e1.tar.gz platform-system-core-680bc0b6b5fa17b36831da0b44ee19f0b43549e1.tar.xz platform-system-core-680bc0b6b5fa17b36831da0b44ee19f0b43549e1.zip |
Merge "init: add keychords_test"
Diffstat (limited to 'init')
-rw-r--r-- | init/Android.bp | 1 | ||||
-rw-r--r-- | init/keychords_test.cpp | 348 |
2 files changed, 349 insertions, 0 deletions
diff --git a/init/Android.bp b/init/Android.bp index a3083c121..517847c3f 100644 --- a/init/Android.bp +++ b/init/Android.bp | |||
@@ -170,6 +170,7 @@ cc_test { | |||
170 | srcs: [ | 170 | srcs: [ |
171 | "devices_test.cpp", | 171 | "devices_test.cpp", |
172 | "init_test.cpp", | 172 | "init_test.cpp", |
173 | "keychords_test.cpp", | ||
173 | "persistent_properties_test.cpp", | 174 | "persistent_properties_test.cpp", |
174 | "property_service_test.cpp", | 175 | "property_service_test.cpp", |
175 | "property_type_test.cpp", | 176 | "property_type_test.cpp", |
diff --git a/init/keychords_test.cpp b/init/keychords_test.cpp new file mode 100644 index 000000000..c8c47a8d5 --- /dev/null +++ b/init/keychords_test.cpp | |||
@@ -0,0 +1,348 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2018 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 "keychords.h" | ||
18 | |||
19 | #include <dirent.h> | ||
20 | #include <fcntl.h> | ||
21 | #include <linux/input.h> | ||
22 | #include <linux/uinput.h> | ||
23 | #include <stdint.h> | ||
24 | #include <sys/types.h> | ||
25 | |||
26 | #include <chrono> | ||
27 | #include <set> | ||
28 | #include <string> | ||
29 | #include <vector> | ||
30 | |||
31 | #include <android-base/properties.h> | ||
32 | #include <android-base/stringprintf.h> | ||
33 | #include <android-base/strings.h> | ||
34 | #include <gtest/gtest.h> | ||
35 | |||
36 | #include "epoll.h" | ||
37 | |||
38 | using namespace std::chrono_literals; | ||
39 | |||
40 | namespace android { | ||
41 | namespace init { | ||
42 | |||
43 | namespace { | ||
44 | |||
45 | // This class is used to inject keys. | ||
46 | class EventHandler { | ||
47 | public: | ||
48 | EventHandler(); | ||
49 | EventHandler(const EventHandler&) = delete; | ||
50 | EventHandler(EventHandler&&); | ||
51 | EventHandler& operator=(const EventHandler&) = delete; | ||
52 | EventHandler& operator=(EventHandler&&); | ||
53 | ~EventHandler() noexcept; | ||
54 | |||
55 | bool init(); | ||
56 | |||
57 | bool send(struct input_event& e); | ||
58 | bool send(uint16_t type, uint16_t code, uint16_t value); | ||
59 | bool send(uint16_t code, bool value); | ||
60 | |||
61 | private: | ||
62 | int fd_; | ||
63 | }; | ||
64 | |||
65 | EventHandler::EventHandler() : fd_(-1) {} | ||
66 | |||
67 | EventHandler::EventHandler(EventHandler&& rval) : fd_(rval.fd_) { | ||
68 | rval.fd_ = -1; | ||
69 | } | ||
70 | |||
71 | EventHandler& EventHandler::operator=(EventHandler&& rval) { | ||
72 | fd_ = rval.fd_; | ||
73 | rval.fd_ = -1; | ||
74 | return *this; | ||
75 | } | ||
76 | |||
77 | EventHandler::~EventHandler() { | ||
78 | if (fd_ == -1) return; | ||
79 | ::ioctl(fd_, UI_DEV_DESTROY); | ||
80 | ::close(fd_); | ||
81 | } | ||
82 | |||
83 | bool EventHandler::init() { | ||
84 | if (fd_ != -1) return true; | ||
85 | auto fd = TEMP_FAILURE_RETRY(::open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC)); | ||
86 | if (fd == -1) return false; | ||
87 | if (::ioctl(fd, UI_SET_EVBIT, EV_KEY) == -1) { | ||
88 | ::close(fd); | ||
89 | return false; | ||
90 | } | ||
91 | |||
92 | static const struct uinput_user_dev u = { | ||
93 | .name = "com.google.android.init.test", | ||
94 | .id.bustype = BUS_VIRTUAL, | ||
95 | .id.vendor = 0x1AE0, // Google | ||
96 | .id.product = 0x494E, // IN | ||
97 | .id.version = 1, | ||
98 | }; | ||
99 | if (TEMP_FAILURE_RETRY(::write(fd, &u, sizeof(u))) != sizeof(u)) { | ||
100 | ::close(fd); | ||
101 | return false; | ||
102 | } | ||
103 | |||
104 | // all keys | ||
105 | for (uint16_t i = 0; i < KEY_MAX; ++i) { | ||
106 | if (::ioctl(fd, UI_SET_KEYBIT, i) == -1) { | ||
107 | ::close(fd); | ||
108 | return false; | ||
109 | } | ||
110 | } | ||
111 | if (::ioctl(fd, UI_DEV_CREATE) == -1) { | ||
112 | ::close(fd); | ||
113 | return false; | ||
114 | } | ||
115 | fd_ = fd; | ||
116 | return true; | ||
117 | } | ||
118 | |||
119 | bool EventHandler::send(struct input_event& e) { | ||
120 | gettimeofday(&e.time, nullptr); | ||
121 | return TEMP_FAILURE_RETRY(::write(fd_, &e, sizeof(e))) == sizeof(e); | ||
122 | } | ||
123 | |||
124 | bool EventHandler::send(uint16_t type, uint16_t code, uint16_t value) { | ||
125 | struct input_event e = {.type = type, .code = code, .value = value}; | ||
126 | return send(e); | ||
127 | } | ||
128 | |||
129 | bool EventHandler::send(uint16_t code, bool value) { | ||
130 | return (code < KEY_MAX) && init() && send(EV_KEY, code, value) && send(EV_SYN, SYN_REPORT, 0); | ||
131 | } | ||
132 | |||
133 | std::string InitFds(const char* prefix, pid_t pid = getpid()) { | ||
134 | std::string ret; | ||
135 | |||
136 | std::string init_fds("/proc/"); | ||
137 | init_fds += std::to_string(pid) + "/fd"; | ||
138 | std::unique_ptr<DIR, decltype(&closedir)> fds(opendir(init_fds.c_str()), closedir); | ||
139 | if (!fds) return ret; | ||
140 | |||
141 | dirent* entry; | ||
142 | while ((entry = readdir(fds.get()))) { | ||
143 | if (entry->d_name[0] == '.') continue; | ||
144 | std::string devname = init_fds + '/' + entry->d_name; | ||
145 | char buf[256]; | ||
146 | auto retval = readlink(devname.c_str(), buf, sizeof(buf) - 1); | ||
147 | if ((retval < 0) || (size_t(retval) >= (sizeof(buf) - 1))) continue; | ||
148 | buf[retval] = '\0'; | ||
149 | if (!android::base::StartsWith(buf, prefix)) continue; | ||
150 | if (ret.size() != 0) ret += ","; | ||
151 | ret += buf; | ||
152 | } | ||
153 | return ret; | ||
154 | } | ||
155 | |||
156 | std::string InitInputFds() { | ||
157 | return InitFds("/dev/input/"); | ||
158 | } | ||
159 | |||
160 | std::string InitInotifyFds() { | ||
161 | return InitFds("anon_inode:inotify"); | ||
162 | } | ||
163 | |||
164 | // NB: caller (this series of tests, or conversely the service parser in init) | ||
165 | // is responsible for validation, sorting and uniqueness of the chords, so no | ||
166 | // fuzzing is advised. | ||
167 | |||
168 | const std::vector<int> escape_chord = {KEY_ESC}; | ||
169 | const std::vector<int> triple1_chord = {KEY_BACKSPACE, KEY_VOLUMEDOWN, KEY_VOLUMEUP}; | ||
170 | const std::vector<int> triple2_chord = {KEY_VOLUMEDOWN, KEY_VOLUMEUP, KEY_BACK}; | ||
171 | |||
172 | const std::vector<const std::vector<int>> empty_chords; | ||
173 | const std::vector<const std::vector<int>> chords = { | ||
174 | escape_chord, | ||
175 | triple1_chord, | ||
176 | triple2_chord, | ||
177 | }; | ||
178 | |||
179 | class TestFrame { | ||
180 | public: | ||
181 | TestFrame(const std::vector<const std::vector<int>>& chords, EventHandler* ev = nullptr); | ||
182 | |||
183 | void RelaxForMs(std::chrono::milliseconds wait = 1ms); | ||
184 | |||
185 | void SetChord(int key, bool value = true); | ||
186 | void SetChords(const std::vector<int>& chord, bool value = true); | ||
187 | void ClrChord(int key); | ||
188 | void ClrChords(const std::vector<int>& chord); | ||
189 | |||
190 | bool IsOnlyChord(const std::vector<int>& chord) const; | ||
191 | bool IsNoChord() const; | ||
192 | bool IsChord(const std::vector<int>& chord) const; | ||
193 | void WaitForChord(const std::vector<int>& chord); | ||
194 | |||
195 | std::string Format() const; | ||
196 | |||
197 | private: | ||
198 | static std::string Format(const std::vector<const std::vector<int>>& chords); | ||
199 | |||
200 | Epoll epoll_; | ||
201 | Keychords keychords_; | ||
202 | std::vector<const std::vector<int>> keycodes_; | ||
203 | EventHandler* ev_; | ||
204 | }; | ||
205 | |||
206 | TestFrame::TestFrame(const std::vector<const std::vector<int>>& chords, EventHandler* ev) | ||
207 | : ev_(ev) { | ||
208 | if (!epoll_.Open()) return; | ||
209 | for (const auto& keycodes : chords) keychords_.Register(keycodes); | ||
210 | keychords_.Start(&epoll_, [this](const std::vector<int>& keycodes) { | ||
211 | this->keycodes_.emplace_back(keycodes); | ||
212 | }); | ||
213 | } | ||
214 | |||
215 | void TestFrame::RelaxForMs(std::chrono::milliseconds wait) { | ||
216 | epoll_.Wait(wait); | ||
217 | } | ||
218 | |||
219 | void TestFrame::SetChord(int key, bool value) { | ||
220 | ASSERT_TRUE(!!ev_); | ||
221 | RelaxForMs(); | ||
222 | EXPECT_TRUE(ev_->send(key, value)); | ||
223 | } | ||
224 | |||
225 | void TestFrame::SetChords(const std::vector<int>& chord, bool value) { | ||
226 | ASSERT_TRUE(!!ev_); | ||
227 | for (auto& key : chord) SetChord(key, value); | ||
228 | RelaxForMs(); | ||
229 | } | ||
230 | |||
231 | void TestFrame::ClrChord(int key) { | ||
232 | ASSERT_TRUE(!!ev_); | ||
233 | SetChord(key, false); | ||
234 | } | ||
235 | |||
236 | void TestFrame::ClrChords(const std::vector<int>& chord) { | ||
237 | ASSERT_TRUE(!!ev_); | ||
238 | SetChords(chord, false); | ||
239 | } | ||
240 | |||
241 | bool TestFrame::IsOnlyChord(const std::vector<int>& chord) const { | ||
242 | auto ret = false; | ||
243 | for (const auto& keycode : keycodes_) { | ||
244 | if (keycode != chord) return false; | ||
245 | ret = true; | ||
246 | } | ||
247 | return ret; | ||
248 | } | ||
249 | |||
250 | bool TestFrame::IsNoChord() const { | ||
251 | return keycodes_.empty(); | ||
252 | } | ||
253 | |||
254 | bool TestFrame::IsChord(const std::vector<int>& chord) const { | ||
255 | for (const auto& keycode : keycodes_) { | ||
256 | if (keycode == chord) return true; | ||
257 | } | ||
258 | return false; | ||
259 | } | ||
260 | |||
261 | void TestFrame::WaitForChord(const std::vector<int>& chord) { | ||
262 | for (int retry = 1000; retry && !IsChord(chord); --retry) RelaxForMs(); | ||
263 | } | ||
264 | |||
265 | std::string TestFrame::Format(const std::vector<const std::vector<int>>& chords) { | ||
266 | std::string ret("{"); | ||
267 | if (!chords.empty()) { | ||
268 | ret += android::base::Join(chords.front(), ' '); | ||
269 | for (auto it = std::next(chords.begin()); it != chords.end(); ++it) { | ||
270 | ret += ','; | ||
271 | ret += android::base::Join(*it, ' '); | ||
272 | } | ||
273 | } | ||
274 | return ret + '}'; | ||
275 | } | ||
276 | |||
277 | std::string TestFrame::Format() const { | ||
278 | return Format(keycodes_); | ||
279 | } | ||
280 | |||
281 | } // namespace | ||
282 | |||
283 | TEST(keychords, not_instantiated) { | ||
284 | TestFrame test_frame(empty_chords); | ||
285 | EXPECT_TRUE(InitInotifyFds().size() == 0); | ||
286 | } | ||
287 | |||
288 | TEST(keychords, instantiated) { | ||
289 | // Test if a valid set of chords results in proper instantiation of the | ||
290 | // underlying mechanisms for /dev/input/ attachment. | ||
291 | TestFrame test_frame(chords); | ||
292 | EXPECT_TRUE(InitInotifyFds().size() != 0); | ||
293 | } | ||
294 | |||
295 | TEST(keychords, init_inotify) { | ||
296 | std::string before(InitInputFds()); | ||
297 | |||
298 | TestFrame test_frame(chords); | ||
299 | |||
300 | EventHandler ev; | ||
301 | EXPECT_TRUE(ev.init()); | ||
302 | |||
303 | for (int retry = 1000; retry && before == InitInputFds(); --retry) test_frame.RelaxForMs(); | ||
304 | std::string after(InitInputFds()); | ||
305 | EXPECT_NE(before, after); | ||
306 | } | ||
307 | |||
308 | TEST(keychords, key) { | ||
309 | EventHandler ev; | ||
310 | EXPECT_TRUE(ev.init()); | ||
311 | TestFrame test_frame(chords, &ev); | ||
312 | |||
313 | test_frame.SetChords(escape_chord); | ||
314 | test_frame.WaitForChord(escape_chord); | ||
315 | test_frame.ClrChords(escape_chord); | ||
316 | EXPECT_TRUE(test_frame.IsOnlyChord(escape_chord)) | ||
317 | << "expected only " << android::base::Join(escape_chord, ' ') << " got " | ||
318 | << test_frame.Format(); | ||
319 | } | ||
320 | |||
321 | TEST(keychords, keys_in_series) { | ||
322 | EventHandler ev; | ||
323 | EXPECT_TRUE(ev.init()); | ||
324 | TestFrame test_frame(chords, &ev); | ||
325 | |||
326 | for (auto& key : triple1_chord) { | ||
327 | test_frame.SetChord(key); | ||
328 | test_frame.ClrChord(key); | ||
329 | } | ||
330 | test_frame.WaitForChord(triple1_chord); | ||
331 | EXPECT_TRUE(test_frame.IsNoChord()) << "expected nothing got " << test_frame.Format(); | ||
332 | } | ||
333 | |||
334 | TEST(keychords, keys_in_parallel) { | ||
335 | EventHandler ev; | ||
336 | EXPECT_TRUE(ev.init()); | ||
337 | TestFrame test_frame(chords, &ev); | ||
338 | |||
339 | test_frame.SetChords(triple2_chord); | ||
340 | test_frame.WaitForChord(triple2_chord); | ||
341 | test_frame.ClrChords(triple2_chord); | ||
342 | EXPECT_TRUE(test_frame.IsOnlyChord(triple2_chord)) | ||
343 | << "expected only " << android::base::Join(triple2_chord, ' ') << " got " | ||
344 | << test_frame.Format(); | ||
345 | } | ||
346 | |||
347 | } // namespace init | ||
348 | } // namespace android | ||