File: porttostrtest.c

package info (click to toggle)
tinyssh 20230101-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,244 kB
  • sloc: ansic: 12,106; sh: 1,168; python: 479; makefile: 42
file content (96 lines) | stat: -rw-r--r-- 2,437 bytes parent folder | download | duplicates (4)
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
/*
20140201
Jan Mojzis
Public domain.
*/

#include <unistd.h>
#include <stdio.h>
#include "fail.h"
#include "porttostr.h"
#include "crypto_uint16.h"

static void porttostr_bufferoverflow(void) {

    char buf[PORTTOSTR_LEN + 16];
    long long i, j;
    unsigned char port[2] = { 5, 5 };
    char ch[2] = {0x00, (char)0xff };

    if ((PORTTOSTR_LEN) < sizeof("65535")) fail("PORTTOSTR_LEN too small");

    for (j = 0; j < 2; ++j) {
        for (i = 0; i < sizeof buf; ++i) buf[i] = ch[j];
        porttostr(buf + 8, port);
        for (i = 0; i < 8; ++i) if (buf[i] != ch[j]) fail("porttostr writes before output");
        for (i = 0; i < 8; ++i) if (buf[i + PORTTOSTR_LEN + 8] != ch[j]) fail("porttostr writes after output");
    }
}

static struct vectors {
    const char *port;
    const char *portstr;
} testvectors[] = {
    { "\377\377", "65535" },
    { "\000\000", "0\0\0\0\0" },
    { 0, 0 }
};

static void porttostr_testvectors(void) {

    char *x, *y, *z;
    long long i, j;
    char buf[PORTTOSTR_LEN];

    for (i = 0; testvectors[i].port; ++i) {
        for (j = 0; j < sizeof buf; ++j) buf[j] = (char)0xff;
        x = porttostr(buf, (unsigned char *)testvectors[i].port);
        y = porttostr(0,   (unsigned char *)testvectors[i].port);
        z = (char *)testvectors[i].portstr;
        for (j = 0; j < PORTTOSTR_LEN; ++j) {
            if (buf[j] != z[j]) fail("bad output");
            if (x[j] != z[j]) fail("bad output");
            if (y[j] != z[j]) fail("bad output");
        }
    }
}

static void packport(unsigned char *y, crypto_uint16 x) {

    y[1] = x & 255; x >>= 8;
    y[0] = x & 255;
}

static void porttostr_full(void) {

    long long i, j;
    unsigned char port[2];
    char *x, *y;
    char buf[PORTTOSTR_LEN];
    char buf2[PORTTOSTR_LEN + 10];

    for (i = 0; i < 65536; ++i) {
        for (j = 0; j < sizeof buf; ++j) buf[j] = (char)0xff;
        for (j = 0; j < sizeof buf; ++j) buf2[j] = 0;

        packport(port, i);

        x = porttostr(buf, port);
        y = porttostr(0,   port);

        snprintf(buf2, sizeof buf2, "%lld", i);
        for (j = 0; j < PORTTOSTR_LEN; ++j) {
            if (buf[j] != buf2[j]) fail("bad output");
            if (x[j] != buf2[j]) fail("bad output");
            if (y[j] != buf2[j]) fail("bad output");
        }
    }
}

int main(void) {

    porttostr_bufferoverflow();
    porttostr_testvectors();
    porttostr_full();
    _exit(0);
}