Skip to content

Commit 49d5ece

Browse files
authored
Update test_uuid. NFC (#21060)
- Split into an other test and a browser test. - Give it a standard filename - Use assertContains - Use do_runf helper
1 parent 7f84b88 commit 49d5ece

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

test/test_browser.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,29 +2666,7 @@ def test_cpuprofiler_memoryprofiler(self, opts):
26662666
self.btest_exit('hello_world_gles.c', args=['-DLONGTEST=1', '-DTEST_MEMORYPROFILER_ALLOCATIONS_MAP=1', '--cpuprofiler', '--memoryprofiler', '-lGL', '-lglut', '-DANIMATE'] + opts)
26672667

26682668
def test_uuid(self):
2669-
# Run with ./runner browser.test_uuid
2670-
# We run this test in Node/SPIDERMONKEY and browser environments because we try to make use of
2671-
# high quality crypto random number generators such as crypto.getRandomValues or randomBytes (if available).
2672-
2673-
# First run tests in Node and/or SPIDERMONKEY using self.run_js. Use closure compiler so we can check that
2674-
# require('crypto').randomBytes and window.crypto.getRandomValues doesn't get minified out.
2675-
self.run_process([EMCC, '-O2', '--closure=1', test_file('uuid/test.c'), '-o', 'test.js', '-luuid'])
2676-
2677-
test_js_closure = read_file('test.js')
2678-
2679-
# Check that test.js compiled with --closure 1 contains ").randomBytes" and "window.crypto.getRandomValues"
2680-
assert ").randomBytes" in test_js_closure
2681-
assert "window.crypto.getRandomValues" in test_js_closure
2682-
2683-
out = self.run_js('test.js')
2684-
print(out)
2685-
2686-
# Tidy up files that might have been created by this test.
2687-
delete_file(test_file('uuid/test.js'))
2688-
delete_file(test_file('uuid/test.js.map'))
2689-
2690-
# Now run test in browser
2691-
self.btest_exit('uuid/test.c', args=['-luuid'])
2669+
self.btest_exit('test_uuid.c', args=['-luuid'])
26922670

26932671
@requires_graphics_hardware
26942672
def test_glew(self):

test/test_other.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14479,3 +14479,19 @@ def test_browser_too_old(self):
1447914479
def test_js_only_settings(self):
1448014480
err = self.run_process([EMCC, test_file('hello_world.c'), '-o', 'foo.wasm', '-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=emscripten_get_heap_max'], stderr=PIPE).stderr
1448114481
self.assertContained('emcc: warning: DEFAULT_LIBRARY_FUNCS_TO_INCLUDE is only valid when generating JavaScript output', err)
14482+
14483+
def test_uuid(self):
14484+
# We run this test in Node/SPIDERMONKEY and browser environments because we
14485+
# try to make use of high quality crypto random number generators such as
14486+
# crypto.getRandomValues or randomBytes (if available).
14487+
14488+
# Use closure compiler so we can check that require('crypto').randomBytes and
14489+
# window.crypto.getRandomValues doesn't get minified out.
14490+
self.do_runf('test_uuid.c', emcc_args=['-O2', '--closure=1', '-luuid'])
14491+
14492+
js_out = read_file('test_uuid.js')
14493+
14494+
# Check that test.js compiled with --closure 1 contains ").randomBytes" and
14495+
# "window.crypto.getRandomValues"
14496+
self.assertContained(").randomBytes", js_out)
14497+
self.assertContained("window.crypto.getRandomValues", js_out)

test/uuid/test.c renamed to test/test_uuid.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <assert.h>
1010
#include <ctype.h>
1111
#include <stdio.h>
12-
#include <stdlib.h>
13-
#include <emscripten.h>
1412

1513
int isUUID(char* p, int upper) {
1614
char* p1 = p;
@@ -25,7 +23,7 @@ int isUUID(char* p, int upper) {
2523
return 1;
2624
} else {
2725
return 0;
28-
}
26+
}
2927
}
3028

3129
int main() {
@@ -40,7 +38,7 @@ int main() {
4038
assert(uuid_variant(uuid) == UUID_VARIANT_DCE);
4139
assert((uuid[8] & 0xC0) == 0x80); // RFC-4122 variant marker
4240

43-
char *generated = (char *)malloc(37*sizeof(char));
41+
char generated[37];
4442
uuid_unparse(uuid, generated);
4543
assert(isUUID(generated, 0) == 1); // Check it's a valid lower case UUID string.
4644
printf("\nuuid = %s\n", generated);
@@ -56,15 +54,13 @@ int main() {
5654
assert(isUUID(generated, 1) == 1); // Check it's a valid upper case UUID string.
5755
printf("uuid = %s\n", generated);
5856

59-
6057
uuid_copy(uuid2, uuid);
6158
assert(uuid_compare(uuid2, uuid) == 0);
6259

6360
uuid_clear(uuid);
6461
assert(uuid_compare(empty_uuid, uuid) == 0);
6562

6663
assert(uuid_is_null(uuid) == 1);
67-
6864
return 0;
6965
}
7066

0 commit comments

Comments
 (0)