isinetaddr/test/iscidraddr4_test.c

59 lines
1.4 KiB
C
Raw Normal View History

2023-08-26 21:25:52 +02:00
#include <isinetaddr.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
const char *valid[] = {
2023-08-27 10:38:26 +02:00
/* valid IPv4 */
"192.168.1.1", "0.0.0.0",
"255.255.255.255", "123.45.67.89",
2023-08-26 21:25:52 +02:00
"123.45.67.255",
2023-08-27 10:38:26 +02:00
/* valid cidr notation */
"10.0.0.1/8", "10.0.0.1/16",
"10.0.0.1/24", "10.0.0.1/32",
"255.255.255.255/32"
2023-08-26 21:25:52 +02:00
};
const char *invalid[] = {
2023-08-27 10:38:26 +02:00
/* invalid IPv4 */
"123.45.67.891", "192.168.500.1",
".192.168.1.1", "192..168.1.1",
"192.168.1.1.", "192.168.1.1..",
"192.168.1.1a", "192.2.2.",
/* invalid cidr notation */
"10.0.0.1/33", "127.0.0.1/64",
"127.0.0.1/", "127.0.0.1/a",
2023-08-28 05:12:08 +02:00
"127.0.0.1/322",
2023-08-27 10:38:26 +02:00
"/", "/123.","127/2",
/* edge cases */
2023-08-26 21:25:52 +02:00
"555555555555555555555555555555555555",
2023-08-27 10:38:26 +02:00
"", ".", ".......", "...4", "4...4",
2023-08-30 11:04:41 +02:00
"255.255.255.255.", "255.255.255.2555",
"255.255.255.255a",
2023-08-26 21:25:52 +02:00
NULL
};
int
main(void) {
size_t len;
/* IPv4: valid */
len = sizeof(valid) / sizeof(valid[0]);
for (size_t i = 0; i < len; i++) {
if (iscidraddr4(valid[i]) != 1) {
2023-08-26 21:25:52 +02:00
fprintf(stderr, "assertion failed: '%s' should be valid\n", valid[i]);
abort();
}
}
/* IPv4: invalid */
len = sizeof(invalid) / sizeof(invalid[0]);
for (size_t i = 0; i < len; i++) {
if (iscidraddr4(invalid[i]) != 0) {
2023-08-26 21:25:52 +02:00
fprintf(stderr, "assertion failed: '%s' should NOT be valid\n", invalid[i]);
abort();
}
}
/* Done */
printf("OK\n");
return EXIT_SUCCESS;
}