| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2014 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 <input/InputTransport.h> | 
|  | 18 | #include <input/Input.h> | 
|  | 19 |  | 
|  | 20 | namespace android { | 
|  | 21 |  | 
|  | 22 | #define CHECK_OFFSET(type, member, expected_offset) \ | 
| Chih-Hung Hsieh | 4a186d4 | 2016-05-20 11:33:26 -0700 | [diff] [blame] | 23 | static_assert((offsetof(type, member) == (expected_offset)), "") | 
| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 24 |  | 
|  | 25 | struct Foo { | 
|  | 26 | uint32_t dummy; | 
|  | 27 | PointerCoords coords; | 
|  | 28 | }; | 
|  | 29 |  | 
|  | 30 | void TestPointerCoordsAlignment() { | 
|  | 31 | CHECK_OFFSET(Foo, coords, 8); | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | void TestInputMessageAlignment() { | 
|  | 35 | CHECK_OFFSET(InputMessage, body, 8); | 
|  | 36 |  | 
|  | 37 | CHECK_OFFSET(InputMessage::Body::Key, seq, 0); | 
|  | 38 | CHECK_OFFSET(InputMessage::Body::Key, eventTime, 8); | 
|  | 39 | CHECK_OFFSET(InputMessage::Body::Key, deviceId, 16); | 
|  | 40 | CHECK_OFFSET(InputMessage::Body::Key, source, 20); | 
| Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 41 | CHECK_OFFSET(InputMessage::Body::Key, displayId, 24); | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 42 | CHECK_OFFSET(InputMessage::Body::Key, hmac, 28); | 
|  | 43 | CHECK_OFFSET(InputMessage::Body::Key, action, 60); | 
|  | 44 | CHECK_OFFSET(InputMessage::Body::Key, flags, 64); | 
|  | 45 | CHECK_OFFSET(InputMessage::Body::Key, keyCode, 68); | 
|  | 46 | CHECK_OFFSET(InputMessage::Body::Key, scanCode, 72); | 
|  | 47 | CHECK_OFFSET(InputMessage::Body::Key, metaState, 76); | 
|  | 48 | CHECK_OFFSET(InputMessage::Body::Key, repeatCount, 80); | 
|  | 49 | CHECK_OFFSET(InputMessage::Body::Key, downTime, 88); | 
| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 50 |  | 
|  | 51 | CHECK_OFFSET(InputMessage::Body::Motion, seq, 0); | 
|  | 52 | CHECK_OFFSET(InputMessage::Body::Motion, eventTime, 8); | 
|  | 53 | CHECK_OFFSET(InputMessage::Body::Motion, deviceId, 16); | 
|  | 54 | CHECK_OFFSET(InputMessage::Body::Motion, source, 20); | 
| Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 55 | CHECK_OFFSET(InputMessage::Body::Motion, displayId, 24); | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 56 | CHECK_OFFSET(InputMessage::Body::Motion, hmac, 28); | 
|  | 57 | CHECK_OFFSET(InputMessage::Body::Motion, action, 60); | 
|  | 58 | CHECK_OFFSET(InputMessage::Body::Motion, actionButton, 64); | 
|  | 59 | CHECK_OFFSET(InputMessage::Body::Motion, flags, 68); | 
|  | 60 | CHECK_OFFSET(InputMessage::Body::Motion, metaState, 72); | 
|  | 61 | CHECK_OFFSET(InputMessage::Body::Motion, buttonState, 76); | 
|  | 62 | CHECK_OFFSET(InputMessage::Body::Motion, classification, 80); | 
|  | 63 | CHECK_OFFSET(InputMessage::Body::Motion, edgeFlags, 84); | 
|  | 64 | CHECK_OFFSET(InputMessage::Body::Motion, downTime, 88); | 
|  | 65 | CHECK_OFFSET(InputMessage::Body::Motion, xScale, 96); | 
|  | 66 | CHECK_OFFSET(InputMessage::Body::Motion, yScale, 100); | 
|  | 67 | CHECK_OFFSET(InputMessage::Body::Motion, xOffset, 104); | 
|  | 68 | CHECK_OFFSET(InputMessage::Body::Motion, yOffset, 108); | 
|  | 69 | CHECK_OFFSET(InputMessage::Body::Motion, xPrecision, 112); | 
|  | 70 | CHECK_OFFSET(InputMessage::Body::Motion, yPrecision, 116); | 
|  | 71 | CHECK_OFFSET(InputMessage::Body::Motion, xCursorPosition, 120); | 
|  | 72 | CHECK_OFFSET(InputMessage::Body::Motion, yCursorPosition, 124); | 
|  | 73 | CHECK_OFFSET(InputMessage::Body::Motion, pointerCount, 128); | 
|  | 74 | CHECK_OFFSET(InputMessage::Body::Motion, pointers, 136); | 
| Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 75 |  | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 76 | CHECK_OFFSET(InputMessage::Body::Focus, seq, 0); | 
|  | 77 | CHECK_OFFSET(InputMessage::Body::Focus, hasFocus, 4); | 
|  | 78 | CHECK_OFFSET(InputMessage::Body::Focus, inTouchMode, 6); | 
|  | 79 |  | 
| Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 80 | CHECK_OFFSET(InputMessage::Body::Finished, seq, 0); | 
|  | 81 | CHECK_OFFSET(InputMessage::Body::Finished, handled, 4); | 
| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 84 | void TestHeaderSize() { | 
|  | 85 | static_assert(sizeof(InputMessage::Header) == 8); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | /** | 
|  | 89 | * We cannot use the Body::size() method here because it is not static for | 
|  | 90 | * the Motion type, where "pointerCount" variable affects the size and can change at runtime. | 
|  | 91 | */ | 
|  | 92 | void TestBodySize() { | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 93 | static_assert(sizeof(InputMessage::Body::Key) == 96); | 
| Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 94 | static_assert(sizeof(InputMessage::Body::Motion) == | 
|  | 95 | offsetof(InputMessage::Body::Motion, pointers) + | 
|  | 96 | sizeof(InputMessage::Body::Motion::Pointer) * MAX_POINTERS); | 
|  | 97 | static_assert(sizeof(InputMessage::Body::Finished) == 8); | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 98 | static_assert(sizeof(InputMessage::Body::Focus) == 8); | 
| Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 101 | // --- VerifiedInputEvent --- | 
|  | 102 | // Ensure that VerifiedInputEvent, VerifiedKeyEvent, VerifiedMotionEvent are packed. | 
|  | 103 | // We will treat them as byte collections when signing them. There should not be any uninitialized | 
|  | 104 | // data in-between fields. Otherwise, the padded data will affect the hmac value and verifications | 
|  | 105 | // will fail. | 
|  | 106 |  | 
|  | 107 | void TestVerifiedEventSize() { | 
|  | 108 | // VerifiedInputEvent | 
|  | 109 | constexpr size_t VERIFIED_INPUT_EVENT_SIZE = sizeof(VerifiedInputEvent::type) + | 
|  | 110 | sizeof(VerifiedInputEvent::deviceId) + sizeof(VerifiedInputEvent::eventTimeNanos) + | 
|  | 111 | sizeof(VerifiedInputEvent::source) + sizeof(VerifiedInputEvent::displayId); | 
|  | 112 | static_assert(sizeof(VerifiedInputEvent) == VERIFIED_INPUT_EVENT_SIZE); | 
|  | 113 |  | 
|  | 114 | // VerifiedKeyEvent | 
|  | 115 | constexpr size_t VERIFIED_KEY_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE + | 
|  | 116 | sizeof(VerifiedKeyEvent::action) + sizeof(VerifiedKeyEvent::downTimeNanos) + | 
|  | 117 | sizeof(VerifiedKeyEvent::flags) + sizeof(VerifiedKeyEvent::keyCode) + | 
|  | 118 | sizeof(VerifiedKeyEvent::scanCode) + sizeof(VerifiedKeyEvent::metaState) + | 
|  | 119 | sizeof(VerifiedKeyEvent::repeatCount); | 
|  | 120 | static_assert(sizeof(VerifiedKeyEvent) == VERIFIED_KEY_EVENT_SIZE); | 
|  | 121 |  | 
|  | 122 | // VerifiedMotionEvent | 
|  | 123 | constexpr size_t VERIFIED_MOTION_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE + | 
|  | 124 | sizeof(VerifiedMotionEvent::rawX) + sizeof(VerifiedMotionEvent::rawY) + | 
|  | 125 | sizeof(VerifiedMotionEvent::actionMasked) + sizeof(VerifiedMotionEvent::downTimeNanos) + | 
|  | 126 | sizeof(VerifiedMotionEvent::flags) + sizeof(VerifiedMotionEvent::metaState) + | 
|  | 127 | sizeof(VerifiedMotionEvent::buttonState); | 
|  | 128 | static_assert(sizeof(VerifiedMotionEvent) == VERIFIED_MOTION_EVENT_SIZE); | 
|  | 129 | } | 
|  | 130 |  | 
| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 131 | } // namespace android |