diff options
author | Pawel Szewczyk <p.szewczyk@samsung.com> | 2015-02-12 16:01:52 +0100 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2015-03-06 16:12:07 +0100 |
commit | 737a58a08c7e915c18d16a46a968d765b8416dec (patch) | |
tree | b1d67085af0d947339e7c02ed44c196b2dc36fd6 /tests/usbg-test.c | |
parent | 9f7091ec49df7ed454eb9435070865fa5a0903f3 (diff) | |
download | libusbg-737a58a08c7e915c18d16a46a968d765b8416dec.tar.gz libusbg-737a58a08c7e915c18d16a46a968d765b8416dec.tar.bz2 libusbg-737a58a08c7e915c18d16a46a968d765b8416dec.zip |
libusbg: tests: Add simulation of configfs for gadget attrs get/set
Before executing libusbg functions which get and set gadget attributes,
we must prepare virtual filesystem to behave as expected.
push_* and pull_* functions take arguments similar to usbg functions
which will be called after them, e.g. push_gadget_attrs(g, a)
will prepare for calling usbg_get_gadget_attrs on gadget matching g,
where a is value passed to be read.
Change-Id: I805544dbd35a1a7f8c2e4073f9f4607d9c70bb95
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
Reviewed-by: Krzysztof Opasiak <k.opasiak@samsung.com>
Diffstat (limited to 'tests/usbg-test.c')
-rw-r--r-- | tests/usbg-test.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/usbg-test.c b/tests/usbg-test.c index 492aa5a..7f24771 100644 --- a/tests/usbg-test.c +++ b/tests/usbg-test.c @@ -91,6 +91,28 @@ static int dir_id = 0; will_return(readlink, c);\ } while(0) +#define EXPECT_WRITE(file, content) do {\ + file_id++;\ + expect_path(fopen, path, file);\ + will_return(fopen, file_id);\ + expect_value(fputs, stream, file_id);\ + expect_string(fputs, s, content);\ + will_return(fputs, 0);\ + expect_value(fclose, fp, file_id);\ + will_return(fclose, 0);\ +} while(0) + +#define EXPECT_HEX_WRITE(file, content) do {\ + file_id++;\ + expect_path(fopen, path, file);\ + will_return(fopen, file_id);\ + expect_value(fputs, stream, file_id);\ + expect_check(fputs, s, hex_str_equal_display_error, content);\ + will_return(fputs, 0);\ + expect_value(fclose, fp, file_id);\ + will_return(fclose, 0);\ +} while(0) + /** * @brief Compare test gadgets' names */ @@ -334,6 +356,87 @@ void push_init(struct test_state *state) push_gadget(g); } +int get_gadget_attr(usbg_gadget_attrs *attrs, usbg_gadget_attr attr) { + switch (attr) { + case BCD_USB: + return attrs->bcdUSB; + case B_DEVICE_CLASS: + return attrs->bDeviceClass; + case B_DEVICE_SUB_CLASS: + return attrs->bDeviceSubClass; + case B_DEVICE_PROTOCOL: + return attrs->bDeviceProtocol; + case B_MAX_PACKET_SIZE_0: + return attrs->bMaxPacketSize0; + case ID_VENDOR: + return attrs->idVendor; + case ID_PRODUCT: + return attrs->idProduct; + case BCD_DEVICE: + return attrs->bcdDevice; + default: + return -1; + } +} + +void pull_gadget_attribute(struct test_gadget *gadget, + usbg_gadget_attr attr, int value) +{ + char *path; + char *content; + int tmp; + + tmp = asprintf(&path, "%s/%s/%s", + gadget->path, gadget->name, gadget_attr_names[attr]); + if (tmp >= USBG_MAX_PATH_LENGTH) + fail(); + free_later(path); + + tmp = asprintf(&content, "0x%x\n", value); + if (tmp < 0) + fail(); + free_later(content); + + EXPECT_HEX_WRITE(path, content); +} + +void push_gadget_attribute(struct test_gadget *gadget, + usbg_gadget_attr attr, int value) +{ + char *path; + char *content; + int tmp; + + tmp = asprintf(&path, "%s/%s/%s", + gadget->path, gadget->name, gadget_attr_names[attr]); + if (tmp < 0) + fail(); + free_later(path); + + tmp = asprintf(&content, "0x%x\n", value); + if (tmp < 0) + fail(); + free_later(content); + + PUSH_FILE(path, content); +} + +void push_gadget_attrs(struct test_gadget *gadget, 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, 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; @@ -432,6 +535,26 @@ void assert_path_equal(const char *actual, const char *expected) 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 for_each_test_function(void **state, FunctionTest fun) { usbg_state *s = NULL; |