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
|
#include <usbg/usbg.h>
#include <stdio.h>
#include <stdarg.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <limits.h>
#include <errno.h>
#include <time.h>
#include "usbg-test.h"
static struct simple_stack{
void *ptr;
struct simple_stack *next;
} *cleanup_top = NULL;
static const char *gadget_str_names[] = {
"manufacturer",
"product",
"serialnumber",
};
static const char *config_attr_names[] = {
"MaxPower",
"bmAttributes"
};
static attr_format config_attr_format[] = {
[MAX_POWER] = FORMAT_DEC,
[BM_ATTRIBUTES] = FORMAT_HEX
};
void free_later(void *ptr)
{
struct simple_stack *t;
t = malloc(sizeof(*t));
t->ptr = ptr;
t->next = cleanup_top;
cleanup_top = t;
}
void cleanup_stack()
{
struct simple_stack *t;
while (cleanup_top) {
free(cleanup_top->ptr);
t = cleanup_top->next;
free(cleanup_top);
cleanup_top = t;
}
}
/* Represent last file/dir opened, next should have bigger numbers.*/
static int file_id = 0;
static int dir_id = 0;
#define PUSH_FILE(file, content, len) do {\
file_id++;\
expect_path(fopen, path, file);\
will_return(fopen, file_id);\
expect_value(fread, stream, file_id);\
will_return(fread, content);\
will_return(fread, len);\
expect_value(fclose, fp, file_id);\
will_return(fclose, 0);\
} while(0)
#define PUSH_FILE_STR(file, content) \
PUSH_FILE(file, content, strlen(content) + 1);
#define PUSH_EMPTY_DIR(p) do {\
expect_string(scandir, dirp, p);\
will_return(scandir, 0);\
} while(0)
#define EXPECT_OPENDIR(n) do {\
dir_id++;\
expect_path(opendir, name, n);\
will_return(opendir, 0);\
will_return(opendir, dir_id);\
expect_value(closedir, dirp, dir_id);\
will_return(closedir, 0);\
} while(0)
#define EXPECT_OPENDIR_ERROR(n, e) do {\
expect_path(opendir, name, n);\
will_return(opendir, e);\
will_return(opendir, NULL);\
} while(0)
#define PUSH_DIR(p, c) do {\
expect_path(scandir, dirp, p);\
will_return(scandir, c);\
} while(0)
#define PUSH_DIR_ENTRY(name, type) do {\
will_return(scandir, name);\
will_return(scandir, type);\
will_return(scandir, 1);\
} while(0)
#define PUSH_LINK(p, c, len) do {\
expect_path(readlink, path, p);\
expect_in_range(readlink, bufsiz, len, INT_MAX);\
will_return(readlink, c);\
} while(0)
#define EXPECT_WRITE(file, content, len) do { \
file_id++;\
expect_path(fopen, path, file);\
will_return(fopen, file_id);\
expect_value(fwrite, stream, file_id);\
expect_memory(fwrite, ptr, content, len); \
will_return(fwrite, len);\
expect_value(fclose, fp, file_id);\
will_return(fclose, 0);\
} while(0)
#define EXPECT_WRITE_STR(file, content)\
EXPECT_WRITE(file, content, strlen(content) + 1)
#define EXPECT_HEX_WRITE(file, content) do {\
file_id++;\
expect_path(fopen, path, file);\
will_return(fopen, file_id);\
expect_value(fwrite, stream, file_id);\
expect_check(fwrite, ptr, hex_str_equal_display_error, content);\
will_return(fwrite, 0);\
expect_value(fclose, fp, file_id);\
will_return(fclose, 0);\
} while(0)
#define EXPECT_MKDIR(p) do {\
expect_path(mkdir, pathname, p);\
expect_value(mkdir, mode, 00777);\
will_return(mkdir, 0);\
} while(0)
/**
* @brief Compare test gadgets' names
*/
static int test_gadget_cmp(struct test_gadget *a, struct test_gadget *b)
{
return strcoll(a->name, b->name);
}
/**
* @brief Compare test functions' names
*/
static int test_function_cmp(struct test_function *a, struct test_function *b)
{
return strcoll(a->name, b->name);
}
/**
* @brief Compare test bindings' names
*/
static int test_binding_cmp(struct test_binding *a, struct test_binding *b)
{
return strcoll(a->name, b->name);
}
/**
* @brief Compare test configs' names
*/
static int test_config_cmp(struct test_config *a, struct test_config *b)
{
return strcoll(a->name, b->name);
}
void prepare_binding(struct test_binding *b, struct test_function *f,
char *fpath)
{
if (!f->name)
prepare_function(f, fpath);
if (!b->name) {
b->name = strdup(f->name);
if (b->name == NULL)
fail();
free_later(b->name);
}
b->target = f;
}
void prepare_config(struct test_config *c, char *cpath, char *fpath)
{
int count = 0;
struct test_function *f;
struct test_binding *b;
int i;
safe_asprintf(&c->name, "%s.%d", c->label, c->id);
c->path = cpath;
/* check if bindings has been already filled */
if (!c->bindings) {
for (f = c->bound_funcs; f->instance; f++)
count++;
c->bindings = safe_calloc(count + 1, sizeof(*c->bindings));
} else {
for (b = c->bindings; b->name; b++)
count++;
}
for (i = 0; i < count; i++)
prepare_binding(&c->bindings[i], &c->bound_funcs[i], fpath);
qsort(c->bindings, count, sizeof(*c->bindings),
(int (*)(const void *, const void *))test_binding_cmp);
}
void prepare_function(struct test_function *f, char *path)
{
const char *func_type;
func_type = usbg_get_function_type_str(f->type);
if (func_type == NULL)
fail();
safe_asprintf(&f->name, "%s.%s", func_type, f->instance);
f->path = path;
}
void prepare_gadget(struct test_state *state, struct test_gadget *g)
{
struct test_config *c;
struct test_function *f;
char *fpath;
char *cpath;
int count;
g->path = strdup(state->path);
if (!g->path)
fail();
free_later(g->path);
safe_asprintf(&fpath, "%s/%s/functions", g->path, g->name);
count = 0;
for (f = g->functions; f->instance; f++) {
prepare_function(f, fpath);
count++;
}
/* Path needs to be known somehow when list is empty */
f->path = fpath;
qsort(g->functions, count, sizeof(*g->functions),
(int (*)(const void *, const void *))test_function_cmp);
safe_asprintf(&cpath, "%s/%s/configs", g->path, g->name);
count = 0;
for (c = g->configs; c->label; c++) {
prepare_config(c, cpath, fpath);
count++;
}
/* Path needs to be known somehow when list is empty */
c->path = cpath;
qsort(g->configs, count, sizeof(*g->configs),
(int (*)(const void *, const void *))test_config_cmp);
}
static void cpy_test_function(struct test_function *to,
struct test_function *from)
{
/* Reuse instance */
to->instance = from->instance;
to->type = from->type;
/* path and name is not being copied because
it has not been allocated now */
to->writable = 1;
}
static struct test_function *dup_test_functions(struct test_function *functions)
{
struct test_function *f, *nf, *new_functions;
int count = 0;
for (f = functions; f->instance; ++f)
++count;
new_functions = safe_calloc(count + 1, sizeof(*f));
for (f = functions, nf = new_functions; f->instance; ++f, ++nf)
cpy_test_function(nf, f);
return new_functions;
}
static struct test_function *get_new_binding_target(struct test_function *which,
struct test_function *old,
int count,
struct test_function *new)
{
struct test_function *ret = NULL;
/* Should duplicate function? */
if (which < old || ((which - old) > count)) {
/* We may need to do a deep copy */
if (!which->writable) {
ret = safe_calloc(1, sizeof(*ret));
cpy_test_function(ret, which);
} else {
ret = which;
}
} else if (old != new) {
/* Function has been copied in bound_funcs so just
set new address */
ret = which - old + new;
} else {
/* Functions are reused so leave address as is */
ret = which;
}
return ret;
}
static void cpy_test_binding(struct test_binding *to,
struct test_binding *from,
struct test_function *old,
int func_count,
struct test_function *new)
{
/* Reuse name */
to->name = from->name;
to->target = get_new_binding_target(from->target, old, func_count, new);
to->writable = 1;
}
static struct test_binding *dup_test_bindings(struct test_binding *bindings,
struct test_function *old,
int func_count,
struct test_function *new)
{
struct test_binding *b, *nb, *new_bindings;
int count = 0;
for (b = bindings; b->name; ++b)
++count;
new_bindings = safe_calloc(count + 1, sizeof(*b));
for (b = bindings, nb = new_bindings; b->name; ++b, ++nb)
cpy_test_binding(nb, b, old, func_count, new);
return new_bindings;
}
static void cpy_test_config(struct test_config *to,
struct test_config *from)
{
int func_count = 0;
struct test_function *f;
struct test_binding *b;
/* Reuse label */
to->label = from->label;
to->id = from->id;
to->strs = from->strs;
to->attrs = from->attrs;
if (from->bound_funcs) {
/* If at least one function is not writable
we have to copy all of them */
for (f = from->bound_funcs; f->instance; ++f) {
++func_count;
if (!f->writable && !to->bound_funcs) {
to->bound_funcs =
dup_test_functions(from->bound_funcs);
}
}
if (!f->name && !to->bound_funcs)
to->bound_funcs = from->bound_funcs;
}
/* If bindings are set copy also them */
if (from->bindings) {
/* If at least one function is not writable
we have to copy all of them */
for (b = from->bindings; b->name; ++b)
if (!b->writable)
to->bindings =
dup_test_bindings(from->bindings,
from->bound_funcs,
func_count,
to->bound_funcs);
/* if we are reusing binding we have to translate target
address to new one which is writable */
if (!b->name && !to->bindings) {
to->bindings = from->bindings;
for (b = from->bindings; b->name; ++b)
b->target =
get_new_binding_target(
b->target,
from->bound_funcs,
func_count,
to->bound_funcs);
}
}
to->writable = 1;
}
static struct test_config *dup_test_configs(struct test_config *configs)
{
struct test_config *c, *nc, *new_configs;
int count = 0;
for (c = configs; c->label; ++c)
++count;
new_configs = safe_calloc(count + 1, sizeof(*c));
for (c = configs, nc = new_configs; c->label; ++c, ++nc)
cpy_test_config(nc, c);
return new_configs;
}
static void cpy_test_gadget(struct test_gadget *to, struct test_gadget *from)
{
struct test_function *f;
struct test_config *c;
/* Reuse name and udc */
to->name = from->name;
to->udc = from->udc;
/* path is not being copied because it has not been allocated */
/* If at least one function is not writable
we have to copy all of them */
for (f = from->functions; f->instance; ++f)
if (!f->writable) {
to->functions =
dup_test_functions(from->functions);
break;
}
if (!f->name && !to->functions)
to->functions = from->functions;
/* If at least one config is not writable
we have to copy all of them */
for (c = from->configs; c->label; ++c)
if (!c->writable) {
to->configs = dup_test_configs(from->configs);
break;
}
if (!c->name && !to->configs)
to->configs = from->configs;
to->writable = 1;
}
static struct test_gadget *dup_test_gadgets(struct test_gadget *gadgets)
{
struct test_gadget *g, *ng, *new_gadgets;
int count = 0;
for (g = gadgets; g->name; ++g)
++count;
new_gadgets = safe_calloc(count + 1, sizeof(*g));
for (g = gadgets, ng = new_gadgets; g->name; ++g, ++ng)
cpy_test_gadget(ng, g);
return new_gadgets;
}
static struct test_state *dup_test_state(struct test_state *state)
{
struct test_state *new_state;
struct test_gadget *g;
new_state = safe_calloc(1, sizeof(*new_state));
/* We don't copy configfs path because it is never changed
if you would like to free it before test end replace
this code with strdup */
new_state->configfs_path = state->configfs_path;
/* path is not being copied because it has not been allocated */
/* If at least one gadget is not writable we have to copy all of them */
for (g = state->gadgets; g->name; ++g)
if (!g->writable) {
new_state->gadgets =
dup_test_gadgets(state->gadgets);
break;
}
if (!g->name && !new_state->gadgets)
new_state->gadgets = state->gadgets;
/* udcs are also never changed so leave them as they are */
new_state->udcs = state->udcs;
new_state->writable = 1;
return new_state;
}
struct test_state *prepare_state(struct test_state *state)
{
struct test_gadget *g;
struct test_state *new_state;
int count = 0;
if (!state->writable)
new_state = dup_test_state(state);
else
new_state = state;
safe_asprintf(&(new_state->path), "%s/usb_gadget",
new_state->configfs_path);
for (g = new_state->gadgets; g->name; g++) {
prepare_gadget(new_state, g);
count++;
}
qsort(new_state->gadgets, count, sizeof(*new_state->gadgets),
(int (*)(const void *, const void *))test_gadget_cmp);
return new_state;
}
struct test_state *build_empty_gadget_state(struct test_state *ts)
{
struct test_state *ret;
struct test_gadget *tg;
int count = 0;
ret = safe_malloc(sizeof(*ret));
ret->udcs = ts->udcs;
ret->configfs_path = ts->configfs_path;
for (tg = ts->gadgets; tg->name; ++tg)
count++;
ret->gadgets = safe_calloc(count+1, sizeof(*ts->gadgets));
memcpy(ret->gadgets, ts->gadgets, count*sizeof(*ts->gadgets));
for (tg = ret->gadgets; tg->name; ++tg) {
tg->configs = safe_calloc(1, sizeof(*tg->configs));
tg->functions = safe_calloc(1, sizeof(*tg->functions));
}
return prepare_state(ret);
}
/* Simulation of configfs for init */
static void push_binding(struct test_config *conf, struct test_binding *binding)
{
char *s_path;
char *d_path;
safe_asprintf(&s_path, "%s/%s/%s",
conf->path, conf->name, binding->name);
safe_asprintf(&d_path, "%s/%s",
binding->target->path, binding->target->name);
PUSH_LINK(s_path, d_path, USBG_MAX_PATH_LENGTH - 1);
}
static void push_config(struct test_config *c)
{
struct test_binding *b;
int count = 0;
char *path;
safe_asprintf(&path, "%s/%s", c->path, c->name);
for (b = c->bindings; b->name; b++)
count++;
PUSH_DIR(path, count);
for (b = c->bindings; b->name; b++) {
PUSH_DIR_ENTRY(b->name, DT_LNK);
push_binding(c, b);
}
}
static void push_gadget(struct test_gadget *g)
{
int count;
struct test_config *c;
struct test_function *f;
char *path, *os_desc_path;
safe_asprintf(&path, "%s/%s/UDC", g->path, g->name);
PUSH_FILE_STR(path, g->udc);
count = 0;
for (f = g->functions; f->instance; f++)
count++;
PUSH_DIR(f->path, count);
for (f = g->functions; f->instance; f++)
PUSH_DIR_ENTRY(f->name, DT_DIR);
count = 0;
for (c = g->configs; c->label; c++)
count++;
PUSH_DIR(c->path, count);
for (c = g->configs; c->label; c++)
PUSH_DIR_ENTRY(c->name, DT_DIR);
for (c = g->configs; c->label; c++)
push_config(c);
safe_asprintf(&os_desc_path, "%s/%s/os_desc", g->path, g->name);
PUSH_DIR(os_desc_path, 0);
}
void push_init(struct test_state *state)
{
char **udc;
struct test_gadget *g;
int count = 0;
EXPECT_OPENDIR(state->path);
for (udc = state->udcs; *udc; udc++)
count++;
PUSH_DIR("/sys/class/udc", count);
for (udc = state->udcs; *udc; udc++)
PUSH_DIR_ENTRY(*udc, DT_REG);
count = 0;
for (g = state->gadgets; g->name; g++)
count++;
PUSH_DIR(state->path, count);
for (g = state->gadgets; g->name; g++) {
PUSH_DIR_ENTRY(g->name, DT_DIR);
}
for (g = state->gadgets; g->name; g++)
push_gadget(g);
}
int get_gadget_attr(struct usbg_gadget_attrs *attrs, usbg_gadget_attr attr)
{
int ret = -1;
switch (attr) {
case USBG_BCD_USB:
ret = attrs->bcdUSB;
break;
case USBG_B_DEVICE_CLASS:
ret = attrs->bDeviceClass;
break;
case USBG_B_DEVICE_SUB_CLASS:
ret = attrs->bDeviceSubClass;
break;
case USBG_B_DEVICE_PROTOCOL:
ret = attrs->bDeviceProtocol;
break;
case USBG_B_MAX_PACKET_SIZE_0:
ret = attrs->bMaxPacketSize0;
break;
case USBG_ID_VENDOR:
ret = attrs->idVendor;
break;
case USBG_ID_PRODUCT:
ret = attrs->idProduct;
break;
case USBG_BCD_DEVICE:
ret = attrs->bcdDevice;
break;
default:
ret = -1;
break;
}
return ret;
}
void pull_gadget_attribute(struct test_gadget *gadget,
usbg_gadget_attr attr, int value)
{
char *path;
char *content;
safe_asprintf(&path, "%s/%s/%s",
gadget->path, gadget->name,
usbg_get_gadget_attr_str(attr));
safe_asprintf(&content, "0x%x\n", value);
EXPECT_HEX_WRITE(path, content);
}
void push_gadget_attribute(struct test_gadget *gadget,
usbg_gadget_attr attr, int value)
{
char *path;
char *content;
safe_asprintf(&path, "%s/%s/%s",
gadget->path, gadget->name,
usbg_get_gadget_attr_str(attr));
safe_asprintf(&content, "0x%x\n", value);
PUSH_FILE_STR(path, content);
}
void push_gadget_attrs(struct test_gadget *gadget,
struct usbg_gadget_attrs *attrs)
{
int i;
for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++)
push_gadget_attribute(gadget, i, get_gadget_attr(attrs, i));
}
void pull_gadget_attrs(struct test_gadget *gadget,
struct usbg_gadget_attrs *attrs)
{
int i;
for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++)
pull_gadget_attribute(gadget, i, get_gadget_attr(attrs, i));
}
void init_with_state(struct test_state *in, usbg_state **out)
{
int usbg_ret;
push_init(in);
usbg_ret = usbg_init(in->configfs_path, out);
assert_int_equal(usbg_ret, USBG_SUCCESS);
}
void safe_init_with_state(void **state, struct test_state **ts, usbg_state **s)
{
*ts = (struct test_state *)(*state);
*state = NULL;
init_with_state(*ts, s);
*state = *s;
}
static int get_config_attr(struct usbg_config_attrs *attrs, config_attr attr)
{
int ret;
switch (attr) {
case MAX_POWER:
ret = attrs->bMaxPower;
break;
case BM_ATTRIBUTES:
ret = attrs->bmAttributes;
break;
default:
ret = -1;
break;
}
return ret;
}
void push_config_attribute(struct test_config *config, config_attr attr,
int value)
{
char *path;
char *content;
safe_asprintf(&path, "%s/%s/%s",
config->path, config->name, config_attr_names[attr]);
switch (config_attr_format[attr]) {
case FORMAT_HEX:
safe_asprintf(&content, "0x%x\n", value);
break;
case FORMAT_DEC:
safe_asprintf(&content, "%d\n", value);
break;
}
PUSH_FILE_STR(path, content);
}
void push_config_attrs(struct test_config *config,
struct usbg_config_attrs *attrs)
{
int i;
for (i = 0; i < CONFIG_ATTR_MAX; ++i)
push_config_attribute(config, i, get_config_attr(attrs, i));
}
void pull_config_attribute(struct test_config *config, config_attr attr,
int value)
{
char *path;
char *content;
safe_asprintf(&path, "%s/%s/%s",
config->path, config->name, config_attr_names[attr]);
switch (config_attr_format[attr]) {
case FORMAT_HEX:
safe_asprintf(&content, "0x%x\n", value);
break;
case FORMAT_DEC:
safe_asprintf(&content, "%d\n", value);
break;
}
switch (config_attr_format[attr]) {
case FORMAT_HEX:
EXPECT_HEX_WRITE(path, content);
break;
case FORMAT_DEC:
EXPECT_WRITE_STR(path, content);
break;
}
}
void pull_config_attrs(struct test_config *config,
struct usbg_config_attrs *attrs)
{
int i;
for (i = 0; i < CONFIG_ATTR_MAX; ++i)
pull_config_attribute(config, i, get_config_attr(attrs, i));
}
const char *get_gadget_str(struct usbg_gadget_strs *strs, gadget_str str)
{
const char *ret = NULL;
switch (str) {
case STR_SER:
ret = strs->serial;
break;
case STR_MNF:
ret = strs->manufacturer;
break;
case STR_PRD:
ret = strs->product;
break;
default:
ret = NULL;
break;
}
return ret;
}
static void pull_gadget_str_dir(struct test_gadget *gadget, int lang)
{
char *dir;
int tmp;
safe_asprintf(&dir, "%s/%s/strings/0x%x",
gadget->path, gadget->name, lang);
srand(time(NULL));
tmp = rand() % 2;
if (tmp) {
EXPECT_OPENDIR(dir);
} else {
EXPECT_OPENDIR_ERROR(dir, ENOENT);
EXPECT_MKDIR(dir);
}
}
static void pull_gadget_str(struct test_gadget *gadget, const char *attr_name,
int lang, const char *content)
{
char *path;
safe_asprintf(&path, "%s/%s/strings/0x%x/%s",
gadget->path, gadget->name, lang, attr_name);
EXPECT_WRITE_STR(path, content);
}
void pull_gadget_string(struct test_gadget *gadget, int lang,
gadget_str str, const char *content)
{
pull_gadget_str_dir(gadget, lang);
pull_gadget_str(gadget, gadget_str_names[str], lang, content);
}
void pull_gadget_strs(struct test_gadget *gadget, int lang,
struct usbg_gadget_strs *strs)
{
int i;
pull_gadget_str_dir(gadget, lang);
for (i = 0; i < GADGET_STR_MAX; i++)
pull_gadget_str(gadget, gadget_str_names[i],
lang, get_gadget_str(strs, i));
}
static void push_gadget_str_dir(struct test_gadget *gadget, int lang)
{
char *dir;
safe_asprintf(&dir, "%s/%s/strings/0x%x",
gadget->path, gadget->name, lang);
EXPECT_OPENDIR(dir);
}
static void push_gadget_str(struct test_gadget *gadget, const char *attr_name,
int lang, const char *content)
{
char *path;
safe_asprintf(&path, "%s/%s/strings/0x%x/%s",
gadget->path, gadget->name, lang, attr_name);
PUSH_FILE_STR(path, content);
}
void push_gadget_strs(struct test_gadget *gadget, int lang,
struct usbg_gadget_strs *strs)
{
int i;
push_gadget_str_dir(gadget, lang);
for (i = 0; i < GADGET_STR_MAX; i++)
push_gadget_str(gadget, gadget_str_names[i],
lang, get_gadget_str(strs, i));
}
void pull_config_string(struct test_config *config, int lang, const char *str)
{
char *path;
int tmp;
safe_asprintf(&path, "%s/%s/strings/0x%x",
config->path, config->name, lang);
srand(time(NULL));
tmp = rand() % 2;
if (tmp) {
EXPECT_OPENDIR(path);
} else {
EXPECT_OPENDIR_ERROR(path, ENOENT);
EXPECT_MKDIR(path);
}
safe_asprintf(&path, "%s/configuration", path);
EXPECT_WRITE_STR(path, str);
}
void pull_config_strs(struct test_config *config, int lang,
struct usbg_config_strs *strs)
{
pull_config_string(config, lang, strs->configuration);
}
void push_config_string(struct test_config *config, int lang, const char *str)
{
char *path;
safe_asprintf(&path, "%s/%s/strings/0x%x",
config->path, config->name, lang);
EXPECT_OPENDIR(path);
safe_asprintf(&path, "%s/configuration", path);
PUSH_FILE_STR(path, str);
}
void push_config_strs(struct test_config *config, int lang,
struct usbg_config_strs *strs)
{
push_config_string(config, lang, strs->configuration);
}
void assert_config_attrs_equal(struct usbg_config_attrs *actual,
struct usbg_config_attrs *expected)
{
assert_int_equal(actual->bmAttributes, expected->bmAttributes);
assert_int_equal(actual->bMaxPower, expected->bMaxPower);
}
void pull_create_config(struct test_config *tc)
{
char *path;
safe_asprintf(&path, "%s/%s", tc->path, tc->name);
EXPECT_MKDIR(path);
if (tc->attrs)
pull_config_attrs(tc, tc->attrs);
if (tc->strs)
pull_config_strs(tc, LANG_US_ENG, tc->strs);
}
#define ETHER_ADDR_STR_LEN 19
static void push_serial_attrs(struct test_function *func,
int *port_num)
{
char *path;
char *content;
safe_asprintf(&path, "%s/%s/port_num", func->path, func->name);
safe_asprintf(&content, "%d\n", *port_num);
PUSH_FILE_STR(path, content);
}
static void push_net_attrs(struct test_function *func,
struct usbg_f_net_attrs *attrs)
{
char *path;
char *content;
safe_asprintf(&path, "%s/%s/dev_addr", func->path, func->name);
content = safe_malloc(ETHER_ADDR_STR_LEN * sizeof(char));
ether_ntoa_r(&attrs->dev_addr, content);
PUSH_FILE_STR(path, content);
path = safe_malloc(USBG_MAX_PATH_LENGTH * sizeof(char));
sprintf(path, "%s/%s/host_addr",
func->path, func->name);
content = safe_malloc(ETHER_ADDR_STR_LEN * sizeof(char));
ether_ntoa_r(&attrs->host_addr, content);
PUSH_FILE_STR(path, content);
safe_asprintf(&path, "%s/%s/ifname", func->path, func->name);
safe_asprintf(&content, "%s\n", attrs->ifname);
PUSH_FILE_STR(path, content);
safe_asprintf(&path, "%s/%s/qmult", func->path, func->name);
safe_asprintf(&content, "%d\n", attrs->qmult);
PUSH_FILE_STR(path, content);
safe_asprintf(&path, "%s/%s/class", func->path, func->name);
safe_asprintf(&content, "%d\n", attrs->class_);
PUSH_FILE_STR(path, content);
safe_asprintf(&path, "%s/%s/subclass", func->path, func->name);
safe_asprintf(&content, "%d\n", attrs->subclass);
PUSH_FILE_STR(path, content);
safe_asprintf(&path, "%s/%s/protocol", func->path, func->name);
safe_asprintf(&content, "%d\n", attrs->protocol);
PUSH_FILE_STR(path, content);
}
static void push_phonet_attrs(struct test_function *func,
char **ifname)
{
char *path;
char *content;
safe_asprintf(&path, "%s/%s/ifname", func->path, func->name);
safe_asprintf(&content, "%s\n", *ifname);
PUSH_FILE_STR(path, content);
}
void push_function_attrs(struct test_function *func, void *function_attrs)
{
switch (func->type) {
case USBG_F_ACM:
case USBG_F_OBEX:
case USBG_F_SERIAL:
push_serial_attrs(func, function_attrs);
break;
case USBG_F_ECM:
case USBG_F_SUBSET:
case USBG_F_NCM:
case USBG_F_EEM:
case USBG_F_RNDIS:
push_net_attrs(func, function_attrs);
break;
case USBG_F_PHONET:
push_phonet_attrs(func, function_attrs);
break;
case USBG_F_FFS:
// ffs does not exist in filesystem
default:
break;
}
}
static void pull_function_net_attrs(struct test_function *func,
struct usbg_f_net_attrs *attrs)
{
char *path;
char *content;
safe_asprintf(&path, "%s/%s/dev_addr", func->path, func->name);
content = safe_malloc(ETHER_ADDR_STR_LEN * sizeof(char));
usbg_ether_ntoa_r(&attrs->dev_addr, content);
EXPECT_WRITE_STR(path, content);
safe_asprintf(&path, "%s/%s/host_addr", func->path, func->name);
content = safe_malloc(ETHER_ADDR_STR_LEN * sizeof(char));
usbg_ether_ntoa_r(&attrs->host_addr, content);
EXPECT_WRITE_STR(path, content);
safe_asprintf(&path, "%s/%s/qmult", func->path, func->name);
safe_asprintf(&content, "%d\n", attrs->qmult);
EXPECT_WRITE_STR(path, content);
safe_asprintf(&path, "%s/%s/class", func->path, func->name);
safe_asprintf(&content, "%d\n", attrs->class_);
EXPECT_WRITE_STR(path, content);
safe_asprintf(&path, "%s/%s/subclass", func->path, func->name);
safe_asprintf(&content, "%d\n", attrs->subclass);
EXPECT_WRITE_STR(path, content);
safe_asprintf(&path, "%s/%s/protocol", func->path, func->name);
safe_asprintf(&content, "%d\n", attrs->protocol);
EXPECT_WRITE_STR(path, content);
}
void pull_function_attrs(struct test_function *func, void *attrs)
{
switch (func->type) {
case USBG_F_ECM:
case USBG_F_SUBSET:
case USBG_F_NCM:
case USBG_F_EEM:
case USBG_F_RNDIS:
pull_function_net_attrs(func, attrs);
break;
default:
break;
}
}
void pull_create_function(struct test_function *tf)
{
char *path;
int tmp;
tmp = asprintf(&path, "%s/%s", tf->path, tf->name);
if (tmp < 0)
fail();
free_later(path);
EXPECT_MKDIR(path);
if (tf->attrs)
pull_function_attrs(tf, tf->attrs);
}
void assert_func_equal(usbg_function *f, struct test_function *expected)
{
assert_string_equal(f->instance, expected->instance);
assert_int_equal(f->type, expected->type);
assert_path_equal(f->path, expected->path);
}
void assert_binding_equal(usbg_binding *b, struct test_binding *expected)
{
assert_string_equal(b->name, expected->name);
assert_func_equal(b->target, expected->target);
}
void assert_config_equal(usbg_config *c, struct test_config *expected)
{
int i = 0;
usbg_binding *b;
assert_int_equal(c->id, expected->id);
assert_string_equal(c->label, expected->label);
assert_path_equal(c->path, expected->path);
usbg_for_each_binding(b, c)
assert_binding_equal(b, &expected->bindings[i++]);
}
void assert_gadget_equal(usbg_gadget *g, struct test_gadget *expected)
{
usbg_config *c;
usbg_function *f;
int i;
assert_string_equal(g->name, expected->name);
assert_path_equal(g->path, expected->path);
i = 0;
usbg_for_each_function(f, g)
assert_func_equal(f, &expected->functions[i++]);
i = 0;
usbg_for_each_config(c, g)
assert_config_equal(c, &expected->configs[i++]);
}
void assert_state_equal(usbg_state *s, struct test_state *expected)
{
usbg_gadget *g;
int i = 0;
assert_path_equal(s->path, expected->path);
assert_path_equal(s->configfs_path, expected->configfs_path);
usbg_for_each_gadget(g, s)
assert_gadget_equal(g, &expected->gadgets[i++]);
}
#define SIGNUM(x) (((x) > 0) - ((x) < 0))
int path_cmp(const char *actual, const char *expected)
{
const char *a = actual;
const char *b = expected;
while (*a != '\0' && *b != '\0') {
if (*a != *b)
break;
do
++a;
while (*a == '/');
do
++b;
while (*b == '/');
}
return SIGNUM(*a - *b);
}
int path_equal_display_error(const LargestIntegralType actual,
const LargestIntegralType expected)
{
if (path_cmp((const char *)actual, (const char *)expected) == 0) {
return 1;
}
fprintf(stderr, "%s != %s\n",
(const char *)actual, (const char *)expected);
return 0;
}
void assert_path_equal(const char *actual, const char *expected)
{
if (path_equal_display_error(
cast_to_largest_integral_type(actual),
cast_to_largest_integral_type(expected)) == 0)
fail();
}
int hex_str_cmp(const char *actual, const char *expected)
{
int a, b;
sscanf(actual, "%x", &a);
sscanf(expected, "%x", &b);
return SIGNUM(a - b);
}
int hex_str_equal_display_error(const LargestIntegralType actual,
const LargestIntegralType expected)
{
if (hex_str_cmp((const char *)actual, (const char *)expected) == 0) {
return 1;
}
fprintf(stderr, "%s != %s\n",
(const char *)actual, (const char *)expected);
return 0;
}
void assert_gadget_attrs_equal(struct usbg_gadget_attrs *actual,
struct usbg_gadget_attrs *expected)
{
int i;
for (i = USBG_GADGET_ATTR_MIN; i < USBG_GADGET_ATTR_MAX; i++)
assert_int_equal(get_gadget_attr(actual, i),
get_gadget_attr(expected, i));
}
void assert_gadget_strs_equal(struct usbg_gadget_strs *actual,
struct usbg_gadget_strs *expected)
{
int i;
for (i = 0; i < GADGET_STR_MAX; i++)
assert_string_equal(get_gadget_str(actual, i),
get_gadget_str(expected, i));
}
void assert_f_serial_attrs_equal(int *actual, int *expected)
{
assert_int_equal(*actual, *expected);
}
static void assert_ether_addrs_equal(const struct ether_addr *ea1,
const struct ether_addr *ea2)
{
assert_memory_equal(ea1->ether_addr_octet, ea2->ether_addr_octet,
ETHER_ADDR_LEN);
}
void assert_f_net_attrs_equal(struct usbg_f_net_attrs *actual,
struct usbg_f_net_attrs *expected)
{
assert_ether_addrs_equal(&actual->dev_addr, &expected->dev_addr);
assert_ether_addrs_equal(&actual->host_addr, &expected->host_addr);
assert_string_equal(actual->ifname, expected->ifname);
assert_int_equal(actual->qmult, expected->qmult);
assert_int_equal(actual->class_, expected->class_);
assert_int_equal(actual->subclass, expected->subclass);
assert_int_equal(actual->protocol, expected->protocol);
}
void assert_f_phonet_attrs_equal(char **actual, char **expected)
{
assert_string_equal(*actual, *expected);
}
void assert_f_ffs_attrs_equal(char **actual, char **expected)
{
assert_string_equal(*actual, *expected);
}
void assert_function_attrs_equal(void *actual, void *expected,
usbg_function_type type)
{
switch (type) {
case USBG_F_ACM:
case USBG_F_OBEX:
case USBG_F_SERIAL:
assert_f_serial_attrs_equal(actual, expected);
break;
case USBG_F_ECM:
case USBG_F_SUBSET:
case USBG_F_NCM:
case USBG_F_EEM:
case USBG_F_RNDIS:
assert_f_net_attrs_equal(actual, expected);
break;
case USBG_F_PHONET:
assert_f_phonet_attrs_equal(actual, expected);
break;
case USBG_F_FFS:
assert_f_ffs_attrs_equal(actual, expected);
break;
default:
fail();
}
}
void for_each_test_function(struct test_state *ts, usbg_state *s,
FunctionTest fun)
{
struct test_gadget *tg;
struct test_function *tf;
usbg_gadget *g = NULL;
usbg_function *f = NULL;
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) {
f = usbg_get_function(g, tf->type, tf->instance);
fun(f, tf);
}
}
}
void for_each_test_config(struct test_state *ts, usbg_state *s, ConfigTest fun)
{
usbg_gadget *g = NULL;
usbg_config *c = NULL;
struct test_gadget *tg;
struct test_config *tc;
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, tc->label);
fun(c, tc);
}
}
}
void for_each_binding(struct test_state *ts, usbg_state *s, BindingTestFunc fun)
{
struct test_gadget *tg;
struct test_config *tc;
struct test_binding *tb;
usbg_gadget *g = NULL;
usbg_config *c = NULL;
usbg_binding *b = NULL;
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, tc->label);
assert_non_null(c);
b = usbg_get_first_binding(c);
for (tb = tc->bindings; tb->name; ++tb) {
assert_non_null(b);
fun(tb, b);
b = usbg_get_next_binding(b);
}
}
}
}
void for_each_test_gadget(struct test_state *ts, usbg_state *s,
GadgetTestFunc fun)
{
struct test_gadget *tg;
usbg_gadget *g = NULL;
for (tg = ts->gadgets; tg->name; ++tg) {
g = usbg_get_gadget(s, tg->name);
assert_non_null(g);
fun(g, tg);
}
}
|