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
|
/*
* lws-api-test-lws_struct-sqlite
*
* Written in 2010-2020 by Andy Green <andy@warmcat.com>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*
* lws_struct apis are used to serialize and deserialize your C structs and
* linked-lists in a standardized way that's very modest on memory but
* convenient and easy to maintain.
*
* The API test shows how to serialize and deserialize a struct with a linked-
* list of child structs in JSON using lws_struct APIs.
*/
#include <libwebsockets.h>
typedef struct teststruct {
lws_dll2_t list; /* not directly serialized */
char str1[32];
const char *str2;
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
int32_t s32;
} teststruct_t;
/*
* These are the members that we will serialize and deserialize, not every
* member in the struct (eg, the dll2 list member)
*/
static const lws_struct_map_t lsm_teststruct[] = {
LSM_CARRAY (teststruct_t, str1, "str1"),
LSM_STRING_PTR (teststruct_t, str2, "str2"),
LSM_UNSIGNED (teststruct_t, u8, "u8"),
LSM_UNSIGNED (teststruct_t, u16, "u16"),
LSM_UNSIGNED (teststruct_t, u32, "u32"),
LSM_UNSIGNED (teststruct_t, u64, "u64"),
LSM_SIGNED (teststruct_t, s32, "s32"),
};
static const lws_struct_map_t lsm_schema_apitest[] = {
LSM_SCHEMA_DLL2 (teststruct_t, list, NULL, lsm_teststruct, "apitest")
};
static const char *test_string =
"No one would have believed in the last years of the nineteenth "
"century that this world was being watched keenly and closely by "
"intelligences greater than man's and yet as mortal as his own; that as "
"men busied themselves about their various concerns they were "
"scrutinised and studied, perhaps almost as narrowly as a man with a "
"microscope might scrutinise the transient creatures that swarm and "
"multiply in a drop of water. With infinite complacency men went to "
"and fro over this globe about their little affairs, serene in their "
"assurance of their empire over matter. It is possible that the "
"infusoria under the microscope do the same. No one gave a thought to "
"the older worlds of space as sources of human danger, or thought of "
"them only to dismiss the idea of life upon them as impossible or "
"improbable. It is curious to recall some of the mental habits of "
"those departed days. At most terrestrial men fancied there might be "
"other men upon Mars, perhaps inferior to themselves and ready to "
"welcome a missionary enterprise. Yet across the gulf of space, minds "
"that are to our minds as ours are to those of the beasts that perish, "
"intellects vast and cool and unsympathetic, regarded this earth with "
"envious eyes, and slowly and surely drew their plans against us. And "
"early in the twentieth century came the great disillusionment. ";
int main(int argc, const char **argv)
{
int e = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
struct lws_context_creation_info info;
struct lws_context *context;
struct lwsac *ac = NULL;
lws_dll2_owner_t resown;
teststruct_t ts, *pts;
const char *p;
sqlite3 *db;
if ((p = lws_cmdline_option(argc, argv, "-d")))
logs = atoi(p);
lws_set_log_level(logs, NULL);
lwsl_user("LWS API selftest: lws_struct SQLite\n");
memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
#if defined(LWS_WITH_NETWORK)
info.port = CONTEXT_PORT_NO_LISTEN;
#endif
context = lws_create_context(&info);
if (!context) {
lwsl_err("lws init failed\n");
return 1;
}
unlink("_lws_apitest.sq3");
if (lws_struct_sq3_open(context, "_lws_apitest.sq3", 1, &db)) {
lwsl_err("%s: failed to open table\n", __func__);
goto bail;
}
/* 1. populate the struct */
memset(&ts, 0, sizeof(ts));
lws_strncpy(ts.str1, "hello", sizeof(ts.str1));
ts.str2 = test_string;
ts.u8 = 1;
ts.u16 = 512,
ts.u32 = 0x55aa1234; /* 1437209140, */
ts.u64 = 0x34abcdef01ull;
ts.s32 = -1;
/* add our struct to the dll2 owner list */
lws_dll2_owner_clear(&resown);
lws_dll2_add_head(&ts.list, &resown);
/* gratuitously create the table */
if (lws_struct_sq3_create_table(db, lsm_schema_apitest)) {
lwsl_err("%s: Create table failed\n", __func__);
e++;
goto done;
}
/* serialize the items on the dll2 owner */
if (lws_struct_sq3_serialize(db, lsm_schema_apitest, &resown, 0)) {
lwsl_err("%s: Serialize failed\n", __func__);
e++;
goto done;
}
/* resown should be cleared by deserialize, ac is already NULL */
lws_dll2_owner_clear(&resown); /* make sure old resown data is gone */
if (lws_struct_sq3_deserialize(db, NULL, NULL, lsm_schema_apitest,
&resown, &ac, 0, 1)) {
lwsl_err("%s: Deserialize failed\n", __func__);
e++;
goto done;
}
/* we should have 1 entry in resown now (created into the ac) */
if (resown.count != 1) {
lwsl_err("%s: Expected 1 result got %d\n", __func__,
resown.count);
e++;
goto done;
}
/*
* Convert the pointer to the embedded lws_dll2 into a pointer
* to the actual struct with the correct type
*/
pts = lws_container_of(lws_dll2_get_head(&resown),
teststruct_t, list);
if (strcmp(pts->str1, "hello") ||
strcmp(pts->str2, test_string) ||
pts->u8 != 1 ||
pts->u16 != 512 ||
pts->u32 != 0x55aa1234 ||
pts->u64 != 0x34abcdef01ull ||
pts->s32 != -1) {
lwsl_err("%s: unexpected deser values: %s\n", __func__, pts->str1);
lwsl_err("%s: %s\n", __func__, pts->str2);
lwsl_err("%s: %u %u %u 0x%llx %d\n", __func__, pts->u8, pts->u16,
pts->u32, (unsigned long long)pts->u64, pts->s32);
e++;
goto done;
}
done:
lwsac_free(&ac);
lws_struct_sq3_close(&db);
if (e)
goto bail;
lws_context_destroy(context);
lwsl_user("Completed: PASS\n");
return 0;
bail:
lws_context_destroy(context);
lwsl_user("Completed: FAIL\n");
return 1;
}
|