tildefriends/src/tests.c

499 lines
12 KiB
C
Raw Normal View History

#include "tests.h"
#include "ssb.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
static void _test_nop(const char* exe_path)
{
FILE* file = fopen("out/test.js", "w");
fprintf(file, "print('hi');");
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);
(void)result;
assert(WIFEXITED(result));
assert(WEXITSTATUS(result) == 0);
}
static void _test_child(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(function() {\n"
" print('child started');\n"
"});");
fclose(file);
file = fopen("out/child.js", "w");
fprintf(file,
"print('I am the child process.');\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);
(void)result;
assert(WIFEXITED(result));
assert(WEXITSTATUS(result) == 0);
unlink("out/test.js");
unlink("out/child.js");
}
static void _test_promise(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(exports) {\n"
" return exports.add(1, 1);\n"
" }).then(function(sum) {\n"
" if (sum == 2) {\n"
" exit(0);\n"
" } else {\n"
" exit(1);\n"
" }\n"
" });\n"
"});\n");
fclose(file);
file = fopen("out/child.js", "w");
fprintf(file,
"exports = {\n"
" add: function(left, right) {\n"
" return left + right;\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);
(void)result;
assert(WIFEXITED(result));
assert(WEXITSTATUS(result) == 0);
unlink("out/test.js");
unlink("out/child.js");
}
static void _test_promise_remote_throw(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"
" throw new Error('fail');\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);
(void)result;
assert(WIFEXITED(result));
assert(WEXITSTATUS(result) == 0);
unlink("out/test.js");
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);
(void)result;
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 ? data.length : null);\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 ? data.length : null);\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();
_test_nop(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");
}