diff options
Diffstat (limited to 'test/src/beacon_tests.c')
-rw-r--r-- | test/src/beacon_tests.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/test/src/beacon_tests.c b/test/src/beacon_tests.c new file mode 100644 index 0000000..d152d5e --- /dev/null +++ b/test/src/beacon_tests.c | |||
@@ -0,0 +1,140 @@ | |||
1 | #include "../../src/libwifi.h" | ||
2 | |||
3 | #include <errno.h> | ||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | |||
7 | #define BCAST_MAC "\xff\xff\xff\xff\xff\xff" | ||
8 | #define TO_MAC "\x00\x20\x91\xAA\xBB\xCC" | ||
9 | const unsigned char to[] = TO_MAC; | ||
10 | const unsigned char bcast[] = BCAST_MAC; | ||
11 | |||
12 | const unsigned char beacon[] = "\x00\x00\x18\x00\x8e\x58\x00\x00\x10\x02\x6c\x09\xa0\x00\x54\x00" \ | ||
13 | "\x00\x2b\x00\x00\x9f\x61\xc9\x5c\x80\x00\x00\x00\xff\xff\xff\xff" \ | ||
14 | "\xff\xff\x00\x0c\x41\x82\xb2\x55\x00\x0c\x41\x82\xb2\x55\x50\xf8" \ | ||
15 | "\x89\xf1\xd4\x1b\x01\x00\x00\x00\x64\x00\x11\x04\x00\x07\x43\x6f" \ | ||
16 | "\x68\x65\x72\x65\x72\x01\x08\x82\x84\x8b\x96\x24\x30\x48\x6c\x03" \ | ||
17 | "\x01\x01\x05\x04\x00\x01\x00\x00\x2a\x01\x02\x2f\x01\x02\x30\x18" \ | ||
18 | "\x01\x00\x00\x0f\xac\x02\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x02" \ | ||
19 | "\x01\x00\x00\x0f\xac\x02\x00\x00\x32\x04\x0c\x12\x18\x60\xdd\x06" \ | ||
20 | "\x00\x10\x18\x02\x00\x04\xdd\x1c\x00\x50\xf2\x01\x01\x00\x00\x50" \ | ||
21 | "\xf2\x02\x02\x00\x00\x50\xf2\x04\x00\x50\xf2\x02\x01\x00\x00\x50" \ | ||
22 | "\xf2\x02\x00\x00\x9f\x61\xc9\x5c"; | ||
23 | |||
24 | int test_beacon_gen_full() { | ||
25 | struct libwifi_beacon beacon = {0}; | ||
26 | |||
27 | int ret = libwifi_create_beacon(&beacon, bcast, to, "Some SSID", 11); | ||
28 | if (ret != 0) { | ||
29 | fprintf(stderr, "Failed to create beacon: %s\n", strerror(ret)); | ||
30 | return ret; | ||
31 | } | ||
32 | |||
33 | int beacon_len = libwifi_get_beacon_length(&beacon); | ||
34 | if (beacon_len <= 0) { | ||
35 | fprintf(stderr, "Invalid beacon length: %d\n", beacon_len); | ||
36 | return beacon_len; | ||
37 | } | ||
38 | |||
39 | unsigned char *buf = malloc(beacon_len); | ||
40 | if (buf == NULL) { | ||
41 | fprintf(stderr, "Failed to allocate buffer\n"); | ||
42 | return -1; | ||
43 | } | ||
44 | |||
45 | int dump_len = libwifi_dump_beacon(&beacon, buf, beacon_len); | ||
46 | if (dump_len <= 0) { | ||
47 | fprintf(stderr, "Failed to dump beacon\n"); | ||
48 | return ret; | ||
49 | } | ||
50 | |||
51 | return 0; | ||
52 | } | ||
53 | |||
54 | int test_beacon_add_tag() { | ||
55 | struct libwifi_beacon beacon = {0}; | ||
56 | |||
57 | int ret = libwifi_create_beacon(&beacon, bcast, to, "Some SSID", 11); | ||
58 | if (ret != 0) { | ||
59 | fprintf(stderr, "Failed to create beacon: %s\n", strerror(ret)); | ||
60 | return ret; | ||
61 | } | ||
62 | |||
63 | ret = libwifi_quick_add_tag(&beacon.tags, | ||
64 | TAG_VENDOR_SPECIFIC, | ||
65 | (const unsigned char *) "\x00\x11\x22\xAAHello World", | ||
66 | 15); | ||
67 | if (ret != 0) { | ||
68 | fprintf(stderr, "Failed to add beacon tagged parameter: %s\n", strerror(ret)); | ||
69 | return ret; | ||
70 | } | ||
71 | |||
72 | int beacon_len = libwifi_get_beacon_length(&beacon); | ||
73 | if (beacon_len <= 0) { | ||
74 | fprintf(stderr, "Invalid beacon length: %d\n", beacon_len); | ||
75 | return beacon_len; | ||
76 | } | ||
77 | |||
78 | unsigned char *buf = malloc(beacon_len); | ||
79 | if (buf == NULL) { | ||
80 | fprintf(stderr, "Failed to allocate buffer\n"); | ||
81 | return -1; | ||
82 | } | ||
83 | |||
84 | int dump_len = libwifi_dump_beacon(&beacon, buf, beacon_len); | ||
85 | if (dump_len <= 0) { | ||
86 | fprintf(stderr, "Failed to dump beacon\n"); | ||
87 | return ret; | ||
88 | } | ||
89 | |||
90 | return 0; | ||
91 | } | ||
92 | |||
93 | int test_beacon_parse() { | ||
94 | struct libwifi_frame frame = {0}; | ||
95 | struct libwifi_bss bss = {0}; | ||
96 | |||
97 | int ret = libwifi_get_wifi_frame(&frame, beacon, sizeof(beacon), 1); | ||
98 | if (ret != 0) { | ||
99 | return ret; | ||
100 | } | ||
101 | |||
102 | if (frame.frame_control.type != TYPE_MANAGEMENT || frame.frame_control.subtype != SUBTYPE_BEACON) { | ||
103 | return -1; | ||
104 | } | ||
105 | |||
106 | ret = libwifi_parse_beacon(&bss, &frame); | ||
107 | if (ret != 0) { | ||
108 | return ret; | ||
109 | } | ||
110 | |||
111 | if (strcmp(bss.ssid, "Coherer") != 0) { | ||
112 | return -2; | ||
113 | } | ||
114 | |||
115 | if (bss.channel != 1) { | ||
116 | return -3; | ||
117 | } | ||
118 | |||
119 | libwifi_free_bss(&bss); | ||
120 | libwifi_free_wifi_frame(&frame); | ||
121 | |||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | int main(int argc, char **argv) { | ||
126 | if (argc < 2) { | ||
127 | printf("Specify test\n"); | ||
128 | return -1; | ||
129 | } | ||
130 | |||
131 | if (strcmp(argv[1], "--beacon-gen-full") == 0) { | ||
132 | return test_beacon_gen_full(); | ||
133 | } else if (strcmp(argv[1], "--beacon-gen-tags") == 0) { | ||
134 | return test_beacon_add_tag(); | ||
135 | } else if (strcmp(argv[1], "--beacon-parse") == 0) { | ||
136 | return test_beacon_parse(); | ||
137 | } | ||
138 | |||
139 | return -1; | ||
140 | } | ||