summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPawel Szewczyk <p.szewczyk@samsung.com>2015-05-25 15:11:24 +0200
committerKrzysztof Opasiak <k.opasiak@samsung.com>2015-07-30 15:56:47 +0200
commit0398c8135ac81303ef4e106ca42324b7274a3066 (patch)
treecf0cab13a8df2672c24cdcd42618ea11d2034cf9
parentf1a89d1c08d4d173032aaf2f29bde090b9c82490 (diff)
downloadlibusbg-0398c8135ac81303ef4e106ca42324b7274a3066.tar.gz
libusbg-0398c8135ac81303ef4e106ca42324b7274a3066.tar.bz2
libusbg-0398c8135ac81303ef4e106ca42324b7274a3066.zip
libusbg: tests: Add getting function attributes tests
Change-Id: Ia82f5f124e7b57c2694c605ddc942513fc337645 Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com> Reviewed-by: Krzysztof Opasiak <k.opasiak@samsung.com>
-rw-r--r--tests/test.c360
-rw-r--r--tests/usbg-test.c132
-rw-r--r--tests/usbg-test.h28
3 files changed, 520 insertions, 0 deletions
diff --git a/tests/test.c b/tests/test.c
index 7c19198..0c1ac5b 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -194,11 +194,33 @@ static struct test_gadget long_udc_gadgets[] = {
TEST_GADGET_LIST_END
};
+struct test_function_attrs_data {
+ struct test_state *state;
+ usbg_function_attrs *attrs;
+};
struct test_data {
struct test_state *state;
struct usbg_state *usbg_state;
};
+#define FUNC_ATTRS(t, label, a...) { \
+ .header = { \
+ .attrs_type = t \
+ }, \
+ .attrs = { \
+ .label = { a } \
+ }, \
+}
+
+static usbg_function_attrs simple_serial_attrs = FUNC_ATTRS(USBG_F_ATTRS_SERIAL, serial, 42);
+static usbg_function_attrs simple_net_attrs = FUNC_ATTRS(USBG_F_ATTRS_NET, net, {}, {}, "if", 1);
+static usbg_function_attrs simple_phonet_attrs = FUNC_ATTRS(USBG_F_ATTRS_PHONET, phonet, "if");
+static usbg_function_attrs writable_serial_attrs = FUNC_ATTRS(USBG_F_ATTRS_SERIAL, serial, 0);
+static usbg_function_attrs writable_net_attrs = FUNC_ATTRS(USBG_F_ATTRS_NET, net, {}, {}, "", 42);
+static usbg_function_attrs writable_phonet_attrs = FUNC_ATTRS(USBG_F_ATTRS_PHONET, phonet, "");
+static usbg_function_attrs simple_ffs_attrs = FUNC_ATTRS(USBG_F_ATTRS_FFS, ffs, "0");
+static usbg_function_attrs writable_ffs_attrs = FUNC_ATTRS(USBG_F_ATTRS_FFS, ffs, "");
+
struct test_gadget_strs_data {
struct test_state *state;
usbg_gadget_strs *strs;
@@ -416,6 +438,143 @@ static int setup_random_len_gadget_strs_data(void **state)
return 0;
}
+static void *setup_f_attrs(int f_type, usbg_function_attrs *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(F_SERIAL, &simple_serial_attrs);
+ return 0;
+}
+
+static int setup_f_serial_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_SERIAL, &writable_serial_attrs);
+ return 0;
+}
+
+static int setup_f_acm_attrs(void **state)
+{
+ *state = setup_f_attrs(F_ACM, &simple_serial_attrs);
+ return 0;
+}
+
+static int setup_f_acm_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_ACM, &writable_serial_attrs);
+ return 0;
+}
+
+static int setup_f_obex_attrs(void **state)
+{
+ *state = setup_f_attrs(F_OBEX, &simple_serial_attrs);
+ return 0;
+}
+
+static int setup_f_obex_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_OBEX, &writable_serial_attrs);
+ return 0;
+}
+
+static int setup_f_ecm_attrs(void **state)
+{
+ *state = setup_f_attrs(F_ECM, &simple_net_attrs);
+ return 0;
+}
+
+static int setup_f_ecm_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_ECM, &writable_net_attrs);
+ return 0;
+}
+
+static int setup_f_subset_attrs(void **state)
+{
+ *state = setup_f_attrs(F_SUBSET, &simple_net_attrs);
+ return 0;
+}
+
+static int setup_f_subset_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_SUBSET, &writable_net_attrs);
+ return 0;
+}
+
+static int setup_f_ncm_attrs(void **state)
+{
+ *state = setup_f_attrs(F_NCM, &simple_net_attrs);
+ return 0;
+}
+
+static int setup_f_ncm_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_NCM, &writable_net_attrs);
+ return 0;
+}
+
+static int setup_f_eem_attrs(void **state)
+{
+ *state = setup_f_attrs(F_EEM, &simple_net_attrs);
+ return 0;
+}
+
+static int setup_f_eem_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_EEM, &writable_net_attrs);
+ return 0;
+}
+
+static int setup_f_rndis_attrs(void **state)
+{
+ *state = setup_f_attrs(F_RNDIS, &simple_net_attrs);
+ return 0;
+}
+
+static int setup_f_rndis_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_RNDIS, &writable_net_attrs);
+ return 0;
+}
+
+static int setup_f_phonet_attrs(void **state)
+{
+ *state = setup_f_attrs(F_PHONET, &simple_phonet_attrs);
+ return 0;
+}
+
+static int setup_f_phonet_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_PHONET, &writable_phonet_attrs);
+ return 0;
+}
+
+static int setup_f_ffs_attrs(void **state)
+{
+ *state = setup_f_attrs(F_FFS, &simple_ffs_attrs);
+ return 0;
+}
+
+static int setup_f_ffs_writable_attrs(void **state)
+{
+ *state = setup_f_attrs(F_FFS, &writable_ffs_attrs);
+ return 0;
+}
+
/**
* @brief Tests usbg_get_gadget function with given state
* @details Check if gadgets are returned correctly
@@ -1511,6 +1670,71 @@ static void test_create_config(void **state)
}
/**
+
+/**
+ * @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;
+ usbg_function_attrs 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, data->attrs->header.attrs_type);
+
+ usbg_cleanup_function_attrs(&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
*/
@@ -1975,6 +2199,142 @@ static struct CMUnitTest tests[] = {
*/
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_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_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_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),
#ifndef DOXYGEN
};
diff --git a/tests/usbg-test.c b/tests/usbg-test.c
index 5e062dc..917a5a0 100644
--- a/tests/usbg-test.c
+++ b/tests/usbg-test.c
@@ -1006,6 +1006,116 @@ void pull_create_config(struct test_config *tc)
pull_config_strs(tc, LANG_US_ENG, tc->strs);
}
+#define ETHER_ADDR_STR_LEN 19
+
+static void push_serial_attrs(struct test_function *func,
+ usbg_f_serial_attrs *attrs)
+{
+ char *path;
+ char *content;
+
+ safe_asprintf(&path, "%s/%s/port_num", func->path, func->name);
+ safe_asprintf(&content, "%d\n", attrs->port_num);
+ PUSH_FILE(path, content);
+}
+
+static void push_net_attrs(struct test_function *func,
+ 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(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(path, content);
+
+ safe_asprintf(&path, "%s/%s/qmult", func->path, func->name);
+ safe_asprintf(&content, "%d\n", attrs->qmult);
+ PUSH_FILE(path, content);
+
+ safe_asprintf(&path, "%s/%s/ifname", func->path, func->name);
+ safe_asprintf(&content, "%s\n", attrs->ifname);
+ PUSH_FILE(path, content);
+}
+
+static void push_phonet_attrs(struct test_function *func,
+ usbg_f_phonet_attrs *attrs)
+{
+ char *path;
+ char *content;
+
+ safe_asprintf(&path, "%s/%s/ifname", func->path, func->name);
+ safe_asprintf(&content, "%s\n", attrs->ifname);
+ PUSH_FILE(path, content);
+}
+
+void push_function_attrs(struct test_function *func, usbg_function_attrs *function_attrs)
+{
+ int attrs_type;
+ usbg_f_attrs *attrs = &function_attrs->attrs;
+
+ attrs_type = usbg_lookup_function_attrs_type(func->type);
+
+ switch (attrs_type) {
+ case USBG_F_ATTRS_SERIAL:
+ push_serial_attrs(func, &attrs->serial);
+ break;
+ case USBG_F_ATTRS_NET:
+ push_net_attrs(func, &attrs->net);
+ break;
+ case USBG_F_ATTRS_PHONET:
+ push_phonet_attrs(func, &attrs->phonet);
+ break;
+ case USBG_F_ATTRS_FFS:
+ // ffs does not exist in filesystem
+ default:
+ break;
+ }
+}
+
+static void pull_function_net_attrs(struct test_function *func, 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(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(path, content);
+
+ safe_asprintf(&path, "%s/%s/qmult", func->path, func->name);
+ safe_asprintf(&content, "%d\n", attrs->qmult);
+ EXPECT_WRITE(path, content);
+}
+
+void pull_function_attrs(struct test_function *func, usbg_function_attrs *attrs)
+{
+ /* only net attributes are writtable */
+ if (attrs->header.attrs_type == USBG_F_ATTRS_NET)
+ pull_function_net_attrs(func, &attrs->attrs.net);
+}
+
void assert_func_equal(usbg_function *f, struct test_function *expected)
{
assert_string_equal(f->instance, expected->instance);
@@ -1169,6 +1279,28 @@ void assert_f_ffs_attrs_equal(usbg_f_ffs_attrs *actual, usbg_f_ffs_attrs *expect
assert_string_equal(actual->dev_name, expected->dev_name);
}
+void assert_function_attrs_equal(usbg_function_attrs *actual,
+ usbg_function_attrs *expected, usbg_f_attrs_type type)
+{
+ switch (type) {
+ case USBG_F_ATTRS_SERIAL:
+ assert_f_serial_attrs_equal(&actual->attrs.serial, &expected->attrs.serial);
+ break;
+ case USBG_F_ATTRS_NET:
+ assert_f_net_attrs_equal(&actual->attrs.net, &expected->attrs.net);
+ break;
+ case USBG_F_ATTRS_PHONET:
+ assert_f_phonet_attrs_equal(&actual->attrs.phonet, &expected->attrs.phonet);
+ break;
+ case USBG_F_ATTRS_FFS:
+ assert_f_ffs_attrs_equal(&actual->attrs.ffs, &expected->attrs.ffs);
+ break;
+ default:
+ fail();
+ }
+}
+
+
void for_each_test_function(struct test_state *ts, usbg_state *s, FunctionTest fun)
{
struct test_gadget *tg;
diff --git a/tests/usbg-test.h b/tests/usbg-test.h
index e28ea55..aa90a2e 100644
--- a/tests/usbg-test.h
+++ b/tests/usbg-test.h
@@ -18,6 +18,7 @@ struct test_function
char *path;
char *name;
+ usbg_function_attrs *attrs;
int writable;
};
@@ -224,6 +225,24 @@ void push_gadget_attrs(struct test_gadget *gadget, usbg_gadget_attrs *attrs);
void pull_gadget_attrs(struct test_gadget *gadget, usbg_gadget_attrs *attrs);
/**
+ * @brief Prepare fake filesystem to get given function attributes
+ * @details Prepare queue of values passed to wrapped i/o functions,
+ * all values got from given attributes structure.
+ * @warning Calling usbg_get_function_attrs function whithout this
+ * preparation and with wrapped i/o may fail.
+ */
+void push_function_attrs(struct test_function *func, usbg_function_attrs *attrs);
+
+/**
+ * @brief Prepare fake filesystem to set given function attributes
+ * @details Prepare queue of values passed to wrapped i/o functions,
+ * all values got from given attributes structure.
+ * @warning Calling usbg_set_function_attrs function whithout this
+ * preparation and with wrapped i/o may fail.
+ */
+void pull_function_attrs(struct test_function *func, usbg_function_attrs *attrs);
+
+/**
* @brief Get gadget string
* @param[in] strs Set of gadget strings
* @param[in] str Identifier of string which should be returned
@@ -416,6 +435,15 @@ void assert_gadget_attrs_equal(usbg_gadget_attrs *actual,
usbg_gadget_attrs *expected);
/**
+ * @brief Assert that given function attributes are the same.
+ * @param[in] actual Pointer to actual attributes object
+ * @param[in] expected Pointer to expected attributes obejct
+ * @param[in] type Type of function, which attributes are checked
+ */
+void assert_function_attrs_equal(usbg_function_attrs *actual,
+ usbg_function_attrs *expected, usbg_f_attrs_type type);
+
+/**
* @brief Assert that given gadget strings are equal
*/
void assert_gadget_strs_equal(usbg_gadget_strs *actual, usbg_gadget_strs *expected);