Skip to content
Snippets Groups Projects
Commit 849cb917 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

More parameter validation for JS 'system' methods

And corresponding tests
parent f300a32e
No related branches found
No related tags found
No related merge requests found
Pipeline #7890 failed
// Test system function exceptions
// Value (e.g. 0, 1) is number of non-null/undefined args required
var func_list = {
'alias': 1,
'check_filename': 0,
'check_name': 0,
'check_pid': 1,
'check_syspass': 0,
'datestr': 1,
'del_user': 1,
'exec': 1,
'filter_ip': 1,
'find_login_id': 0,
'findstr': 2,
'get_node': 1,
'get_node_message': 1,
'get_telegram': 1,
'hacklog': 1,
'illegal_filename': 0,
'matchuser': 0,
'matchuserdata': 2,
'new_user': 1,
'notify': 2,
'popen': 1,
'put_node_message': 2,
'put_telegram': 2,
'safest_filename': 0,
'secondstr': 1,
'spamlog': 1,
'terminate_pid': 1,
'text': 1,
'timestr': 1,
'trashcan': 2,
'username': 0,
'zonestr': 1,
};
var noargs_required = [
'datestr',
'secondstr',
'timestr',
'zonestr',
];
var prefix = "system.";
for (var func in func_list) {
if (system[func] === undefined)
throw new Error("Function " + prefix + func + " isn't defined");
if (noargs_required.indexOf(func) < 0) {
var exp = prefix + func + "()";
var success = false;
try {
eval(exp);
} catch (e) {
if(e instanceof Error)
success = true;
}
if (!success)
throw new Error("Invocation of '" + exp + "' did not throw the expected Error exception");
}
if (func_list[func] < 1)
continue;
var arglist = ["(null)", "(undefined)"];
for(var i in arglist) {
exp = prefix + func + arglist[i];
success = false;
try {
eval(exp);
} catch (e) {
if(e instanceof Error)
success = true;
}
if (!success)
throw new Error("Invocation of '" + exp + "' did not throw the expected Error exception");
}
if (func_list[func] < 2)
continue;
var arglist = ["(0)", "(0, undefined)"];
for(var i in arglist) {
exp = prefix + func + arglist[i];
success = false;
try {
eval(exp);
} catch (e) {
if(e instanceof Error)
success = true;
}
if (!success)
throw new Error("Invocation of '" + exp + "' did not throw the expected Error exception");
}
}
// Test system method return types
// @format.tab-size 4, @format.use-tabs true
var type = {
'username("")' : 'string',
'alias("")' : 'string',
'find_login_id("")' : 'number',
'matchuser("")' : 'number',
'matchuserdata(1,"")' : 'number',
'trashcan("","")' : 'boolean',
'findstr("", "")' : 'boolean',
'zonestr()' : 'string',
'datestr()' : 'string',
'secondstr(0)' : 'string',
'get_node(0)' : 'object',
'get_node_message(0)' : 'string',
'get_telegram(0)' : 'string',
'exec(" ")' : 'number',
'check_syspass(null)' : 'boolean',
'check_name(null)' : 'boolean',
'check_filename(null)' : 'boolean',
'allowed_filename(null)': 'boolean',
'safest_filename(null)' : 'boolean',
'illegal_filename(null)': 'boolean',
'text(0)' : 'object', // null
'text(1)' : 'string',
'text("")' : 'object', // null
'text("Email")' : 'string',
};
var prefix = "system.";
for (var i in type) {
var result;
var exp = prefix + i;
try {
// writeln(exp);
result = eval(exp);
} catch(e) {
log("Error evaluating: " + exp);
throw e;
}
if (typeof result !== type[i])
throw new Error("typeof " + exp + " = '" + (typeof result) + "' instead of '" + type[i] + "'");
}
jsdoor_revision !== undefined
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment