diff options
Diffstat (limited to 'testing/hstox/methods.c')
-rw-r--r-- | testing/hstox/methods.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/testing/hstox/methods.c b/testing/hstox/methods.c new file mode 100644 index 00000000..08ad50fa --- /dev/null +++ b/testing/hstox/methods.c | |||
@@ -0,0 +1,147 @@ | |||
1 | #include "methods.h" | ||
2 | |||
3 | #include "util.h" | ||
4 | |||
5 | #include <crypto_core.h> | ||
6 | #include <net_crypto.h> | ||
7 | |||
8 | char const *const failure = "Failure"; | ||
9 | char const *const pending = "Pending"; | ||
10 | char const *const unimplemented = "Unimplemented"; | ||
11 | |||
12 | METHOD(array, Box, encrypt) | ||
13 | { | ||
14 | return pending; | ||
15 | } | ||
16 | |||
17 | METHOD(array, Box, decrypt) | ||
18 | { | ||
19 | return pending; | ||
20 | } | ||
21 | |||
22 | METHOD(array, CombinedKey, precompute) | ||
23 | { | ||
24 | return pending; | ||
25 | } | ||
26 | |||
27 | METHOD(array, KeyPair, newKeyPair) | ||
28 | { | ||
29 | uint8_t key1[crypto_box_PUBLICKEYBYTES]; | ||
30 | uint8_t key2[crypto_box_SECRETKEYBYTES]; | ||
31 | crypto_box_keypair(key1, key2); | ||
32 | |||
33 | SUCCESS { | ||
34 | // init array | ||
35 | msgpack_pack_array(res, 2); | ||
36 | msgpack_pack_bin(res, crypto_box_PUBLICKEYBYTES); | ||
37 | msgpack_pack_bin_body(res, key1, crypto_box_PUBLICKEYBYTES); | ||
38 | |||
39 | msgpack_pack_bin(res, crypto_box_SECRETKEYBYTES); | ||
40 | msgpack_pack_bin_body(res, key2, crypto_box_SECRETKEYBYTES); | ||
41 | } | ||
42 | return 0; | ||
43 | } | ||
44 | |||
45 | METHOD(array, KeyPair, fromSecretKey) | ||
46 | { | ||
47 | CHECK_SIZE(args, 1); | ||
48 | CHECK_TYPE(args.ptr[0], MSGPACK_OBJECT_BIN); | ||
49 | CHECK_SIZE(args.ptr[0].via.bin, crypto_box_SECRETKEYBYTES); | ||
50 | |||
51 | Net_Crypto c; | ||
52 | uint8_t secret_key[crypto_box_SECRETKEYBYTES]; | ||
53 | memcpy(secret_key, args.ptr[0].via.bin.ptr, crypto_box_SECRETKEYBYTES); | ||
54 | load_secret_key(&c, secret_key); | ||
55 | |||
56 | SUCCESS { | ||
57 | msgpack_pack_array(res, 2); | ||
58 | |||
59 | msgpack_pack_bin(res, crypto_box_PUBLICKEYBYTES); | ||
60 | msgpack_pack_bin_body(res, c.self_secret_key, crypto_box_PUBLICKEYBYTES); | ||
61 | msgpack_pack_bin(res, crypto_box_SECRETKEYBYTES); | ||
62 | msgpack_pack_bin_body(res, c.self_public_key, crypto_box_SECRETKEYBYTES); | ||
63 | } | ||
64 | return 0; | ||
65 | } | ||
66 | |||
67 | METHOD(array, Nonce, newNonce) | ||
68 | { | ||
69 | uint8_t nonce[24] = {0}; | ||
70 | new_nonce(nonce); | ||
71 | |||
72 | SUCCESS { | ||
73 | msgpack_pack_bin(res, sizeof nonce); | ||
74 | msgpack_pack_bin_body(res, nonce, sizeof nonce); | ||
75 | } | ||
76 | |||
77 | return 0; | ||
78 | } | ||
79 | |||
80 | METHOD(array, Nonce, increment) | ||
81 | { | ||
82 | CHECK_SIZE(args, 1); | ||
83 | CHECK_TYPE(args.ptr[0], MSGPACK_OBJECT_BIN); | ||
84 | CHECK_SIZE(args.ptr[0].via.bin, 24); | ||
85 | |||
86 | uint8_t nonce[24]; | ||
87 | memcpy(nonce, args.ptr[0].via.bin.ptr, 24); | ||
88 | increment_nonce(nonce); | ||
89 | |||
90 | SUCCESS { | ||
91 | msgpack_pack_bin(res, sizeof nonce); | ||
92 | msgpack_pack_bin_body(res, nonce, sizeof nonce); | ||
93 | } | ||
94 | |||
95 | return 0; | ||
96 | } | ||
97 | |||
98 | METHOD(array, rpc, capabilities) | ||
99 | { | ||
100 | return pending; | ||
101 | } | ||
102 | |||
103 | char const *call_method(msgpack_object_str name, msgpack_object_array args, msgpack_packer *res) | ||
104 | { | ||
105 | #define DISPATCH(SERVICE, NAME) \ | ||
106 | if (name.size == sizeof #SERVICE "." #NAME - 1 && \ | ||
107 | memcmp(name.ptr, #SERVICE "." #NAME, name.size) == 0) \ | ||
108 | return SERVICE##_##NAME(args, res) | ||
109 | DISPATCH(Binary, decode); | ||
110 | DISPATCH(Binary, encode); | ||
111 | DISPATCH(Box, decrypt); | ||
112 | DISPATCH(Box, encrypt); | ||
113 | DISPATCH(CombinedKey, precompute); | ||
114 | DISPATCH(KeyPair, fromSecretKey); | ||
115 | DISPATCH(KeyPair, newKeyPair); | ||
116 | DISPATCH(Nonce, increment); | ||
117 | DISPATCH(Nonce, newNonce); | ||
118 | #undef DISPATCH | ||
119 | |||
120 | // Default action: "Unimplemented" exception. New tests should be added here | ||
121 | // returning "Pending" until they are properly implemented. | ||
122 | return unimplemented; | ||
123 | } | ||
124 | |||
125 | int method_cmp(char const *ptr, char const *expected, size_t max_size) | ||
126 | { | ||
127 | char *transformed = malloc(max_size); | ||
128 | |||
129 | if (transformed == NULL) { | ||
130 | return memcmp(ptr, expected, max_size); | ||
131 | } | ||
132 | |||
133 | memcpy(transformed, ptr, max_size); | ||
134 | size_t i; | ||
135 | |||
136 | for (i = 0; i < max_size; i++) { | ||
137 | switch (transformed[i]) { | ||
138 | case '(': | ||
139 | case ')': | ||
140 | case ' ': | ||
141 | transformed[i] = '_'; | ||
142 | break; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | return memcmp(transformed, expected, max_size); | ||
147 | } | ||