hidraw.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2022-2024 Red Hat */
  3. #include "hid_common.h"
  4. #include <linux/input.h>
  5. #include <string.h>
  6. #include <sys/ioctl.h>
  7. /* for older kernels */
  8. #ifndef HIDIOCREVOKE
  9. #define HIDIOCREVOKE _IOW('H', 0x0D, int) /* Revoke device access */
  10. #endif /* HIDIOCREVOKE */
  11. FIXTURE(hidraw) {
  12. struct uhid_device hid;
  13. int hidraw_fd;
  14. };
  15. static void close_hidraw(FIXTURE_DATA(hidraw) * self)
  16. {
  17. if (self->hidraw_fd)
  18. close(self->hidraw_fd);
  19. self->hidraw_fd = 0;
  20. }
  21. FIXTURE_TEARDOWN(hidraw) {
  22. void *uhid_err;
  23. uhid_destroy(_metadata, &self->hid);
  24. close_hidraw(self);
  25. pthread_join(self->hid.tid, &uhid_err);
  26. }
  27. #define TEARDOWN_LOG(fmt, ...) do { \
  28. TH_LOG(fmt, ##__VA_ARGS__); \
  29. hidraw_teardown(_metadata, self, variant); \
  30. } while (0)
  31. FIXTURE_SETUP(hidraw)
  32. {
  33. int err;
  34. err = setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a37, rdesc, sizeof(rdesc));
  35. ASSERT_OK(err);
  36. self->hidraw_fd = open_hidraw(&self->hid);
  37. ASSERT_GE(self->hidraw_fd, 0) TH_LOG("open_hidraw");
  38. }
  39. /*
  40. * A simple test to see if the fixture is working fine.
  41. * If this fails, none of the other tests will pass.
  42. */
  43. TEST_F(hidraw, test_create_uhid)
  44. {
  45. }
  46. /*
  47. * Inject one event in the uhid device,
  48. * check that we get the same data through hidraw
  49. */
  50. TEST_F(hidraw, raw_event)
  51. {
  52. __u8 buf[10] = {0};
  53. int err;
  54. /* inject one event */
  55. buf[0] = 1;
  56. buf[1] = 42;
  57. uhid_send_event(_metadata, &self->hid, buf, 6);
  58. /* read the data from hidraw */
  59. memset(buf, 0, sizeof(buf));
  60. err = read(self->hidraw_fd, buf, sizeof(buf));
  61. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  62. ASSERT_EQ(buf[0], 1);
  63. ASSERT_EQ(buf[1], 42);
  64. }
  65. /*
  66. * After initial opening/checks of hidraw, revoke the hidraw
  67. * node and check that we can not read any more data.
  68. */
  69. TEST_F(hidraw, raw_event_revoked)
  70. {
  71. __u8 buf[10] = {0};
  72. int err;
  73. /* inject one event */
  74. buf[0] = 1;
  75. buf[1] = 42;
  76. uhid_send_event(_metadata, &self->hid, buf, 6);
  77. /* read the data from hidraw */
  78. memset(buf, 0, sizeof(buf));
  79. err = read(self->hidraw_fd, buf, sizeof(buf));
  80. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  81. ASSERT_EQ(buf[0], 1);
  82. ASSERT_EQ(buf[1], 42);
  83. /* call the revoke ioctl */
  84. err = ioctl(self->hidraw_fd, HIDIOCREVOKE, NULL);
  85. ASSERT_OK(err) TH_LOG("couldn't revoke the hidraw fd");
  86. /* inject one other event */
  87. buf[0] = 1;
  88. buf[1] = 43;
  89. uhid_send_event(_metadata, &self->hid, buf, 6);
  90. /* read the data from hidraw */
  91. memset(buf, 0, sizeof(buf));
  92. err = read(self->hidraw_fd, buf, sizeof(buf));
  93. ASSERT_EQ(err, -1) TH_LOG("read_hidraw");
  94. ASSERT_EQ(errno, ENODEV) TH_LOG("unexpected error code while reading the hidraw node: %d",
  95. errno);
  96. }
  97. /*
  98. * Revoke the hidraw node and check that we can not do any ioctl.
  99. */
  100. TEST_F(hidraw, ioctl_revoked)
  101. {
  102. int err, desc_size = 0;
  103. /* call the revoke ioctl */
  104. err = ioctl(self->hidraw_fd, HIDIOCREVOKE, NULL);
  105. ASSERT_OK(err) TH_LOG("couldn't revoke the hidraw fd");
  106. /* do an ioctl */
  107. err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size);
  108. ASSERT_EQ(err, -1) TH_LOG("ioctl_hidraw");
  109. ASSERT_EQ(errno, ENODEV) TH_LOG("unexpected error code while doing an ioctl: %d",
  110. errno);
  111. }
  112. /*
  113. * Setup polling of the fd, and check that revoke works properly.
  114. */
  115. TEST_F(hidraw, poll_revoked)
  116. {
  117. struct pollfd pfds[1];
  118. __u8 buf[10] = {0};
  119. int err, ready;
  120. /* setup polling */
  121. pfds[0].fd = self->hidraw_fd;
  122. pfds[0].events = POLLIN;
  123. /* inject one event */
  124. buf[0] = 1;
  125. buf[1] = 42;
  126. uhid_send_event(_metadata, &self->hid, buf, 6);
  127. while (true) {
  128. ready = poll(pfds, 1, 5000);
  129. ASSERT_EQ(ready, 1) TH_LOG("poll return value");
  130. if (pfds[0].revents & POLLIN) {
  131. memset(buf, 0, sizeof(buf));
  132. err = read(self->hidraw_fd, buf, sizeof(buf));
  133. ASSERT_EQ(err, 6) TH_LOG("read_hidraw");
  134. ASSERT_EQ(buf[0], 1);
  135. ASSERT_EQ(buf[1], 42);
  136. /* call the revoke ioctl */
  137. err = ioctl(self->hidraw_fd, HIDIOCREVOKE, NULL);
  138. ASSERT_OK(err) TH_LOG("couldn't revoke the hidraw fd");
  139. } else {
  140. break;
  141. }
  142. }
  143. ASSERT_TRUE(pfds[0].revents & POLLHUP);
  144. }
  145. /*
  146. * After initial opening/checks of hidraw, revoke the hidraw
  147. * node and check that we can not read any more data.
  148. */
  149. TEST_F(hidraw, write_event_revoked)
  150. {
  151. struct timespec time_to_wait;
  152. __u8 buf[10] = {0};
  153. int err;
  154. /* inject one event from hidraw */
  155. buf[0] = 1; /* report ID */
  156. buf[1] = 2;
  157. buf[2] = 42;
  158. pthread_mutex_lock(&uhid_output_mtx);
  159. memset(output_report, 0, sizeof(output_report));
  160. clock_gettime(CLOCK_REALTIME, &time_to_wait);
  161. time_to_wait.tv_sec += 2;
  162. err = write(self->hidraw_fd, buf, 3);
  163. ASSERT_EQ(err, 3) TH_LOG("unexpected error while writing to hidraw node: %d", err);
  164. err = pthread_cond_timedwait(&uhid_output_cond, &uhid_output_mtx, &time_to_wait);
  165. ASSERT_OK(err) TH_LOG("error while calling waiting for the condition");
  166. ASSERT_EQ(output_report[0], 1);
  167. ASSERT_EQ(output_report[1], 2);
  168. ASSERT_EQ(output_report[2], 42);
  169. /* call the revoke ioctl */
  170. err = ioctl(self->hidraw_fd, HIDIOCREVOKE, NULL);
  171. ASSERT_OK(err) TH_LOG("couldn't revoke the hidraw fd");
  172. /* inject one other event */
  173. buf[0] = 1;
  174. buf[1] = 43;
  175. err = write(self->hidraw_fd, buf, 3);
  176. ASSERT_LT(err, 0) TH_LOG("unexpected success while writing to hidraw node: %d", err);
  177. ASSERT_EQ(errno, ENODEV) TH_LOG("unexpected error code while writing to hidraw node: %d",
  178. errno);
  179. pthread_mutex_unlock(&uhid_output_mtx);
  180. }
  181. /*
  182. * Test HIDIOCGRDESCSIZE ioctl to get report descriptor size
  183. */
  184. TEST_F(hidraw, ioctl_rdescsize)
  185. {
  186. int desc_size = 0;
  187. int err;
  188. /* call HIDIOCGRDESCSIZE ioctl */
  189. err = ioctl(self->hidraw_fd, HIDIOCGRDESCSIZE, &desc_size);
  190. ASSERT_EQ(err, 0) TH_LOG("HIDIOCGRDESCSIZE ioctl failed");
  191. /* verify the size matches our test report descriptor */
  192. ASSERT_EQ(desc_size, sizeof(rdesc))
  193. TH_LOG("expected size %zu, got %d", sizeof(rdesc), desc_size);
  194. }
  195. /*
  196. * Test HIDIOCGRDESC ioctl to get report descriptor data
  197. */
  198. TEST_F(hidraw, ioctl_rdesc)
  199. {
  200. struct hidraw_report_descriptor desc;
  201. int err;
  202. /* get the full report descriptor */
  203. desc.size = sizeof(rdesc);
  204. err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &desc);
  205. ASSERT_EQ(err, 0) TH_LOG("HIDIOCGRDESC ioctl failed");
  206. /* verify the descriptor data matches our test descriptor */
  207. ASSERT_EQ(memcmp(desc.value, rdesc, sizeof(rdesc)), 0)
  208. TH_LOG("report descriptor data mismatch");
  209. }
  210. /*
  211. * Test HIDIOCGRDESC ioctl with smaller buffer size
  212. */
  213. TEST_F(hidraw, ioctl_rdesc_small_buffer)
  214. {
  215. struct hidraw_report_descriptor desc;
  216. int err;
  217. size_t small_size = sizeof(rdesc) / 2; /* request half the descriptor size */
  218. /* get partial report descriptor */
  219. desc.size = small_size;
  220. err = ioctl(self->hidraw_fd, HIDIOCGRDESC, &desc);
  221. ASSERT_EQ(err, 0) TH_LOG("HIDIOCGRDESC ioctl failed with small buffer");
  222. /* verify we got the first part of the descriptor */
  223. ASSERT_EQ(memcmp(desc.value, rdesc, small_size), 0)
  224. TH_LOG("partial report descriptor data mismatch");
  225. }
  226. /*
  227. * Test HIDIOCGRAWINFO ioctl to get device information
  228. */
  229. TEST_F(hidraw, ioctl_rawinfo)
  230. {
  231. struct hidraw_devinfo devinfo;
  232. int err;
  233. /* get device info */
  234. err = ioctl(self->hidraw_fd, HIDIOCGRAWINFO, &devinfo);
  235. ASSERT_EQ(err, 0) TH_LOG("HIDIOCGRAWINFO ioctl failed");
  236. /* verify device info matches our test setup */
  237. ASSERT_EQ(devinfo.bustype, BUS_USB)
  238. TH_LOG("expected bustype 0x03, got 0x%x", devinfo.bustype);
  239. ASSERT_EQ(devinfo.vendor, 0x0001)
  240. TH_LOG("expected vendor 0x0001, got 0x%x", devinfo.vendor);
  241. ASSERT_EQ(devinfo.product, 0x0a37)
  242. TH_LOG("expected product 0x0a37, got 0x%x", devinfo.product);
  243. }
  244. /*
  245. * Test HIDIOCGFEATURE ioctl to get feature report
  246. */
  247. TEST_F(hidraw, ioctl_gfeature)
  248. {
  249. __u8 buf[10] = {0};
  250. int err;
  251. /* set report ID 1 in first byte */
  252. buf[0] = 1;
  253. /* get feature report */
  254. err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
  255. ASSERT_EQ(err, sizeof(feature_data)) TH_LOG("HIDIOCGFEATURE ioctl failed, got %d", err);
  256. /* verify we got the expected feature data */
  257. ASSERT_EQ(buf[0], feature_data[0])
  258. TH_LOG("expected feature_data[0] = %d, got %d", feature_data[0], buf[0]);
  259. ASSERT_EQ(buf[1], feature_data[1])
  260. TH_LOG("expected feature_data[1] = %d, got %d", feature_data[1], buf[1]);
  261. }
  262. /*
  263. * Test HIDIOCGFEATURE ioctl with invalid report ID
  264. */
  265. TEST_F(hidraw, ioctl_gfeature_invalid)
  266. {
  267. __u8 buf[10] = {0};
  268. int err;
  269. /* set invalid report ID (not 1) */
  270. buf[0] = 2;
  271. /* try to get feature report */
  272. err = ioctl(self->hidraw_fd, HIDIOCGFEATURE(sizeof(buf)), buf);
  273. ASSERT_LT(err, 0) TH_LOG("HIDIOCGFEATURE should have failed with invalid report ID");
  274. ASSERT_EQ(errno, EIO) TH_LOG("expected EIO, got errno %d", errno);
  275. }
  276. /*
  277. * Test ioctl with incorrect nr bits
  278. */
  279. TEST_F(hidraw, ioctl_invalid_nr)
  280. {
  281. char buf[256] = {0};
  282. int err;
  283. unsigned int bad_cmd;
  284. /*
  285. * craft an ioctl command with wrong _IOC_NR bits
  286. */
  287. bad_cmd = _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x00, sizeof(buf)); /* 0 is not valid */
  288. /* test the ioctl */
  289. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  290. ASSERT_LT(err, 0) TH_LOG("ioctl read-write with wrong _IOC_NR (0) should have failed");
  291. ASSERT_EQ(errno, ENOTTY)
  292. TH_LOG("expected ENOTTY for wrong read-write _IOC_NR (0), got errno %d", errno);
  293. /*
  294. * craft an ioctl command with wrong _IOC_NR bits
  295. */
  296. bad_cmd = _IOC(_IOC_READ, 'H', 0x00, sizeof(buf)); /* 0 is not valid */
  297. /* test the ioctl */
  298. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  299. ASSERT_LT(err, 0) TH_LOG("ioctl read-only with wrong _IOC_NR (0) should have failed");
  300. ASSERT_EQ(errno, ENOTTY)
  301. TH_LOG("expected ENOTTY for wrong read-only _IOC_NR (0), got errno %d", errno);
  302. /* also test with bigger number */
  303. bad_cmd = _IOC(_IOC_READ, 'H', 0x42, sizeof(buf)); /* 0x42 is not valid as well */
  304. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  305. ASSERT_LT(err, 0) TH_LOG("ioctl read-only with wrong _IOC_NR (0x42) should have failed");
  306. ASSERT_EQ(errno, ENOTTY)
  307. TH_LOG("expected ENOTTY for wrong read-only _IOC_NR (0x42), got errno %d", errno);
  308. /* also test with bigger number: 0x42 is not valid as well */
  309. bad_cmd = _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x42, sizeof(buf));
  310. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  311. ASSERT_LT(err, 0) TH_LOG("ioctl read-write with wrong _IOC_NR (0x42) should have failed");
  312. ASSERT_EQ(errno, ENOTTY)
  313. TH_LOG("expected ENOTTY for wrong read-write _IOC_NR (0x42), got errno %d", errno);
  314. }
  315. /*
  316. * Test ioctl with incorrect type bits
  317. */
  318. TEST_F(hidraw, ioctl_invalid_type)
  319. {
  320. char buf[256] = {0};
  321. int err;
  322. unsigned int bad_cmd;
  323. /*
  324. * craft an ioctl command with wrong _IOC_TYPE bits
  325. */
  326. bad_cmd = _IOC(_IOC_WRITE|_IOC_READ, 'I', 0x01, sizeof(buf)); /* 'I' should be 'H' */
  327. /* test the ioctl */
  328. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  329. ASSERT_LT(err, 0) TH_LOG("ioctl with wrong _IOC_TYPE (I) should have failed");
  330. ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_NR, got errno %d", errno);
  331. }
  332. /*
  333. * Test HIDIOCGFEATURE ioctl with incorrect _IOC_DIR bits
  334. */
  335. TEST_F(hidraw, ioctl_gfeature_invalid_dir)
  336. {
  337. __u8 buf[10] = {0};
  338. int err;
  339. unsigned int bad_cmd;
  340. /* set report ID 1 in first byte */
  341. buf[0] = 1;
  342. /*
  343. * craft an ioctl command with wrong _IOC_DIR bits
  344. * HIDIOCGFEATURE should have _IOC_WRITE|_IOC_READ, let's use only _IOC_WRITE
  345. */
  346. bad_cmd = _IOC(_IOC_WRITE, 'H', 0x07, sizeof(buf)); /* should be _IOC_WRITE|_IOC_READ */
  347. /* try to get feature report with wrong direction bits */
  348. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  349. ASSERT_LT(err, 0) TH_LOG("HIDIOCGFEATURE with wrong _IOC_DIR should have failed");
  350. ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_DIR, got errno %d", errno);
  351. /* also test with only _IOC_READ */
  352. bad_cmd = _IOC(_IOC_READ, 'H', 0x07, sizeof(buf)); /* should be _IOC_WRITE|_IOC_READ */
  353. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  354. ASSERT_LT(err, 0) TH_LOG("HIDIOCGFEATURE with wrong _IOC_DIR should have failed");
  355. ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_DIR, got errno %d", errno);
  356. }
  357. /*
  358. * Test read-only ioctl with incorrect _IOC_DIR bits
  359. */
  360. TEST_F(hidraw, ioctl_readonly_invalid_dir)
  361. {
  362. char buf[256] = {0};
  363. int err;
  364. unsigned int bad_cmd;
  365. /*
  366. * craft an ioctl command with wrong _IOC_DIR bits
  367. * HIDIOCGRAWNAME should have _IOC_READ, let's use _IOC_WRITE
  368. */
  369. bad_cmd = _IOC(_IOC_WRITE, 'H', 0x04, sizeof(buf)); /* should be _IOC_READ */
  370. /* try to get device name with wrong direction bits */
  371. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  372. ASSERT_LT(err, 0) TH_LOG("HIDIOCGRAWNAME with wrong _IOC_DIR should have failed");
  373. ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_DIR, got errno %d", errno);
  374. /* also test with _IOC_WRITE|_IOC_READ */
  375. bad_cmd = _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x04, sizeof(buf)); /* should be only _IOC_READ */
  376. err = ioctl(self->hidraw_fd, bad_cmd, buf);
  377. ASSERT_LT(err, 0) TH_LOG("HIDIOCGRAWNAME with wrong _IOC_DIR should have failed");
  378. ASSERT_EQ(errno, EINVAL) TH_LOG("expected EINVAL for wrong _IOC_DIR, got errno %d", errno);
  379. }
  380. /*
  381. * Test HIDIOCSFEATURE ioctl to set feature report
  382. */
  383. TEST_F(hidraw, ioctl_sfeature)
  384. {
  385. __u8 buf[10] = {0};
  386. int err;
  387. /* prepare feature report data */
  388. buf[0] = 1; /* report ID */
  389. buf[1] = 0x42;
  390. buf[2] = 0x24;
  391. /* set feature report */
  392. err = ioctl(self->hidraw_fd, HIDIOCSFEATURE(3), buf);
  393. ASSERT_EQ(err, 3) TH_LOG("HIDIOCSFEATURE ioctl failed, got %d", err);
  394. /*
  395. * Note: The uhid mock doesn't validate the set report data,
  396. * so we just verify the ioctl succeeds
  397. */
  398. }
  399. /*
  400. * Test HIDIOCGINPUT ioctl to get input report
  401. */
  402. TEST_F(hidraw, ioctl_ginput)
  403. {
  404. __u8 buf[10] = {0};
  405. int err;
  406. /* set report ID 1 in first byte */
  407. buf[0] = 1;
  408. /* get input report */
  409. err = ioctl(self->hidraw_fd, HIDIOCGINPUT(sizeof(buf)), buf);
  410. ASSERT_EQ(err, sizeof(feature_data)) TH_LOG("HIDIOCGINPUT ioctl failed, got %d", err);
  411. /* verify we got the expected input data */
  412. ASSERT_EQ(buf[0], feature_data[0])
  413. TH_LOG("expected feature_data[0] = %d, got %d", feature_data[0], buf[0]);
  414. ASSERT_EQ(buf[1], feature_data[1])
  415. TH_LOG("expected feature_data[1] = %d, got %d", feature_data[1], buf[1]);
  416. }
  417. /*
  418. * Test HIDIOCGINPUT ioctl with invalid report ID
  419. */
  420. TEST_F(hidraw, ioctl_ginput_invalid)
  421. {
  422. __u8 buf[10] = {0};
  423. int err;
  424. /* set invalid report ID (not 1) */
  425. buf[0] = 2;
  426. /* try to get input report */
  427. err = ioctl(self->hidraw_fd, HIDIOCGINPUT(sizeof(buf)), buf);
  428. ASSERT_LT(err, 0) TH_LOG("HIDIOCGINPUT should have failed with invalid report ID");
  429. ASSERT_EQ(errno, EIO) TH_LOG("expected EIO, got errno %d", errno);
  430. }
  431. /*
  432. * Test HIDIOCSINPUT ioctl to set input report
  433. */
  434. TEST_F(hidraw, ioctl_sinput)
  435. {
  436. __u8 buf[10] = {0};
  437. int err;
  438. /* prepare input report data */
  439. buf[0] = 1; /* report ID */
  440. buf[1] = 0x55;
  441. buf[2] = 0xAA;
  442. /* set input report */
  443. err = ioctl(self->hidraw_fd, HIDIOCSINPUT(3), buf);
  444. ASSERT_EQ(err, 3) TH_LOG("HIDIOCSINPUT ioctl failed, got %d", err);
  445. /*
  446. * Note: The uhid mock doesn't validate the set report data,
  447. * so we just verify the ioctl succeeds
  448. */
  449. }
  450. /*
  451. * Test HIDIOCGOUTPUT ioctl to get output report
  452. */
  453. TEST_F(hidraw, ioctl_goutput)
  454. {
  455. __u8 buf[10] = {0};
  456. int err;
  457. /* set report ID 1 in first byte */
  458. buf[0] = 1;
  459. /* get output report */
  460. err = ioctl(self->hidraw_fd, HIDIOCGOUTPUT(sizeof(buf)), buf);
  461. ASSERT_EQ(err, sizeof(feature_data)) TH_LOG("HIDIOCGOUTPUT ioctl failed, got %d", err);
  462. /* verify we got the expected output data */
  463. ASSERT_EQ(buf[0], feature_data[0])
  464. TH_LOG("expected feature_data[0] = %d, got %d", feature_data[0], buf[0]);
  465. ASSERT_EQ(buf[1], feature_data[1])
  466. TH_LOG("expected feature_data[1] = %d, got %d", feature_data[1], buf[1]);
  467. }
  468. /*
  469. * Test HIDIOCGOUTPUT ioctl with invalid report ID
  470. */
  471. TEST_F(hidraw, ioctl_goutput_invalid)
  472. {
  473. __u8 buf[10] = {0};
  474. int err;
  475. /* set invalid report ID (not 1) */
  476. buf[0] = 2;
  477. /* try to get output report */
  478. err = ioctl(self->hidraw_fd, HIDIOCGOUTPUT(sizeof(buf)), buf);
  479. ASSERT_LT(err, 0) TH_LOG("HIDIOCGOUTPUT should have failed with invalid report ID");
  480. ASSERT_EQ(errno, EIO) TH_LOG("expected EIO, got errno %d", errno);
  481. }
  482. /*
  483. * Test HIDIOCSOUTPUT ioctl to set output report
  484. */
  485. TEST_F(hidraw, ioctl_soutput)
  486. {
  487. __u8 buf[10] = {0};
  488. int err;
  489. /* prepare output report data */
  490. buf[0] = 1; /* report ID */
  491. buf[1] = 0x33;
  492. buf[2] = 0xCC;
  493. /* set output report */
  494. err = ioctl(self->hidraw_fd, HIDIOCSOUTPUT(3), buf);
  495. ASSERT_EQ(err, 3) TH_LOG("HIDIOCSOUTPUT ioctl failed, got %d", err);
  496. /*
  497. * Note: The uhid mock doesn't validate the set report data,
  498. * so we just verify the ioctl succeeds
  499. */
  500. }
  501. /*
  502. * Test HIDIOCGRAWNAME ioctl to get device name string
  503. */
  504. TEST_F(hidraw, ioctl_rawname)
  505. {
  506. char name[256] = {0};
  507. char expected_name[64];
  508. int err;
  509. /* get device name */
  510. err = ioctl(self->hidraw_fd, HIDIOCGRAWNAME(sizeof(name)), name);
  511. ASSERT_GT(err, 0) TH_LOG("HIDIOCGRAWNAME ioctl failed, got %d", err);
  512. /* construct expected name based on device id */
  513. snprintf(expected_name, sizeof(expected_name), "test-uhid-device-%d", self->hid.dev_id);
  514. /* verify the name matches expected pattern */
  515. ASSERT_EQ(strcmp(name, expected_name), 0)
  516. TH_LOG("expected name '%s', got '%s'", expected_name, name);
  517. }
  518. /*
  519. * Test HIDIOCGRAWPHYS ioctl to get device physical address string
  520. */
  521. TEST_F(hidraw, ioctl_rawphys)
  522. {
  523. char phys[256] = {0};
  524. char expected_phys[64];
  525. int err;
  526. /* get device physical address */
  527. err = ioctl(self->hidraw_fd, HIDIOCGRAWPHYS(sizeof(phys)), phys);
  528. ASSERT_GT(err, 0) TH_LOG("HIDIOCGRAWPHYS ioctl failed, got %d", err);
  529. /* construct expected phys based on device id */
  530. snprintf(expected_phys, sizeof(expected_phys), "%d", self->hid.dev_id);
  531. /* verify the phys matches expected value */
  532. ASSERT_EQ(strcmp(phys, expected_phys), 0)
  533. TH_LOG("expected phys '%s', got '%s'", expected_phys, phys);
  534. }
  535. /*
  536. * Test HIDIOCGRAWUNIQ ioctl to get device unique identifier string
  537. */
  538. TEST_F(hidraw, ioctl_rawuniq)
  539. {
  540. char uniq[256] = {0};
  541. int err;
  542. /* get device unique identifier */
  543. err = ioctl(self->hidraw_fd, HIDIOCGRAWUNIQ(sizeof(uniq)), uniq);
  544. ASSERT_GE(err, 0) TH_LOG("HIDIOCGRAWUNIQ ioctl failed, got %d", err);
  545. /* uniq is typically empty in our test setup */
  546. ASSERT_EQ(strlen(uniq), 0) TH_LOG("expected empty uniq, got '%s'", uniq);
  547. }
  548. /*
  549. * Test device string ioctls with small buffer sizes
  550. */
  551. TEST_F(hidraw, ioctl_strings_small_buffer)
  552. {
  553. char small_buf[8] = {0};
  554. char expected_name[64];
  555. int err;
  556. /* test HIDIOCGRAWNAME with small buffer */
  557. err = ioctl(self->hidraw_fd, HIDIOCGRAWNAME(sizeof(small_buf)), small_buf);
  558. ASSERT_EQ(err, sizeof(small_buf))
  559. TH_LOG("HIDIOCGRAWNAME with small buffer failed, got %d", err);
  560. /* construct expected truncated name */
  561. snprintf(expected_name, sizeof(expected_name), "test-uhid-device-%d", self->hid.dev_id);
  562. /* verify we got truncated name (first 8 chars, no null terminator guaranteed) */
  563. ASSERT_EQ(strncmp(small_buf, expected_name, sizeof(small_buf)), 0)
  564. TH_LOG("expected truncated name to match first %zu chars", sizeof(small_buf));
  565. /* Note: hidraw driver doesn't guarantee null termination when buffer is too small */
  566. }
  567. int main(int argc, char **argv)
  568. {
  569. return test_harness_run(argc, argv);
  570. }