| 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 |  | 
| Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 37 | CHECK_OFFSET(InputMessage::Body::Key, eventId, 0); | 
| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 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 |  | 
| Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 51 | CHECK_OFFSET(InputMessage::Body::Motion, eventId, 0); | 
| Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 52 | CHECK_OFFSET(InputMessage::Body::Motion, empty1, 4); | 
| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 53 | CHECK_OFFSET(InputMessage::Body::Motion, eventTime, 8); | 
|  | 54 | CHECK_OFFSET(InputMessage::Body::Motion, deviceId, 16); | 
|  | 55 | CHECK_OFFSET(InputMessage::Body::Motion, source, 20); | 
| Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 56 | CHECK_OFFSET(InputMessage::Body::Motion, displayId, 24); | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 57 | CHECK_OFFSET(InputMessage::Body::Motion, hmac, 28); | 
|  | 58 | CHECK_OFFSET(InputMessage::Body::Motion, action, 60); | 
|  | 59 | CHECK_OFFSET(InputMessage::Body::Motion, actionButton, 64); | 
|  | 60 | CHECK_OFFSET(InputMessage::Body::Motion, flags, 68); | 
|  | 61 | CHECK_OFFSET(InputMessage::Body::Motion, metaState, 72); | 
|  | 62 | CHECK_OFFSET(InputMessage::Body::Motion, buttonState, 76); | 
|  | 63 | CHECK_OFFSET(InputMessage::Body::Motion, classification, 80); | 
| Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 64 | CHECK_OFFSET(InputMessage::Body::Motion, empty2, 81); | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 65 | CHECK_OFFSET(InputMessage::Body::Motion, edgeFlags, 84); | 
|  | 66 | CHECK_OFFSET(InputMessage::Body::Motion, downTime, 88); | 
| chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 67 | CHECK_OFFSET(InputMessage::Body::Motion, dsdx, 96); | 
|  | 68 | CHECK_OFFSET(InputMessage::Body::Motion, dtdx, 100); | 
|  | 69 | CHECK_OFFSET(InputMessage::Body::Motion, dtdy, 104); | 
|  | 70 | CHECK_OFFSET(InputMessage::Body::Motion, dsdy, 108); | 
|  | 71 | CHECK_OFFSET(InputMessage::Body::Motion, tx, 112); | 
|  | 72 | CHECK_OFFSET(InputMessage::Body::Motion, ty, 116); | 
|  | 73 | CHECK_OFFSET(InputMessage::Body::Motion, xPrecision, 120); | 
|  | 74 | CHECK_OFFSET(InputMessage::Body::Motion, yPrecision, 124); | 
|  | 75 | CHECK_OFFSET(InputMessage::Body::Motion, xCursorPosition, 128); | 
|  | 76 | CHECK_OFFSET(InputMessage::Body::Motion, yCursorPosition, 132); | 
| Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 77 | CHECK_OFFSET(InputMessage::Body::Motion, displayWidth, 136); | 
|  | 78 | CHECK_OFFSET(InputMessage::Body::Motion, displayHeight, 140); | 
|  | 79 | CHECK_OFFSET(InputMessage::Body::Motion, pointerCount, 144); | 
|  | 80 | CHECK_OFFSET(InputMessage::Body::Motion, empty3, 148); | 
|  | 81 | CHECK_OFFSET(InputMessage::Body::Motion, pointers, 152); | 
| Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 82 |  | 
| Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 83 | CHECK_OFFSET(InputMessage::Body::Focus, eventId, 0); | 
|  | 84 | CHECK_OFFSET(InputMessage::Body::Focus, hasFocus, 4); | 
| Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 85 | CHECK_OFFSET(InputMessage::Body::Focus, inTouchMode, 5); | 
|  | 86 | CHECK_OFFSET(InputMessage::Body::Focus, empty, 6); | 
| Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 87 |  | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 88 | CHECK_OFFSET(InputMessage::Body::Capture, eventId, 0); | 
|  | 89 | CHECK_OFFSET(InputMessage::Body::Capture, pointerCaptureEnabled, 4); | 
| Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 90 | CHECK_OFFSET(InputMessage::Body::Capture, empty, 5); | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 91 |  | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 92 | CHECK_OFFSET(InputMessage::Body::Drag, eventId, 0); | 
|  | 93 | CHECK_OFFSET(InputMessage::Body::Drag, x, 4); | 
|  | 94 | CHECK_OFFSET(InputMessage::Body::Drag, y, 8); | 
|  | 95 | CHECK_OFFSET(InputMessage::Body::Drag, isExiting, 12); | 
|  | 96 | CHECK_OFFSET(InputMessage::Body::Drag, empty, 13); | 
|  | 97 |  | 
| Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 98 | CHECK_OFFSET(InputMessage::Body::Finished, handled, 0); | 
|  | 99 | CHECK_OFFSET(InputMessage::Body::Finished, empty, 1); | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 100 | CHECK_OFFSET(InputMessage::Body::Finished, consumeTime, 8); | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 101 |  | 
|  | 102 | CHECK_OFFSET(InputMessage::Body::Timeline, eventId, 0); | 
|  | 103 | CHECK_OFFSET(InputMessage::Body::Timeline, empty, 4); | 
|  | 104 | CHECK_OFFSET(InputMessage::Body::Timeline, graphicsTimeline, 8); | 
| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 107 | void TestHeaderSize() { | 
| Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 108 | CHECK_OFFSET(InputMessage::Header, type, 0); | 
|  | 109 | CHECK_OFFSET(InputMessage::Header, seq, 4); | 
| Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 110 | static_assert(sizeof(InputMessage::Header) == 8); | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | /** | 
|  | 114 | * We cannot use the Body::size() method here because it is not static for | 
|  | 115 | * the Motion type, where "pointerCount" variable affects the size and can change at runtime. | 
|  | 116 | */ | 
|  | 117 | void TestBodySize() { | 
| Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 118 | static_assert(sizeof(InputMessage::Body::Key) == 96); | 
| Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 119 | static_assert(sizeof(InputMessage::Body::Motion) == | 
|  | 120 | offsetof(InputMessage::Body::Motion, pointers) + | 
|  | 121 | sizeof(InputMessage::Body::Motion::Pointer) * MAX_POINTERS); | 
| Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 122 | static_assert(sizeof(InputMessage::Body::Finished) == 16); | 
| Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 123 | static_assert(sizeof(InputMessage::Body::Focus) == 8); | 
| Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 124 | static_assert(sizeof(InputMessage::Body::Capture) == 8); | 
| arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 125 | static_assert(sizeof(InputMessage::Body::Drag) == 16); | 
| Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 126 | // Timeline | 
|  | 127 | static_assert(GraphicsTimeline::SIZE == 2); | 
|  | 128 | static_assert(sizeof(InputMessage::Body::Timeline) == 24); | 
| Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Siarhei Vishniakou | 54d3e18 | 2020-01-15 17:38:38 -0800 | [diff] [blame] | 131 | // --- VerifiedInputEvent --- | 
|  | 132 | // Ensure that VerifiedInputEvent, VerifiedKeyEvent, VerifiedMotionEvent are packed. | 
|  | 133 | // We will treat them as byte collections when signing them. There should not be any uninitialized | 
|  | 134 | // data in-between fields. Otherwise, the padded data will affect the hmac value and verifications | 
|  | 135 | // will fail. | 
|  | 136 |  | 
|  | 137 | void TestVerifiedEventSize() { | 
|  | 138 | // VerifiedInputEvent | 
|  | 139 | constexpr size_t VERIFIED_INPUT_EVENT_SIZE = sizeof(VerifiedInputEvent::type) + | 
|  | 140 | sizeof(VerifiedInputEvent::deviceId) + sizeof(VerifiedInputEvent::eventTimeNanos) + | 
|  | 141 | sizeof(VerifiedInputEvent::source) + sizeof(VerifiedInputEvent::displayId); | 
|  | 142 | static_assert(sizeof(VerifiedInputEvent) == VERIFIED_INPUT_EVENT_SIZE); | 
|  | 143 |  | 
|  | 144 | // VerifiedKeyEvent | 
|  | 145 | constexpr size_t VERIFIED_KEY_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE + | 
|  | 146 | sizeof(VerifiedKeyEvent::action) + sizeof(VerifiedKeyEvent::downTimeNanos) + | 
|  | 147 | sizeof(VerifiedKeyEvent::flags) + sizeof(VerifiedKeyEvent::keyCode) + | 
|  | 148 | sizeof(VerifiedKeyEvent::scanCode) + sizeof(VerifiedKeyEvent::metaState) + | 
|  | 149 | sizeof(VerifiedKeyEvent::repeatCount); | 
|  | 150 | static_assert(sizeof(VerifiedKeyEvent) == VERIFIED_KEY_EVENT_SIZE); | 
|  | 151 |  | 
|  | 152 | // VerifiedMotionEvent | 
|  | 153 | constexpr size_t VERIFIED_MOTION_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE + | 
|  | 154 | sizeof(VerifiedMotionEvent::rawX) + sizeof(VerifiedMotionEvent::rawY) + | 
|  | 155 | sizeof(VerifiedMotionEvent::actionMasked) + sizeof(VerifiedMotionEvent::downTimeNanos) + | 
|  | 156 | sizeof(VerifiedMotionEvent::flags) + sizeof(VerifiedMotionEvent::metaState) + | 
|  | 157 | sizeof(VerifiedMotionEvent::buttonState); | 
|  | 158 | static_assert(sizeof(VerifiedMotionEvent) == VERIFIED_MOTION_EVENT_SIZE); | 
|  | 159 | } | 
|  | 160 |  | 
| Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 161 | } // namespace android |