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
|
/*
* lws-api-test-lws_dsh
*
* Written in 2010-2019 by Andy Green <andy@warmcat.com>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*/
#include <libwebsockets.h>
int
test1(void)
{
struct lws_dsh *dsh;
size_t size;
void *a1;
/*
* test 1: single dsh, alloc 2 kinds and free everything back to a
* single free obj
*/
dsh = lws_dsh_create(NULL, 16384, 2);
if (!dsh) {
lwsl_err("%s: Failed to create dsh\n", __func__);
return 1;
}
if (lws_dsh_alloc_tail(dsh, 0, "hello", 5, NULL, 0)) {
lwsl_err("%s: Failed to alloc 1\n", __func__);
goto bail;
}
if (lws_dsh_alloc_tail(dsh, 1, "some other string", 17, NULL, 0)) {
lwsl_err("%s: Failed to alloc 2\n", __func__);
goto bail;
}
if (lws_dsh_alloc_tail(dsh, 0, "hello again", 11, NULL, 0)) {
lwsl_err("%s: Failed to alloc 3\n", __func__);
goto bail;
}
if (lws_dsh_get_head(dsh, 1, &a1, &size)) {
lwsl_err("%s: no head 1\n", __func__);
goto bail;
}
if (size != 17 || memcmp(a1, "some other string", 17)) {
lwsl_err("%s: test 1 mismatch\n", __func__);
goto bail;
}
lws_dsh_free(&a1);
if (lws_dsh_get_head(dsh, 0, &a1, &size)) {
lwsl_err("%s: no head 2\n", __func__);
goto bail;
}
if (size != 5 || memcmp(a1, "hello", 5)) {
lwsl_err("%s: test 2 mismatch\n", __func__);
goto bail;
}
lws_dsh_free(&a1);
if (lws_dsh_get_head(dsh, 0, &a1, &size)) {
lwsl_err("%s: no head 3\n", __func__);
goto bail;
}
if (size != 11 || memcmp(a1, "hello again", 11)) {
lwsl_err("%s: test 3 mismatch\n", __func__);
goto bail;
}
lws_dsh_free(&a1);
lws_dsh_destroy(&dsh);
return 0;
bail:
lws_dsh_destroy(&dsh);
return 1;
}
int
test2(void)
{
struct lws_dsh *dsh, *dsh2;
lws_dll2_owner_t owner;
uint8_t blob[4096];
memset(blob, 0, sizeof(blob));
/*
* test 2: multiple dsh, overflow allocation and dynamic destroy
*/
lws_dll2_owner_clear(&owner);
dsh = lws_dsh_create(&owner, 4096, 2);
if (!dsh) {
lwsl_err("%s: Failed to create dsh1\n", __func__);
return 1;
}
dsh2 = lws_dsh_create(&owner, 4096, 2);
if (!dsh2) {
lwsl_err("%s: Failed to create dsh2\n", __func__);
goto bail;
}
if (lws_dsh_alloc_tail(dsh, 0, blob, 4000, NULL, 0)) {
lwsl_err("%s: Failed to alloc 1\n", __func__);
goto bail2;
}
if (lws_dsh_alloc_tail(dsh2, 0, "hello", 5, NULL, 0)) {
lwsl_err("%s: Failed to alloc 2\n", __func__);
goto bail2;
}
/*
* We create this logically on dsh. But there's no room for the body.
* It should figure out it can use space on dsh2.
*/
if (lws_dsh_alloc_tail(dsh, 0, blob, 2000, NULL, 0)) {
lwsl_err("%s: Failed to alloc 3\n", __func__);
goto bail2;
}
if (lws_dsh_alloc_tail(dsh2, 0, "hello again", 11, NULL, 0)) {
lwsl_err("%s: Failed to alloc 4\n", __func__);
goto bail2;
}
/*
* When we destroy dsh2 it will try to migrate out the 2000 allocation
* from there but find there is no space in dsh1. It should handle it
* by logicalling dropping the object.
*/
lws_dsh_destroy(&dsh2);
lws_dsh_destroy(&dsh);
return 0;
bail2:
lws_dsh_destroy(&dsh2);
bail:
lws_dsh_destroy(&dsh);
return 1;
}
int
test3(void)
{
struct lws_dsh *dsh, *dsh2;
lws_dll2_owner_t owner;
uint8_t blob[4096];
memset(blob, 0, sizeof(blob));
/*
* test 3: multiple dsh, umeetable allocation request
*/
lws_dll2_owner_clear(&owner);
dsh = lws_dsh_create(&owner, 4096, 2);
if (!dsh) {
lwsl_err("%s: Failed to create dsh1\n", __func__);
return 1;
}
dsh2 = lws_dsh_create(&owner, 4096, 2);
if (!dsh2) {
lwsl_err("%s: Failed to create dsh2\n", __func__);
goto bail;
}
if (lws_dsh_alloc_tail(dsh, 0, blob, 4000, NULL, 0)) {
lwsl_err("%s: Failed to alloc 1\n", __func__);
goto bail2;
}
if (lws_dsh_alloc_tail(dsh2, 0, "hello", 5, NULL, 0)) {
lwsl_err("%s: Failed to alloc 2\n", __func__);
goto bail2;
}
/*
* There's just no room for this, we expect it to fail
*/
if (!lws_dsh_alloc_tail(dsh, 0, blob, 5000, NULL, 0)) {
lwsl_err("%s: Didn't fail to alloc as expected\n", __func__);
goto bail2;
}
if (lws_dsh_alloc_tail(dsh2, 0, "hello again", 11, NULL, 0)) {
lwsl_err("%s: Failed to alloc 4\n", __func__);
goto bail2;
}
lws_dsh_destroy(&dsh2);
lws_dsh_destroy(&dsh);
return 0;
bail2:
lws_dsh_destroy(&dsh2);
bail:
lws_dsh_destroy(&dsh);
return 1;
}
int
test4(void)
{
uint8_t blob[4096];
struct lws_dsh *dsh;
size_t size;
void *a1;
memset(blob, 0, sizeof(blob));
/*
* test 1: use up whole free list, then recover and alloc something
* else
*/
dsh = lws_dsh_create(NULL, 4096, 2);
if (!dsh) {
lwsl_err("%s: Failed to create dsh\n", __func__);
return 1;
}
if (lws_dsh_alloc_tail(dsh, 0, blob, 4000, NULL, 0)) {
lwsl_err("%s: Failed to alloc 1\n", __func__);
goto bail;
}
if (lws_dsh_get_head(dsh, 0, &a1, &size)) {
lwsl_err("%s: no head 1\n", __func__);
goto bail;
}
if (size != 4000) {
lwsl_err("%s: test 1 mismatch\n", __func__);
goto bail;
}
lws_dsh_free(&a1);
if (lws_dsh_alloc_tail(dsh, 0, "some other string", 17, NULL, 0)) {
lwsl_err("%s: Failed to alloc 2\n", __func__);
goto bail;
}
if (lws_dsh_alloc_tail(dsh, 0, "hello again", 11, NULL, 0)) {
lwsl_err("%s: Failed to alloc 3\n", __func__);
goto bail;
}
if (lws_dsh_get_head(dsh, 0, &a1, &size)) {
lwsl_err("%s: no head 1\n", __func__);
goto bail;
}
if (size != 17 || memcmp(a1, "some other string", 17)) {
lwsl_err("%s: test 1 mismatch\n", __func__);
goto bail;
}
lws_dsh_free(&a1);
if (lws_dsh_get_head(dsh, 0, &a1, &size)) {
lwsl_err("%s: no head 2\n", __func__);
goto bail;
}
if (size != 11 || memcmp(a1, "hello again", 11)) {
lwsl_err("%s: test 2 mismatch (%zu)\n", __func__, size);
goto bail;
}
lws_dsh_free(&a1);
lws_dsh_destroy(&dsh);
return 0;
bail:
lws_dsh_destroy(&dsh);
return 1;
}
int main(int argc, const char **argv)
{
int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
int ret = 0, n;
const char *p;
if ((p = lws_cmdline_option(argc, argv, "-d")))
logs = atoi(p);
lws_set_log_level(logs, NULL);
lwsl_user("LWS API selftest: lws_dsh\n");
n = test1();
lwsl_user("%s: test1: %d\n", __func__, n);
ret |= n;
n = test2();
lwsl_user("%s: test2: %d\n", __func__, n);
ret |= n;
n = test3();
lwsl_user("%s: test3: %d\n", __func__, n);
ret |= n;
n = test4();
lwsl_user("%s: test4: %d\n", __func__, n);
ret |= n;
lwsl_user("Completed: %s\n", ret ? "FAIL" : "PASS");
return ret;
}
|