File: tests-auth.c

package info (click to toggle)
n2n 3.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,776 kB
  • sloc: ansic: 29,672; sh: 1,502; python: 621; makefile: 442; perl: 270; exp: 4
file content (128 lines) | stat: -rw-r--r-- 3,582 bytes parent folder | download
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
/*
 * (C) 2007-22 - ntop.org and contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>
 *
 */

#include <inttypes.h>

#include "n2n.h"
#include "hexdump.h"


uint8_t PKT_CONTENT1[]={
    0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
};

char *PKT_CONTENT2 = "00420mG51WS82GeB30qE3m";

void test_bin_to_ascii (void *buf, unsigned int bufsize) {
    char *test_name = "bin_to_ascii";
    char out[32];

    printf("%s: input size = 0x%x\n", test_name, bufsize);
    fhexdump(0, buf, bufsize, stdout);

    bin_to_ascii(out, buf, bufsize);

    printf("%s: output: %s\n", test_name, out);

    fprintf(stderr, "%s: tested\n", test_name);
    printf("\n");
}

void test_ascii_to_bin (char *buf) {
    char *test_name = "ascii_to_bin";
    uint8_t out[32];
    memset(out, 0, sizeof(out));

    printf("%s: input = %s\n", test_name, buf);

    ascii_to_bin(out, buf);
    // TODO:
    // - it would be nice if the function returned the bufsize,
    // - or took an allocation size as input

    printf("%s: output:\n", test_name);
    fhexdump(0, out, sizeof(out), stdout);

    fprintf(stderr, "%s: tested\n", test_name);
    printf("\n");
}

void test_generate_private_key (char *in, n2n_private_public_key_t prv) {
    char *test_name = "generate_private_key";

    printf("%s: input = %s\n", test_name, in);

    generate_private_key(prv, in);

    printf("%s: output:\n", test_name);
    fhexdump(0, prv, sizeof(n2n_private_public_key_t), stdout);

    fprintf(stderr, "%s: tested\n", test_name);
    printf("\n");
}

void test_generate_public_key (n2n_private_public_key_t prv, n2n_private_public_key_t pub) {
    char *test_name = "generate_public_key";

    printf("%s: input:\n", test_name);
    fhexdump(0, prv, sizeof(n2n_private_public_key_t), stdout);

    generate_public_key(pub, prv);

    printf("%s: output:\n", test_name);
    fhexdump(0, pub, sizeof(n2n_private_public_key_t), stdout);

    fprintf(stderr, "%s: tested\n", test_name);
    printf("\n");
}

void test_generate_shared_secret (n2n_private_public_key_t prv, n2n_private_public_key_t pub) {
    char *test_name = "generate_shared_secret";
    n2n_private_public_key_t out;

    printf("%s: input: prv\n", test_name);
    fhexdump(0, prv, sizeof(n2n_private_public_key_t), stdout);
    printf("%s: input: pub\n", test_name);
    fhexdump(0, pub, sizeof(n2n_private_public_key_t), stdout);

    generate_shared_secret(out, prv, pub);

    printf("%s: output:\n", test_name);
    fhexdump(0, out, sizeof(out), stdout);

    fprintf(stderr, "%s: tested\n", test_name);
    printf("\n");
}

int main (int argc, char * argv[]) {

    test_bin_to_ascii(PKT_CONTENT1, sizeof(PKT_CONTENT1));
    test_ascii_to_bin(PKT_CONTENT2);

    n2n_private_public_key_t prv;
    memset(prv, 0, sizeof(prv));
    n2n_private_public_key_t pub;
    memset(pub, 0, sizeof(pub));

    test_generate_private_key(PKT_CONTENT2, prv);
    test_generate_public_key(prv, pub);
    test_generate_shared_secret(prv, pub);

    return 0;
}