1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754
|
#include <usbg/usbg.h>
#include <stdio.h>
#include <stdarg.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#ifdef HAS_LIBCONFIG
#include <libconfig.h>
#endif
#include "usbg-test.h"
/**
* @file tests/test.c
*/
#define USBG_TEST(name, test, setup, teardown) \
{name, test, setup, teardown}
#define FILLED_STR(len, c) \
{ [0 ... len - 2] = c, [len - 1] = '\0' }
/* two levels of macros allow to strigify result of macro expansion */
#define STR(s) #s
#define XSTR(s) STR(s)
/* unique string */
#define UNIQUE XSTR(__COUNTER__)
#define FUNC_FROM_TYPE(t) { \
.type = t, \
.instance = "instance"UNIQUE \
}
#define CONF_FROM_BOUND(b) { \
.label = "c", \
.id = __COUNTER__, \
.bound_funcs = b \
}
static struct usbg_gadget_attrs min_gadget_attrs = {
.bcdUSB = 0x0000,
.bDeviceClass = 0x0,
.bDeviceSubClass = 0x0,
.bDeviceProtocol = 0x0,
.bMaxPacketSize0 = 0x0,
.idVendor = 0x0000,
.idProduct = 0x0000,
.bcdDevice = 0x0000
};
static struct usbg_gadget_attrs max_gadget_attrs = {
.bcdUSB = 0xffff,
.bDeviceClass = 0xff,
.bDeviceSubClass = 0xff,
.bDeviceProtocol = 0xff,
.bMaxPacketSize0 = 0xff,
.idVendor = 0xffff,
.idProduct = 0xffff,
.bcdDevice = 0xffff
};
/* PATH_MAX is limit for path length */
#define LONG_PATH_LEN PATH_MAX/2
static char long_path_str[] = FILLED_STR(LONG_PATH_LEN, 'x');
/* NAME_MAX is limit for filename length */
static char long_usbg_string[] = FILLED_STR(NAME_MAX, 'x');
static struct usbg_config_strs simple_config_strs= {
.configuration = "configuration string"
};
static struct usbg_config_attrs max_config_attrs = {
.bmAttributes = 0xff,
.bMaxPower = 0xff
};
static struct usbg_config_attrs min_config_attrs = {
.bmAttributes = 0x00,
.bMaxPower = 0x00
};
/**
* @brief Simplest udcs names
* @details Used to go through init when testing other things
*/
static char *simple_udcs[] = {
"UDC1",
"UDC2",
NULL
};
static char *long_udcs[] = {
long_usbg_string,
"UDC1",
NULL
};
/**
* @brief Simplest functions names
* @details Used to go through init when testing other things
*/
static struct test_function simple_funcs[] = {
FUNC_FROM_TYPE(USBG_F_ECM),
FUNC_FROM_TYPE(USBG_F_ACM),
TEST_FUNCTION_LIST_END
};
/**
* @brief All functions types
* @details When testing with this in state, check if all func types are
* processed correctly
*/
static struct test_function all_funcs[] = {
FUNC_FROM_TYPE(USBG_F_SERIAL),
FUNC_FROM_TYPE(USBG_F_ACM),
FUNC_FROM_TYPE(USBG_F_OBEX),
FUNC_FROM_TYPE(USBG_F_ECM),
FUNC_FROM_TYPE(USBG_F_SUBSET),
FUNC_FROM_TYPE(USBG_F_NCM),
FUNC_FROM_TYPE(USBG_F_EEM),
FUNC_FROM_TYPE(USBG_F_RNDIS),
FUNC_FROM_TYPE(USBG_F_PHONET),
FUNC_FROM_TYPE(USBG_F_FFS),
TEST_FUNCTION_LIST_END
};
static struct test_function same_type_funcs[] = {
FUNC_FROM_TYPE(USBG_F_SERIAL),
FUNC_FROM_TYPE(USBG_F_SERIAL),
FUNC_FROM_TYPE(USBG_F_SERIAL),
TEST_FUNCTION_LIST_END
};
/**
* @brief No functions at all
* @details Check if gadget with no functions (or config with no bindings)
* is processed correctly.
*/
static struct test_function no_funcs[] = {
TEST_FUNCTION_LIST_END
};
/**
* @brief Simple configs
* @details Used to pass through init when testing other things
*/
static struct test_config simple_confs[] = {
CONF_FROM_BOUND(simple_funcs),
TEST_CONFIG_LIST_END
};
/**
* @brief Configs bound to all avaible function types
*/
static struct test_config all_bindings_confs[] = {
CONF_FROM_BOUND(no_funcs),
CONF_FROM_BOUND(all_funcs),
TEST_CONFIG_LIST_END
};
#define GADGET(n, u, c, f) \
{ \
.name = n, \
.udc = u, \
.configs = c, \
.functions = f \
}
/**
* @brief Simplest gadget
*/
static struct test_gadget simple_gadgets[] = {
GADGET("g1", "UDC1", simple_confs, simple_funcs),
TEST_GADGET_LIST_END
};
/**
* @brief Gadgets with all avaible functions
*/
static struct test_gadget all_funcs_gadgets[] = {
GADGET("all_funcs_gadget1", "UDC1", all_bindings_confs, all_funcs),
TEST_GADGET_LIST_END
};
static struct test_gadget long_udc_gadgets[] = {
GADGET("long_udc_gadgets", long_usbg_string,
simple_confs, simple_funcs),
TEST_GADGET_LIST_END
};
struct test_function_attrs_data {
struct test_state *state;
void *attrs;
};
struct test_data {
struct test_state *state;
struct usbg_state *usbg_state;
};
static int simple_serial_attrs = 42;
static struct usbg_f_net_attrs simple_net_attrs = {
.dev_addr = {}, .host_addr = {}, .ifname = "if", .qmult = 1,
};
static char *simple_phonet_attrs = "if";
static char *simple_ffs_attrs = "0";
static int writable_serial_attrs = 0;
static struct usbg_f_net_attrs writable_net_attrs = {
.dev_addr = {}, .host_addr = {}, .ifname = "", .qmult = 1,
};
static char *writable_phonet_attrs = "";
static char *writable_ffs_attrs = "";
struct test_gadget_strs_data {
struct test_state *state;
struct usbg_gadget_strs *strs;
};
#define STATE(p, g, u) { \
.configfs_path = p, \
.gadgets = g, \
.udcs = u \
}
/**
* @brief Simple state
*/
static struct test_state simple_state =
STATE("config", simple_gadgets, simple_udcs);
/**
* @brief State with all functions avaible
*/
static struct test_state all_funcs_state =
STATE("all_funcs_configfs", all_funcs_gadgets, simple_udcs);
static struct test_state long_path_state =
STATE(long_path_str, simple_gadgets, simple_udcs);
static struct test_state long_udc_state =
STATE("simple_path", long_udc_gadgets, long_udcs);
static struct usbg_config_attrs *get_random_config_attrs()
{
struct usbg_config_attrs *ret;
ret = safe_malloc(sizeof(*ret));
srand(time(NULL));
ret->bmAttributes = rand() % max_config_attrs.bmAttributes;
ret->bMaxPower = rand() % max_config_attrs.bMaxPower;
return ret;
}
static struct usbg_gadget_attrs *get_random_gadget_attrs()
{
struct usbg_gadget_attrs *ret;
ret = safe_malloc(sizeof(*ret));
srand(time(NULL));
ret->bcdUSB = rand() % max_gadget_attrs.bcdUSB;
ret->bDeviceClass = rand() % max_gadget_attrs.bDeviceClass;
ret->bDeviceSubClass = rand() % max_gadget_attrs.bDeviceSubClass;
ret->bDeviceProtocol = rand() % max_gadget_attrs.bDeviceProtocol;
ret->bMaxPacketSize0 = rand() % max_gadget_attrs.bMaxPacketSize0;
ret->idVendor = rand() % max_gadget_attrs.idVendor;
ret->idProduct = rand() % max_gadget_attrs.idProduct;
ret->bcdDevice = rand() % max_gadget_attrs.bcdDevice;
return ret;
}
/**
* @brief Add given attributes to all configs in state
* @return Prepared state where configs has given attributes
*/
static void *prepare_state_with_config_attrs(struct test_state *state,
struct usbg_config_attrs *attrs)
{
struct test_gadget *tg;
struct test_config *tc;
for (tg = state->gadgets; tg->name; ++tg)
for (tc = tg->configs; tc->label; ++tc)
tc->attrs = attrs;
state = prepare_state(state);
return state;
}
static int setup_max_config_attrs_state(void **state)
{
*state = prepare_state_with_config_attrs(&simple_state,
&max_config_attrs);
return 0;
}
static int setup_min_config_attrs_state(void **state)
{
*state = prepare_state_with_config_attrs(&simple_state,
&min_config_attrs);
return 0;
}
static int setup_random_config_attrs_state(void **state)
{
*state = prepare_state_with_config_attrs(&simple_state,
get_random_config_attrs());
return 0;
}
static int setup_simple_config_strs_state(void **state)
{
struct test_gadget *tg;
struct test_config *tc;
for (tg = simple_state.gadgets; tg->name; ++tg)
for (tc = tg->configs; tc->label; ++tc)
tc->strs = &simple_config_strs;
*state = prepare_state(&simple_state);
return 0;
}
/**
* @brief Prepare test_state with one gadget containing given function list
* @details For testing only functions. We put them in a gadget as simply
* as possible.
* @param[in] func Pointer to list of functions
* @return Pointer to test state with given functions
*/
static struct test_state *put_func_in_state(struct test_function *func)
{
struct test_state *st;
struct test_gadget *g;
struct test_config *c;
char **udcs;
st = safe_calloc(1, sizeof(*st));
/* Do not need config */
c = safe_calloc(1, sizeof(*c));
g = safe_calloc(2, sizeof(*g));
udcs = safe_calloc(2, sizeof(*udcs));
g[0].name = "g1";
g[0].udc = "UDC1";
g[0].configs = c;
g[0].functions = func;
g[0].writable = 1;
udcs[0] = "UDC1";
g[0].writable = 1;
st->configfs_path = "config";
st->gadgets = g;
st->udcs = udcs;
st->writable = 1;
st = prepare_state(st);
return st;
}
/**
* @brief Setup simple state with some gadgets, configs and functions
*/
static int setup_simple_state(void **state)
{
*state = prepare_state(&simple_state);
return 0;
}
/**
* @brief Setup state with all avaible functions
*/
static int setup_all_funcs_state(void **state)
{
*state = prepare_state(&all_funcs_state);
return 0;
}
/**
* @brief Setup state with few functions of the same type
*/
static int setup_same_type_funcs_state(void **state)
{
*state = put_func_in_state(same_type_funcs);
return 0;
}
/**
* @brief Setup state with very long path name
*/
static int setup_long_path_state(void **state)
{
*state = prepare_state(&long_path_state);
return 0;
}
/**
* @brief Setup state with long udc name
*/
static int setup_long_udc_state(void **state)
{
*state = prepare_state(&long_udc_state);
return 0;
}
static void alloc_random_len_str(char **str)
{
int len;
len = rand() % USBG_MAX_FILE_SIZE;
*str = safe_malloc(len);
memset(*str, 'x', len - 1);
(*str)[len - 1] = '\0';
}
/**
* @brief Setup state with gadget strings of random length
* @param[out] state Pointer to pointer to test_gadget_strs_data structure
* with initialized state and strings
*/
static int setup_random_len_gadget_strs_data(void **state)
{
struct usbg_gadget_strs *strs;
struct test_gadget_strs_data *data;
/* will fill memory with zeros */
strs = safe_calloc(1, sizeof(*strs));
data = safe_malloc(sizeof(*data));
srand(time(NULL));
alloc_random_len_str(&strs->manufacturer);
alloc_random_len_str(&strs->product);
alloc_random_len_str(&strs->serial);
data->strs = strs;
data->state = prepare_state(&simple_state);
*state = data;
return 0;
}
static void *setup_f_attrs(int f_type, void *attrs)
{
struct test_function_attrs_data *data;
struct test_function *func;
data = safe_malloc(sizeof(*data));
func = safe_calloc(2, sizeof(*func));
func[0].type = f_type;
func[0].instance = "0";
func[0].writable = 1;
data->state = put_func_in_state(func);
data->attrs = attrs;
return data;
}
static int setup_f_serial_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_SERIAL, &simple_serial_attrs);
return 0;
}
static int setup_f_serial_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_SERIAL, &writable_serial_attrs);
return 0;
}
static int setup_f_acm_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_ACM, &simple_serial_attrs);
return 0;
}
static int setup_f_acm_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_ACM, &writable_serial_attrs);
return 0;
}
static int setup_f_obex_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_OBEX, &simple_serial_attrs);
return 0;
}
static int setup_f_obex_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_OBEX, &writable_serial_attrs);
return 0;
}
static int setup_f_ecm_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_ECM, &simple_net_attrs);
return 0;
}
static int setup_f_ecm_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_ECM, &writable_net_attrs);
return 0;
}
static int setup_f_subset_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_SUBSET, &simple_net_attrs);
return 0;
}
static int setup_f_subset_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_SUBSET, &writable_net_attrs);
return 0;
}
static int setup_f_ncm_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_NCM, &simple_net_attrs);
return 0;
}
static int setup_f_ncm_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_NCM, &writable_net_attrs);
return 0;
}
static int setup_f_eem_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_EEM, &simple_net_attrs);
return 0;
}
static int setup_f_eem_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_EEM, &writable_net_attrs);
return 0;
}
static int setup_f_rndis_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_RNDIS, &simple_net_attrs);
return 0;
}
static int setup_f_rndis_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_RNDIS, &writable_net_attrs);
return 0;
}
static int setup_f_phonet_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_PHONET, &simple_phonet_attrs);
return 0;
}
static int setup_f_phonet_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_PHONET, &writable_phonet_attrs);
return 0;
}
static int setup_f_ffs_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_FFS, &simple_ffs_attrs);
return 0;
}
static int setup_f_ffs_writable_attrs(void **state)
{
*state = setup_f_attrs(USBG_F_FFS, &writable_ffs_attrs);
return 0;
}
/**
* @brief Tests usbg_get_gadget function with given state
* @details Check if gadgets are returned correctly
*/
static void test_get_gadget(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_gadget(ts, s, assert_gadget_equal);
}
/**
* @brief Tests usbg_get_gadget with non-existing gadget name
* @details Check if get_gadget will not find non-existing gadgets and
* will not cause crash.
*/
static void test_get_gadget_fail(void **state)
{
usbg_gadget *g = NULL;
usbg_state *s = NULL;
struct test_state *st;
safe_init_with_state(state, &st, &s);
g = usbg_get_gadget(s, "non-existing-gadget");
assert_null(g);
}
/**
* @brief Tests usbg_get_first_gadget function
* @details Check if gadget returned by get_first_gadget is actually first one
*/
static void test_get_first_gadget(void **state)
{
usbg_gadget *g = NULL;
usbg_state *s = NULL;
struct test_state *st;
safe_init_with_state(state, &st, &s);
g = usbg_get_first_gadget(s);
assert_non_null(g);
assert_gadget_equal(g, &st->gadgets[0]);
}
/**
* @brief Tests get_first_gadget with invalid arguments
*/
static void test_get_first_gadget_fail(void **state)
{
usbg_gadget *g;
g = usbg_get_first_gadget(NULL);
assert_null(g);
}
static void try_get_gadget_name(usbg_gadget *g, struct test_gadget *tg)
{
const char *name;
name = usbg_get_gadget_name(g);
assert_string_equal(name, tg->name);
}
/**
* @brief Tests getting name of gadget
* @details Check if gadget name is returned correctly
*/
static void test_get_gadget_name(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_gadget(ts, s, try_get_gadget_name);
}
static void try_get_gadget_name_len(usbg_gadget *g, struct test_gadget *tg)
{
int len;
char buf;
int expected;
expected = strlen(tg->name);
len = usbg_get_gadget_name_s(g, &buf, 0);
assert_int_equal(len, expected);
}
/**
* @brief Tests getting name length of gadget
* @details Check if returned name length is equal original
*/
static void test_get_gadget_name_len(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_gadget(ts, s, try_get_gadget_name_len);
}
/**
* @brief Tests getting name of gadget with invalid arguments
* @details Check if trying to get name of wrong (non-existing) gadget
* will not cause crash, but return NULL as expected.
*/
static void test_get_gadget_name_fail(void **state)
{
const char *name;
name = usbg_get_gadget_name(NULL);
assert_null(name);
}
static void try_get_gadget_name_s(usbg_gadget *g, struct test_gadget *tg)
{
char name[USBG_MAX_NAME_LENGTH];
int ret;
ret = usbg_get_gadget_name_s(g, name, USBG_MAX_NAME_LENGTH);
assert_int_equal(ret, strlen(tg->name));
assert_string_equal(name, tg->name);
}
/**
* @brief Tests copying gadget's name
* @details Check if copying gadget name copy actual name correctly
*/
static void test_get_gadget_name_s(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_gadget(ts, s, try_get_gadget_name_s);
}
/**
* @brief Test copying gadet name with invalid arguments
* @details Check if trying to copy gadget name into non-existing buffer,
* or give invalid buffer length, or invalid gadget will be handled by library
* and return correct error codes
*/
static void test_get_gadget_name_s_fail(void **state)
{
usbg_gadget *g = NULL;
usbg_state *s = NULL;
struct test_state *st;
int i = 0;
char name[USBG_MAX_NAME_LENGTH];
int ret;
safe_init_with_state(state, &st, &s);
for (i = 0; st->gadgets[i].name; i++) {
g = usbg_get_gadget(s, st->gadgets[i].name);
assert_non_null(g);
ret = usbg_get_gadget_name_s(g, NULL, USBG_MAX_NAME_LENGTH);
assert_int_equal(ret, USBG_ERROR_INVALID_PARAM);
}
ret = usbg_get_gadget_name_s(NULL, name, USBG_MAX_NAME_LENGTH);
assert_int_equal(ret, USBG_ERROR_INVALID_PARAM);
}
/**
* @brief Tests init by comparing test state and usbg state
* @details Check if usbg state after init match given state and
* if init returned success code
*/
static void test_init(void **state)
{
usbg_state *s = NULL;
struct test_state *st;
safe_init_with_state(state, &st, &s);
assert_state_equal(s, st);
}
/**
* @brief Test getting function by name
* @param[in] state Pointer to pointer to correctly initialized
* test_state structure
*/
static void test_get_function(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_function(ts, s, assert_func_equal);
}
/**
* @brief Tests usbg_get_function with some non-existing functions
* @details Check if get function will return NULL, when invalid
* functions names and types are passed as arguments and will not cause crash.
* @param[in] state Pointer to pointer to correctly initialized
* test_state structure
*/
static void test_get_function_fail(void **state)
{
usbg_state *s = NULL;
usbg_gadget *g = NULL;
usbg_function *f = NULL;
struct test_state *st;
safe_init_with_state(state, &st, &s);
g = usbg_get_first_gadget(s);
assert_non_null(g);
f = usbg_get_function(g, USBG_F_ACM, "non-existing-instance");
assert_null(f);
f = usbg_get_function(g, 9001, "0");
assert_null(f);
}
/**
* @brief Tests function type translation to string
* @param[in] state Pointer to pointer to correctly initialized
* test_state structure
* @details Check if get_function_type_str returns proper strings for all types.
*/
static void test_get_function_type_str(void **state)
{
struct {
usbg_function_type type;
const char *str;
} types[] = {
{USBG_F_SERIAL, "gser"},
{USBG_F_ACM, "acm"},
{USBG_F_OBEX, "obex"},
{USBG_F_ECM, "ecm"},
{USBG_F_SUBSET, "geth"},
{USBG_F_NCM, "ncm"},
{USBG_F_EEM, "eem"},
{USBG_F_RNDIS, "rndis"},
{USBG_F_PHONET, "phonet"},
{USBG_F_FFS, "ffs"},
};
const char *str;
int i;
for (i = 0; i < ARRAY_SIZE(types); i++) {
str = usbg_get_function_type_str(types[i].type);
assert_non_null(str);
assert_string_equal(str, types[i].str);
}
}
static struct {
usbg_gadget_str code;
const char *name;
} gadget_str_names[] = {
{USBG_STR_PRODUCT, "product"},
{USBG_STR_MANUFACTURER, "manufacturer"},
{USBG_STR_SERIAL_NUMBER, "serialnumber"},
};
/**
* @brief Tests gadget codeing name getting
* @param[in] state Pointer to pointer to correctly initialized
* test_state structure
* @details Check if usbg_get_gadget_code_name returns proper
* codeings for all types.
*/
static void test_get_gadget_str_name(void **state)
{
const char *name;
int i;
for (i = 0; i < ARRAY_SIZE(gadget_str_names); i++) {
name = usbg_get_gadget_str_name(gadget_str_names[i].code);
assert_non_null(name);
assert_string_equal(name, gadget_str_names[i].name);
}
}
/**
* @brief Tests gadget codeing code getting by its name
* @param[in] state Pointer to pointer to correctly initialized
* test_state structure
* @details Check if usbg_lookup_gadget_code returns values matching codeings
*/
static void test_lookup_gadget_str(void **state)
{
int i, code;
for (i = 0; i < ARRAY_SIZE(gadget_str_names); i++) {
code = usbg_lookup_gadget_str(gadget_str_names[i].name);
assert_return_code(code, 0);
assert_int_equal(code, gadget_str_names[i].code);
}
}
/**
* @brief Tests function type translation to string with unknown funcs
* @param[in] state Not used parameter
* @details Check if get_function_type_str returns NULL, when given
* function type is unknown.
*/
static void test_get_function_type_str_fail(void **state)
{
const char *str;
str = usbg_get_function_type_str(-1);
assert_null(str);
}
/**
* @brief Get instance of given function and check it
* @param[in] f Usbg function
* @param[in] tf Test function which should match f
*/
static void try_get_function_instance(usbg_function *f,
struct test_function *tf)
{
const char *str;
str = usbg_get_function_instance(f);
assert_string_equal(str, tf->instance);
}
/**
* @brief Tests getting function instance from usbg_function structure
* @param[in] state Pointer to pointer to correctly initialized
* test_state structure
* @details Check if returned instance name is correct.
*/
static void test_get_function_instance(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_function(ts, s, try_get_function_instance);
}
/**
* @brief Cpy instance of given usbg function and check it
* @param[in] f Usbg function
* @param[in] tf Test function which should match f
*/
static void try_get_function_instance_s(usbg_function *f,
struct test_function *tf)
{
char str[USBG_MAX_NAME_LENGTH];
int ret;
int small_len = 2;
ret = usbg_get_function_instance_s(f, str, USBG_MAX_NAME_LENGTH);
assert_int_equal(ret, strlen(tf->instance));
assert_string_equal(str, tf->instance);
ret = usbg_get_function_instance_s(f, str, small_len);
assert_int_equal(ret, strlen(tf->instance));
assert_memory_equal(str, tf->instance, small_len - 1);
assert_int_equal(str[small_len - 1], '\0');
}
/**
* @brief Tests copying function instance from usbg_function structure
* into buffer
* @param[in] state Pointer to pointer to correctly initialized state
* @details Check if buffer contains expected string
*/
static void test_get_function_instance_s(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_function(ts, s, try_get_function_instance_s);
}
/**
* @brief Get function type and check it
* @param[in] f Usbg function
* @param[in] tf Test function which should match f by type
*/
static void try_get_function_type(usbg_function *f, struct test_function *tf)
{
usbg_function_type type;
type = usbg_get_function_type(f);
assert_int_equal(type, tf->type);
}
/**
* @brief Tests getting function type
* @details Check if getting function type returns what was expected.
* State must be proper (init must end with success).
* @param[in] state Pointer to pointer to correctly initialized state
*/
static void test_get_function_type(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_function(ts, s, try_get_function_type);
}
/**
* @brief Check if function instance length is correct
* @param[in] f Usbg function
* @param[in] tf Test function which should match f
*/
static void try_get_function_instance_len(usbg_function *f,
struct test_function *tf)
{
size_t len;
char buf;
len = usbg_get_function_instance_s(f, &buf, 0);
assert_int_equal(len, strlen(tf->instance));
}
/**
* @brief Tests getting length of function instance name
* @details Check if returned instance name length matches
* actual length of instance name
* @param[in] state Pointer to pointer to correctly initialized state
*/
static void test_get_function_instance_len(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_function(ts, s, try_get_function_instance_len);
}
/**
* @brief Tests getting configfs path from usbg state
* @param[in,out] state Pointer to pointer to correctly initialized test state.
* When finished, it contains pointer to usbg_state which should be cleaned.
*/
static void test_get_configfs_path(void **state)
{
usbg_state *s = NULL;
struct test_state *st;
const char *path;
safe_init_with_state(state, &st, &s);
path = usbg_get_configfs_path(s);
assert_path_equal(path, st->configfs_path);
}
/**
* @brief Tests getting configfs path length from usbg state
* @param[in,out] state Pointer to pointer to correctly initialized test state.
* When finished, it contains pointer to usbg_state which should be cleaned.
*/
static void test_get_configfs_path_len(void **state)
{
usbg_state *s = NULL;
struct test_state *st;
char buf;
int ret, len;
safe_init_with_state(state, &st, &s);
ret = usbg_get_configfs_path_s(s, &buf, 0);
len = strlen(st->configfs_path);
assert_int_equal(ret, len);
}
/**
* @brief Tests copying configfs path into buffer
* @param[in,out] state Pointer to pointer to correctly initialized test state.
* When finished, it contains pointer to usbg_state which should be cleaned.
*/
static void test_get_configfs_path_s(void **state)
{
usbg_state *s = NULL;
struct test_state *st;
char path[PATH_MAX];
int ret;
safe_init_with_state(state, &st, &s);
ret = usbg_get_configfs_path_s(s, path, PATH_MAX);
assert_int_equal(ret, strlen(st->configfs_path));
assert_path_equal(path, st->configfs_path);
}
/**
* @brief Tests getting config by name
* @param[in,out] state Pointer to pointer to correctly initialized test state.
* When finished, it contains pointer to usbg_state which should be cleaned.
*/
static void test_get_config(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_config(ts, s, assert_config_equal);
}
static void test_get_config_without_label(void **state)
{
usbg_state *s = NULL;
usbg_gadget *g = NULL;
usbg_config *c = NULL;
struct test_state *ts;
struct test_gadget *tg;
struct test_config *tc;
safe_init_with_state(state, &ts, &s);
for (tg = ts->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
assert_non_null(g);
for (tc = tg->configs; tc->label; tc++) {
c = usbg_get_config(g, tc->id, NULL);
assert_non_null(c);
assert_config_equal(c, tc);
}
}
}
/**
* @bried Tests getting non-existing config
* @param[in,out] state Pointer to pointer to correctly initialized test state.
* When finished, it contains pointer to usbg_state which should be cleaned.
*/
static void test_get_config_fail(void **state)
{
usbg_state *s = NULL;
usbg_gadget *g = NULL;
usbg_config *c = NULL;
struct test_state *ts;
struct test_gadget *tg;
safe_init_with_state(state, &ts, &s);
for (tg = ts->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
assert_non_null(g);
c = usbg_get_config(g, 0, "non-existing-config");
assert_null(c);
c = usbg_get_config(g, -9001, "c");
assert_null(c);
c = usbg_get_config(g, -9001, NULL);
assert_null(c);
}
}
/**
* @brief Get config label and check it
* @param[in] c Usbg config
* @param[in] tc Test config which should match c
*/
static void try_get_config_label(usbg_config *c, struct test_config *tc)
{
const char *label;
label = usbg_get_config_label(c);
assert_string_equal(label, tc->label);
}
/**
* @brief Tests getting config label
* @param[in,out] state Pointer to pointer to correctly initialized test state.
* When finished, it contains pointer to usbg_state which should be cleaned.
*/
static void test_get_config_label(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_config(ts, s, try_get_config_label);
}
/**
* @brief Check config id with test structure
* @param[in] c Usbg config
* @param[in] tc Test config which should match c
*/
static void try_get_config_id(usbg_config *c, struct test_config *tc)
{
int id;
id = usbg_get_config_id(c);
assert_int_equal(id, tc->id);
}
/**
* @brief Tests getting config id
* @param[in,out] state Pointer to pointer to correctly initialized test state.
* When finished, it contains pointer to usbg_state which should be cleaned.
*/
static void test_get_config_id(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_config(ts, s, try_get_config_id);
}
/**
* @brief Test getting given attributes from gadgets present in state
* @param[in] s Pointer to usbg state
* @param[in] ts Pointer to test state matching given usbg state
* @param[in] attrs Pointer to gadget attributes which should be put in
* virtual filesystem for writting by usbg
*/
static void try_get_gadget_attrs(usbg_state *s, struct test_state *ts,
struct usbg_gadget_attrs *attrs)
{
usbg_gadget *g = NULL;
struct usbg_gadget_attrs actual;
struct test_gadget *tg;
int ret;
for (tg = ts->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
assert_non_null(g);
push_gadget_attrs(tg, attrs);
ret = usbg_get_gadget_attrs(g, &actual);
assert_int_equal(ret, 0);
assert_gadget_attrs_equal(&actual, attrs);
}
}
/**
* @brief Tests getting gadget attributes
* @param[in] state Pointer to correctly initialized test_state structure
**/
static void test_get_gadget_attrs(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
try_get_gadget_attrs(s, ts, &min_gadget_attrs);
try_get_gadget_attrs(s, ts, &max_gadget_attrs);
try_get_gadget_attrs(s, ts, get_random_gadget_attrs());
}
/**
* @brief Test setting given attributes on gadgets present in state
* @param[in] s Pointer to usbg state
* @param[in] ts Pointer to test state matching given usbg state
* @param[in] attrs Pointer to gadget attributes to be set
*/
static void try_set_gadget_attrs(usbg_state *s, struct test_state *ts,
struct usbg_gadget_attrs *attrs)
{
usbg_gadget *g = NULL;
struct test_gadget *tg;
int ret;
for (tg = ts->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
assert_non_null(g);
pull_gadget_attrs(tg, attrs);
ret = usbg_set_gadget_attrs(g, attrs);
assert_int_equal(ret, 0);
}
}
/**
* @brief Tests setting gadget attributes
* @param[in] state Pointer to correctly initialized test_state structure
**/
static void test_set_gadget_attrs(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
try_set_gadget_attrs(s, ts, &min_gadget_attrs);
try_set_gadget_attrs(s, ts, &max_gadget_attrs);
try_set_gadget_attrs(s, ts, get_random_gadget_attrs());
}
/**
* @brief Test setting given attributes on gadgets present in state one by one,
* using functions specific for each attribute
* @param[in] s Pointer to usbg state
* @param[in] ts Pointer to test state matching given usbg state
* @param[in] attrs Pointer to gadget attributes to be set
*/
static void try_set_specific_gadget_attr(usbg_state *s, struct test_state *ts,
struct usbg_gadget_attrs *attrs)
{
usbg_gadget *g = NULL;
struct test_gadget *tg;
int ret;
int i;
int attr;
for (tg = ts->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
assert_non_null(g);
for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++) {
attr = get_gadget_attr(attrs, i);
pull_gadget_attribute(tg, i, attr);
usbg_set_gadget_attr(g, i, attr);
assert_int_equal(ret, 0);
}
}
}
/**
* @brief Tests setting gadget attributes one by one
* @param[in] state Pointer to correctly initialized test_state structure
**/
static void test_set_specific_gadget_attr(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
try_set_specific_gadget_attr(s, ts, &min_gadget_attrs);
try_set_specific_gadget_attr(s, ts, &max_gadget_attrs);
try_set_specific_gadget_attr(s, ts, get_random_gadget_attrs());
}
/**
* @brief Tests getting udc from state
* @param[in] state Pointer to correctly initialized test_state structure
**/
void test_get_udc(void **state)
{
struct test_state *ts;
char **tu;
struct test_gadget *tg;
usbg_state *s = NULL;
usbg_udc *u = NULL;
usbg_gadget *g = NULL;
safe_init_with_state(state, &ts, &s);
for (tu = ts->udcs; *tu; tu++) {
u = usbg_get_udc(s, *tu);
assert_non_null(u);
assert_string_equal(*tu, u->name);
assert_int_equal(s, u->parent);
}
for (tg = ts->gadgets; tg->name; tg++) {
u = usbg_get_udc(s, tg->udc);
g = usbg_get_gadget(s, tg->name);
assert_int_equal(u->gadget, g);
}
}
static void test_get_gadget_attr_str(void **state)
{
struct {
usbg_gadget_attr attr;
const char *str;
} attrs[] = {
{USBG_BCD_USB, "bcdUSB"},
{USBG_B_DEVICE_CLASS, "bDeviceClass"},
{USBG_B_DEVICE_SUB_CLASS, "bDeviceSubClass"},
{USBG_B_DEVICE_PROTOCOL, "bDeviceProtocol"},
{USBG_B_MAX_PACKET_SIZE_0, "bMaxPacketSize0"},
{USBG_ID_VENDOR, "idVendor"},
{USBG_ID_PRODUCT, "idProduct"},
{USBG_BCD_DEVICE, "bcdDevice"},
};
const char *str;
int i, j;
for (i = 0; i < ARRAY_SIZE(attrs); i++) {
str = usbg_get_gadget_attr_str(attrs[i].attr);
assert_non_null(str);
assert_string_equal(str, attrs[i].str);
}
/* Check if iteration over values works */
for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; ++i) {
str = usbg_get_gadget_attr_str(i);
assert_non_null(str);
for (j = 0; j < ARRAY_SIZE(attrs); ++j)
if (attrs[j].attr == i) {
assert_string_equal(str, attrs[j].str);
break;
}
assert_int_not_equal(j, ARRAY_SIZE(attrs));
}
}
static void test_get_gadget_attr_str_fail(void **state)
{
const char *str;
str = usbg_get_gadget_attr_str(USBG_GADGET_ATTR_MIN - 1);
assert_null(str);
str = usbg_get_gadget_attr_str(USBG_GADGET_ATTR_MAX);
assert_null(str);
}
/**
* @brief set gadget strings
* @details Also do it one by one
* @param[in] data Pointer to correctly initialized
* test_gadget_strs_data structure
*/
static void test_set_gadget_strs(void **data)
{
struct test_gadget_strs_data *ts;
struct test_gadget *tg;
usbg_state *s = NULL;
usbg_gadget *g = NULL;
int i;
int ret;
ts = (struct test_gadget_strs_data *)(*data);
*data = NULL;
init_with_state(ts->state, &s);
*data = s;
for (tg = ts->state->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
pull_gadget_strs(tg, LANG_US_ENG, ts->strs);
ret = usbg_set_gadget_strs(g, LANG_US_ENG, ts->strs);
assert_int_equal(ret, 0);
for (i = 0; i < GADGET_STR_MAX; i++)
pull_gadget_string(tg, LANG_US_ENG, i,
get_gadget_str(ts->strs, i));
ret = usbg_set_gadget_manufacturer(g, LANG_US_ENG,
ts->strs->manufacturer);
assert_int_equal(ret, 0);
ret = usbg_set_gadget_product(g, LANG_US_ENG,
ts->strs->product);
assert_int_equal(ret, 0);
ret = usbg_set_gadget_serial_number(g, LANG_US_ENG,
ts->strs->serial);
assert_int_equal(ret, 0);
for (i = 0; i < GADGET_STR_MAX; i++)
pull_gadget_string(tg, LANG_US_ENG,
i, get_gadget_str(ts->strs, i));
ret = usbg_set_gadget_str(g, USBG_STR_MANUFACTURER,
LANG_US_ENG, ts->strs->manufacturer);
assert_int_equal(ret, 0);
ret = usbg_set_gadget_str(g, USBG_STR_PRODUCT,
LANG_US_ENG, ts->strs->product);
assert_int_equal(ret, 0);
ret = usbg_set_gadget_str(g, USBG_STR_SERIAL_NUMBER,
LANG_US_ENG, ts->strs->serial);
assert_int_equal(ret, 0);
}
}
/**
* @brief get gadget strings
* @param[in] data Pointer to correctly initialized
* test_gadget_strs_data structure
*/
static void test_get_gadget_strs(void **data)
{
struct test_gadget_strs_data *ts;
struct test_gadget *tg;
usbg_state *s = NULL;
usbg_gadget *g = NULL;
struct usbg_gadget_strs strs;
ts = (struct test_gadget_strs_data *)(*data);
*data = NULL;
init_with_state(ts->state, &s);
*data = s;
for (tg = ts->state->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
push_gadget_strs(tg, LANG_US_ENG, ts->strs);
usbg_get_gadget_strs(g, LANG_US_ENG, &strs);
assert_gadget_strs_equal(&strs, ts->strs);
usbg_free_gadget_strs(&strs);
}
}
/**
* @brief Get binding target
* @details Check if given function is target of given binding
* @param[in] tb Test function
* @param[in] b Binding
*/
static void try_get_binding_target(struct test_binding *tb, usbg_binding *b)
{
usbg_function *f;
f = usbg_get_binding_target(b);
assert_non_null(f);
assert_func_equal(f, tb->target);
}
/**
* @brief Test get binding target
* @details Test all bindings present in given state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_get_binding_target(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_binding(ts, s, try_get_binding_target);
}
/**
* @brief Get binding name
* @details Check if name of given binding is equal name of given function
* @param[in] tb Test function
* @param[in] b Binding
*/
static void try_get_binding_name(struct test_binding *tb, usbg_binding *b)
{
const char *s;
s = usbg_get_binding_name(b);
assert_non_null(s);
assert_string_equal(s, tb->name);
}
/**
* @brief Test get bindig name from all binding present in state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_get_binding_name(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_binding(ts, s, try_get_binding_name);
}
/**
* @brief Get binding name length
* @param[in] tb Test function
* @param[in] b Binding
*/
static void try_get_binding_name_len(struct test_binding *tb, usbg_binding *b)
{
int n;
char buf;
n = usbg_get_binding_name_s(b, &buf, 0);
assert_int_equal(n, strlen(tb->name));
}
/**
* @brief Test get binding name length from all bindings present in state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_get_binding_name_len(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_binding(ts, s, try_get_binding_name_len);
}
/**
* @brief Set config strings
* @param[in] c Config on which string should be set
* @param[in] tc Test config containing strings to be set
*/
static void try_set_config_strs(usbg_config *c, struct test_config *tc)
{
pull_config_strs(tc, LANG_US_ENG, tc->strs);
usbg_set_config_strs(c, LANG_US_ENG, tc->strs);
}
/**
* @brief Test setting strings
* @details Set strings in all configs present in state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_set_config_strs(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_config(ts, s, try_set_config_strs);
}
/**
* @brief Set strings one by one on config
* @param[in] c Config on which string should be set
* @param[in] tc Test config containing strings to be set
*/
static void try_set_config_string(usbg_config *c, struct test_config *tc)
{
pull_config_string(tc, LANG_US_ENG, tc->strs->configuration);
usbg_set_config_string(c, LANG_US_ENG, tc->strs->configuration);
}
/**
* @brief Test setting strings one by one
* @details Set strings on all configs present in given state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_set_config_string(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_config(ts, s, try_set_config_string);
}
/**
* @brief Get config strings
* @details Assume that given configs have the same strings
* @param[in] c Config from which strings should be get
* @param[in] tc Test config expected to have the same string as c
*/
static void try_get_config_strs(usbg_config *c, struct test_config *tc)
{
struct usbg_config_strs strs;
push_config_strs(tc, LANG_US_ENG, tc->strs);
usbg_get_config_strs(c, LANG_US_ENG, &strs);
assert_string_equal(tc->strs->configuration, strs.configuration);
usbg_free_config_strs(&strs);
}
/**
* @brief Test getting congig strings
* @details Get config strings on all configs present in given state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_get_config_strs(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_config(ts, s, try_get_config_strs);
}
/**
* @brief Get config attributes
* @details Assume that attributes which will be returned are the same as
* given test state contains.
* @param[in] c Usbg config
* @param[in] tc Test config with set attributes
*/
static void try_get_config_attrs(usbg_config *c, struct test_config *tc)
{
struct usbg_config_attrs attrs;
push_config_attrs(tc, tc->attrs);
usbg_get_config_attrs(c, &attrs);
assert_config_attrs_equal(tc->attrs, &attrs);
}
/**
* @brief Test getting config attributes
* @details Get config attributes on all configfs in state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_get_config_attrs(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_config(ts, s, try_get_config_attrs);
}
/**
* @brief Set config attributes in given config
* @param[in] c Usbg config
* @param[in] tc Test config with attributes which will be set
*/
static void try_set_config_attrs(usbg_config *c, struct test_config *tc)
{
pull_config_attrs(tc, tc->attrs);
usbg_set_config_attrs(c, tc->attrs);
}
/**
* @brief Test setting config attributes
* @details Set config attributes on all configs in state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_set_config_attrs(void **state)
{
usbg_state *s = NULL;
struct test_state *ts;
safe_init_with_state(state, &ts, &s);
for_each_test_config(ts, s, try_set_config_attrs);
}
/**
* @brieg Test creating config
* @details Start with empty gadgets, add all functions from given state
* @param[in, out] state Pointer to pointer to correctly initialized test state,
* will point to usbg state when finished.
*/
static void test_create_config(void **state)
{
usbg_state *s = NULL;
usbg_gadget *g = NULL;
usbg_config *c = NULL;
struct test_state *ts;
struct test_state *empty;
struct test_gadget *tg;
struct test_config *tc;
ts = (struct test_state *)(*state);
*state = NULL;
empty = build_empty_gadget_state(ts);
init_with_state(empty, &s);
*state = s;
for (tg = ts->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
assert_non_null(g);
for (tc = tg->configs; tc->label; tc++) {
pull_create_config(tc);
usbg_create_config(g, tc->id, tc->label,
tc->attrs, tc->strs, &c);
assert_config_equal(c, tc);
}
}
}
/**
* @brief Start with empty gadget, add all functions from given one
*/
static void test_create_function(void **state)
{
usbg_state *s = NULL;
usbg_gadget *g = NULL;
usbg_function *f = NULL;
struct test_state *ts;
struct test_state *empty;
struct test_gadget *tg;
struct test_function *tf;
ts = (struct test_state *)(*state);
*state = NULL;
empty = build_empty_gadget_state(ts);
init_with_state(empty, &s);
*state = s;
for (tg = ts->gadgets; tg->name; tg++) {
g = usbg_get_gadget(s, tg->name);
assert_non_null(g);
for (tf = tg->functions; tf->instance; tf++) {
pull_create_function(tf);
usbg_create_function(g, tf->type, tf->instance,
tf->attrs, &f);
assert_func_equal(f, tf);
}
}
}
/**
* @brief Test only one given function for attribute getting
* @param[in] state Pointer to pointer to correctly initialized state
*/
static void test_get_function_attrs(void **state)
{
struct test_function_attrs_data *data;
usbg_state *s;
usbg_function *f;
usbg_gadget *g;
union {
struct usbg_f_net_attrs net;
char *ffs_dev_name;
struct usbg_f_ms_attrs ms;
struct usbg_f_midi_attrs midi;
int serial_port_num;
char *phonet_ifname;
} actual;
int ret;
data = (struct test_function_attrs_data *)(*state);
*state = NULL;
init_with_state(data->state, &s);
*state = s;
g = usbg_get_first_gadget(s);
assert_non_null(g);
f = usbg_get_first_function(g);
assert_non_null(f);
push_function_attrs(&data->state->gadgets[0].functions[0], data->attrs);
ret = usbg_get_function_attrs(f, &actual);
assert_int_equal(ret, 0);
assert_function_attrs_equal(&actual, data->attrs, f->type);
usbg_cleanup_function_attrs(f, &actual);
}
/**
* @brief Test setting attributes in only one given function
* @param[in] state Pointer to pointer to correctly initialized state
*/
static void test_set_function_attrs(void **state)
{
struct test_function_attrs_data *data;
usbg_state *s;
usbg_function *f;
usbg_gadget *g;
int ret;
data = (struct test_function_attrs_data *)(*state);
*state = NULL;
init_with_state(data->state, &s);
*state = s;
g = usbg_get_first_gadget(s);
assert_non_null(g);
f = usbg_get_first_function(g);
assert_non_null(f);
pull_function_attrs(&data->state->gadgets[0].functions[0], data->attrs);
ret = usbg_set_function_attrs(f, data->attrs);
assert_int_equal(ret, 0);
}
/**
*
* @brief cleanup usbg state
*/
static int teardown_state(void **state)
{
usbg_state *s = NULL;
s = (usbg_state *)(*state);
if (s != NULL)
usbg_cleanup(s);
cleanup_stack();
return 0;
}
/* Custom macro for defining test with given name and fixed teardown function */
#define USBG_TEST_TS(name, test, setup) \
USBG_TEST(name, test, setup, teardown_state)
/**
* @page usbg_tests Tests
* @brief This is list of test cases
* @tests_start
*/
#ifndef DOXYGEN
static struct CMUnitTest tests[] = {
#endif
/**
* @usbg_test
* @test_desc{test_init_simple,
* Check if init was successfull on simple configfs state,
* usbg_init}
*/
USBG_TEST_TS("test_init_simple",
test_init, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_init_all_funcs,
* Check if init was successfull when all avaible functions
* are present in configfs,
* usbg_init}
*/
USBG_TEST_TS("test_init_all_funcs",
test_init, setup_all_funcs_state),
/**
* @usbg_test
* @test_desc{test_init_long_path,
* Try to initialize libusbg with long configfs path,
* usbg_init}
*/
USBG_TEST_TS("test_init_long_path",
test_init, setup_long_path_state),
/**
* @usbg_test
* @test_desc{test_init_long_udc,
* Try to initialize libusbg with long udc name,
* usbg_init}
*/
USBG_TEST_TS("test_init_long_udc",
test_init, setup_long_udc_state),
/**
* @usbg_test
* @test_desc{test_get_gadget_simple,
* Check if simple gadget will be correcty returned,
* usbg_get_gadget}
*/
USBG_TEST_TS("test_get_gadget_simple",
test_get_gadget, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_gadget_fail_simple,
* Check if getting non-existing and wrong gadgets cause
* expected failure and error codes are correct,
* usbg_get_gadget}
*/
USBG_TEST_TS("test_get_gadget_fail_simple",
test_get_gadget_fail, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_first_gadget_simple,
* Check if gadget returned by get_first_gadget
* is actually first one,
* usbg_get_first_gadget}
*/
USBG_TEST_TS("test_get_first_gadget_simple",
test_get_first_gadget, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_first_gadget_fail,
* Check if getting first gadget from state returns NULL when
* invalid parameters are passed,
* usbg_get_first_gadget}
*/
unit_test(test_get_first_gadget_fail),
/**
* @usbg_test
* @test_desc{test_get_gadget_name_simple,
* Check if returned gadget name matches expected value,
* usbg_get_gadget_name}
*/
USBG_TEST_TS("test_get_gadget_name_simple",
test_get_gadget_name, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_gadget_name_len,
* Check if returned simple gadget name length matches expected value,
* usbg_get_gadget_name}
*/
USBG_TEST_TS("test_get_gadget_name_len_simple",
test_get_gadget_name_len, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_gadget_name_fail,
* Check if trying to get name of invalid gadget
* cause expected failure (name is null),
* usbg_get_gadget_name}
*/
unit_test(test_get_gadget_name_fail),
/**
* @usbg_test
* @test_desc{test_get_gadget_name_s_simple,
* Check if getting simple gadget name into buffer work as expected,
* usbg_get_gadget_name_s}
*/
USBG_TEST_TS("test_get_gadget_name_s_simple",
test_get_gadget_name_s, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_gadget_name_s_fail_simple,
* Check if writting gadget name into buffer fail when
* invalid parameters are passed,
* usbg_get_gadget_name_s}
*/
USBG_TEST_TS("test_get_gadget_name_s_fail_simple",
test_get_gadget_name_s_fail, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_function_simple,
* Check if function can be correctly get from simple state,
* usbg_get_function}
*/
USBG_TEST_TS("test_get_function_simple",
test_get_function, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_function_all_funcs,
* Check if getting function work on all function types,
* usbg_get_function}
*/
USBG_TEST_TS("test_get_function_all_funcs",
test_get_function, setup_all_funcs_state),
/**
* @usbg_test
* @test_desc{test_get_function_same_type_funcs,
* Check if having multiple functions with the same type does not
* cause failure
* usbg_get_function}
*/
USBG_TEST_TS("test_get_function_same_type_funcs",
test_get_function, setup_same_type_funcs_state),
/**
* @usbg_test
* @test_desc{test_get_function_fail_simple,
* Check if trying to get invalid function's name ends
* with expected error,
* usbg_get_function}
*/
USBG_TEST_TS("test_get_function_fail_simple",
test_get_function_fail, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_function_instance_simple,
* Check if getting simple instance returns what expected,
* usbg_get_function_instance}
*/
USBG_TEST_TS("test_get_function_instance_simple",
test_get_function_instance, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_function_instance_s_simple,
* Check if copying simple instance into buffer returns what expected,
* usbg_get_function_instance_s}
*/
USBG_TEST_TS("test_get_function_instance_s_simple",
test_get_function_instance_s, setup_all_funcs_state),
/**
* @usbg_test
* @test_desc{test_get_function_type_simple,
* Check if function type is returned correctly,
* usbg_get_function_type}
*/
USBG_TEST_TS("test_get_function_type_simple",
test_get_function_type, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_function_type_all_funcs,
* Check if all function types are returned correctly,
* usbg_get_function_type}
*/
USBG_TEST_TS("test_get_function_type_all_funcs",
test_get_function_type, setup_all_funcs_state),
/**
* @usbg_test
* @test_desc{test_get_function_instance_len_simple,
* Check if function instance length is returned correctly,
* usbg_get_function_instance_len}
*/
USBG_TEST_TS("test_get_function_instance_len_simple",
test_get_function_instance_len, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_function_type_str,
* Compare returned function types strings with expected values,
* usbg_get_function_type_str}
*/
unit_test(test_get_function_type_str),
/**
* @usbg_test
* @test_desc{test_get_function_type_str_fail,
* Try to get type string of unknown type,
* usbg_get_function_type_str}
*/
unit_test(test_get_function_type_str_fail),
/**
* @usbg_test
* @test_desc{test_get_configfs_path_simple,
* heck if simple configfs path was returned correctly,
* usbg_get_configfs_path}
*/
USBG_TEST_TS("test_get_configfs_path_simple",
test_get_configfs_path, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_configfs_path_len,
* Check if configfs path length is correctly calculated,
* usbg_get_configfs_path_len}
*/
USBG_TEST_TS("test_get_configfs_path_len_simple",
test_get_configfs_path_len, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_configfs_path_s_simple,
* Copy simple configfs path into buffer and compare with original,
* usbg_get_configfs_path_s}
*/
USBG_TEST_TS("test_get_configfs_path_s_simple",
test_get_configfs_path_s, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_config_simple,
* Check if returned simple config matches original one,
* usbg_get_config}
*/
USBG_TEST_TS("test_get_config_simple",
test_get_config, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_config_without_label_simple,
* Check if returned simple config matches original one,
* usbg_get_config}
*/
USBG_TEST_TS("test_get_config_without_label_simple",
test_get_config_without_label, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_config_fail,
* Check if trying to get non-existing or invalid config
* fails as expected,
* usbg_get_config}*/
USBG_TEST_TS("test_get_config_fail",
test_get_config_fail, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_config_label_simple,
* Check if returned simple config label matches original one,
* usbg_get_config_label}
*/
USBG_TEST_TS("test_get_config_label_simple",
test_get_config_label, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_config_id_simple,
* Check if returned simple config id matches original one,
* usbg_get_config_id}
*/
USBG_TEST_TS("test_get_config_id_simple",
test_get_config_id, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_gadget_attrs_simple,
* Get gadget attributes list and compare them with original,
* usbg_get_gadget_attrs}
*/
USBG_TEST_TS("test_get_gadget_attrs_simple",
test_get_gadget_attrs, setup_simple_state),
/**
* @usbg_tets
* @test_desc{test_set_gadget_attrs_simple,
* Set gadget attributes list\, check if everything is wrote
* as expected,
* usbg_set_gadget_attrs}
*/
USBG_TEST_TS("test_set_gadget_attrs_simple",
test_set_gadget_attrs, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_set_specific_gadget_attr_simple,
* Set gadget attributes one by one,
* usbg_set_gadget_attrs}
*/
USBG_TEST_TS("test_set_specific_gadget_attr_simple",
test_set_specific_gadget_attr, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_udc_simple,
* Get udc name from state,
* usbg_get_udc}
*/
USBG_TEST_TS("test_get_udc_simple",
test_get_udc, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_udc_long,
* Get udc name witch very long name,
* usbg_get_udc}
*/
USBG_TEST_TS("test_get_udc_long",
test_get_udc, setup_long_udc_state),
/**
* @usbg_test
* @test_desc{test_get_gadget_attr_str,
* Compare returned gadget attribute strings witch expected values
* usbg_get_gadget_attr_str}
*/
unit_test(test_get_gadget_attr_str),
/**
* @usbg_test
* @test_desc{test_get_gadget_attr_str_fail,
* Check returned gadget attribute strings for invalid arguments
* usbg_get_gadget_attr_str}
*/
unit_test(test_get_gadget_attr_str_fail),
/**
* @usbg_test
* @test_desc{test_set_gadget_strs_random,
* Set gadget strings of random length,
* usbg_set_gadget_strs}
*/
USBG_TEST_TS("test_set_gadget_strs_random",
test_set_gadget_strs, setup_random_len_gadget_strs_data),
/**
* @usbg_test
* @test_desc{test_get_gadget_strs_random,
* Get gadget strings,
* usbg_get_gadget_strs}
*/
USBG_TEST_TS("test_get_gadget_strs_random",
test_get_gadget_strs, setup_random_len_gadget_strs_data),
/**
* @usbg_test
* @test_desc{test_get_binding_target_simple,
* Get binding target,
* usbg_get_binding_target}
*/
USBG_TEST_TS("test_get_binding_target_simple",
test_get_binding_target, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_binding_name_simple,
* Get binding name,
* usbg_get_binding_name}
*/
USBG_TEST_TS("test_get_binding_name_simple",
test_get_binding_name, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_get_binding_name_len_simple,
* Get binding name length,
* usbg_get_binding_name_len}
*/
USBG_TEST_TS("test_get_binding_name_len_simple",
test_get_binding_name_len, setup_simple_state),
/**
* @usbg_test
* @test_desc{test_set_config_strs_simple,
* Set simple strings in set of configurations,
* usbg_set_config_strs}
*/
USBG_TEST_TS("test_set_config_strs_simple",
test_set_config_strs, setup_simple_config_strs_state),
/**
* @usbg_test
* @test_desc{test_set_config_string_simple,
* Set simple string in set of configurations,
* usbg_set_config_string}
*/
USBG_TEST_TS("test_set_config_string_simple",
test_set_config_string, setup_simple_config_strs_state),
/**
* @usbg_test
* @test_desc{test_get_config_strs_simple,
* Get simple strings from set of configurations,
* usbg_get_config_strs}
*/
USBG_TEST_TS("test_get_config_strs_simple",
test_get_config_strs, setup_simple_config_strs_state),
/**
* @usbg_test
* @test_desc{test_get_config_attrs_max,
* Get config attributes with max values,
* usbg_get_config_attrs}
*/
USBG_TEST_TS("test_get_config_attrs_max",
test_get_config_attrs, setup_max_config_attrs_state),
/**
* @usbg_test
* @test_desc{test_get_config_attrs_min,
* Get config attributes with minimum values,
* usbg_get_config_attrs}
*/
USBG_TEST_TS("test_get_config_attrs_min",
test_get_config_attrs, setup_min_config_attrs_state),
/**
* @usbg_test
* @test_desc{test_get_config_attrs_random,
* Get config attributes with random values,
* usbg_get_config_attrs}
*/
USBG_TEST_TS("test_get_config_attrs_random",
test_get_config_attrs, setup_random_config_attrs_state),
/**
* @usbg_test
* @test_desc{test_set_config_attrs_max,
* Set config attributes with max values,
* usbg_set_config_attrs}
*/
USBG_TEST_TS("test_set_config_attrs_max",
test_set_config_attrs, setup_max_config_attrs_state),
/**
* @usbg_test
* @test_desc{test_set_config_attrs_min,
* Set config attributes with minimum values,
* usbg_set_config_attrs}
*/
USBG_TEST_TS("test_set_config_attrs_min",
test_set_config_attrs, setup_min_config_attrs_state),
/**
* @usbg_test
* @test_desc{test_set_config_attrs_random,
* Set config attributes with random values,
* usbg_set_config_attrs}
*/
USBG_TEST_TS("test_set_config_attrs_random",
test_set_config_attrs, setup_random_config_attrs_state),
/**
* @usbg_test
* @test_desc{test_create_config_random,
* Create config with random attributes
* usbg_create_config}
*/
USBG_TEST_TS("test_create_config_random",
test_create_config, setup_random_config_attrs_state),
/**
* @usbg_test
* @test_desc{test_get_f_serial_attrs,
* Get f_serial function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_serial_attrs",
test_get_function_attrs, setup_f_serial_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_obex_attrs,
* Get f_obex function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_obex_attrs",
test_get_function_attrs, setup_f_obex_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_acm_attrs,
* Get f_acm function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_acm_attrs",
test_get_function_attrs, setup_f_acm_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_ecm_attrs,
* Get f_ecm function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_ecm_attrs",
test_get_function_attrs, setup_f_ecm_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_eem_attrs,
* Get f_eem function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_eem_attrs",
test_get_function_attrs, setup_f_eem_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_subset_attrs,
* Get f_subset function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_subset_attrs",
test_get_function_attrs, setup_f_subset_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_ncm_attrs,
* Get f_ncm function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_ncm_attrs",
test_get_function_attrs, setup_f_ncm_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_serial_attrs,
* Get f_rndis function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_rndis_attrs",
test_get_function_attrs, setup_f_rndis_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_phonet_attrs,
* Get f_phonet function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_phonet_attrs",
test_get_function_attrs, setup_f_phonet_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_serial_attrs,
* Get f_ffs function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_get_f_ffs_attrs",
test_get_function_attrs, setup_f_ffs_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_serial_attrs,
* Set f_serial function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_serial_attrs",
test_set_function_attrs, setup_f_serial_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_acm_attrs,
* Set f_acm function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_acm_attrs",
test_set_function_attrs, setup_f_acm_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_serial_obex,
* Set f_obex function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_obex_attrs",
test_set_function_attrs, setup_f_obex_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_ecm_attrs,
* Set f_ecm function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_ecm_attrs",
test_set_function_attrs, setup_f_ecm_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_eem_attrs,
* Set f_eem function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_eem_attrs",
test_set_function_attrs, setup_f_eem_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_subset_attrs,
* Set f_subset function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_subset_attrs",
test_set_function_attrs, setup_f_subset_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_ncm_attrs,
* Set f_ncm function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_ncm_attrs",
test_set_function_attrs, setup_f_ncm_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_serial_attrs,
* Set f_rndis function attributes,
* usbg_get_function_attrs}
*/
USBG_TEST_TS("test_set_f_rndis_attrs",
test_set_function_attrs, setup_f_rndis_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_phonet_attrs,
* Set f_phonet function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_phonet_attrs",
test_set_function_attrs, setup_f_phonet_writable_attrs),
/**
* @usbg_test
* @test_desc{test_get_f_serial_attrs,
* Set f_ffs function attributes,
* usbg_set_function_attrs}
*/
USBG_TEST_TS("test_set_f_ffs_attrs",
test_set_function_attrs, setup_f_ffs_writable_attrs),
/**
* @usbg_test
* @test_desc{test_create_all_functions,
* Create full set of functions in empty state,
* usbg_get_binding_name_len}
*/
USBG_TEST_TS("test_create_all_functions",
test_create_function, setup_all_funcs_state),
/**
* @usbg_test
* @test_desc{test_get_gadget_str_name,
* Compare returned gadget string name with expected
* usbg_get_gadget_str_name}
*/
unit_test(test_get_gadget_str_name),
/**
* @usbg_test
* @test_desc{test_lookup_gadget_str,
* Compare returned gadget string code with expected
* usbg_lookup_gadget_str}
*/
unit_test(test_lookup_gadget_str),
#ifndef DOXYGEN
};
#endif
/**
* @usbg_test
* @tests_end
*/
#define TESTS_TAG "tests"
/* for autotools compability */
#define SKIPPED_CODE 77
#ifdef HAS_LIBCONFIG
static int gen_test_config(FILE *output)
{
config_t cfg;
config_setting_t *root;
config_setting_t *tests_node, *node;
int i;
int ret = SKIPPED_CODE, cfg_ret = 0;
config_init(&cfg);
config_set_tab_width(&cfg, 4);
root = config_root_setting(&cfg);
tests_node = config_setting_add(root, TESTS_TAG, CONFIG_TYPE_LIST);
if (!tests_node) {
ret = -ENOMEM;
goto out;
}
for (i = 0; i < ARRAY_SIZE(tests); ++i) {
node = config_setting_add(tests_node, NULL, CONFIG_TYPE_STRING);
if (!node) {
ret = -ENOMEM;
goto out;
}
cfg_ret = config_setting_set_string(node, tests[i].name);
if (cfg_ret != CONFIG_TRUE) {
ret = -EINVAL;
goto out;
}
}
config_write(&cfg, output);
out:
config_destroy(&cfg);
return ret;
}
#else
static int gen_test_config(FILE *output)
{
fprintf(stderr, "Libconfig is not supported\n");
return -ENOTSUP;
}
#endif /* HAS_LIBCONFIG */
__attribute__((unused))
static int lookup_test(const char *name)
{
int i;
for (i = 0; i < ARRAY_SIZE(tests); ++i)
if (!strcmp(name, tests[i].name))
return i;
return -1;
}
__attribute__((unused))
static void test_skipped(void **state)
{
skip();
}
#ifdef HAS_LIBCONFIG
static int apply_test_config(FILE *input)
{
config_t cfg;
config_setting_t *root;
config_setting_t *tests_node, *node;
int i, count, ind;
int ret = 0, cfg_ret = 0;
const char *test_name;
char selected[ARRAY_SIZE(tests)];
for (i = 0; i < ARRAY_SIZE(selected); ++i)
selected[i] = 0;
config_init(&cfg);
cfg_ret = config_read(&cfg, input);
if (cfg_ret != CONFIG_TRUE) {
fprintf(stderr, "Wrong config format\n");
ret = -EINVAL;
goto out;
}
root = config_root_setting(&cfg);
tests_node = config_setting_get_member(root, TESTS_TAG);
if (!tests_node || !config_setting_is_list(tests_node)) {
fprintf(stderr, "Missing or incorrect tests list\n");
ret = -EINVAL;
goto out;
}
count = config_setting_length(tests_node);
for (i = 0; i < count; ++i) {
node = config_setting_get_elem(tests_node, i);
if (!node) {
ret = -EINVAL;
goto out;
}
test_name = config_setting_get_string(node);
if (!test_name) {
fprintf(stderr,
"Incorrect tests list. Element %d\n", i);
ret = -EINVAL;
goto out;
}
ind = lookup_test(test_name);
if (ind < 0) {
fprintf(stderr, "Test %s not found.\n", test_name);
ret = -EINVAL;
goto out;
}
selected[ind] = 1;
}
for (i = 0; i < ARRAY_SIZE(selected); ++i) {
if (selected[i])
continue;
tests[i].test_func = &test_skipped;
tests[i].setup_func = NULL;
tests[i].teardown_func = NULL;
}
out:
config_destroy(&cfg);
return ret;
}
#else
static int apply_test_config(FILE *input)
{
fprintf(stderr, "Libconfig is not supported\n");
return -ENOTSUP;
}
#endif /* HAS_LIBCONFIG */
static void print_help()
{
fprintf(stderr,
"libusbgx test suit:\n"
" --generate-config - generates config to stdout and exit\n"
" --use-config - runs test suit using config from stdin\n"
" -h --help - print this message\n"
);
}
int main(int argc, char **argv)
{
enum {
GENERATE_CONFIG = 0x01,
USE_CONFIG = 0x02,
};
int options = 0;
int opt;
int ret = -EINVAL;
static struct option long_options[] = {
{"generate-config", no_argument, 0, 1},
{"use-config", no_argument, 0, 2},
{"help", no_argument, 0, 'h'},
{NULL, 0, 0, 0}
};
while (1) {
opt = getopt_long(argc, argv, "h", long_options, NULL);
if (opt < 0)
break;
switch (opt) {
case 1:
options |= GENERATE_CONFIG;
break;
case 2:
options |= USE_CONFIG;
break;
case 'h':
default:
print_help();
goto out;
}
}
if (optind < argc ||
((options & GENERATE_CONFIG) &&
(options & USE_CONFIG))) {
print_help();
goto out;
}
if (options & GENERATE_CONFIG) {
ret = gen_test_config(stdout);
goto out;
}
if (options & USE_CONFIG) {
ret = apply_test_config(stdin);
if (ret)
goto out;
}
ret = cmocka_run_group_tests(tests, NULL, NULL);
out:
return ret;
}
|