blob: 36f87b8a6acd72b50102631812441833ccfea24f [file] [log] [blame]
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -08001/*
2 * Copyright (C) 2020 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
chaviw09c8d2d2020-08-24 15:48:26 -070017#include <attestation/HmacKeyManager.h>
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080018#include <gtest/gtest.h>
19#include <input/Input.h>
20
21namespace android {
22
23static KeyEvent getKeyEventWithFlags(int32_t flags) {
24 KeyEvent event;
Garfield Tan4cc839f2020-01-24 11:26:14 -080025 event.initialize(InputEvent::nextId(), 2 /*deviceId*/, AINPUT_SOURCE_GAMEPAD,
26 ADISPLAY_ID_DEFAULT, INVALID_HMAC, AKEY_EVENT_ACTION_DOWN, flags,
27 AKEYCODE_BUTTON_X, 121 /*scanCode*/, AMETA_ALT_ON, 1 /*repeatCount*/,
28 1000 /*downTime*/, 2000 /*eventTime*/);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080029 return event;
30}
31
32static MotionEvent getMotionEventWithFlags(int32_t flags) {
33 MotionEvent event;
34 constexpr size_t pointerCount = 1;
35 PointerProperties pointerProperties[pointerCount];
36 PointerCoords pointerCoords[pointerCount];
37 for (size_t i = 0; i < pointerCount; i++) {
38 pointerProperties[i].clear();
39 pointerProperties[i].id = i;
40 pointerCoords[i].clear();
41 }
42
chaviw9eaa22c2020-07-01 16:21:27 -070043 ui::Transform transform;
44 transform.set({2, 0, 4, 0, 3, 5, 0, 0, 1});
Garfield Tan4cc839f2020-01-24 11:26:14 -080045 event.initialize(InputEvent::nextId(), 0 /*deviceId*/, AINPUT_SOURCE_MOUSE, ADISPLAY_ID_DEFAULT,
46 INVALID_HMAC, AMOTION_EVENT_ACTION_DOWN, 0 /*actionButton*/, flags,
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080047 AMOTION_EVENT_EDGE_FLAG_NONE, AMETA_NONE, 0 /*buttonState*/,
chaviw9eaa22c2020-07-01 16:21:27 -070048 MotionClassification::NONE, transform, 0.1 /*xPrecision*/, 0.2 /*yPrecision*/,
49 280 /*xCursorPosition*/, 540 /*yCursorPosition*/, 100 /*downTime*/,
50 200 /*eventTime*/, pointerCount, pointerProperties, pointerCoords);
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -080051 return event;
52}
53
54TEST(VerifiedKeyEventTest, ConvertKeyEventToVerifiedKeyEvent) {
55 KeyEvent event = getKeyEventWithFlags(0);
56 VerifiedKeyEvent verified = verifiedKeyEventFromKeyEvent(event);
57
58 ASSERT_EQ(VerifiedInputEvent::Type::KEY, verified.type);
59
60 ASSERT_EQ(event.getDeviceId(), verified.deviceId);
61 ASSERT_EQ(event.getEventTime(), verified.eventTimeNanos);
62 ASSERT_EQ(event.getSource(), verified.source);
63 ASSERT_EQ(event.getDisplayId(), verified.displayId);
64
65 ASSERT_EQ(event.getAction(), verified.action);
66 ASSERT_EQ(event.getDownTime(), verified.downTimeNanos);
67 ASSERT_EQ(event.getFlags() & VERIFIED_KEY_EVENT_FLAGS, verified.flags);
68 ASSERT_EQ(event.getKeyCode(), verified.keyCode);
69 ASSERT_EQ(event.getScanCode(), verified.scanCode);
70 ASSERT_EQ(event.getMetaState(), verified.metaState);
71 ASSERT_EQ(event.getRepeatCount(), verified.repeatCount);
72}
73
74TEST(VerifiedKeyEventTest, VerifiedKeyEventContainsOnlyVerifiedFlags) {
75 KeyEvent event = getKeyEventWithFlags(AKEY_EVENT_FLAG_CANCELED | AKEY_EVENT_FLAG_FALLBACK);
76 VerifiedKeyEvent verified = verifiedKeyEventFromKeyEvent(event);
77 ASSERT_EQ(AKEY_EVENT_FLAG_CANCELED, verified.flags);
78}
79
80TEST(VerifiedKeyEventTest, VerifiedKeyEventDoesNotContainUnverifiedFlags) {
81 KeyEvent event = getKeyEventWithFlags(AKEY_EVENT_FLAG_EDITOR_ACTION);
82 VerifiedKeyEvent verified = verifiedKeyEventFromKeyEvent(event);
83 ASSERT_EQ(0, verified.flags);
84}
85
86TEST(VerifiedMotionEventTest, ConvertMotionEventToVerifiedMotionEvent) {
87 MotionEvent event = getMotionEventWithFlags(0);
88 VerifiedMotionEvent verified = verifiedMotionEventFromMotionEvent(event);
89
90 ASSERT_EQ(VerifiedInputEvent::Type::MOTION, verified.type);
91
92 ASSERT_EQ(event.getDeviceId(), verified.deviceId);
93 ASSERT_EQ(event.getEventTime(), verified.eventTimeNanos);
94 ASSERT_EQ(event.getSource(), verified.source);
95 ASSERT_EQ(event.getDisplayId(), verified.displayId);
96
97 ASSERT_EQ(event.getRawX(0), verified.rawX);
98 ASSERT_EQ(event.getRawY(0), verified.rawY);
99 ASSERT_EQ(event.getAction(), verified.actionMasked);
100 ASSERT_EQ(event.getDownTime(), verified.downTimeNanos);
101 ASSERT_EQ(event.getFlags() & VERIFIED_MOTION_EVENT_FLAGS, verified.flags);
102 ASSERT_EQ(event.getMetaState(), verified.metaState);
103 ASSERT_EQ(event.getButtonState(), verified.buttonState);
104}
105
106TEST(VerifiedMotionEventTest, VerifiedMotionEventContainsOnlyVerifiedFlags) {
107 MotionEvent event = getMotionEventWithFlags(AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED |
108 AMOTION_EVENT_FLAG_IS_GENERATED_GESTURE);
109 VerifiedMotionEvent verified = verifiedMotionEventFromMotionEvent(event);
110 ASSERT_EQ(AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED, verified.flags);
111}
112
113TEST(VerifiedMotionEventTest, VerifiedMotionEventDoesNotContainUnverifiedFlags) {
114 MotionEvent event = getMotionEventWithFlags(AMOTION_EVENT_FLAG_TAINTED);
115 VerifiedMotionEvent verified = verifiedMotionEventFromMotionEvent(event);
116 ASSERT_EQ(0, verified.flags);
117}
118
119} // namespace android