diff options
author | Pawel Szewczyk <p.szewczyk@samsung.com> | 2015-05-26 10:04:41 +0200 |
---|---|---|
committer | Krzysztof Opasiak <k.opasiak@samsung.com> | 2015-07-30 15:56:19 +0200 |
commit | 679abafed1790d270af01fa2d707d772a9a44e83 (patch) | |
tree | 178fe03445b34ad66bbaae4ebc2236b8d018f6db | |
parent | 085a2a19f50185f6d10aaceb8bed323336742981 (diff) | |
download | libusbg-679abafed1790d270af01fa2d707d772a9a44e83.tar.gz libusbg-679abafed1790d270af01fa2d707d772a9a44e83.tar.bz2 libusbg-679abafed1790d270af01fa2d707d772a9a44e83.zip |
libusbg: tests: Separate safe state initialization from tests
Test functions usually need to initialize usbg state before testing.
However, initialization may fail, causing test to finish immediatelly,
running teardown function.
This commit add function which takes care of state pointer when
initializing.
Change-Id: I797e56ac3cda67e871c6c4fbd2184220b05068f6
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
Reviewed-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r-- | tests/test.c | 84 | ||||
-rw-r--r-- | tests/usbg-test.c | 33 | ||||
-rw-r--r-- | tests/usbg-test.h | 8 |
3 files changed, 35 insertions, 90 deletions
diff --git a/tests/test.c b/tests/test.c index 1fba40e..8f02f0a 100644 --- a/tests/test.c +++ b/tests/test.c @@ -436,11 +436,7 @@ static void test_get_gadget_fail(void **state) usbg_state *s = NULL; struct test_state *st; - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; + safe_init_with_state(state, &st, &s); g = usbg_get_gadget(s, "non-existing-gadget"); assert_null(g); @@ -456,11 +452,7 @@ static void test_get_first_gadget(void **state) usbg_state *s = NULL; struct test_state *st; - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; + safe_init_with_state(state, &st, &s); g = usbg_get_first_gadget(s); assert_non_null(g); @@ -561,11 +553,7 @@ static void test_cpy_gadget_name_fail(void **state) char name[USBG_MAX_NAME_LENGTH]; int ret; - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; + safe_init_with_state(state, &st, &s); for (i = 0; st->gadgets[i].name; i++) { g = usbg_get_gadget(s, st->gadgets[i].name); @@ -592,11 +580,7 @@ static void test_init(void **state) usbg_state *s = NULL; struct test_state *st; - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; + safe_init_with_state(state, &st, &s); assert_state_equal(s, st); } @@ -623,11 +607,7 @@ static void test_get_function_fail(void **state) usbg_function *f = NULL; struct test_state *st; - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; + safe_init_with_state(state, &st, &s); g = usbg_get_first_gadget(s); assert_non_null(g); @@ -799,11 +779,7 @@ static void test_get_configfs_path(void **state) struct test_state *st; const char *path; - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; + safe_init_with_state(state, &st, &s); path = usbg_get_configfs_path(s); assert_path_equal(path, st->configfs_path); @@ -820,11 +796,7 @@ static void test_get_configfs_path_len(void **state) struct test_state *st; int ret, len; - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; + safe_init_with_state(state, &st, &s); ret = usbg_get_configfs_path_len(s); len = strlen(st->configfs_path); @@ -843,11 +815,7 @@ static void test_cpy_configfs_path(void **state) char path[PATH_MAX]; int ret; - st = (struct test_state *)(*state); - *state = NULL; - - init_with_state(st, &s); - *state = s; + safe_init_with_state(state, &st, &s); ret = usbg_cpy_configfs_path(s, path, PATH_MAX); assert_int_equal(ret, USBG_SUCCESS); @@ -873,11 +841,7 @@ static void test_get_config_without_label(void **state) struct test_gadget *tg; struct test_config *tc; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + safe_init_with_state(state, &ts, &s); for (tg = ts->gadgets; tg->name; tg++) { g = usbg_get_gadget(s, tg->name); @@ -903,11 +867,7 @@ static void test_get_config_fail(void **state) struct test_state *ts; struct test_gadget *tg; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + safe_init_with_state(state, &ts, &s); for (tg = ts->gadgets; tg->name; tg++) { g = usbg_get_gadget(s, tg->name); @@ -1004,11 +964,7 @@ static void test_get_gadget_attrs(void **state) usbg_state *s = NULL; struct test_state *ts; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + 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); @@ -1047,11 +1003,7 @@ static void test_set_gadget_attrs(void **state) usbg_state *s = NULL; struct test_state *ts; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + 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); @@ -1096,11 +1048,7 @@ static void test_set_specific_gadget_attr(void **state) usbg_state *s = NULL; struct test_state *ts; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + 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); @@ -1120,11 +1068,7 @@ void test_get_udc(void **state) usbg_udc *u = NULL; usbg_gadget *g = NULL; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + safe_init_with_state(state, &ts, &s); for (tu = ts->udcs; *tu; tu++) { u = usbg_get_udc(s, *tu); diff --git a/tests/usbg-test.c b/tests/usbg-test.c index 3e00006..3aeb2d1 100644 --- a/tests/usbg-test.c +++ b/tests/usbg-test.c @@ -727,6 +727,15 @@ void init_with_state(struct test_state *in, usbg_state **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(usbg_config_attrs *attrs, config_attr attr) { int ret; @@ -1100,11 +1109,7 @@ void for_each_test_function(void **state, FunctionTest fun) usbg_gadget *g = NULL; usbg_function *f = NULL; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + safe_init_with_state(state, &ts, &s); for (tg = ts->gadgets; tg->name; ++tg) { g = usbg_get_gadget(s, tg->name); @@ -1125,11 +1130,7 @@ void for_each_test_config(void **state, ConfigTest fun) struct test_gadget *tg; struct test_config *tc; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + safe_init_with_state(state, &ts, &s); for (tg = ts->gadgets; tg->name; tg++) { g = usbg_get_gadget(s, tg->name); @@ -1152,11 +1153,7 @@ void for_each_binding(void **state, BindingTestFunc fun) usbg_config *c = NULL; usbg_binding *b = NULL; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + safe_init_with_state(state, &ts, &s); for (tg = ts->gadgets; tg->name; tg++) { g = usbg_get_gadget(s, tg->name); @@ -1182,11 +1179,7 @@ void for_each_test_gadget(void **state, GadgetTestFunc fun) usbg_state *s = NULL; usbg_gadget *g = NULL; - ts = (struct test_state *)(*state); - *state = NULL; - - init_with_state(ts, &s); - *state = s; + safe_init_with_state(state, &ts, &s); for (tg = ts->gadgets; tg->name; ++tg) { g = usbg_get_gadget(s, tg->name); diff --git a/tests/usbg-test.h b/tests/usbg-test.h index 6abfa7f..9bb6bfd 100644 --- a/tests/usbg-test.h +++ b/tests/usbg-test.h @@ -310,6 +310,14 @@ void cleanup_stack(); void init_with_state(struct test_state *in, usbg_state **out); /** + * @brief Safely initialize usbg state from pointer given to test + * @param[in] state Pointer given to test function + * @param[out] ts Pointer to be filled with test state + * @param[out] s Pointer to be filled with usbg state + */ +void safe_init_with_state(void **state, struct test_state **ts, usbg_state **s); + +/** * @brief Assert that given config attributes are equal */ void assert_config_attrs_equal(usbg_config_attrs *actual, usbg_config_attrs *expected); |