]> err.no Git - util-linux/commitdiff
login: add regression test for IP address checking code
authorKarel Zak <kzak@redhat.com>
Fri, 9 Mar 2007 13:56:18 +0000 (14:56 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 9 Mar 2007 13:56:18 +0000 (14:56 +0100)
The hnmatch() in checktty.c is checking IP addresses if the login util
is compiled without PAM support.

Signed-off-by: Karel Zak <kzak@redhat.com>
login-utils/Makefile.am
login-utils/checktty.c
tests/Makefile.am
tests/commands.sh.in
tests/expected/ts-login-checktty [new file with mode: 0644]
tests/ts-login-checktty [new file with mode: 0755]

index cc894e19656102b18fcfcc79ccd7e5178f2895ef..0a36e158f758bfed2bc9af46f651c623475933c1 100644 (file)
@@ -116,3 +116,8 @@ install-exec-hook::
        cd $(DESTDIR)$(mandir)/man8 && ln -sf initctl.8 provide.8
 
 endif
+
+noinst_PROGRAMS = checktty_test
+checktty_test_SOURCES = checktty.c login.h
+checktty_test_CFLAGS = -DMAIN_TEST_CHECKTTY
+
index 9c3ce46d0125606e1a9e25a775f0cc39c6258553..f47b7ad4d67bc0a5b87b62ed7bb6e9de8f484c3d 100644 (file)
@@ -186,6 +186,65 @@ hnmatch(const char *hn, const char *pat)
     }
 }
 
+#ifdef MAIN_TEST_CHECKTTY
+
+char   hostaddress[4];
+char   *hostname;
+void   sleepexit(int eval) {}          /* dummy for this test */
+void   badlogin(const char *s) {}      /* dummy for this test */
+
+int
+main(int argc, char **argv)
+{
+       struct addrinfo hints, *info = NULL;
+       struct addrexp {
+               const char *range;
+               const char *ip;
+       } alist[] = {
+               { "130.225.16.0/255.255.254.0", "130.225.16.1" },
+               { "130.225.16.0/255.255.254.0", "10.20.30.1" },
+               { "130.225.0.0/255.254.0.0", "130.225.16.1" },
+               { "130.225.0.0/255.254.0.0", "130.225.17.1" },
+               { "130.225.0.0/255.254.0.0", "150.160.170.180" },
+               { NULL, NULL }
+       }, *item;
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = AF_INET;
+       hints.ai_flags = AI_NUMERICHOST |  AI_PASSIVE | AI_ADDRCONFIG;
+       hints.ai_socktype = SOCK_STREAM;
+
+       for (item = alist; item->range; item++) {
+
+               printf("hnmatch() on %-30s <-- %-15s: ", item->range, item->ip);
+
+               if (getaddrinfo(item->ip, NULL, &hints, &info)==0 && info) {
+                       if (info->ai_family == AF_INET) {
+                           struct sockaddr_in *sa =
+                                       (struct sockaddr_in *) info->ai_addr;
+                           memcpy(hostaddress, &(sa->sin_addr),
+                                       sizeof(sa->sin_addr));
+                       }
+/***
+                       if (info->ai_family == AF_INET6) {
+                           struct sockaddr_in6 *sa =
+                                       (struct sockaddr_in6 *) info->ai_addr;
+                           memcpy(hostaddress, &(sa->sin6_addr),
+                                       sizeof(sa->sin6_addr));
+                       }
+***/
+                       freeaddrinfo(info);
+                       printf("%s\n", hnmatch("dummy", item->range) ?
+                                               "match" : "mismatch");
+               }
+               else
+                       printf("getaddrinfo() failed\n");
+
+       }
+       return 0;
+}
+#endif /* MAIN_TEST_CHECKTTY */
+
 static char *wdays[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
 
 /* example timespecs:
index 02f47e8b644a0d209d1aa8031410b9057694cca4..7347c22898e67af1678e3b4a6a6a28cd48834eda 100644 (file)
@@ -16,7 +16,8 @@ EXTRA_DIST =  expected/* \
                ts-mount-paths \
                ts-namei-logic \
                ts-cramfs-mkfs \
-               ts-look-separator
+               ts-look-separator \
+               ts-login-checktty
 
 distclean-local:
        rm -rf output diff
index de329032cf9983ef562f92b63cafc110c314bf46..7e2aadbe5894fc1f6e1dd620142c30f79d7662a8 100644 (file)
@@ -21,3 +21,5 @@ TS_CMD_COL=${TS_CMD_COL:-"$TOPDIR/text-utils/col"}
 TS_CMD_NAMEI=${TS_CMD_NAMEI-"$TOPDIR/misc-utils/namei"}
 TS_CMD_LOOK=${TS_CMD_LOOK-"$TOPDIR/misc-utils/look"}
 
+TS_CMD_CHECKTTY=${TS_CMD_CHECKTTY-"$TOPDIR/login-utils/checktty_test"}
+
diff --git a/tests/expected/ts-login-checktty b/tests/expected/ts-login-checktty
new file mode 100644 (file)
index 0000000..7bb6029
--- /dev/null
@@ -0,0 +1,5 @@
+hnmatch() on 130.225.16.0/255.255.254.0     <-- 130.225.16.1   : match
+hnmatch() on 130.225.16.0/255.255.254.0     <-- 10.20.30.1     : mismatch
+hnmatch() on 130.225.0.0/255.254.0.0        <-- 130.225.16.1   : match
+hnmatch() on 130.225.0.0/255.254.0.0        <-- 130.225.17.1   : match
+hnmatch() on 130.225.0.0/255.254.0.0        <-- 150.160.170.180: mismatch
diff --git a/tests/ts-login-checktty b/tests/ts-login-checktty
new file mode 100755 (executable)
index 0000000..f51ab0f
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+. commands.sh
+. functions.sh
+
+TS_COMPONENT="login"
+TS_DESC="checktty"
+
+ts_init
+
+touch $TS_OUTPUT
+
+$TS_CMD_CHECKTTY  >> $TS_OUTPUT
+
+ts_finalize
+