FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
emscripten/tests/fetch/idb_store.cpp at incoming · JavaScriptIOT/emscripten · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
JavaScriptIOT
/
emscripten
Public
forked from
emscripten-core/emscripten
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
emscripten
/
tests
/
fetch
/
idb_store.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
66 lines (59 loc) · 2.61 KB
Breadcrumbs
emscripten
/
tests
/
fetch
/
idb_store.cpp
Copy path
File metadata and controls
66 lines (59 loc) · 2.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
string.h
>
#
include
<
stdio.h
>
#
include
<
math.h
>
#
include
<
assert.h
>
#
include
<
stdlib.h
>
#
include
<
time.h
>
#
include
<
emscripten/fetch.h
>
#
define
TEST_SIZE
512000
int
main
()
{
//
1. Populate an IndexedDB file entry with custom data bytes in memory.
emscripten_fetch_attr_t
attr;
emscripten_fetch_attr_init
(&attr);
strcpy
(attr.
requestMethod
,
"
EM_IDB_STORE
"
);
attr.
attributes
=
EMSCRIPTEN_FETCH_REPLACE
|
EMSCRIPTEN_FETCH_SYNCHRONOUS
|
EMSCRIPTEN_FETCH_PERSIST_FILE
;
uint8_t
*data = (
uint8_t
*)
malloc
(
TEST_SIZE
);
srand
(
time
(
NULL
));
for
(
int
i =
0
; i <
TEST_SIZE
; ++i)
data[i] = (
uint8_t
)
rand
() |
0x40
;
attr.
requestData
= (
char
*)data;
attr.
requestDataSize
=
TEST_SIZE
;
emscripten_fetch_t
*fetch =
emscripten_fetch
(&attr,
"
myfile.dat
"
);
assert
(fetch->
status
==
200
&&
"
Initial IndexedDB store of myfile.dat should have succeeded
"
);
emscripten_fetch_close
(fetch);
//
2. Make sure it is there.
emscripten_fetch_attr_init
(&attr);
strcpy
(attr.
requestMethod
,
"
GET
"
);
attr.
attributes
=
EMSCRIPTEN_FETCH_NO_DOWNLOAD
|
EMSCRIPTEN_FETCH_SYNCHRONOUS
|
EMSCRIPTEN_FETCH_LOAD_TO_MEMORY
;
fetch =
emscripten_fetch
(&attr,
"
myfile.dat
"
);
assert
(fetch->
status
==
200
&&
"
emscripten_fetch from IndexedDB should have found the file
"
);
assert
(fetch->
numBytes
==
TEST_SIZE
);
assert
(fetch->
totalBytes
==
TEST_SIZE
);
assert
(fetch->
data
!=
0
);
assert
(fetch->
data
!= (
char
*)data);
//
We should really have obtained a new copy of the memory.
assert
(!
memcmp
((
char
*)data, fetch->
data
,
TEST_SIZE
));
emscripten_fetch_close
(fetch);
//
3. Delete the file from IndexedDB by invoking an Emscripten-specific request "EM_IDB_DELETE".
emscripten_fetch_attr_init
(&attr);
strcpy
(attr.
requestMethod
,
"
EM_IDB_DELETE
"
);
attr.
attributes
=
EMSCRIPTEN_FETCH_SYNCHRONOUS
;
fetch =
emscripten_fetch
(&attr,
"
myfile.dat
"
);
assert
(fetch->
status
==
200
&&
"
Deleting the file from IndexedDB should have succeeded
"
);
assert
(fetch->
numBytes
==
0
);
assert
(fetch->
data
==
0
);
emscripten_fetch_close
(fetch);
//
4. Now try to get the file again from IndexedDB, and ensure it is no longer available.
emscripten_fetch_attr_init
(&attr);
strcpy
(attr.
requestMethod
,
"
GET
"
);
attr.
attributes
=
EMSCRIPTEN_FETCH_NO_DOWNLOAD
|
EMSCRIPTEN_FETCH_SYNCHRONOUS
|
EMSCRIPTEN_FETCH_LOAD_TO_MEMORY
;
fetch =
emscripten_fetch
(&attr,
"
myfile.dat
"
);
assert
(fetch->
status
==
404
&&
"
Attempting to GET the file from IndexedDB again should have failed after the file has been deleted
"
);
assert
(fetch->
numBytes
==
0
);
assert
(fetch->
data
==
0
);
emscripten_fetch_close
(fetch);
printf
(
"
Test succeeded!
\n
"
);
#
ifdef
REPORT_RESULT
REPORT_RESULT
(
0
);
#
endif
}
Back
|
FazBrowse Home
|
New Git URL