about summary refs log tree commit diff stats
path: root/test/src/beacon_tests.c
blob: bc94cbd13b745a92956dde0b6d4c4818ffc20b29 (plain) (blame)
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
#include "../../src/libwifi.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>

#define BCAST_MAC "\xff\xff\xff\xff\xff\xff"
#define TO_MAC "\x00\x20\x91\xAA\xBB\xCC"
const unsigned char to[] = TO_MAC;
const unsigned char bcast[] = BCAST_MAC;

const unsigned char beacon[] = "\x00\x00\x18\x00\x8e\x58\x00\x00\x10\x02\x6c\x09\xa0\x00\x54\x00" \
                               "\x00\x2b\x00\x00\x9f\x61\xc9\x5c\x80\x00\x00\x00\xff\xff\xff\xff" \
                               "\xff\xff\x00\x0c\x41\x82\xb2\x55\x00\x0c\x41\x82\xb2\x55\x50\xf8" \
                               "\x89\xf1\xd4\x1b\x01\x00\x00\x00\x64\x00\x11\x04\x00\x07\x43\x6f" \
                               "\x68\x65\x72\x65\x72\x01\x08\x82\x84\x8b\x96\x24\x30\x48\x6c\x03" \
                               "\x01\x01\x05\x04\x00\x01\x00\x00\x2a\x01\x02\x2f\x01\x02\x30\x18" \
                               "\x01\x00\x00\x0f\xac\x02\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x02" \
                               "\x01\x00\x00\x0f\xac\x02\x00\x00\x32\x04\x0c\x12\x18\x60\xdd\x06" \
                               "\x00\x10\x18\x02\x00\x04\xdd\x1c\x00\x50\xf2\x01\x01\x00\x00\x50" \
                               "\xf2\x02\x02\x00\x00\x50\xf2\x04\x00\x50\xf2\x02\x01\x00\x00\x50" \
                               "\xf2\x02\x00\x00\x9f\x61\xc9\x5c";

int test_beacon_gen_full() {
    struct libwifi_beacon beacon = {0};

    int ret = libwifi_create_beacon(&beacon, bcast, to, to, "Some SSID", 11);
    if (ret != 0) {
        fprintf(stderr, "Failed to create beacon: %s\n", strerror(ret));
        return ret;
    }

    int beacon_len = libwifi_get_beacon_length(&beacon);
    if (beacon_len <= 0) {
        fprintf(stderr, "Invalid beacon length: %d\n", beacon_len);
        return beacon_len;
    }

    unsigned char *buf = malloc(beacon_len);
    if (buf == NULL) {
        fprintf(stderr, "Failed to allocate buffer\n");
        return -1;
    }

    int dump_len = libwifi_dump_beacon(&beacon, buf, beacon_len);
    if (dump_len <= 0) {
        fprintf(stderr, "Failed to dump beacon\n");
        return ret;
    }

    return 0;
}

int test_beacon_add_tag() {
    struct libwifi_beacon beacon = {0};

    int ret = libwifi_create_beacon(&beacon, bcast, to, to, "Some SSID", 11);
    if (ret != 0) {
        fprintf(stderr, "Failed to create beacon: %s\n", strerror(ret));
        return ret;
    }

    ret = libwifi_quick_add_tag(&beacon.tags,
                                TAG_VENDOR_SPECIFIC,
                                (const unsigned char *) "\x00\x11\x22\xAAHello World",
                                15);
    if (ret != 0) {
        fprintf(stderr, "Failed to add beacon tagged parameter: %s\n", strerror(ret));
        return ret;
    }

    int beacon_len = libwifi_get_beacon_length(&beacon);
    if (beacon_len <= 0) {
        fprintf(stderr, "Invalid beacon length: %d\n", beacon_len);
        return beacon_len;
    }

    unsigned char *buf = malloc(beacon_len);
    if (buf == NULL) {
        fprintf(stderr, "Failed to allocate buffer\n");
        return -1;
    }

    int dump_len = libwifi_dump_beacon(&beacon, buf, beacon_len);
    if (dump_len <= 0) {
        fprintf(stderr, "Failed to dump beacon\n");
        return ret;
    }

    return 0;
}

int test_beacon_parse() {
    struct libwifi_frame frame = {0};
    struct libwifi_bss bss = {0};

    int ret = libwifi_get_wifi_frame(&frame, beacon, sizeof(beacon), 1);
    if (ret != 0) {
        return ret;
    }

    if (frame.frame_control.type != TYPE_MANAGEMENT || frame.frame_control.subtype != SUBTYPE_BEACON) {
        return -1;
    }

    ret = libwifi_parse_beacon(&bss, &frame);
    if (ret != 0) {
        return ret;
    }

    if (strcmp(bss.ssid, "Coherer") != 0) {
        return -2;
    }

    if (bss.channel != 1) {
        return -3;
    }

    libwifi_free_bss(&bss);
    libwifi_free_wifi_frame(&frame);

    return 0;
}

int main(int argc, char **argv) {
    if (argc < 2) {
        printf("Specify test\n");
        return -1;
    }

    if (strcmp(argv[1], "--beacon-gen-full") == 0) {
        return test_beacon_gen_full();
    } else if (strcmp(argv[1], "--beacon-gen-tags") == 0) {
        return test_beacon_add_tag();
    } else if (strcmp(argv[1], "--beacon-parse") == 0) {
        return test_beacon_parse();
    }

    return -1;
}