Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 1 | /* |
Rich Salz | 440e5d8 | 2016-05-17 14:20:24 -0400 | [diff] [blame] | 2 | * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 8 | */ |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 9 | |
| 10 | #include "../ssl/packet_locl.h" |
| 11 | |
| 12 | #define BUF_LEN 255 |
| 13 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 14 | static int test_PACKET_remaining(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 15 | { |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 16 | PACKET pkt; |
| 17 | |
| 18 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 19 | || PACKET_remaining(&pkt) != BUF_LEN |
| 20 | || !PACKET_forward(&pkt, BUF_LEN - 1) |
| 21 | || PACKET_remaining(&pkt) != 1 |
| 22 | || !PACKET_forward(&pkt, 1) |
| 23 | || PACKET_remaining(&pkt) != 0) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 24 | fprintf(stderr, "test_PACKET_remaining() failed\n"); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | return 1; |
| 29 | } |
| 30 | |
Emilia Kasper | 0621786 | 2015-09-22 15:20:26 +0200 | [diff] [blame] | 31 | static int test_PACKET_end(unsigned char buf[BUF_LEN]) |
| 32 | { |
| 33 | PACKET pkt; |
| 34 | |
| 35 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 36 | || PACKET_remaining(&pkt) != BUF_LEN |
| 37 | || PACKET_end(&pkt) != buf + BUF_LEN |
| 38 | || !PACKET_forward(&pkt, BUF_LEN - 1) |
| 39 | || PACKET_end(&pkt) != buf + BUF_LEN |
| 40 | || !PACKET_forward(&pkt, 1) |
| 41 | || PACKET_end(&pkt) != buf + BUF_LEN) { |
| 42 | fprintf(stderr, "test_PACKET_end() failed\n"); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | return 1; |
| 47 | } |
| 48 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 49 | static int test_PACKET_get_1(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 50 | { |
| 51 | unsigned int i; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 52 | PACKET pkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 53 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 54 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 55 | || !PACKET_get_1(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 56 | || i != 0x02 |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 57 | || !PACKET_forward(&pkt, BUF_LEN - 2) |
| 58 | || !PACKET_get_1(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 59 | || i != 0xfe |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 60 | || PACKET_get_1(&pkt, &i)) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 61 | fprintf(stderr, "test_PACKET_get_1() failed\n"); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | return 1; |
| 66 | } |
| 67 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 68 | static int test_PACKET_get_4(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 69 | { |
| 70 | unsigned long i; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 71 | PACKET pkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 72 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 73 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 74 | || !PACKET_get_4(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 75 | || i != 0x08060402UL |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 76 | || !PACKET_forward(&pkt, BUF_LEN - 8) |
| 77 | || !PACKET_get_4(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 78 | || i != 0xfefcfaf8UL |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 79 | || PACKET_get_4(&pkt, &i)) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 80 | fprintf(stderr, "test_PACKET_get_4() failed\n"); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | return 1; |
| 85 | } |
| 86 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 87 | static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 88 | { |
| 89 | unsigned int i; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 90 | PACKET pkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 91 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 92 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 93 | || !PACKET_get_net_2(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 94 | || i != 0x0204 |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 95 | || !PACKET_forward(&pkt, BUF_LEN - 4) |
| 96 | || !PACKET_get_net_2(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 97 | || i != 0xfcfe |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 98 | || PACKET_get_net_2(&pkt, &i)) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 99 | fprintf(stderr, "test_PACKET_get_net_2() failed\n"); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | return 1; |
| 104 | } |
| 105 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 106 | static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 107 | { |
Matt Caswell | 04fe876 | 2015-08-06 22:44:29 +0100 | [diff] [blame] | 108 | unsigned long i; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 109 | PACKET pkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 110 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 111 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 112 | || !PACKET_get_net_3(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 113 | || i != 0x020406UL |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 114 | || !PACKET_forward(&pkt, BUF_LEN - 6) |
| 115 | || !PACKET_get_net_3(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 116 | || i != 0xfafcfeUL |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 117 | || PACKET_get_net_3(&pkt, &i)) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 118 | fprintf(stderr, "test_PACKET_get_net_3() failed\n"); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | return 1; |
| 123 | } |
| 124 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 125 | static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 126 | { |
| 127 | unsigned long i; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 128 | PACKET pkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 129 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 130 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 131 | || !PACKET_get_net_4(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 132 | || i != 0x02040608UL |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 133 | || !PACKET_forward(&pkt, BUF_LEN - 8) |
| 134 | || !PACKET_get_net_4(&pkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 135 | || i != 0xf8fafcfeUL |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 136 | || PACKET_get_net_4(&pkt, &i)) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 137 | fprintf(stderr, "test_PACKET_get_net_4() failed\n"); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | return 1; |
| 142 | } |
| 143 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 144 | static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 145 | { |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 146 | PACKET pkt, subpkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 147 | unsigned long i; |
| 148 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 149 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 150 | || !PACKET_get_sub_packet(&pkt, &subpkt, 4) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 151 | || !PACKET_get_net_4(&subpkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 152 | || i != 0x02040608UL |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 153 | || PACKET_remaining(&subpkt) |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 154 | || !PACKET_forward(&pkt, BUF_LEN - 8) |
| 155 | || !PACKET_get_sub_packet(&pkt, &subpkt, 4) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 156 | || !PACKET_get_net_4(&subpkt, &i) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 157 | || i != 0xf8fafcfeUL |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 158 | || PACKET_remaining(&subpkt) |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 159 | || PACKET_get_sub_packet(&pkt, &subpkt, 4)) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 160 | fprintf(stderr, "test_PACKET_get_sub_packet() failed\n"); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | return 1; |
| 165 | } |
| 166 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 167 | static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 168 | { |
Emilia Kasper | b698174 | 2016-02-01 15:26:18 +0100 | [diff] [blame] | 169 | const unsigned char *bytes; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 170 | PACKET pkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 171 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 172 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 173 | || !PACKET_get_bytes(&pkt, &bytes, 4) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 174 | || bytes[0] != 2 || bytes[1] != 4 |
| 175 | || bytes[2] != 6 || bytes[3] != 8 |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 176 | || PACKET_remaining(&pkt) != BUF_LEN -4 |
| 177 | || !PACKET_forward(&pkt, BUF_LEN - 8) |
| 178 | || !PACKET_get_bytes(&pkt, &bytes, 4) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 179 | || bytes[0] != 0xf8 || bytes[1] != 0xfa |
| 180 | || bytes[2] != 0xfc || bytes[3] != 0xfe |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 181 | || PACKET_remaining(&pkt)) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 182 | fprintf(stderr, "test_PACKET_get_bytes() failed\n"); |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | return 1; |
| 187 | } |
| 188 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 189 | static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 190 | { |
| 191 | unsigned char bytes[4]; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 192 | PACKET pkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 193 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 194 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 195 | || !PACKET_copy_bytes(&pkt, bytes, 4) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 196 | || bytes[0] != 2 || bytes[1] != 4 |
| 197 | || bytes[2] != 6 || bytes[3] != 8 |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 198 | || PACKET_remaining(&pkt) != BUF_LEN - 4 |
| 199 | || !PACKET_forward(&pkt, BUF_LEN - 8) |
| 200 | || !PACKET_copy_bytes(&pkt, bytes, 4) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 201 | || bytes[0] != 0xf8 || bytes[1] != 0xfa |
| 202 | || bytes[2] != 0xfc || bytes[3] != 0xfe |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 203 | || PACKET_remaining(&pkt)) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 204 | fprintf(stderr, "test_PACKET_copy_bytes() failed\n"); |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | return 1; |
| 209 | } |
| 210 | |
Emilia Kasper | 6720297 | 2015-10-01 13:54:11 +0200 | [diff] [blame] | 211 | static int test_PACKET_copy_all(unsigned char buf[BUF_LEN]) |
| 212 | { |
Alessandro Ghedini | 2d28462 | 2015-10-06 12:23:42 -0400 | [diff] [blame] | 213 | unsigned char tmp[BUF_LEN]; |
Emilia Kasper | 6720297 | 2015-10-01 13:54:11 +0200 | [diff] [blame] | 214 | PACKET pkt; |
| 215 | size_t len; |
| 216 | |
| 217 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
Alessandro Ghedini | 2d28462 | 2015-10-06 12:23:42 -0400 | [diff] [blame] | 218 | || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len) |
Emilia Kasper | 6720297 | 2015-10-01 13:54:11 +0200 | [diff] [blame] | 219 | || len != BUF_LEN |
Alessandro Ghedini | 2d28462 | 2015-10-06 12:23:42 -0400 | [diff] [blame] | 220 | || memcmp(buf, tmp, BUF_LEN) != 0 |
Emilia Kasper | 6720297 | 2015-10-01 13:54:11 +0200 | [diff] [blame] | 221 | || PACKET_remaining(&pkt) != BUF_LEN |
Alessandro Ghedini | 2d28462 | 2015-10-06 12:23:42 -0400 | [diff] [blame] | 222 | || PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)) { |
Emilia Kasper | 6720297 | 2015-10-01 13:54:11 +0200 | [diff] [blame] | 223 | fprintf(stderr, "test_PACKET_copy_bytes() failed\n"); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | return 1; |
| 228 | } |
| 229 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 230 | static int test_PACKET_memdup(unsigned char buf[BUF_LEN]) |
Emilia Kasper | 6d41fc8 | 2015-09-01 18:19:14 +0200 | [diff] [blame] | 231 | { |
| 232 | unsigned char *data = NULL; |
| 233 | size_t len; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 234 | PACKET pkt; |
| 235 | |
| 236 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 237 | || !PACKET_memdup(&pkt, &data, &len) |
Emilia Kasper | 6d41fc8 | 2015-09-01 18:19:14 +0200 | [diff] [blame] | 238 | || len != BUF_LEN |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 239 | || memcmp(data, PACKET_data(&pkt), len) |
| 240 | || !PACKET_forward(&pkt, 10) |
| 241 | || !PACKET_memdup(&pkt, &data, &len) |
Emilia Kasper | 6d41fc8 | 2015-09-01 18:19:14 +0200 | [diff] [blame] | 242 | || len != BUF_LEN - 10 |
Emilia Kasper | 88f84eb | 2015-09-17 18:55:19 +0200 | [diff] [blame] | 243 | || memcmp(data, PACKET_data(&pkt), len)) { |
Emilia Kasper | 6d41fc8 | 2015-09-01 18:19:14 +0200 | [diff] [blame] | 244 | fprintf(stderr, "test_PACKET_memdup() failed\n"); |
| 245 | OPENSSL_free(data); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | OPENSSL_free(data); |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | static int test_PACKET_strndup() |
| 254 | { |
| 255 | char buf[10], buf2[10]; |
Dr. Stephen Henson | 2dcac13 | 2015-09-09 17:28:17 +0100 | [diff] [blame] | 256 | char *data = NULL; |
| 257 | PACKET pkt; |
| 258 | |
Emilia Kasper | 6d41fc8 | 2015-09-01 18:19:14 +0200 | [diff] [blame] | 259 | memset(buf, 'x', 10); |
| 260 | memset(buf2, 'y', 10); |
| 261 | buf2[5] = '\0'; |
Emilia Kasper | 6d41fc8 | 2015-09-01 18:19:14 +0200 | [diff] [blame] | 262 | |
| 263 | if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10) |
| 264 | || !PACKET_strndup(&pkt, &data) |
| 265 | || strlen(data) != 10 |
| 266 | || strncmp(data, buf, 10) |
| 267 | || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10) |
| 268 | || !PACKET_strndup(&pkt, &data) |
| 269 | || strlen(data) != 5 |
| 270 | || strcmp(data, buf2)) { |
| 271 | fprintf(stderr, "test_PACKET_strndup failed\n"); |
| 272 | OPENSSL_free(data); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | OPENSSL_free(data); |
| 277 | return 1; |
| 278 | } |
| 279 | |
Emilia Kasper | 0621786 | 2015-09-22 15:20:26 +0200 | [diff] [blame] | 280 | static int test_PACKET_contains_zero_byte() |
| 281 | { |
| 282 | char buf[10], buf2[10]; |
| 283 | PACKET pkt; |
| 284 | |
| 285 | memset(buf, 'x', 10); |
| 286 | memset(buf2, 'y', 10); |
| 287 | buf2[5] = '\0'; |
| 288 | |
| 289 | if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10) |
| 290 | || PACKET_contains_zero_byte(&pkt) |
| 291 | || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10) |
| 292 | || !PACKET_contains_zero_byte(&pkt)) { |
| 293 | fprintf(stderr, "test_PACKET_contains_zero_byte failed\n"); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | return 1; |
| 298 | } |
| 299 | |
Emilia Kasper | 88f84eb | 2015-09-17 18:55:19 +0200 | [diff] [blame] | 300 | static int test_PACKET_forward(unsigned char buf[BUF_LEN]) |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 301 | { |
Emilia Kasper | b698174 | 2016-02-01 15:26:18 +0100 | [diff] [blame] | 302 | const unsigned char *byte; |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 303 | PACKET pkt; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 304 | |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 305 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 306 | || !PACKET_forward(&pkt, 1) |
| 307 | || !PACKET_get_bytes(&pkt, &byte, 1) |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 308 | || byte[0] != 4 |
Emilia Kasper | 88f84eb | 2015-09-17 18:55:19 +0200 | [diff] [blame] | 309 | || !PACKET_forward(&pkt, BUF_LEN - 3) |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 310 | || !PACKET_get_bytes(&pkt, &byte, 1) |
| 311 | || byte[0] != 0xfe) { |
Emilia Kasper | 88f84eb | 2015-09-17 18:55:19 +0200 | [diff] [blame] | 312 | fprintf(stderr, "test_PACKET_forward() failed\n"); |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | static int test_PACKET_buf_init() |
| 320 | { |
| 321 | unsigned char buf[BUF_LEN]; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 322 | PACKET pkt; |
| 323 | |
Emilia Kasper | 6720297 | 2015-10-01 13:54:11 +0200 | [diff] [blame] | 324 | /* Also tests PACKET_remaining() */ |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 325 | if ( !PACKET_buf_init(&pkt, buf, 4) |
Emilia Kasper | 6a12a57 | 2015-09-17 21:28:07 +0200 | [diff] [blame] | 326 | || PACKET_remaining(&pkt) != 4 |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 327 | || !PACKET_buf_init(&pkt, buf, BUF_LEN) |
Emilia Kasper | 6a12a57 | 2015-09-17 21:28:07 +0200 | [diff] [blame] | 328 | || PACKET_remaining(&pkt) != BUF_LEN |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 329 | || PACKET_buf_init(&pkt, buf, -1)) { |
| 330 | fprintf(stderr, "test_PACKET_buf_init() failed\n"); |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | return 1; |
| 335 | } |
| 336 | |
Emilia Kasper | b3e2272 | 2015-09-30 15:33:12 +0200 | [diff] [blame] | 337 | static int test_PACKET_null_init() |
| 338 | { |
| 339 | PACKET pkt; |
| 340 | |
| 341 | PACKET_null_init(&pkt); |
Emilia Kasper | b3e2272 | 2015-09-30 15:33:12 +0200 | [diff] [blame] | 342 | if ( PACKET_remaining(&pkt) != 0 |
| 343 | || PACKET_forward(&pkt, 1)) { |
| 344 | fprintf(stderr, "test_PACKET_null_init() failed\n"); |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | return 1; |
| 349 | } |
| 350 | |
Emilia Kasper | 3101154 | 2015-10-06 17:20:32 +0200 | [diff] [blame] | 351 | static int test_PACKET_equal(unsigned char buf[BUF_LEN]) |
| 352 | { |
| 353 | PACKET pkt; |
| 354 | |
| 355 | if ( !PACKET_buf_init(&pkt, buf, 4) |
| 356 | || !PACKET_equal(&pkt, buf, 4) |
| 357 | || PACKET_equal(&pkt, buf + 1, 4) |
| 358 | || !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 359 | || !PACKET_equal(&pkt, buf, BUF_LEN) |
| 360 | || PACKET_equal(&pkt, buf, BUF_LEN - 1) |
| 361 | || PACKET_equal(&pkt, buf, BUF_LEN + 1) |
| 362 | || PACKET_equal(&pkt, buf, 0)) { |
| 363 | fprintf(stderr, "test_PACKET_equal() failed\n"); |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | return 1; |
| 368 | } |
| 369 | |
Emilia Kasper | ec30e85 | 2015-08-18 12:29:36 +0200 | [diff] [blame] | 370 | static int test_PACKET_get_length_prefixed_1() |
| 371 | { |
| 372 | unsigned char buf[BUF_LEN]; |
| 373 | const size_t len = 16; |
| 374 | unsigned int i; |
| 375 | PACKET pkt, short_pkt, subpkt; |
| 376 | |
| 377 | buf[0] = len; |
| 378 | for (i = 1; i < BUF_LEN; i++) { |
| 379 | buf[i] = (i * 2) & 0xff; |
| 380 | } |
| 381 | |
| 382 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 383 | || !PACKET_buf_init(&short_pkt, buf, len) |
| 384 | || !PACKET_get_length_prefixed_1(&pkt, &subpkt) |
| 385 | || PACKET_remaining(&subpkt) != len |
| 386 | || !PACKET_get_net_2(&subpkt, &i) |
| 387 | || i != 0x0204 |
| 388 | || PACKET_get_length_prefixed_1(&short_pkt, &subpkt) |
| 389 | || PACKET_remaining(&short_pkt) != len) { |
| 390 | fprintf(stderr, "test_PACKET_get_length_prefixed_1() failed\n"); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | static int test_PACKET_get_length_prefixed_2() |
| 398 | { |
| 399 | unsigned char buf[1024]; |
| 400 | const size_t len = 516; /* 0x0204 */ |
| 401 | unsigned int i; |
| 402 | PACKET pkt, short_pkt, subpkt; |
| 403 | |
| 404 | for (i = 1; i <= 1024; i++) { |
| 405 | buf[i-1] = (i * 2) & 0xff; |
| 406 | } |
| 407 | |
| 408 | if ( !PACKET_buf_init(&pkt, buf, 1024) |
| 409 | || !PACKET_buf_init(&short_pkt, buf, len) |
| 410 | || !PACKET_get_length_prefixed_2(&pkt, &subpkt) |
| 411 | || PACKET_remaining(&subpkt) != len |
| 412 | || !PACKET_get_net_2(&subpkt, &i) |
| 413 | || i != 0x0608 |
| 414 | || PACKET_get_length_prefixed_2(&short_pkt, &subpkt) |
| 415 | || PACKET_remaining(&short_pkt) != len) { |
| 416 | fprintf(stderr, "test_PACKET_get_length_prefixed_2() failed\n"); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | static int test_PACKET_get_length_prefixed_3() |
| 424 | { |
| 425 | unsigned char buf[1024]; |
| 426 | const size_t len = 516; /* 0x000204 */ |
| 427 | unsigned int i; |
| 428 | PACKET pkt, short_pkt, subpkt; |
| 429 | |
| 430 | for (i = 0; i < 1024; i++) { |
| 431 | buf[i] = (i * 2) & 0xff; |
| 432 | } |
| 433 | |
| 434 | if ( !PACKET_buf_init(&pkt, buf, 1024) |
| 435 | || !PACKET_buf_init(&short_pkt, buf, len) |
| 436 | || !PACKET_get_length_prefixed_3(&pkt, &subpkt) |
| 437 | || PACKET_remaining(&subpkt) != len |
| 438 | || !PACKET_get_net_2(&subpkt, &i) |
| 439 | || i != 0x0608 |
| 440 | || PACKET_get_length_prefixed_3(&short_pkt, &subpkt) |
| 441 | || PACKET_remaining(&short_pkt) != len) { |
| 442 | fprintf(stderr, "test_PACKET_get_length_prefixed_3() failed\n"); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | return 1; |
| 447 | } |
| 448 | |
Emilia Kasper | 0621786 | 2015-09-22 15:20:26 +0200 | [diff] [blame] | 449 | static int test_PACKET_as_length_prefixed_1() |
| 450 | { |
| 451 | unsigned char buf[BUF_LEN]; |
| 452 | const size_t len = 16; |
| 453 | unsigned int i; |
| 454 | PACKET pkt, exact_pkt, subpkt; |
| 455 | |
| 456 | buf[0] = len; |
| 457 | for (i = 1; i < BUF_LEN; i++) { |
| 458 | buf[i] = (i * 2) & 0xff; |
| 459 | } |
| 460 | |
| 461 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) |
| 462 | || !PACKET_buf_init(&exact_pkt, buf, len + 1) |
| 463 | || PACKET_as_length_prefixed_1(&pkt, &subpkt) |
| 464 | || PACKET_remaining(&pkt) != BUF_LEN |
| 465 | || !PACKET_as_length_prefixed_1(&exact_pkt, &subpkt) |
| 466 | || PACKET_remaining(&exact_pkt) != 0 |
| 467 | || PACKET_remaining(&subpkt) != len) { |
| 468 | fprintf(stderr, "test_PACKET_as_length_prefixed_1() failed\n"); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | static int test_PACKET_as_length_prefixed_2() |
| 476 | { |
| 477 | unsigned char buf[1024]; |
| 478 | const size_t len = 516; /* 0x0204 */ |
| 479 | unsigned int i; |
| 480 | PACKET pkt, exact_pkt, subpkt; |
| 481 | |
| 482 | for (i = 1; i <= 1024; i++) { |
| 483 | buf[i-1] = (i * 2) & 0xff; |
| 484 | } |
| 485 | |
| 486 | if ( !PACKET_buf_init(&pkt, buf, 1024) |
| 487 | || !PACKET_buf_init(&exact_pkt, buf, len + 2) |
| 488 | || PACKET_as_length_prefixed_2(&pkt, &subpkt) |
| 489 | || PACKET_remaining(&pkt) != 1024 |
| 490 | || !PACKET_as_length_prefixed_2(&exact_pkt, &subpkt) |
| 491 | || PACKET_remaining(&exact_pkt) != 0 |
| 492 | || PACKET_remaining(&subpkt) != len) { |
| 493 | fprintf(stderr, "test_PACKET_as_length_prefixed_2() failed\n"); |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | return 1; |
| 498 | } |
| 499 | |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 500 | int main(int argc, char **argv) |
| 501 | { |
| 502 | unsigned char buf[BUF_LEN]; |
| 503 | unsigned int i; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 504 | |
| 505 | for (i=1; i<=BUF_LEN; i++) { |
Matt Caswell | 4412884 | 2015-08-04 13:03:20 +0100 | [diff] [blame] | 506 | buf[i-1] = (i * 2) & 0xff; |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 507 | } |
| 508 | i = 0; |
| 509 | |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 510 | if ( !test_PACKET_buf_init() |
Emilia Kasper | b3e2272 | 2015-09-30 15:33:12 +0200 | [diff] [blame] | 511 | || !test_PACKET_null_init() |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 512 | || !test_PACKET_remaining(buf) |
Emilia Kasper | 0621786 | 2015-09-22 15:20:26 +0200 | [diff] [blame] | 513 | || !test_PACKET_end(buf) |
Emilia Kasper | 3101154 | 2015-10-06 17:20:32 +0200 | [diff] [blame] | 514 | || !test_PACKET_equal(buf) |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 515 | || !test_PACKET_get_1(buf) |
| 516 | || !test_PACKET_get_4(buf) |
| 517 | || !test_PACKET_get_net_2(buf) |
| 518 | || !test_PACKET_get_net_3(buf) |
| 519 | || !test_PACKET_get_net_4(buf) |
| 520 | || !test_PACKET_get_sub_packet(buf) |
| 521 | || !test_PACKET_get_bytes(buf) |
| 522 | || !test_PACKET_copy_bytes(buf) |
Emilia Kasper | 6720297 | 2015-10-01 13:54:11 +0200 | [diff] [blame] | 523 | || !test_PACKET_copy_all(buf) |
Emilia Kasper | 4bd1646 | 2015-09-17 18:11:46 +0200 | [diff] [blame] | 524 | || !test_PACKET_memdup(buf) |
Emilia Kasper | 6d41fc8 | 2015-09-01 18:19:14 +0200 | [diff] [blame] | 525 | || !test_PACKET_strndup() |
Emilia Kasper | 0621786 | 2015-09-22 15:20:26 +0200 | [diff] [blame] | 526 | || !test_PACKET_contains_zero_byte() |
Emilia Kasper | 88f84eb | 2015-09-17 18:55:19 +0200 | [diff] [blame] | 527 | || !test_PACKET_forward(buf) |
Emilia Kasper | ec30e85 | 2015-08-18 12:29:36 +0200 | [diff] [blame] | 528 | || !test_PACKET_get_length_prefixed_1() |
| 529 | || !test_PACKET_get_length_prefixed_2() |
Emilia Kasper | 0621786 | 2015-09-22 15:20:26 +0200 | [diff] [blame] | 530 | || !test_PACKET_get_length_prefixed_3() |
| 531 | || !test_PACKET_as_length_prefixed_1() |
| 532 | || !test_PACKET_as_length_prefixed_2()) { |
Matt Caswell | 6fc2ef2 | 2015-04-17 16:10:23 +0100 | [diff] [blame] | 533 | return 1; |
| 534 | } |
| 535 | printf("PASS\n"); |
| 536 | return 0; |
| 537 | } |