test-quirks.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. /*
  2. * Copyright © 2018 Red Hat, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <config.h>
  24. #include <libinput.h>
  25. #include "libinput-util.h"
  26. #include "litest.h"
  27. #include "quirks.h"
  28. static void
  29. log_handler(struct libinput *this_is_null,
  30. enum libinput_log_priority priority,
  31. const char *format,
  32. va_list args)
  33. {
  34. #if 0
  35. vprintf(format, args);
  36. #endif
  37. }
  38. struct data_dir {
  39. char *dirname;
  40. char *filename;
  41. };
  42. static struct data_dir *
  43. data_dir_new(const char *file_content)
  44. {
  45. struct data_dir *dir = zalloc(sizeof(*dir));
  46. char dirname[PATH_MAX] = "/tmp/litest-quirk-test-XXXXXX";
  47. litest_assert_notnull(mkdtemp(dirname));
  48. dir->dirname = safe_strdup(dirname);
  49. if (file_content) {
  50. char *filename = strdup_printf("%s/testfile.quirks", dirname);
  51. _autofclose_ FILE *fp = fopen(filename, "w+");
  52. #ifndef __clang_analyzer__
  53. litest_assert_notnull(fp);
  54. #else
  55. assert(fp);
  56. #endif
  57. int rc = fputs(file_content, fp); // NOLINT: unix.Stream
  58. litest_assert_errno_success(rc);
  59. dir->filename = filename;
  60. }
  61. return dir;
  62. }
  63. static void
  64. data_dir_destroy(struct data_dir *dd)
  65. {
  66. if (dd->filename) {
  67. unlink(dd->filename);
  68. free(dd->filename);
  69. }
  70. if (dd->dirname) {
  71. rmdir(dd->dirname);
  72. free(dd->dirname);
  73. }
  74. free(dd);
  75. }
  76. DEFINE_DESTROY_CLEANUP_FUNC(data_dir);
  77. START_TEST(quirks_invalid_dir)
  78. {
  79. _unref_(quirks_context) *ctx = quirks_init_subsystem("/does-not-exist",
  80. NULL,
  81. log_handler,
  82. NULL,
  83. QLOG_LIBINPUT_LOGGING);
  84. litest_assert(ctx == NULL);
  85. }
  86. END_TEST
  87. START_TEST(quirks_empty_dir)
  88. {
  89. _destroy_(data_dir) *dd = data_dir_new(NULL);
  90. _unref_(quirks_context) *ctx = quirks_init_subsystem(dd->dirname,
  91. NULL,
  92. log_handler,
  93. NULL,
  94. QLOG_LIBINPUT_LOGGING);
  95. litest_assert(ctx == NULL);
  96. }
  97. END_TEST
  98. START_TEST(quirks_section_empty)
  99. {
  100. const char quirks_file[] = "[Empty Section]";
  101. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  102. _unref_(quirks_context) *ctx =
  103. quirks_init_subsystem(dd->dirname,
  104. NULL,
  105. log_handler,
  106. NULL,
  107. QLOG_CUSTOM_LOG_PRIORITIES);
  108. litest_assert(ctx == NULL);
  109. }
  110. END_TEST
  111. START_TEST(quirks_section_double)
  112. {
  113. const char quirks_file[] = "[Section name]";
  114. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  115. _unref_(quirks_context) *ctx =
  116. quirks_init_subsystem(dd->dirname,
  117. NULL,
  118. log_handler,
  119. NULL,
  120. QLOG_CUSTOM_LOG_PRIORITIES);
  121. litest_assert(ctx == NULL);
  122. }
  123. END_TEST
  124. START_TEST(quirks_section_missing_match)
  125. {
  126. const char quirks_file[] =
  127. "[Section name]\n"
  128. "AttrSizeHint=10x10\n";
  129. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  130. _unref_(quirks_context) *ctx =
  131. quirks_init_subsystem(dd->dirname,
  132. NULL,
  133. log_handler,
  134. NULL,
  135. QLOG_CUSTOM_LOG_PRIORITIES);
  136. litest_assert(ctx == NULL);
  137. }
  138. END_TEST
  139. START_TEST(quirks_section_missing_attr)
  140. {
  141. const char quirks_file[] =
  142. "[Section name]\n"
  143. "MatchUdevType=mouse\n";
  144. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  145. _unref_(quirks_context) *ctx =
  146. quirks_init_subsystem(dd->dirname,
  147. NULL,
  148. log_handler,
  149. NULL,
  150. QLOG_CUSTOM_LOG_PRIORITIES);
  151. litest_assert(ctx == NULL);
  152. }
  153. END_TEST
  154. START_TEST(quirks_section_match_after_attr)
  155. {
  156. const char quirks_file[] =
  157. "[Section name]\n"
  158. "MatchUdevType=mouse\n"
  159. "AttrSizeHint=10x10\n"
  160. "MatchName=mouse\n";
  161. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  162. _unref_(quirks_context) *ctx =
  163. quirks_init_subsystem(dd->dirname,
  164. NULL,
  165. log_handler,
  166. NULL,
  167. QLOG_CUSTOM_LOG_PRIORITIES);
  168. litest_assert(ctx == NULL);
  169. }
  170. END_TEST
  171. START_TEST(quirks_section_duplicate_match)
  172. {
  173. const char quirks_file[] =
  174. "[Section name]\n"
  175. "MatchUdevType=mouse\n"
  176. "MatchUdevType=mouse\n"
  177. "AttrSizeHint=10x10\n";
  178. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  179. _unref_(quirks_context) *ctx =
  180. quirks_init_subsystem(dd->dirname,
  181. NULL,
  182. log_handler,
  183. NULL,
  184. QLOG_CUSTOM_LOG_PRIORITIES);
  185. litest_assert(ctx == NULL);
  186. }
  187. END_TEST
  188. START_TEST(quirks_section_duplicate_attr)
  189. {
  190. /* This shouldn't be allowed but the current parser
  191. is happy with it */
  192. const char quirks_file[] =
  193. "[Section name]\n"
  194. "MatchUdevType=mouse\n"
  195. "AttrSizeHint=10x10\n"
  196. "AttrSizeHint=10x10\n";
  197. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  198. _unref_(quirks_context) *ctx =
  199. quirks_init_subsystem(dd->dirname,
  200. NULL,
  201. log_handler,
  202. NULL,
  203. QLOG_CUSTOM_LOG_PRIORITIES);
  204. litest_assert_notnull(ctx);
  205. }
  206. END_TEST
  207. START_TEST(quirks_parse_error_section)
  208. {
  209. const char quirks_file[] =
  210. "[Section Missing Bracket\n"
  211. "MatchUdevType=mouse\n"
  212. "AttrSizeHint=10x10\n";
  213. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  214. _unref_(quirks_context) *ctx =
  215. quirks_init_subsystem(dd->dirname,
  216. NULL,
  217. log_handler,
  218. NULL,
  219. QLOG_CUSTOM_LOG_PRIORITIES);
  220. litest_assert(ctx == NULL);
  221. }
  222. END_TEST
  223. START_TEST(quirks_parse_error_trailing_whitespace)
  224. {
  225. const char quirks_file[] =
  226. "[Section name]\n"
  227. "MatchUdevType=mouse \n"
  228. "AttrSizeHint=10x10\n";
  229. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  230. _unref_(quirks_context) *ctx =
  231. quirks_init_subsystem(dd->dirname,
  232. NULL,
  233. log_handler,
  234. NULL,
  235. QLOG_CUSTOM_LOG_PRIORITIES);
  236. litest_assert(ctx == NULL);
  237. }
  238. END_TEST
  239. START_TEST(quirks_parse_error_unknown_match)
  240. {
  241. const char quirks_file[] =
  242. "[Section name]\n"
  243. "Matchblahblah=mouse\n"
  244. "AttrSizeHint=10x10\n";
  245. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  246. _unref_(quirks_context) *ctx =
  247. quirks_init_subsystem(dd->dirname,
  248. NULL,
  249. log_handler,
  250. NULL,
  251. QLOG_CUSTOM_LOG_PRIORITIES);
  252. litest_assert(ctx == NULL);
  253. }
  254. END_TEST
  255. START_TEST(quirks_parse_error_unknown_attr)
  256. {
  257. const char quirks_file[] =
  258. "[Section name]\n"
  259. "MatchUdevType=mouse\n"
  260. "Attrblahblah=10x10\n";
  261. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  262. _unref_(quirks_context) *ctx =
  263. quirks_init_subsystem(dd->dirname,
  264. NULL,
  265. log_handler,
  266. NULL,
  267. QLOG_CUSTOM_LOG_PRIORITIES);
  268. litest_assert(ctx == NULL);
  269. }
  270. END_TEST
  271. START_TEST(quirks_parse_error_unknown_model)
  272. {
  273. const char quirks_file[] =
  274. "[Section name]\n"
  275. "MatchUdevType=mouse\n"
  276. "Modelblahblah=1\n";
  277. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  278. _unref_(quirks_context) *ctx =
  279. quirks_init_subsystem(dd->dirname,
  280. NULL,
  281. log_handler,
  282. NULL,
  283. QLOG_CUSTOM_LOG_PRIORITIES);
  284. litest_assert(ctx == NULL);
  285. }
  286. END_TEST
  287. START_TEST(quirks_parse_error_unknown_prefix)
  288. {
  289. const char quirks_file[] =
  290. "[Section name]\n"
  291. "MatchUdevType=mouse\n"
  292. "Fooblahblah=10x10\n";
  293. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  294. _unref_(quirks_context) *ctx =
  295. quirks_init_subsystem(dd->dirname,
  296. NULL,
  297. log_handler,
  298. NULL,
  299. QLOG_CUSTOM_LOG_PRIORITIES);
  300. litest_assert(ctx == NULL);
  301. }
  302. END_TEST
  303. START_TEST(quirks_parse_error_model_not_one)
  304. {
  305. const char quirks_file[] =
  306. "[Section name]\n"
  307. "MatchUdevType=mouse\n"
  308. "ModelAppleTouchpad=true\n";
  309. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  310. _unref_(quirks_context) *ctx =
  311. quirks_init_subsystem(dd->dirname,
  312. NULL,
  313. log_handler,
  314. NULL,
  315. QLOG_CUSTOM_LOG_PRIORITIES);
  316. }
  317. END_TEST
  318. START_TEST(quirks_parse_comment_inline)
  319. {
  320. const char quirks_file[] =
  321. "[Section name] # some inline comment\n"
  322. "MatchUdevType=mouse\t # another inline comment\n"
  323. "ModelAppleTouchpad=1#\n";
  324. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  325. _unref_(quirks_context) *ctx =
  326. quirks_init_subsystem(dd->dirname,
  327. NULL,
  328. log_handler,
  329. NULL,
  330. QLOG_CUSTOM_LOG_PRIORITIES);
  331. litest_assert_notnull(ctx);
  332. }
  333. END_TEST
  334. START_TEST(quirks_parse_comment_empty)
  335. {
  336. const char quirks_file[] =
  337. "[Section name]\n"
  338. "#\n"
  339. " #\n"
  340. "MatchUdevType=mouse\n"
  341. "ModelAppleTouchpad=1\n";
  342. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  343. _unref_(quirks_context) *ctx =
  344. quirks_init_subsystem(dd->dirname,
  345. NULL,
  346. log_handler,
  347. NULL,
  348. QLOG_CUSTOM_LOG_PRIORITIES);
  349. litest_assert_notnull(ctx);
  350. }
  351. END_TEST
  352. START_TEST(quirks_parse_string_quotes_single)
  353. {
  354. const char quirks_file[] =
  355. "[Section name]\n"
  356. "MatchUdevType=mouse\n"
  357. "AttrKeyboardIntegration='internal'\n";
  358. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  359. _unref_(quirks_context) *ctx =
  360. quirks_init_subsystem(dd->dirname,
  361. NULL,
  362. log_handler,
  363. NULL,
  364. QLOG_CUSTOM_LOG_PRIORITIES);
  365. litest_assert(ctx == NULL);
  366. }
  367. END_TEST
  368. START_TEST(quirks_parse_string_quotes_double)
  369. {
  370. const char quirks_file[] =
  371. "[Section name]\n"
  372. "MatchUdevType=mouse\n"
  373. "AttrKeyboardIntegration=\"internal\"\n";
  374. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  375. _unref_(quirks_context) *ctx =
  376. quirks_init_subsystem(dd->dirname,
  377. NULL,
  378. log_handler,
  379. NULL,
  380. QLOG_CUSTOM_LOG_PRIORITIES);
  381. litest_assert(ctx == NULL);
  382. }
  383. END_TEST
  384. START_TEST(quirks_parse_bustype)
  385. {
  386. const char quirks_file[] =
  387. "[Section name]\n"
  388. "MatchBus=usb\n"
  389. "ModelAppleTouchpad=1\n"
  390. "\n"
  391. "[Section name]\n"
  392. "MatchBus=bluetooth\n"
  393. "ModelAppleTouchpad=1\n"
  394. "\n"
  395. "[Section name]\n"
  396. "MatchBus=i2c\n"
  397. "ModelAppleTouchpad=1\n"
  398. "\n"
  399. "[Section name]\n"
  400. "MatchBus=rmi\n"
  401. "ModelAppleTouchpad=1\n"
  402. "\n"
  403. "[Section name]\n"
  404. "MatchBus=ps2\n"
  405. "ModelAppleTouchpad=1\n";
  406. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  407. _unref_(quirks_context) *ctx =
  408. quirks_init_subsystem(dd->dirname,
  409. NULL,
  410. log_handler,
  411. NULL,
  412. QLOG_CUSTOM_LOG_PRIORITIES);
  413. litest_assert_notnull(ctx);
  414. }
  415. END_TEST
  416. START_TEST(quirks_parse_bustype_invalid)
  417. {
  418. const char quirks_file[] =
  419. "[Section name]\n"
  420. "MatchBus=venga\n"
  421. "ModelAppleTouchpad=1\n";
  422. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  423. _unref_(quirks_context) *ctx =
  424. quirks_init_subsystem(dd->dirname,
  425. NULL,
  426. log_handler,
  427. NULL,
  428. QLOG_CUSTOM_LOG_PRIORITIES);
  429. litest_assert(ctx == NULL);
  430. }
  431. END_TEST
  432. START_TEST(quirks_parse_vendor)
  433. {
  434. const char quirks_file[] =
  435. "[Section name]\n"
  436. "MatchVendor=0x0000\n"
  437. "ModelAppleTouchpad=1\n"
  438. "\n"
  439. "[Section name]\n"
  440. "MatchVendor=0x0001\n"
  441. "ModelAppleTouchpad=1\n"
  442. "\n"
  443. "[Section name]\n"
  444. "MatchVendor=0x2343\n"
  445. "ModelAppleTouchpad=1\n";
  446. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  447. _unref_(quirks_context) *ctx =
  448. quirks_init_subsystem(dd->dirname,
  449. NULL,
  450. log_handler,
  451. NULL,
  452. QLOG_CUSTOM_LOG_PRIORITIES);
  453. litest_assert_notnull(ctx);
  454. }
  455. END_TEST
  456. START_TEST(quirks_parse_vendor_invalid)
  457. {
  458. const char *quirks_file[] = {
  459. "[Section name]\n"
  460. "MatchVendor=-1\n"
  461. "ModelAppleTouchpad=1\n",
  462. "[Section name]\n"
  463. "MatchVendor=abc\n"
  464. "ModelAppleTouchpad=1\n",
  465. "[Section name]\n"
  466. "MatchVendor=0xFFFFF\n"
  467. "ModelAppleTouchpad=1\n",
  468. "[Section name]\n"
  469. "MatchVendor=123\n"
  470. "ModelAppleTouchpad=1\n",
  471. };
  472. ARRAY_FOR_EACH(quirks_file, qf) {
  473. _destroy_(data_dir) *dd = data_dir_new(*qf);
  474. _unref_(quirks_context) *ctx =
  475. quirks_init_subsystem(dd->dirname,
  476. NULL,
  477. log_handler,
  478. NULL,
  479. QLOG_CUSTOM_LOG_PRIORITIES);
  480. litest_assert(ctx == NULL);
  481. }
  482. }
  483. END_TEST
  484. START_TEST(quirks_parse_product)
  485. {
  486. const char quirks_file[] =
  487. "[Section name]\n"
  488. "MatchProduct=0x12AB\n"
  489. "ModelAppleTouchpad=1\n"
  490. "\n"
  491. "[Section name]\n"
  492. "MatchProduct=0x0001;0x1234;0xABCD\n"
  493. "ModelAppleTouchpad=1\n"
  494. "\n"
  495. "[Section name]\n"
  496. "MatchProduct=0x2343\n"
  497. "ModelAppleTouchpad=1\n";
  498. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  499. _unref_(quirks_context) *ctx =
  500. quirks_init_subsystem(dd->dirname,
  501. NULL,
  502. log_handler,
  503. NULL,
  504. QLOG_CUSTOM_LOG_PRIORITIES);
  505. litest_assert_notnull(ctx);
  506. }
  507. END_TEST
  508. START_TEST(quirks_parse_product_too_many)
  509. {
  510. const char prologue[] =
  511. "[Section name]\n"
  512. "MatchProduct=0x12AB\n"
  513. "ModelAppleTouchpad=1\n"
  514. "\n"
  515. "[Section name]\n"
  516. "MatchProduct=";
  517. const char epilogue[] =
  518. "\n"
  519. "ModelAppleTouchpad=1\n"
  520. "\n"
  521. "[Section name]\n"
  522. "MatchProduct=0x2343\n"
  523. "ModelAppleTouchpad=1\n";
  524. char matches[4096] = { 0 };
  525. for (int i = 0; i < 128; i++) {
  526. int len = strlen(matches);
  527. int remaining = sizeof(matches) - len;
  528. snprintf(&matches[len], remaining, "0x%04X", i);
  529. }
  530. char *quirks_file = strdup_printf("%s%s%s", prologue, matches, epilogue);
  531. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  532. free(quirks_file);
  533. /* This test will only blow up in valgrind/asan */
  534. _unref_(quirks_context) *ctx =
  535. quirks_init_subsystem(dd->dirname,
  536. NULL,
  537. log_handler,
  538. NULL,
  539. QLOG_CUSTOM_LOG_PRIORITIES);
  540. litest_assert(ctx == NULL);
  541. }
  542. END_TEST
  543. START_TEST(quirks_parse_product_invalid)
  544. {
  545. const char *quirks_file[] = {
  546. "[Section name]\n"
  547. "MatchProduct=-1\n"
  548. "ModelAppleTouchpad=1\n",
  549. "[Section name]\n"
  550. "MatchProduct=abc\n"
  551. "ModelAppleTouchpad=1\n",
  552. "[Section name]\n"
  553. "MatchProduct=0xFFFFF\n"
  554. "ModelAppleTouchpad=1\n",
  555. "[Section name]\n"
  556. "MatchProduct=123\n"
  557. "ModelAppleTouchpad=1\n",
  558. };
  559. ARRAY_FOR_EACH(quirks_file, qf) {
  560. _destroy_(data_dir) *dd = data_dir_new(*qf);
  561. _unref_(quirks_context) *ctx =
  562. quirks_init_subsystem(dd->dirname,
  563. NULL,
  564. log_handler,
  565. NULL,
  566. QLOG_CUSTOM_LOG_PRIORITIES);
  567. litest_assert(ctx == NULL);
  568. }
  569. }
  570. END_TEST
  571. START_TEST(quirks_parse_version)
  572. {
  573. const char quirks_file[] =
  574. "[Section name]\n"
  575. "MatchVersion=0x0000\n"
  576. "ModelAppleTouchpad=1\n"
  577. "\n"
  578. "[Section name]\n"
  579. "MatchVersion=0x0001\n"
  580. "ModelAppleTouchpad=1\n"
  581. "\n"
  582. "[Section name]\n"
  583. "MatchVersion=0x2343\n"
  584. "ModelAppleTouchpad=1\n";
  585. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  586. _unref_(quirks_context) *ctx =
  587. quirks_init_subsystem(dd->dirname,
  588. NULL,
  589. log_handler,
  590. NULL,
  591. QLOG_CUSTOM_LOG_PRIORITIES);
  592. litest_assert_notnull(ctx);
  593. }
  594. END_TEST
  595. START_TEST(quirks_parse_version_invalid)
  596. {
  597. const char *quirks_file[] = {
  598. "[Section name]\n"
  599. "MatchVersion=-1\n"
  600. "ModelAppleTouchpad=1\n",
  601. "[Section name]\n"
  602. "MatchVersion=abc\n"
  603. "ModelAppleTouchpad=1\n",
  604. "[Section name]\n"
  605. "MatchVersion=0xFFFFF\n"
  606. "ModelAppleTouchpad=1\n",
  607. "[Section name]\n"
  608. "MatchVersion=123\n"
  609. "ModelAppleTouchpad=1\n",
  610. };
  611. ARRAY_FOR_EACH(quirks_file, qf) {
  612. _destroy_(data_dir) *dd = data_dir_new(*qf);
  613. _unref_(quirks_context) *ctx =
  614. quirks_init_subsystem(dd->dirname,
  615. NULL,
  616. log_handler,
  617. NULL,
  618. QLOG_CUSTOM_LOG_PRIORITIES);
  619. litest_assert(ctx == NULL);
  620. }
  621. }
  622. END_TEST
  623. START_TEST(quirks_parse_name)
  624. {
  625. const char quirks_file[] =
  626. "[Section name]\n"
  627. "MatchName=1235\n"
  628. "ModelAppleTouchpad=1\n"
  629. "\n"
  630. "[Section name]\n"
  631. "MatchName=abc\n"
  632. "ModelAppleTouchpad=1\n"
  633. "\n"
  634. "[Section name]\n"
  635. "MatchName=*foo\n"
  636. "ModelAppleTouchpad=1\n"
  637. "\n"
  638. "[Section name]\n"
  639. "MatchName=foo*\n"
  640. "ModelAppleTouchpad=1\n"
  641. "\n"
  642. "[Section name]\n"
  643. "MatchName=foo[]\n"
  644. "ModelAppleTouchpad=1\n"
  645. "\n"
  646. "[Section name]\n"
  647. "MatchName=*foo*\n"
  648. "ModelAppleTouchpad=1\n";
  649. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  650. _unref_(quirks_context) *ctx =
  651. quirks_init_subsystem(dd->dirname,
  652. NULL,
  653. log_handler,
  654. NULL,
  655. QLOG_CUSTOM_LOG_PRIORITIES);
  656. litest_assert_notnull(ctx);
  657. }
  658. END_TEST
  659. START_TEST(quirks_parse_name_invalid)
  660. {
  661. const char *quirks_file[] = {
  662. "[Section name]\n"
  663. "MatchName=\n"
  664. "ModelAppleTouchpad=1\n",
  665. };
  666. ARRAY_FOR_EACH(quirks_file, qf) {
  667. _destroy_(data_dir) *dd = data_dir_new(*qf);
  668. _unref_(quirks_context) *ctx =
  669. quirks_init_subsystem(dd->dirname,
  670. NULL,
  671. log_handler,
  672. NULL,
  673. QLOG_CUSTOM_LOG_PRIORITIES);
  674. litest_assert(ctx == NULL);
  675. }
  676. }
  677. END_TEST
  678. START_TEST(quirks_parse_uniq)
  679. {
  680. const char quirks_file[] =
  681. "[Section Uniq]\n"
  682. "MatchUniq=1235\n"
  683. "ModelAppleTouchpad=1\n"
  684. "\n"
  685. "[Section Uniq]\n"
  686. "MatchUniq=abc\n"
  687. "ModelAppleTouchpad=1\n"
  688. "\n"
  689. "[Section Uniq]\n"
  690. "MatchUniq=*foo\n"
  691. "ModelAppleTouchpad=1\n"
  692. "\n"
  693. "[Section Uniq]\n"
  694. "MatchUniq=foo*\n"
  695. "ModelAppleTouchpad=1\n"
  696. "\n"
  697. "[Section Uniq]\n"
  698. "MatchUniq=foo[]\n"
  699. "ModelAppleTouchpad=1\n"
  700. "\n"
  701. "[Section Uniq]\n"
  702. "MatchUniq=*foo*\n"
  703. "ModelAppleTouchpad=1\n";
  704. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  705. _unref_(quirks_context) *ctx =
  706. quirks_init_subsystem(dd->dirname,
  707. NULL,
  708. log_handler,
  709. NULL,
  710. QLOG_CUSTOM_LOG_PRIORITIES);
  711. litest_assert_notnull(ctx);
  712. }
  713. END_TEST
  714. START_TEST(quirks_parse_uniq_invalid)
  715. {
  716. const char *quirks_file[] = {
  717. "[Section name]\n"
  718. "MatchUniq=\n"
  719. "ModelAppleTouchpad=1\n",
  720. };
  721. ARRAY_FOR_EACH(quirks_file, qf) {
  722. _destroy_(data_dir) *dd = data_dir_new(*qf);
  723. _unref_(quirks_context) *ctx =
  724. quirks_init_subsystem(dd->dirname,
  725. NULL,
  726. log_handler,
  727. NULL,
  728. QLOG_CUSTOM_LOG_PRIORITIES);
  729. litest_assert(ctx == NULL);
  730. }
  731. }
  732. END_TEST
  733. START_TEST(quirks_parse_udev)
  734. {
  735. const char quirks_file[] =
  736. "[Section name]\n"
  737. "MatchUdevType=touchpad\n"
  738. "ModelAppleTouchpad=1\n"
  739. "\n"
  740. "[Section name]\n"
  741. "MatchUdevType=mouse\n"
  742. "ModelAppleTouchpad=1\n"
  743. "\n"
  744. "[Section name]\n"
  745. "MatchUdevType=pointingstick\n"
  746. "ModelAppleTouchpad=1\n"
  747. "\n"
  748. "[Section name]\n"
  749. "MatchUdevType=tablet\n"
  750. "ModelAppleTouchpad=1\n"
  751. "\n"
  752. "[Section name]\n"
  753. "MatchUdevType=tablet-pad\n"
  754. "ModelAppleTouchpad=1\n"
  755. "\n"
  756. "[Section name]\n"
  757. "MatchUdevType=keyboard\n"
  758. "ModelAppleTouchpad=1\n"
  759. "\n"
  760. "[Section name]\n"
  761. "MatchUdevType=joystick\n"
  762. "ModelAppleTouchpad=1\n";
  763. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  764. _unref_(quirks_context) *ctx =
  765. quirks_init_subsystem(dd->dirname,
  766. NULL,
  767. log_handler,
  768. NULL,
  769. QLOG_CUSTOM_LOG_PRIORITIES);
  770. litest_assert_notnull(ctx);
  771. }
  772. END_TEST
  773. START_TEST(quirks_parse_udev_invalid)
  774. {
  775. const char *quirks_file[] = {
  776. "[Section name]\n"
  777. "MatchUdevType=blah\n"
  778. "ModelAppleTouchpad=1\n",
  779. "[Section name]\n"
  780. "MatchUdevType=\n"
  781. "ModelAppleTouchpad=1\n",
  782. "[Section name]\n"
  783. "MatchUdevType=123\n"
  784. "ModelAppleTouchpad=1\n",
  785. };
  786. ARRAY_FOR_EACH(quirks_file, qf) {
  787. _destroy_(data_dir) *dd = data_dir_new(*qf);
  788. _unref_(quirks_context) *ctx =
  789. quirks_init_subsystem(dd->dirname,
  790. NULL,
  791. log_handler,
  792. NULL,
  793. QLOG_CUSTOM_LOG_PRIORITIES);
  794. litest_assert(ctx == NULL);
  795. }
  796. }
  797. END_TEST
  798. START_TEST(quirks_parse_dmi)
  799. {
  800. const char quirks_file[] =
  801. "[Section name]\n"
  802. "MatchDMIModalias=dmi:*\n"
  803. "ModelAppleTouchpad=1\n"
  804. "\n"
  805. "[Section name]\n"
  806. "MatchDMIModalias=dmi:*svn*pn*:\n"
  807. "ModelAppleTouchpad=1\n";
  808. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  809. _unref_(quirks_context) *ctx =
  810. quirks_init_subsystem(dd->dirname,
  811. NULL,
  812. log_handler,
  813. NULL,
  814. QLOG_CUSTOM_LOG_PRIORITIES);
  815. litest_assert_notnull(ctx);
  816. }
  817. END_TEST
  818. START_TEST(quirks_parse_dmi_invalid)
  819. {
  820. const char *quirks_file[] = {
  821. "[Section name]\n"
  822. "MatchDMIModalias=\n"
  823. "ModelAppleTouchpad=1\n",
  824. "[Section name]\n"
  825. "MatchDMIModalias=*pn*\n"
  826. "ModelAppleTouchpad=1\n",
  827. "[Section name]\n"
  828. "MatchDMIModalias=dmi*pn*\n"
  829. "ModelAppleTouchpad=1\n",
  830. "[Section name]\n"
  831. "MatchDMIModalias=foo\n"
  832. "ModelAppleTouchpad=1\n",
  833. };
  834. ARRAY_FOR_EACH(quirks_file, qf) {
  835. _destroy_(data_dir) *dd = data_dir_new(*qf);
  836. _unref_(quirks_context) *ctx =
  837. quirks_init_subsystem(dd->dirname,
  838. NULL,
  839. log_handler,
  840. NULL,
  841. QLOG_CUSTOM_LOG_PRIORITIES);
  842. litest_assert(ctx == NULL);
  843. }
  844. }
  845. END_TEST
  846. typedef bool (*qparsefunc)(struct quirks *q, enum quirk which, void *data);
  847. /*
  848. Helper for generic testing, matches on a mouse device with the given
  849. quirk set to the given string. Creates a data directory, inits the quirks
  850. and calls func() to return the value in data. The func has to take the
  851. right data, otherwise boom. Usage:
  852. rc = test_attr_parse(dev, QUIRK_ATTR_SIZE_HINT,
  853. "10x30", quirks_get_dimensions,
  854. &some_struct_quirks_dimensions);
  855. if (rc == false) // failed to parse
  856. else // struct now contains the 10, 30 values
  857. */
  858. static bool
  859. test_attr_parse(struct litest_device *dev,
  860. enum quirk which,
  861. const char *str,
  862. qparsefunc func,
  863. void *data)
  864. {
  865. _unref_(udev_device) *ud =
  866. libinput_device_get_udev_device(dev->libinput_device);
  867. char buf[512];
  868. bool result;
  869. snprintf(buf,
  870. sizeof(buf),
  871. "[Section name]\n"
  872. "MatchUdevType=mouse\n"
  873. "%s=%s\n",
  874. quirk_get_name(which),
  875. str);
  876. _destroy_(data_dir) *dd = data_dir_new(buf);
  877. _unref_(quirks_context) *ctx =
  878. quirks_init_subsystem(dd->dirname,
  879. NULL,
  880. log_handler,
  881. NULL,
  882. QLOG_CUSTOM_LOG_PRIORITIES);
  883. if (ctx != NULL) {
  884. _unref_(quirks) *q = quirks_fetch_for_device(ctx, ud);
  885. litest_assert_notnull(q);
  886. litest_assert(func(q, which, data));
  887. litest_assert(quirks_has_quirk(q, which));
  888. result = true;
  889. } else {
  890. result = false;
  891. }
  892. return result;
  893. }
  894. struct qtest_dim {
  895. const char *str;
  896. bool success;
  897. size_t w, h;
  898. };
  899. START_TEST(quirks_parse_dimension_attr)
  900. {
  901. struct litest_device *dev = litest_current_device();
  902. enum quirk attrs[] = {
  903. QUIRK_ATTR_SIZE_HINT,
  904. QUIRK_ATTR_RESOLUTION_HINT,
  905. };
  906. /* clang-format off */
  907. struct qtest_dim test_values[] = {
  908. { "10x10", true, 10, 10 },
  909. { "20x30", true, 20, 30 },
  910. { "-10x30", false, 0, 0 },
  911. { "10:30", false, 0, 0 },
  912. { "30", false, 0, 0 },
  913. { "0x00", false, 0, 0 },
  914. { "0xa0", false, 0, 0 },
  915. };
  916. /* clang-format on */
  917. ARRAY_FOR_EACH(attrs, a) {
  918. ARRAY_FOR_EACH(test_values, t) {
  919. struct quirk_dimensions dim;
  920. bool rc;
  921. rc = test_attr_parse(dev,
  922. *a,
  923. t->str,
  924. (qparsefunc)quirks_get_dimensions,
  925. &dim);
  926. litest_assert_int_eq(rc, t->success);
  927. if (!rc)
  928. continue;
  929. litest_assert_int_eq(dim.x, t->w);
  930. litest_assert_int_eq(dim.y, t->h);
  931. }
  932. }
  933. }
  934. END_TEST
  935. struct qtest_range {
  936. const char *str;
  937. bool success;
  938. int hi, lo;
  939. };
  940. START_TEST(quirks_parse_range_attr)
  941. {
  942. struct litest_device *dev = litest_current_device();
  943. enum quirk attrs[] = {
  944. QUIRK_ATTR_TOUCH_SIZE_RANGE,
  945. QUIRK_ATTR_PRESSURE_RANGE,
  946. };
  947. /* clang-format off */
  948. struct qtest_range test_values[] = {
  949. { "20:10", true, 20, 10 },
  950. { "30:5", true, 30, 5 },
  951. { "30:-10", true, 30, -10 },
  952. { "-30:-100", true, -30, -100 },
  953. { "5:10", false, 0, 0 },
  954. { "5:5", false, 0, 0 },
  955. { "-10:5", false, 0, 0 },
  956. { "-10:-5", false, 0, 0 },
  957. { "10x30", false, 0, 0 },
  958. { "30x10", false, 0, 0 },
  959. { "30", false, 0, 0 },
  960. { "0x00", false, 0, 0 },
  961. { "0xa0", false, 0, 0 },
  962. { "0x10:0x5", false, 0, 0 },
  963. };
  964. /* clang-format on */
  965. ARRAY_FOR_EACH(attrs, a) {
  966. ARRAY_FOR_EACH(test_values, t) {
  967. struct quirk_range r;
  968. bool rc;
  969. rc = test_attr_parse(dev,
  970. *a,
  971. t->str,
  972. (qparsefunc)quirks_get_range,
  973. &r);
  974. litest_assert_int_eq(rc, t->success);
  975. if (!rc)
  976. continue;
  977. litest_assert_int_eq(r.lower, t->lo);
  978. litest_assert_int_eq(r.upper, t->hi);
  979. }
  980. }
  981. }
  982. END_TEST
  983. struct qtest_uint {
  984. const char *str;
  985. bool success;
  986. uint32_t val;
  987. };
  988. START_TEST(quirks_parse_uint_attr)
  989. {
  990. struct litest_device *dev = litest_current_device();
  991. enum quirk attrs[] = {
  992. QUIRK_ATTR_PALM_SIZE_THRESHOLD,
  993. QUIRK_ATTR_PALM_PRESSURE_THRESHOLD,
  994. QUIRK_ATTR_THUMB_PRESSURE_THRESHOLD,
  995. };
  996. /* clang-format off */
  997. struct qtest_uint test_values[] = {
  998. { "10", true, 10 },
  999. { "0", true, 0 },
  1000. { "5", true, 5 },
  1001. { "65535", true, 65535 },
  1002. { "4294967295", true, 4294967295 },
  1003. { "-10", false, 0 },
  1004. { "0x10", false, 0 },
  1005. { "0xab", false, 0 },
  1006. { "ab", false, 0 },
  1007. };
  1008. /* clang-format on */
  1009. ARRAY_FOR_EACH(attrs, a) {
  1010. ARRAY_FOR_EACH(test_values, t) {
  1011. uint32_t v;
  1012. bool rc;
  1013. rc = test_attr_parse(dev,
  1014. *a,
  1015. t->str,
  1016. (qparsefunc)quirks_get_uint32,
  1017. &v);
  1018. litest_assert_int_eq(rc, t->success);
  1019. if (!rc)
  1020. continue;
  1021. litest_assert_int_eq(v, t->val);
  1022. }
  1023. }
  1024. }
  1025. END_TEST
  1026. struct qtest_double {
  1027. const char *str;
  1028. bool success;
  1029. double val;
  1030. };
  1031. START_TEST(quirks_parse_double_attr)
  1032. {
  1033. struct litest_device *dev = litest_current_device();
  1034. enum quirk attrs[] = {
  1035. QUIRK_ATTR_TRACKPOINT_MULTIPLIER,
  1036. };
  1037. /* clang-format off */
  1038. struct qtest_double test_values[] = {
  1039. { "10", true, 10.0 },
  1040. { "10.0", true, 10.0 },
  1041. { "-10.0", true, -10.0 },
  1042. { "0", true, 0.0 },
  1043. { "0.0", true, 0.0 },
  1044. { "5.1", true, 5.1 },
  1045. { "-5.9", true, -5.9 },
  1046. { "65535", true, 65535 },
  1047. { "4294967295", true, 4294967295 },
  1048. { "4294967295.123", true, 4294967295.123 },
  1049. /* our safe_atoi parses hex even though we don't really want
  1050. * to */
  1051. { "0x10", false, 0 },
  1052. { "0xab", false, 0 },
  1053. { "ab", false, 0 },
  1054. { "10:5", false, 0 },
  1055. { "10x5", false, 0 },
  1056. };
  1057. /* clang-format on */
  1058. ARRAY_FOR_EACH(attrs, a) {
  1059. ARRAY_FOR_EACH(test_values, t) {
  1060. double v;
  1061. bool rc;
  1062. rc = test_attr_parse(dev,
  1063. *a,
  1064. t->str,
  1065. (qparsefunc)quirks_get_double,
  1066. &v);
  1067. litest_assert_int_eq(rc, t->success);
  1068. if (!rc)
  1069. continue;
  1070. litest_assert_double_eq(v, t->val);
  1071. }
  1072. }
  1073. }
  1074. END_TEST
  1075. struct qtest_str {
  1076. const char *str;
  1077. enum quirk where;
  1078. };
  1079. START_TEST(quirks_parse_string_attr)
  1080. {
  1081. struct litest_device *dev = litest_current_device();
  1082. enum quirk attrs[] = {
  1083. QUIRK_ATTR_TPKBCOMBO_LAYOUT,
  1084. QUIRK_ATTR_LID_SWITCH_RELIABILITY,
  1085. QUIRK_ATTR_KEYBOARD_INTEGRATION,
  1086. };
  1087. /* clang-format off */
  1088. struct qtest_str test_values[] = {
  1089. { "below", QUIRK_ATTR_TPKBCOMBO_LAYOUT },
  1090. { "reliable", QUIRK_ATTR_LID_SWITCH_RELIABILITY },
  1091. { "write_open", QUIRK_ATTR_LID_SWITCH_RELIABILITY },
  1092. { "internal", QUIRK_ATTR_KEYBOARD_INTEGRATION },
  1093. { "external", QUIRK_ATTR_KEYBOARD_INTEGRATION },
  1094. { "10", 0 },
  1095. { "-10", 0 },
  1096. { "0", 0 },
  1097. { "", 0 },
  1098. { "banana", 0 },
  1099. { "honk honk", 0 },
  1100. { "0x12", 0 },
  1101. { "0xa", 0 },
  1102. { "0.0", 0 },
  1103. };
  1104. /* clang-format on */
  1105. ARRAY_FOR_EACH(attrs, a) {
  1106. ARRAY_FOR_EACH(test_values, t) {
  1107. bool rc;
  1108. char *do_not_use; /* freed before we can use it */
  1109. rc = test_attr_parse(dev,
  1110. *a,
  1111. t->str,
  1112. (qparsefunc)quirks_get_string,
  1113. &do_not_use);
  1114. if (*a == t->where)
  1115. litest_assert_int_eq(rc, true);
  1116. else
  1117. litest_assert_int_eq(rc, false);
  1118. }
  1119. }
  1120. }
  1121. END_TEST
  1122. struct qtest_bool {
  1123. const char *str;
  1124. bool success;
  1125. bool val;
  1126. };
  1127. START_TEST(quirks_parse_bool_attr)
  1128. {
  1129. struct litest_device *dev = litest_current_device();
  1130. enum quirk attrs[] = {
  1131. QUIRK_ATTR_USE_VELOCITY_AVERAGING,
  1132. QUIRK_ATTR_TABLET_SMOOTHING,
  1133. };
  1134. /* clang-format off */
  1135. struct qtest_bool test_values[] = {
  1136. { "0", true, false },
  1137. { "1", true, true },
  1138. { "2", false, false },
  1139. { "-1", false, false },
  1140. { "a", false, false },
  1141. };
  1142. /* clang-format on */
  1143. ARRAY_FOR_EACH(attrs, a) {
  1144. ARRAY_FOR_EACH(test_values, t) {
  1145. bool v;
  1146. bool rc;
  1147. rc = test_attr_parse(dev,
  1148. *a,
  1149. t->str,
  1150. (qparsefunc)quirks_get_bool,
  1151. &v);
  1152. litest_assert(rc == t->success);
  1153. if (!rc)
  1154. continue;
  1155. litest_assert(v == t->val);
  1156. }
  1157. }
  1158. }
  1159. END_TEST
  1160. START_TEST(quirks_parse_integration_attr)
  1161. {
  1162. struct litest_device *dev = litest_current_device();
  1163. char *do_not_use; /* freed before we can use it */
  1164. bool rc = test_attr_parse(dev,
  1165. QUIRK_ATTR_KEYBOARD_INTEGRATION,
  1166. "internal",
  1167. (qparsefunc)quirks_get_string,
  1168. &do_not_use);
  1169. litest_assert(rc);
  1170. rc = test_attr_parse(dev,
  1171. QUIRK_ATTR_KEYBOARD_INTEGRATION,
  1172. "external",
  1173. (qparsefunc)quirks_get_string,
  1174. &do_not_use);
  1175. litest_assert(rc);
  1176. rc = test_attr_parse(dev,
  1177. QUIRK_ATTR_TRACKPOINT_INTEGRATION,
  1178. "internal",
  1179. (qparsefunc)quirks_get_string,
  1180. &do_not_use);
  1181. litest_assert(rc);
  1182. rc = test_attr_parse(dev,
  1183. QUIRK_ATTR_TRACKPOINT_INTEGRATION,
  1184. "external",
  1185. (qparsefunc)quirks_get_string,
  1186. &do_not_use);
  1187. litest_assert(rc);
  1188. }
  1189. END_TEST
  1190. START_TEST(quirks_model_one)
  1191. {
  1192. struct litest_device *dev = litest_current_device();
  1193. _unref_(udev_device) *ud =
  1194. libinput_device_get_udev_device(dev->libinput_device);
  1195. const char quirks_file[] =
  1196. "[Section name]\n"
  1197. "MatchUdevType=mouse\n"
  1198. "ModelAppleTouchpad=1\n";
  1199. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  1200. bool isset;
  1201. _unref_(quirks_context) *ctx =
  1202. quirks_init_subsystem(dd->dirname,
  1203. NULL,
  1204. log_handler,
  1205. NULL,
  1206. QLOG_CUSTOM_LOG_PRIORITIES);
  1207. litest_assert_notnull(ctx);
  1208. _unref_(quirks) *q = quirks_fetch_for_device(ctx, ud);
  1209. litest_assert_notnull(q);
  1210. litest_assert(quirks_get_bool(q, QUIRK_MODEL_APPLE_TOUCHPAD, &isset));
  1211. litest_assert(isset == true);
  1212. }
  1213. END_TEST
  1214. START_TEST(quirks_model_zero)
  1215. {
  1216. struct litest_device *dev = litest_current_device();
  1217. _unref_(udev_device) *ud =
  1218. libinput_device_get_udev_device(dev->libinput_device);
  1219. const char quirks_file[] =
  1220. "[Section name]\n"
  1221. "MatchUdevType=mouse\n"
  1222. "ModelAppleTouchpad=0\n";
  1223. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  1224. bool isset;
  1225. _unref_(quirks_context) *ctx =
  1226. quirks_init_subsystem(dd->dirname,
  1227. NULL,
  1228. log_handler,
  1229. NULL,
  1230. QLOG_CUSTOM_LOG_PRIORITIES);
  1231. litest_assert_notnull(ctx);
  1232. _unref_(quirks) *q = quirks_fetch_for_device(ctx, ud);
  1233. litest_assert_notnull(q);
  1234. litest_assert(quirks_get_bool(q, QUIRK_MODEL_APPLE_TOUCHPAD, &isset));
  1235. litest_assert(isset == false);
  1236. }
  1237. END_TEST
  1238. START_TEST(quirks_model_override)
  1239. {
  1240. struct litest_device *dev = litest_current_device();
  1241. _unref_(udev_device) *ud =
  1242. libinput_device_get_udev_device(dev->libinput_device);
  1243. bool isset;
  1244. bool set = litest_test_param_get_bool(test_env->params, "enable_model");
  1245. /* Test model quirks override by setting, then unsetting (or the
  1246. other way round) */
  1247. _autofree_ char *quirks_file = strdup_printf(
  1248. "[first]\n"
  1249. "MatchUdevType=mouse\n"
  1250. "ModelAppleTouchpad=%d\n"
  1251. "\n"
  1252. "[second]\n"
  1253. "MatchUdevType=mouse\n"
  1254. "ModelAppleTouchpad=%d\n",
  1255. set ? 0 : 1,
  1256. set ? 1 : 0);
  1257. litest_assert_ptr_notnull(quirks_file);
  1258. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  1259. _unref_(quirks_context) *ctx =
  1260. quirks_init_subsystem(dd->dirname,
  1261. NULL,
  1262. log_handler,
  1263. NULL,
  1264. QLOG_CUSTOM_LOG_PRIORITIES);
  1265. litest_assert_notnull(ctx);
  1266. _unref_(quirks) *q = quirks_fetch_for_device(ctx, ud);
  1267. litest_assert_notnull(q);
  1268. litest_assert(quirks_get_bool(q, QUIRK_MODEL_APPLE_TOUCHPAD, &isset));
  1269. litest_assert(isset == set);
  1270. }
  1271. END_TEST
  1272. START_TEST(quirks_model_alps)
  1273. {
  1274. struct litest_device *dev = litest_current_device();
  1275. struct libinput_device *device = dev->libinput_device;
  1276. struct quirks *q;
  1277. bool exists, value = false;
  1278. q = dev->quirks;
  1279. exists = quirks_get_bool(q, QUIRK_MODEL_ALPS_SERIAL_TOUCHPAD, &value);
  1280. if (strstr(libinput_device_get_name(device), "ALPS")) {
  1281. litest_assert(exists);
  1282. litest_assert(value);
  1283. } else {
  1284. litest_assert(!exists);
  1285. litest_assert(!value);
  1286. }
  1287. }
  1288. END_TEST
  1289. START_TEST(quirks_model_wacom)
  1290. {
  1291. struct litest_device *dev = litest_current_device();
  1292. struct quirks *q;
  1293. bool exists, value = false;
  1294. q = dev->quirks;
  1295. exists = quirks_get_bool(q, QUIRK_MODEL_WACOM_TOUCHPAD, &value);
  1296. if (libevdev_get_id_vendor(dev->evdev) == VENDOR_ID_WACOM) {
  1297. litest_assert(exists);
  1298. litest_assert(value);
  1299. } else {
  1300. litest_assert(!exists);
  1301. litest_assert(!value);
  1302. }
  1303. }
  1304. END_TEST
  1305. START_TEST(quirks_model_apple)
  1306. {
  1307. struct litest_device *dev = litest_current_device();
  1308. struct quirks *q;
  1309. bool exists, value = false;
  1310. q = dev->quirks;
  1311. exists = quirks_get_bool(q, QUIRK_MODEL_APPLE_TOUCHPAD, &value);
  1312. if (libevdev_get_id_vendor(dev->evdev) == VENDOR_ID_APPLE) {
  1313. litest_assert(exists);
  1314. litest_assert(value);
  1315. } else {
  1316. litest_assert(!exists);
  1317. litest_assert(!value);
  1318. }
  1319. }
  1320. END_TEST
  1321. START_TEST(quirks_model_synaptics_serial)
  1322. {
  1323. struct litest_device *dev = litest_current_device();
  1324. struct quirks *q;
  1325. bool exists, value = false;
  1326. q = dev->quirks;
  1327. exists = quirks_get_bool(q, QUIRK_MODEL_SYNAPTICS_SERIAL_TOUCHPAD, &value);
  1328. if (libevdev_get_id_vendor(dev->evdev) == VENDOR_ID_SYNAPTICS_SERIAL &&
  1329. libevdev_get_id_product(dev->evdev) == PRODUCT_ID_SYNAPTICS_SERIAL) {
  1330. litest_assert(exists);
  1331. litest_assert(value);
  1332. } else {
  1333. litest_assert(!exists);
  1334. litest_assert(!value);
  1335. }
  1336. }
  1337. END_TEST
  1338. START_TEST(quirks_call_NULL)
  1339. {
  1340. litest_assert(!quirks_fetch_for_device(NULL, NULL));
  1341. litest_assert(!quirks_get_uint32(NULL, QUIRK_NONE, NULL));
  1342. litest_assert(!quirks_get_int32(NULL, QUIRK_NONE, NULL));
  1343. litest_assert(!quirks_get_range(NULL, QUIRK_NONE, NULL));
  1344. litest_assert(!quirks_get_dimensions(NULL, QUIRK_NONE, NULL));
  1345. litest_assert(!quirks_get_double(NULL, QUIRK_NONE, NULL));
  1346. litest_assert(!quirks_get_string(NULL, QUIRK_NONE, NULL));
  1347. litest_assert(!quirks_get_bool(NULL, QUIRK_NONE, NULL));
  1348. }
  1349. END_TEST
  1350. START_TEST(quirks_ctx_ref)
  1351. {
  1352. struct quirks_context *ctx, *ctx2;
  1353. const char quirks_file[] =
  1354. "[Section name]\n"
  1355. "MatchUdevType=mouse\n"
  1356. "AttrSizeHint=10x10\n";
  1357. _destroy_(data_dir) *dd = data_dir_new(quirks_file);
  1358. ctx = quirks_init_subsystem(dd->dirname,
  1359. NULL,
  1360. log_handler,
  1361. NULL,
  1362. QLOG_CUSTOM_LOG_PRIORITIES);
  1363. litest_assert_notnull(ctx);
  1364. ctx2 = quirks_context_ref(ctx);
  1365. litest_assert_ptr_eq(ctx, ctx2);
  1366. ctx2 = quirks_context_unref(ctx);
  1367. litest_assert_ptr_eq(ctx2, NULL);
  1368. ctx2 = quirks_context_unref(ctx);
  1369. litest_assert_ptr_eq(ctx2, NULL);
  1370. }
  1371. END_TEST
  1372. TEST_COLLECTION(quirks)
  1373. {
  1374. /* clang-format off */
  1375. litest_add_deviceless(quirks_invalid_dir);
  1376. litest_add_deviceless(quirks_empty_dir);
  1377. litest_add_deviceless(quirks_section_empty);
  1378. litest_add_deviceless(quirks_section_double);
  1379. litest_add_deviceless(quirks_section_missing_match);
  1380. litest_add_deviceless(quirks_section_missing_attr);
  1381. litest_add_deviceless(quirks_section_match_after_attr);
  1382. litest_add_deviceless(quirks_section_duplicate_match);
  1383. litest_add_deviceless(quirks_section_duplicate_attr);
  1384. litest_add_deviceless(quirks_parse_error_section);
  1385. litest_add_deviceless(quirks_parse_error_trailing_whitespace);
  1386. litest_add_deviceless(quirks_parse_error_unknown_match);
  1387. litest_add_deviceless(quirks_parse_error_unknown_attr);
  1388. litest_add_deviceless(quirks_parse_error_unknown_model);
  1389. litest_add_deviceless(quirks_parse_error_unknown_prefix);
  1390. litest_add_deviceless(quirks_parse_error_model_not_one);
  1391. litest_add_deviceless(quirks_parse_comment_inline);
  1392. litest_add_deviceless(quirks_parse_comment_empty);
  1393. litest_add_deviceless(quirks_parse_string_quotes_single);
  1394. litest_add_deviceless(quirks_parse_string_quotes_double);
  1395. litest_add_deviceless(quirks_parse_bustype);
  1396. litest_add_deviceless(quirks_parse_bustype_invalid);
  1397. litest_add_deviceless(quirks_parse_vendor);
  1398. litest_add_deviceless(quirks_parse_vendor_invalid);
  1399. litest_add_deviceless(quirks_parse_product);
  1400. litest_add_deviceless(quirks_parse_product_too_many);
  1401. litest_add_deviceless(quirks_parse_product_invalid);
  1402. litest_add_deviceless(quirks_parse_version);
  1403. litest_add_deviceless(quirks_parse_version_invalid);
  1404. litest_add_deviceless(quirks_parse_name);
  1405. litest_add_deviceless(quirks_parse_name_invalid);
  1406. litest_add_deviceless(quirks_parse_uniq);
  1407. litest_add_deviceless(quirks_parse_uniq_invalid);
  1408. litest_add_deviceless(quirks_parse_udev);
  1409. litest_add_deviceless(quirks_parse_udev_invalid);
  1410. litest_add_deviceless(quirks_parse_dmi);
  1411. litest_add_deviceless(quirks_parse_dmi_invalid);
  1412. litest_add_for_device(quirks_parse_dimension_attr, LITEST_MOUSE);
  1413. litest_add_for_device(quirks_parse_range_attr, LITEST_MOUSE);
  1414. litest_add_for_device(quirks_parse_uint_attr, LITEST_MOUSE);
  1415. litest_add_for_device(quirks_parse_double_attr, LITEST_MOUSE);
  1416. litest_add_for_device(quirks_parse_string_attr, LITEST_MOUSE);
  1417. litest_add_for_device(quirks_parse_bool_attr, LITEST_MOUSE);
  1418. litest_add_for_device(quirks_parse_integration_attr, LITEST_MOUSE);
  1419. litest_add_for_device(quirks_model_one, LITEST_MOUSE);
  1420. litest_add_for_device(quirks_model_zero, LITEST_MOUSE);
  1421. litest_with_parameters(params, "enable_model", 'b') {
  1422. litest_add_parametrized_for_device(quirks_model_override, LITEST_MOUSE, params);
  1423. }
  1424. litest_add(quirks_model_alps, LITEST_TOUCHPAD, LITEST_ANY);
  1425. litest_add(quirks_model_wacom, LITEST_TOUCHPAD, LITEST_ANY);
  1426. litest_add(quirks_model_apple, LITEST_TOUCHPAD, LITEST_ANY);
  1427. litest_add(quirks_model_synaptics_serial, LITEST_TOUCHPAD, LITEST_ANY);
  1428. litest_add_deviceless(quirks_call_NULL);
  1429. litest_add_deviceless(quirks_ctx_ref);
  1430. /* clang-format on */
  1431. }