From c78d3b0413e57bbd925d79951acea36ab1e51c4c Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 19 Aug 2021 20:10:37 +0000 Subject: [PATCH] That's all of the tests. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3653 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/database.c | 4 + src/tests.c | 348 +++++++++++++++++++++++++++++++++ tests/07-promise-remote-reject | 29 --- tests/08-database | 34 ---- tests/09-this | 9 - tests/10-await | 23 --- tests/11-require | 28 --- tests/12-exit | 10 - tests/13-icu | 6 - tests/14-uint8array | 38 ---- tests/15-socket | 52 ----- tools/run-tests | 73 ------- 12 files changed, 352 insertions(+), 302 deletions(-) delete mode 100755 tests/07-promise-remote-reject delete mode 100755 tests/08-database delete mode 100755 tests/09-this delete mode 100644 tests/10-await delete mode 100644 tests/11-require delete mode 100644 tests/12-exit delete mode 100644 tests/13-icu delete mode 100644 tests/14-uint8array delete mode 100755 tests/15-socket delete mode 100755 tools/run-tests diff --git a/src/database.c b/src/database.c index 3247aded..51a11312 100644 --- a/src/database.c +++ b/src/database.c @@ -93,6 +93,7 @@ static JSValue _database_get(JSContext* context, JSValueConst this_val, int argc sqlite3_step(statement) == SQLITE_ROW) { entry = JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0)); } + JS_FreeCString(context, keyString); sqlite3_finalize(statement); } } @@ -113,6 +114,8 @@ JSValue _database_set(JSContext* context, JSValueConst this_val, int argc, JSVal sqlite3_bind_text(statement, 3, valueString, valueLength, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_OK) { } + JS_FreeCString(context, keyString); + JS_FreeCString(context, valueString); sqlite3_finalize(statement); } } @@ -130,6 +133,7 @@ JSValue _database_remove(JSContext* context, JSValueConst this_val, int argc, JS sqlite3_bind_text(statement, 2, keyString, keyLength, NULL) == SQLITE_OK && sqlite3_step(statement) == SQLITE_OK) { } + JS_FreeCString(context, keyString); sqlite3_finalize(statement); } } diff --git a/src/tests.c b/src/tests.c index ef880cf2..1b9d43e1 100644 --- a/src/tests.c +++ b/src/tests.c @@ -134,6 +134,345 @@ static void _test_promise_remote_throw(const char* exe_path) unlink("out/child.js"); } +static void _test_promise_remote_reject(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "var task = new Task();\n" + "task.activate();\n" + "task.execute({name: 'child.js', source: utf8Decode(File.readFile('out/child.js'))}).then(function() {\n" + " task.getExports().then(function(exp) {\n" + " return exp.add(1, 1);\n" + " }).then(function(sum) {\n" + " exit(1);\n" + " }).catch(function(error) {\n" + " print('Caught: ' + error.message);\n" + " if (error.stack) {\n" + " print('stack: ' + error.stack);\n" + " }\n" + " exit(0);\n" + " });\n" + "}).catch(function(e) {\n" + " print('caught', e.message);\n" + "});\n"); + fclose(file); + + file = fopen("out/child.js", "w"); + fprintf(file, + "exports = {\n" + " add: function(left, right) {\n" + " return new Promise(function(resolve, reject) {\n" + " reject(new Error('oops'));\n" + " });\n" + " }\n" + "}\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); + unlink("out/child.js"); +} + +static void _test_database(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "var db = new Database('testdb');\n" + "if (db.get('a')) {\n" + " exit(1);\n" + "}\n" + "db.set('a', 1);\n" + "if (db.get('a') != 1) {\n" + " exit(2);\n" + "}\n" + "db.set('b', 2);\n" + "db.set('c', 3);\n" + "\n" + "var expected = ['a', 'b', 'c'];\n" + "var have = db.getAll();\n" + "for (var i = 0; i < have.length; i++) {\n" + " var item = have[i];\n" + " if (expected.indexOf(item) == -1) {\n" + " print('Did not find ' + item + ' in db.');\n" + " exit(3);\n" + " } else {\n" + " expected.splice(expected.indexOf(item), 1);\n" + " }\n" + "}\n" + "if (expected.length) {\n" + " print('Expected but did not find: ' + JSON.stringify(expected));\n" + " exit(4);\n" + "}\n"); + fclose(file); + + unlink("out/testdb.sqlite"); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=out/testdb.sqlite -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + printf("returned %d\n", WEXITSTATUS(result)); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); + unlink("out/testdb.sqlite"); +} + +static void _test_this(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "var task = new Task();\n" + "task.activate.bind(null).apply();\n" + "exit(0);\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + printf("returned %d\n", WEXITSTATUS(result)); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); +} + +static void _test_await(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "print('hi');\n" + + "function foobar() {\n" + " return new Promise(function (resolve, reject) {\n" + " resolve(10);\n" + " });\n" + "}\n" + "\n" + "async function huh() {\n" + " let v = await foobar();\n" + " print('v => ' + v);\n" + " if (v != 10) {\n" + " throw new Error('nope');\n" + " }\n" + "}\n" + "\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + printf("returned %d\n", WEXITSTATUS(result)); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); +} + +static void _test_require(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "if (require('required').foo() != 12345) {\n" + " exit(1);\n" + "}\n" + "var gotError = false;\n" + "try {\n" + " require('missing');\n" + "} catch (error) {\n" + " print('nope');\n" + " gotError = true;\n" + "}\n" + "if (!gotError) {\n" + " exit(2);\n" + "}\n" + "exit(0);\n"); + fclose(file); + + file = fopen("out/required.js", "w"); + fprintf(file, + "function foo() {\n" + " return 12345;\n" + "}\n" + "exports.foo = foo;\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + printf("returned %d\n", WEXITSTATUS(result)); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); + unlink("out/required.js"); +} + +static void _test_exit(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, "require('blah');"); + fclose(file); + + file = fopen("out/blah.js", "w"); + fprintf(file, "\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + printf("returned %d\n", WEXITSTATUS(result)); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); + unlink("out/blah.js"); +} + +static void _test_icu(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "print('Hi');\n" + "print(parseInt('3').toLocaleString());\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + printf("returned %d\n", WEXITSTATUS(result)); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); +} + +static void _test_uint8array(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "var task = new Task();\n" + "task.onExit = function() {\n" + " print('child exited');\n" + "};\n" + "task.activate();\n" + "task.execute({name: 'child.js', source: utf8Decode(File.readFile('out/child.js'))}).then(async function() {\n" + " print('child started');\n" + " var input = new Uint8Array(10);\n" + " for (var i = 0; i < 10; i++) {\n" + " input[i] = i;\n" + " }\n" + " var test = (await task.getExports()).test;\n" + " var output = new Uint8Array(await test(input));\n" + " print('input', input, input.length, input.byteLength);\n" + " print('output', output, output.length, output.byteLength);\n" + " for (var i = 0; i < 10; i++) {\n" + " print(output[i]);\n" + " if (output[i] != i) {\n" + " print('output[' + i + '] == ' + output[i]);\n" + " exit(1);\n" + " }\n" + " }\n" + " exit(0);\n" + "});\n"); + fclose(file); + + file = fopen("out/child.js", "w"); + fprintf(file, + "exports = {\n" + " test: function(data) {\n" + " return data;\n" + " }\n" + "}\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + printf("returned %d\n", WEXITSTATUS(result)); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); + unlink("out/child.js"); +} + +static void _test_socket(const char* exe_path) +{ + FILE* file = fopen("out/test.js", "w"); + fprintf(file, + "'use strict';\n" + "\n" + "var s = new Socket();\n" + "print('connecting');\n" + "print('before connect', s.isConnected);\n" + "s.onError(function(e) {\n" + " print(e);\n" + "});\n" + "print('noDelay', s.noDelay);\n" + "s.noDelay = true;\n" + "s.connect('www.unprompted.com', 80).then(function() {\n" + " print('connected', s.isConnected);\n" + " print(s.peerName);\n" + " s.read(function(data) {\n" + " print('read', data.length);\n" + " });\n" + " s.write('GET / HTTP/1.0\\r\\n\\r\\n');\n" + "}).then(function() {\n" + " print('closed');\n" + "});\n" + "\n" + "var s2 = new Socket();\n" + "print('connecting');\n" + "print('before connect', s2.isConnected);\n" + "s2.onError(function(e) {\n" + " print('error');\n" + " print(e);\n" + "});\n" + "print('noDelay', s2.noDelay);\n" + "s2.noDelay = true;\n" + "s2.connect('www.unprompted.com', 443).then(function() {\n" + " print('connected');\n" + " s2.read(function(data) {\n" + " print('read', data.length);\n" + " });\n" + " return s2.startTls();\n" + "}).then(function() {\n" + " print('ready');\n" + " print(s2.peerName);\n" + " s2.write('GET / HTTP/1.0\\r\\nConnection: close\\r\\n\\r\\n').then(function() {\n" + " s2.shutdown();\n" + " });\n" + "}).catch(function(e) {\n" + " printf('caught');\n" + " print(e);\n" + "});\n"); + fclose(file); + + char command[256]; + snprintf(command, sizeof(command), "%s run --ssb-port=0 -s out/test.js", exe_path); + printf("%s\n", command); + int result = system(command); + printf("returned %d\n", WEXITSTATUS(result)); + assert(WIFEXITED(result)); + assert(WEXITSTATUS(result) == 0); + + unlink("out/test.js"); +} + void tf_tests(const char* exe_path) { tf_ssb_test(); @@ -141,5 +480,14 @@ void tf_tests(const char* exe_path) _test_child(exe_path); _test_promise(exe_path); _test_promise_remote_throw(exe_path); + _test_promise_remote_reject(exe_path); + _test_database(exe_path); + _test_this(exe_path); + _test_await(exe_path); + _test_require(exe_path); + _test_exit(exe_path); + _test_icu(exe_path); + _test_uint8array(exe_path); + _test_socket(exe_path); printf("Tests completed.\n"); } diff --git a/tests/07-promise-remote-reject b/tests/07-promise-remote-reject deleted file mode 100755 index 6f426bc9..00000000 --- a/tests/07-promise-remote-reject +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -var task = new Task(); -task.activate(); -task.execute({name: "child.js", source: File.readFile("child.js")}).then(function() { - task.getExports().then(function(exports) { - return exports.add(1, 1); - }).then(function(sum) { - exit(1); - }).catch(function(error) { - print(error); - print("Caught: " + error.message); - exit(0); - }); -}); -EOF - -cat > child.js << EOF -exports = { - add: function(left, right) { - return new Promise(function(resolve, reject) { - reject(new Error("oops")); - }); - } -} -EOF - -$TILDEFRIENDS test.js diff --git a/tests/08-database b/tests/08-database deleted file mode 100755 index ffa331b9..00000000 --- a/tests/08-database +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash - -mkdir testdb - -cat > test.js << EOF -var db = new Database("testdb"); -if (db.get("a")) { - exit(1); -} -db.set("a", 1); -if (db.get("a") != 1) { - exit(1); -} -db.set("b", 2); -db.set("c", 3); - -var expected = ['a', 'b', 'c']; -var have = db.getAll(); -for (var i = 0; i < have.length; i++) { - var item = have[i]; - if (expected.indexOf(item) == -1) { - print("Did not find " + item + " in db."); - exit(2); - } else { - expected.splice(expected.indexOf(item), 1); - } -} -if (expected.length) { - print("Expected but did not find: " + JSON.stringify(expected)); - exit(3); -} -EOF - -$TILDEFRIENDS test.js diff --git a/tests/09-this b/tests/09-this deleted file mode 100755 index 610c5c40..00000000 --- a/tests/09-this +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -var task = new Task(); -task.activate.bind(null).apply(); -exit(0); -EOF - -$TILDEFRIENDS test.js diff --git a/tests/10-await b/tests/10-await deleted file mode 100644 index a6cd986d..00000000 --- a/tests/10-await +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -print("hi"); - -function foobar() { - return new Promise(function (resolve, reject) { - resolve(10); - }); -} - -async function huh() { - let v = await foobar(); - print("v => " + v); - if (v != 10) { - throw new Error("nope"); - } -} - -huh(); -EOF - -$TILDEFRIENDS test.js diff --git a/tests/11-require b/tests/11-require deleted file mode 100644 index dbeb2dde..00000000 --- a/tests/11-require +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash - -cat > required.js << EOF -function foo() { - return 12345; -} - -exports.foo = foo; -EOF - -cat > test.js << EOF -if (require("required").foo() != 12345) { - exit(1); -} -var gotError = false; -try { - require("missing"); -} catch (error) { - print("nope"); - gotError = true; -} -if (!gotError) { - exit(2); -} -exit(0); -EOF - -$TILDEFRIENDS test.js diff --git a/tests/12-exit b/tests/12-exit deleted file mode 100644 index 7b9fdbb1..00000000 --- a/tests/12-exit +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -cat > blah.js << EOF -EOF - -cat > test.js << EOF -require("blah"); -EOF - -$TILDEFRIENDS test.js diff --git a/tests/13-icu b/tests/13-icu deleted file mode 100644 index 74cd1cd0..00000000 --- a/tests/13-icu +++ /dev/null @@ -1,6 +0,0 @@ -cat > test.js << EOF -print("Hi"); -print(parseInt("3").toLocaleString()); -EOF - -$TILDEFRIENDS test.js diff --git a/tests/14-uint8array b/tests/14-uint8array deleted file mode 100644 index 92e45d8e..00000000 --- a/tests/14-uint8array +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -var task = new Task(); -task.onExit = function() { - print("child exited"); -}; -task.activate(); -task.execute({name: "child.js", source: utf8Decode(File.readFile("child.js"))}).then(async function() { - print("child started"); - var input = new Uint8Array(10); - for (var i = 0; i < 10; i++) { - input[i] = i; - } - var test = (await task.getExports()).test; - var output = new Uint8Array(await test(input)); - print("input", input, input.length, input.byteLength); - print("output", output, output.length, output.byteLength); - for (var i = 0; i < 10; i++) { - print(output[i]); - if (output[i] != i) { - print("output[" + i + "] == " + output[i]); - exit(1); - } - } - exit(0); -}); -EOF - -cat > child.js << EOF -exports = { - test: function(data) { - return data; - } -} -EOF - -$TILDEFRIENDS test.js diff --git a/tests/15-socket b/tests/15-socket deleted file mode 100755 index 25940ce2..00000000 --- a/tests/15-socket +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash - -cat > test.js << EOF -"use strict"; - -var s = new Socket(); -print("connecting"); -print("before connect", s.isConnected); -s.onError(function(e) { - print(e); -}); -print("noDelay", s.noDelay); -s.noDelay = true; -s.connect("www.unprompted.com", 80).then(function() { - print("connected", s.isConnected); - print(s.peerName); - s.read(function(data) { - print("read", data.length); - }); - s.write("GET / HTTP/1.0\r\n\r\n"); -}).then(function() { - print("closed"); -}); - -var s2 = new Socket(); -print("connecting"); -print("before connect", s2.isConnected); -s2.onError(function(e) { - print("error"); - print(e); -}); -print("noDelay", s2.noDelay); -s2.noDelay = true; -s2.connect("www.unprompted.com", 443).then(function() { - print("connected"); - s2.read(function(data) { - print("read", data.length); - }); - return s2.startTls(); -}).then(function() { - print("ready"); - print(s2.peerName); - s2.write("GET / HTTP/1.0\r\nConnection: close\r\n\r\n").then(function() { - s2.shutdown(); - }); -}).catch(function(e) { - printf("caught"); - print(e); -}); -EOF - -$TILDEFRIENDS test.js diff --git a/tools/run-tests b/tools/run-tests deleted file mode 100755 index cb7348d7..00000000 --- a/tools/run-tests +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/python - -import argparse -import glob -import os -import shutil -import subprocess -import sys - -if sys.platform not in ('darwin', 'linux2'): - print 'Tests are only enabled on Linux.' - exit(0) - -parser = argparse.ArgumentParser() -parser.add_argument('tests', nargs=argparse.REMAINDER) -arguments = parser.parse_args() - -root = os.path.dirname(os.path.join(os.getcwd(), os.path.dirname(__file__))) -tmp = os.path.join(root, 'tmp') -logs = os.path.join(root, 'logs') -tests = os.path.join(root, 'tests') -executable = os.path.join(root, 'out', 'debug', 'tildefriends') - -if not os.path.isdir(logs): - os.makedirs(logs) - -selectedTests = set() -if not arguments.tests: - for test in glob.glob(os.path.join(tests, '*')): - selectedTests.add(test) -for pattern in arguments.tests: - for match in glob.glob(os.path.join(tests, '*' + pattern + '*')): - selectedTests.add(match) - -env = os.environ.copy() -env['TILDEFRIENDS'] = executable + ' run -s' -env['LOGDIR'] = logs - -def indent(text): - return '\n'.join('\t' + line for line in text.split('\n')) - -passCount = 0 -failCount = 0 - -for test in sorted(selectedTests): - if os.path.isdir(tmp): - shutil.rmtree(tmp) - if not os.path.isdir(tmp): - os.makedirs(tmp) - - process = subprocess.Popen(['bash', test], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=tmp, env=env) - interrupted = False - stdout, stderr = process.communicate() - if interrupted or process.returncode == 0: - if stdout.strip() == "SKIP": - print 'SKIPPED', test - else: - print 'PASSED', test - passCount += 1 - else: - print 'FAILED', test - print 'RETURNED:', process.returncode - print 'STDOUT:' - print indent(stdout) - print 'STDERR:' - print indent(stderr) - failCount += 1 - - if os.path.isdir(tmp): - shutil.rmtree(tmp) - -print passCount, 'tests passed. ', failCount, 'tests failed.' -exit(failCount)