test_control_msg_serialize.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include "common.h"
  2. #include <assert.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include "control_msg.h"
  6. static void test_serialize_inject_keycode(void) {
  7. struct sc_control_msg msg = {
  8. .type = SC_CONTROL_MSG_TYPE_INJECT_KEYCODE,
  9. .inject_keycode = {
  10. .action = AKEY_EVENT_ACTION_UP,
  11. .keycode = AKEYCODE_ENTER,
  12. .repeat = 5,
  13. .metastate = AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON,
  14. },
  15. };
  16. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  17. size_t size = sc_control_msg_serialize(&msg, buf);
  18. assert(size == 14);
  19. const uint8_t expected[] = {
  20. SC_CONTROL_MSG_TYPE_INJECT_KEYCODE,
  21. 0x01, // AKEY_EVENT_ACTION_UP
  22. 0x00, 0x00, 0x00, 0x42, // AKEYCODE_ENTER
  23. 0x00, 0x00, 0x00, 0X05, // repeat
  24. 0x00, 0x00, 0x00, 0x41, // AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON
  25. };
  26. assert(!memcmp(buf, expected, sizeof(expected)));
  27. }
  28. static void test_serialize_inject_text(void) {
  29. struct sc_control_msg msg = {
  30. .type = SC_CONTROL_MSG_TYPE_INJECT_TEXT,
  31. .inject_text = {
  32. .text = "hello, world!",
  33. },
  34. };
  35. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  36. size_t size = sc_control_msg_serialize(&msg, buf);
  37. assert(size == 18);
  38. const uint8_t expected[] = {
  39. SC_CONTROL_MSG_TYPE_INJECT_TEXT,
  40. 0x00, 0x00, 0x00, 0x0d, // text length
  41. 'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // text
  42. };
  43. assert(!memcmp(buf, expected, sizeof(expected)));
  44. }
  45. static void test_serialize_inject_text_long(void) {
  46. struct sc_control_msg msg;
  47. msg.type = SC_CONTROL_MSG_TYPE_INJECT_TEXT;
  48. char text[SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH + 1];
  49. memset(text, 'a', SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
  50. text[SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH] = '\0';
  51. msg.inject_text.text = text;
  52. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  53. size_t size = sc_control_msg_serialize(&msg, buf);
  54. assert(size == 5 + SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
  55. uint8_t expected[5 + SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH];
  56. expected[0] = SC_CONTROL_MSG_TYPE_INJECT_TEXT;
  57. expected[1] = 0x00;
  58. expected[2] = 0x00;
  59. expected[3] = 0x01;
  60. expected[4] = 0x2c; // text length (32 bits)
  61. memset(&expected[5], 'a', SC_CONTROL_MSG_INJECT_TEXT_MAX_LENGTH);
  62. assert(!memcmp(buf, expected, sizeof(expected)));
  63. }
  64. static void test_serialize_inject_touch_event(void) {
  65. struct sc_control_msg msg = {
  66. .type = SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
  67. .inject_touch_event = {
  68. .action = AMOTION_EVENT_ACTION_DOWN,
  69. .pointer_id = UINT64_C(0x1234567887654321),
  70. .position = {
  71. .point = {
  72. .x = 100,
  73. .y = 200,
  74. },
  75. .screen_size = {
  76. .width = 1080,
  77. .height = 1920,
  78. },
  79. },
  80. .pressure = 1.0f,
  81. .action_button = AMOTION_EVENT_BUTTON_PRIMARY,
  82. .buttons = AMOTION_EVENT_BUTTON_PRIMARY,
  83. },
  84. };
  85. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  86. size_t size = sc_control_msg_serialize(&msg, buf);
  87. assert(size == 32);
  88. const uint8_t expected[] = {
  89. SC_CONTROL_MSG_TYPE_INJECT_TOUCH_EVENT,
  90. 0x00, // AKEY_EVENT_ACTION_DOWN
  91. 0x12, 0x34, 0x56, 0x78, 0x87, 0x65, 0x43, 0x21, // pointer id
  92. 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0xc8, // 100 200
  93. 0x04, 0x38, 0x07, 0x80, // 1080 1920
  94. 0xff, 0xff, // pressure
  95. 0x00, 0x00, 0x00, 0x01, // AMOTION_EVENT_BUTTON_PRIMARY (action button)
  96. 0x00, 0x00, 0x00, 0x01, // AMOTION_EVENT_BUTTON_PRIMARY (buttons)
  97. };
  98. assert(!memcmp(buf, expected, sizeof(expected)));
  99. }
  100. static void test_serialize_inject_scroll_event(void) {
  101. struct sc_control_msg msg = {
  102. .type = SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
  103. .inject_scroll_event = {
  104. .position = {
  105. .point = {
  106. .x = 260,
  107. .y = 1026,
  108. },
  109. .screen_size = {
  110. .width = 1080,
  111. .height = 1920,
  112. },
  113. },
  114. .hscroll = 1,
  115. .vscroll = -1,
  116. .buttons = 1,
  117. },
  118. };
  119. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  120. size_t size = sc_control_msg_serialize(&msg, buf);
  121. assert(size == 21);
  122. const uint8_t expected[] = {
  123. SC_CONTROL_MSG_TYPE_INJECT_SCROLL_EVENT,
  124. 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x04, 0x02, // 260 1026
  125. 0x04, 0x38, 0x07, 0x80, // 1080 1920
  126. 0x7F, 0xFF, // 1 (float encoded as i16)
  127. 0x80, 0x00, // -1 (float encoded as i16)
  128. 0x00, 0x00, 0x00, 0x01, // 1
  129. };
  130. assert(!memcmp(buf, expected, sizeof(expected)));
  131. }
  132. static void test_serialize_back_or_screen_on(void) {
  133. struct sc_control_msg msg = {
  134. .type = SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
  135. .back_or_screen_on = {
  136. .action = AKEY_EVENT_ACTION_UP,
  137. },
  138. };
  139. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  140. size_t size = sc_control_msg_serialize(&msg, buf);
  141. assert(size == 2);
  142. const uint8_t expected[] = {
  143. SC_CONTROL_MSG_TYPE_BACK_OR_SCREEN_ON,
  144. 0x01, // AKEY_EVENT_ACTION_UP
  145. };
  146. assert(!memcmp(buf, expected, sizeof(expected)));
  147. }
  148. static void test_serialize_expand_notification_panel(void) {
  149. struct sc_control_msg msg = {
  150. .type = SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
  151. };
  152. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  153. size_t size = sc_control_msg_serialize(&msg, buf);
  154. assert(size == 1);
  155. const uint8_t expected[] = {
  156. SC_CONTROL_MSG_TYPE_EXPAND_NOTIFICATION_PANEL,
  157. };
  158. assert(!memcmp(buf, expected, sizeof(expected)));
  159. }
  160. static void test_serialize_expand_settings_panel(void) {
  161. struct sc_control_msg msg = {
  162. .type = SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL,
  163. };
  164. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  165. size_t size = sc_control_msg_serialize(&msg, buf);
  166. assert(size == 1);
  167. const uint8_t expected[] = {
  168. SC_CONTROL_MSG_TYPE_EXPAND_SETTINGS_PANEL,
  169. };
  170. assert(!memcmp(buf, expected, sizeof(expected)));
  171. }
  172. static void test_serialize_collapse_panels(void) {
  173. struct sc_control_msg msg = {
  174. .type = SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS,
  175. };
  176. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  177. size_t size = sc_control_msg_serialize(&msg, buf);
  178. assert(size == 1);
  179. const uint8_t expected[] = {
  180. SC_CONTROL_MSG_TYPE_COLLAPSE_PANELS,
  181. };
  182. assert(!memcmp(buf, expected, sizeof(expected)));
  183. }
  184. static void test_serialize_get_clipboard(void) {
  185. struct sc_control_msg msg = {
  186. .type = SC_CONTROL_MSG_TYPE_GET_CLIPBOARD,
  187. .get_clipboard = {
  188. .copy_key = SC_COPY_KEY_COPY,
  189. },
  190. };
  191. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  192. size_t size = sc_control_msg_serialize(&msg, buf);
  193. assert(size == 2);
  194. const uint8_t expected[] = {
  195. SC_CONTROL_MSG_TYPE_GET_CLIPBOARD,
  196. SC_COPY_KEY_COPY,
  197. };
  198. assert(!memcmp(buf, expected, sizeof(expected)));
  199. }
  200. static void test_serialize_set_clipboard(void) {
  201. struct sc_control_msg msg = {
  202. .type = SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
  203. .set_clipboard = {
  204. .sequence = UINT64_C(0x0102030405060708),
  205. .paste = true,
  206. .text = "hello, world!",
  207. },
  208. };
  209. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  210. size_t size = sc_control_msg_serialize(&msg, buf);
  211. assert(size == 27);
  212. const uint8_t expected[] = {
  213. SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
  214. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
  215. 1, // paste
  216. 0x00, 0x00, 0x00, 0x0d, // text length
  217. 'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', // text
  218. };
  219. assert(!memcmp(buf, expected, sizeof(expected)));
  220. }
  221. static void test_serialize_set_clipboard_long(void) {
  222. struct sc_control_msg msg = {
  223. .type = SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
  224. .set_clipboard = {
  225. .sequence = UINT64_C(0x0102030405060708),
  226. .paste = true,
  227. .text = NULL,
  228. },
  229. };
  230. char text[SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH + 1];
  231. memset(text, 'a', SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
  232. text[SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH] = '\0';
  233. msg.set_clipboard.text = text;
  234. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  235. size_t size = sc_control_msg_serialize(&msg, buf);
  236. assert(size == SC_CONTROL_MSG_MAX_SIZE);
  237. uint8_t expected[SC_CONTROL_MSG_MAX_SIZE] = {
  238. SC_CONTROL_MSG_TYPE_SET_CLIPBOARD,
  239. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
  240. 1, // paste
  241. // text length
  242. SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 24,
  243. (SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 16) & 0xff,
  244. (SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH >> 8) & 0xff,
  245. SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH & 0xff,
  246. };
  247. memset(expected + 14, 'a', SC_CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH);
  248. assert(!memcmp(buf, expected, sizeof(expected)));
  249. }
  250. static void test_serialize_set_screen_power_mode(void) {
  251. struct sc_control_msg msg = {
  252. .type = SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
  253. .set_screen_power_mode = {
  254. .mode = SC_SCREEN_POWER_MODE_NORMAL,
  255. },
  256. };
  257. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  258. size_t size = sc_control_msg_serialize(&msg, buf);
  259. assert(size == 2);
  260. const uint8_t expected[] = {
  261. SC_CONTROL_MSG_TYPE_SET_SCREEN_POWER_MODE,
  262. 0x02, // SC_SCREEN_POWER_MODE_NORMAL
  263. };
  264. assert(!memcmp(buf, expected, sizeof(expected)));
  265. }
  266. static void test_serialize_rotate_device(void) {
  267. struct sc_control_msg msg = {
  268. .type = SC_CONTROL_MSG_TYPE_ROTATE_DEVICE,
  269. };
  270. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  271. size_t size = sc_control_msg_serialize(&msg, buf);
  272. assert(size == 1);
  273. const uint8_t expected[] = {
  274. SC_CONTROL_MSG_TYPE_ROTATE_DEVICE,
  275. };
  276. assert(!memcmp(buf, expected, sizeof(expected)));
  277. }
  278. static void test_serialize_uhid_create(void) {
  279. const uint8_t report_desc[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  280. struct sc_control_msg msg = {
  281. .type = SC_CONTROL_MSG_TYPE_UHID_CREATE,
  282. .uhid_create = {
  283. .id = 42,
  284. .name = "ABC",
  285. .report_desc_size = sizeof(report_desc),
  286. .report_desc = report_desc,
  287. },
  288. };
  289. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  290. size_t size = sc_control_msg_serialize(&msg, buf);
  291. assert(size == 20);
  292. const uint8_t expected[] = {
  293. SC_CONTROL_MSG_TYPE_UHID_CREATE,
  294. 0, 42, // id
  295. 3, // name size
  296. 65, 66, 67, // "ABC"
  297. 0, 11, // report desc size
  298. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  299. };
  300. assert(!memcmp(buf, expected, sizeof(expected)));
  301. }
  302. static void test_serialize_uhid_input(void) {
  303. struct sc_control_msg msg = {
  304. .type = SC_CONTROL_MSG_TYPE_UHID_INPUT,
  305. .uhid_input = {
  306. .id = 42,
  307. .size = 5,
  308. .data = {1, 2, 3, 4, 5},
  309. },
  310. };
  311. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  312. size_t size = sc_control_msg_serialize(&msg, buf);
  313. assert(size == 10);
  314. const uint8_t expected[] = {
  315. SC_CONTROL_MSG_TYPE_UHID_INPUT,
  316. 0, 42, // id
  317. 0, 5, // size
  318. 1, 2, 3, 4, 5,
  319. };
  320. assert(!memcmp(buf, expected, sizeof(expected)));
  321. }
  322. static void test_serialize_uhid_destroy(void) {
  323. struct sc_control_msg msg = {
  324. .type = SC_CONTROL_MSG_TYPE_UHID_DESTROY,
  325. .uhid_destroy = {
  326. .id = 42,
  327. },
  328. };
  329. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  330. size_t size = sc_control_msg_serialize(&msg, buf);
  331. assert(size == 3);
  332. const uint8_t expected[] = {
  333. SC_CONTROL_MSG_TYPE_UHID_DESTROY,
  334. 0, 42, // id
  335. };
  336. assert(!memcmp(buf, expected, sizeof(expected)));
  337. }
  338. static void test_serialize_open_hard_keyboard(void) {
  339. struct sc_control_msg msg = {
  340. .type = SC_CONTROL_MSG_TYPE_OPEN_HARD_KEYBOARD_SETTINGS,
  341. };
  342. uint8_t buf[SC_CONTROL_MSG_MAX_SIZE];
  343. size_t size = sc_control_msg_serialize(&msg, buf);
  344. assert(size == 1);
  345. const uint8_t expected[] = {
  346. SC_CONTROL_MSG_TYPE_OPEN_HARD_KEYBOARD_SETTINGS,
  347. };
  348. assert(!memcmp(buf, expected, sizeof(expected)));
  349. }
  350. int main(int argc, char *argv[]) {
  351. (void) argc;
  352. (void) argv;
  353. test_serialize_inject_keycode();
  354. test_serialize_inject_text();
  355. test_serialize_inject_text_long();
  356. test_serialize_inject_touch_event();
  357. test_serialize_inject_scroll_event();
  358. test_serialize_back_or_screen_on();
  359. test_serialize_expand_notification_panel();
  360. test_serialize_expand_settings_panel();
  361. test_serialize_collapse_panels();
  362. test_serialize_get_clipboard();
  363. test_serialize_set_clipboard();
  364. test_serialize_set_clipboard_long();
  365. test_serialize_set_screen_power_mode();
  366. test_serialize_rotate_device();
  367. test_serialize_uhid_create();
  368. test_serialize_uhid_input();
  369. test_serialize_uhid_destroy();
  370. test_serialize_open_hard_keyboard();
  371. return 0;
  372. }