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
67
68
69
70
71
72
73
74
pub fn format_callback(function_name: String, arg: String) -> String {
let formatted_string = &format!("window[\"{}\"]({})", function_name, arg);
formatted_string.to_string()
}
pub fn format_callback_result(
result: Result<String, String>,
callback: String,
error_callback: String,
) -> String {
match result {
Ok(res) => format_callback(callback, res),
Err(err) => format_callback(error_callback, format!("\"{}\"", err)),
}
}
#[cfg(test)]
mod test {
use crate::rpc::*;
use quickcheck_macros::quickcheck;
// check abritrary strings in the format callback function
#[quickcheck]
fn qc_formating(f: String, a: String) -> bool {
// can not accept empty strings
if f != "" && a != "" {
// get length of function and argument
let alen = &a.len();
let flen = &f.len();
// call format callback
let fc = format_callback(f, a);
// get length of the resulting string
let fclen = fc.len();
// if formatted string equals the length of the argument and the function plus 12 then its correct.
fclen == alen + flen + 12
} else {
true
}
}
// check arbitrary strings in format_callback_result
#[quickcheck]
fn qc_format_res(result: Result<String, String>, c: String, ec: String) -> bool {
// match on result to decide how to call the function.
match result {
// if ok, get length of result and callback strings.
Ok(r) => {
let rlen = r.len();
let clen = c.len();
// take the ok string from result and pass it into format_callback_result as an ok.
let resp = format_callback_result(Ok(r), c, ec);
// get response string length
let reslen = resp.len();
// if response string length equals result and callback length plus 12 characters then it is correct.
reslen == rlen + clen + 12
}
// If Err, get length of Err and Error callback
Err(err) => {
let eclen = ec.len();
let errlen = err.len();
// pass err as Err into format_callback_result with callback and error callback
let resp = format_callback_result(Err(err), c, ec);
// get response string length
let reslen = resp.len();
// if length of response string equals the error length and the error callback length plus 14 characters then its is correct.
reslen == eclen + errlen + 14
}
}
}
}