test_device_msg_deserialize.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "common.h"
  2. #include <assert.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "device_msg.h"
  7. static void test_deserialize_clipboard(void) {
  8. const uint8_t input[] = {
  9. DEVICE_MSG_TYPE_CLIPBOARD,
  10. 0x00, 0x00, 0x00, 0x03, // text length
  11. 0x41, 0x42, 0x43, // "ABC"
  12. };
  13. struct sc_device_msg msg;
  14. ssize_t r = sc_device_msg_deserialize(input, sizeof(input), &msg);
  15. assert(r == 8);
  16. assert(msg.type == DEVICE_MSG_TYPE_CLIPBOARD);
  17. assert(msg.clipboard.text);
  18. assert(!strcmp("ABC", msg.clipboard.text));
  19. sc_device_msg_destroy(&msg);
  20. }
  21. static void test_deserialize_clipboard_big(void) {
  22. uint8_t input[DEVICE_MSG_MAX_SIZE];
  23. input[0] = DEVICE_MSG_TYPE_CLIPBOARD;
  24. input[1] = (DEVICE_MSG_TEXT_MAX_LENGTH & 0xff000000u) >> 24;
  25. input[2] = (DEVICE_MSG_TEXT_MAX_LENGTH & 0x00ff0000u) >> 16;
  26. input[3] = (DEVICE_MSG_TEXT_MAX_LENGTH & 0x0000ff00u) >> 8;
  27. input[4] = DEVICE_MSG_TEXT_MAX_LENGTH & 0x000000ffu;
  28. memset(input + 5, 'a', DEVICE_MSG_TEXT_MAX_LENGTH);
  29. struct sc_device_msg msg;
  30. ssize_t r = sc_device_msg_deserialize(input, sizeof(input), &msg);
  31. assert(r == DEVICE_MSG_MAX_SIZE);
  32. assert(msg.type == DEVICE_MSG_TYPE_CLIPBOARD);
  33. assert(msg.clipboard.text);
  34. assert(strlen(msg.clipboard.text) == DEVICE_MSG_TEXT_MAX_LENGTH);
  35. assert(msg.clipboard.text[0] == 'a');
  36. sc_device_msg_destroy(&msg);
  37. }
  38. static void test_deserialize_ack_set_clipboard(void) {
  39. const uint8_t input[] = {
  40. DEVICE_MSG_TYPE_ACK_CLIPBOARD,
  41. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // sequence
  42. };
  43. struct sc_device_msg msg;
  44. ssize_t r = sc_device_msg_deserialize(input, sizeof(input), &msg);
  45. assert(r == 9);
  46. assert(msg.type == DEVICE_MSG_TYPE_ACK_CLIPBOARD);
  47. assert(msg.ack_clipboard.sequence == UINT64_C(0x0102030405060708));
  48. }
  49. static void test_deserialize_uhid_output(void) {
  50. const uint8_t input[] = {
  51. DEVICE_MSG_TYPE_UHID_OUTPUT,
  52. 0, 42, // id
  53. 0, 5, // size
  54. 0x01, 0x02, 0x03, 0x04, 0x05, // data
  55. };
  56. struct sc_device_msg msg;
  57. ssize_t r = sc_device_msg_deserialize(input, sizeof(input), &msg);
  58. assert(r == 10);
  59. assert(msg.type == DEVICE_MSG_TYPE_UHID_OUTPUT);
  60. assert(msg.uhid_output.id == 42);
  61. assert(msg.uhid_output.size == 5);
  62. uint8_t expected[] = {1, 2, 3, 4, 5};
  63. assert(!memcmp(msg.uhid_output.data, expected, sizeof(expected)));
  64. sc_device_msg_destroy(&msg);
  65. }
  66. int main(int argc, char *argv[]) {
  67. (void) argc;
  68. (void) argv;
  69. test_deserialize_clipboard();
  70. test_deserialize_clipboard_big();
  71. test_deserialize_ack_set_clipboard();
  72. test_deserialize_uhid_output();
  73. return 0;
  74. }