summaryrefslogtreecommitdiff
path: root/testing/hstox/methods.c
blob: 08ad50fa04c62e256083c255d2e9bcb40c6d0913 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "methods.h"

#include "util.h"

#include <crypto_core.h>
#include <net_crypto.h>

char const *const failure       = "Failure";
char const *const pending       = "Pending";
char const *const unimplemented = "Unimplemented";

METHOD(array, Box, encrypt)
{
    return pending;
}

METHOD(array, Box, decrypt)
{
    return pending;
}

METHOD(array, CombinedKey, precompute)
{
    return pending;
}

METHOD(array, KeyPair, newKeyPair)
{
    uint8_t key1[crypto_box_PUBLICKEYBYTES];
    uint8_t key2[crypto_box_SECRETKEYBYTES];
    crypto_box_keypair(key1, key2);

    SUCCESS {
        // init array
        msgpack_pack_array(res, 2);
        msgpack_pack_bin(res, crypto_box_PUBLICKEYBYTES);
        msgpack_pack_bin_body(res, key1, crypto_box_PUBLICKEYBYTES);

        msgpack_pack_bin(res, crypto_box_SECRETKEYBYTES);
        msgpack_pack_bin_body(res, key2, crypto_box_SECRETKEYBYTES);
    }
    return 0;
}

METHOD(array, KeyPair, fromSecretKey)
{
    CHECK_SIZE(args, 1);
    CHECK_TYPE(args.ptr[0], MSGPACK_OBJECT_BIN);
    CHECK_SIZE(args.ptr[0].via.bin, crypto_box_SECRETKEYBYTES);

    Net_Crypto c;
    uint8_t    secret_key[crypto_box_SECRETKEYBYTES];
    memcpy(secret_key, args.ptr[0].via.bin.ptr, crypto_box_SECRETKEYBYTES);
    load_secret_key(&c, secret_key);

    SUCCESS {
        msgpack_pack_array(res, 2);

        msgpack_pack_bin(res, crypto_box_PUBLICKEYBYTES);
        msgpack_pack_bin_body(res, c.self_secret_key, crypto_box_PUBLICKEYBYTES);
        msgpack_pack_bin(res, crypto_box_SECRETKEYBYTES);
        msgpack_pack_bin_body(res, c.self_public_key, crypto_box_SECRETKEYBYTES);
    }
    return 0;
}

METHOD(array, Nonce, newNonce)
{
    uint8_t nonce[24] = {0};
    new_nonce(nonce);

    SUCCESS {
        msgpack_pack_bin(res, sizeof nonce);
        msgpack_pack_bin_body(res, nonce, sizeof nonce);
    }

    return 0;
}

METHOD(array, Nonce, increment)
{
    CHECK_SIZE(args, 1);
    CHECK_TYPE(args.ptr[0], MSGPACK_OBJECT_BIN);
    CHECK_SIZE(args.ptr[0].via.bin, 24);

    uint8_t nonce[24];
    memcpy(nonce, args.ptr[0].via.bin.ptr, 24);
    increment_nonce(nonce);

    SUCCESS {
        msgpack_pack_bin(res, sizeof nonce);
        msgpack_pack_bin_body(res, nonce, sizeof nonce);
    }

    return 0;
}

METHOD(array, rpc, capabilities)
{
    return pending;
}

char const *call_method(msgpack_object_str name, msgpack_object_array args, msgpack_packer *res)
{
#define DISPATCH(SERVICE, NAME)                                                                    \
  if (name.size == sizeof #SERVICE "." #NAME - 1 &&                                                \
      memcmp(name.ptr, #SERVICE "." #NAME, name.size) == 0)                                        \
  return SERVICE##_##NAME(args, res)
    DISPATCH(Binary, decode);
    DISPATCH(Binary, encode);
    DISPATCH(Box, decrypt);
    DISPATCH(Box, encrypt);
    DISPATCH(CombinedKey, precompute);
    DISPATCH(KeyPair, fromSecretKey);
    DISPATCH(KeyPair, newKeyPair);
    DISPATCH(Nonce, increment);
    DISPATCH(Nonce, newNonce);
#undef DISPATCH

    // Default action: "Unimplemented" exception. New tests should be added here
    // returning "Pending" until they are properly implemented.
    return unimplemented;
}

int method_cmp(char const *ptr, char const *expected, size_t max_size)
{
    char *transformed = malloc(max_size);

    if (transformed == NULL) {
        return memcmp(ptr, expected, max_size);
    }

    memcpy(transformed, ptr, max_size);
    size_t i;

    for (i = 0; i < max_size; i++) {
        switch (transformed[i]) {
            case '(':
            case ')':
            case ' ':
                transformed[i] = '_';
                break;
        }
    }

    return memcmp(transformed, expected, max_size);
}