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