Path: blob/main/test/browser/test_idbstore.c
4150 views
/*1* Copyright 2015 The Emscripten Authors. All rights reserved.2* Emscripten is available under two separate licenses, the MIT license and the3* University of Illinois/NCSA Open Source License. Both these licenses can be4* found in the LICENSE file.5*/67#include <stdbool.h>8#include <stdio.h>9#include <string.h>10#include <assert.h>11#include <stdlib.h>12#include <stdio.h>1314#include <emscripten/emscripten.h>15#include <emscripten/html5.h>1617#define DB "THE_DB"1819long expected;20int result;2122void ok(void* arg) {23assert(expected == (long)arg);24}2526void onerror(void* arg) {27assert(expected == (long)arg);28assert(false);29}3031void onload(void* arg, void* ptr, int num) {32assert(expected == (long)arg);33printf("loaded %s\n", (char*)ptr);34assert(num == strlen(SECRET)+1);35assert(strcmp(ptr, SECRET) == 0);36}3738void onbadload(void* arg, void* ptr, int num) {39printf("load failed, surprising\n");40assert(false);41}4243void oncheck(void* arg, int exists) {44assert(expected == (long)arg);45printf("exists? %d\n", exists);46assert(exists);47}4849void onchecknope(void* arg, int exists) {50assert(expected == (long)arg);51printf("exists (hopefully not)? %d\n", exists);52assert(!exists);53}5455int main() {56result = STAGE;57#if STAGE == 058expected = 12;59printf("storing %s\n", SECRET);60emscripten_idb_async_store(DB, "the_secret", SECRET, strlen(SECRET)+1, (void*)expected, ok, onerror);61#elif STAGE == 162expected = 31;63printf("loading the_secret\n");64emscripten_idb_async_load(DB, "the_secret", (void*)expected, onload, onerror);65#elif STAGE == 266expected = 44;67printf("deleting the_secret\n");68emscripten_idb_async_delete(DB, "the_secret", (void*)expected, ok, onerror);69#elif STAGE == 370expected = 55;71printf("loading, should fail as we deleted\n");72emscripten_idb_async_load(DB, "the_secret", (void*)expected, onbadload, ok);73#elif STAGE == 474expected = 66;75emscripten_idb_async_exists(DB, "the_secret", (void*)expected, oncheck, onerror);76#elif STAGE == 577expected = 77;78emscripten_idb_async_exists(DB, "the_secret", (void*)expected, onchecknope, onerror);79#elif STAGE == 680expected = 88;81printf("clearing\n");82emscripten_idb_async_clear(DB, (void*)expected, ok, onerror);83#else84assert(0);85#endif86}87888990