tests: added unit test for ipv4_prefix_to_mask()

This commit is contained in:
Nikos Mavrogiannopoulos
2015-05-07 08:50:33 +02:00
parent 111e5a9b5f
commit 353e5018ef
2 changed files with 118 additions and 1 deletions

View File

@@ -19,11 +19,22 @@ dist_check_SCRIPTS = test-pass test-pass-cert test-cert test-iroute test-pass-sc
radius-test test-gssapi kerberos-test pam-test test-ban test-sighup \
test-cookie-invalidation
ipv4_prefix_CPPFLAGS = \
-I$(top_srcdir)/src/ \
-I$(top_builddir)/src/ \
-I$(top_srcdir)/gl/ \
-I$(top_builddir)/gl/
ipv4_prefix_SOURCES = ../src/common.c ../src/common.h ipv4-prefix.c
ipv4_prefix_LDADD = ../gl/libgnu.a $(LIBTALLOC_LIBS)
check_PROGRAMS = ipv4-prefix
TESTS = test-pass test-pass-cert test-cert test-iroute test-pass-script \
test-multi-cookie full-test test-group-pass test-pass-group-cert \
ocpasswd-test test-pass-group-cert-no-pass unix-test test-pass-opt-cert \
test-cookie-timeout test-cookie-timeout-2 test-explicit-ip radius-test \
test-gssapi kerberos-test pam-test test-ban test-sighup
test-gssapi kerberos-test pam-test test-ban test-sighup ipv4-prefix
TESTS_ENVIRONMENT = srcdir="$(srcdir)" \
top_builddir="$(top_builddir)"

106
tests/ipv4-prefix.c Normal file
View File

@@ -0,0 +1,106 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../src/common.h"
int main()
{
char *p;
p = ipv4_prefix_to_mask(NULL, 32);
if (p == NULL || strcmp(p, "255.255.255.255") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 30);
if (p == NULL || strcmp(p, "255.255.255.252") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 27);
if (p == NULL || strcmp(p, "255.255.255.224") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 24);
if (p == NULL || strcmp(p, "255.255.255.0") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 22);
if (p == NULL || strcmp(p, "255.255.252.0") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 20);
if (p == NULL || strcmp(p, "255.255.240.0") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 18);
if (p == NULL || strcmp(p, "255.255.192.0") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 16);
if (p == NULL || strcmp(p, "255.255.0.0") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 8);
if (p == NULL || strcmp(p, "255.0.0.0") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 5);
if (p == NULL || strcmp(p, "248.0.0.0") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
p = ipv4_prefix_to_mask(NULL, 3);
if (p == NULL || strcmp(p, "224.0.0.0") != 0) {
fprintf(stderr, "error in %d: %s\n", __LINE__, p);
exit(1);
}
talloc_free(p);
return 0;
}