This patch describes about the uts namespace testcases, there are total five
test combination for testing the unshare functionalities.
Signed-off-by: Rishikesh K Rajak <[EMAIL PROTECTED]>
Cc: Serge Hallyn <[EMAIL PROTECTED]>
---
testcases/kernel/Makefile | 2
testcases/kernel/containers/utsname/Makefile | 24 +
testcases/kernel/containers/utsname/README | 34 +
testcases/kernel/containers/utsname/runtests_noltp.sh | 20 +
testcases/kernel/containers/utsname/runutstest.sh | 12
testcases/kernel/containers/utsname/utstest.c | 307 ++++++++++++++++++
testscripts/test_containers.sh | 47 ++
7 files changed, 445 insertions(+), 1 deletion(-)
Index: ltp-full-20070331/testcases/kernel/containers/utsname/Makefile
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testcases/kernel/containers/utsname/Makefile 2007-04-19 14:44:52.000000000 +0530
@@ -0,0 +1,24 @@
+CC=gcc
+
+CFLAGS += -I../../../../include -I../libclone -Wall
+LDLIBS += -L../../../../lib -L../libclone ../libclone/libclone.a -lltp
+
+SRCS = $(wildcard *.c)
+TARGETS = $(patsubst %.c,%,$(SRCS))
+NOLTP_TARGETS = $(patsubst %.c,%_noltp,$(SRCS))
+
+%_noltp : %.c
+ $(CC) -g -DNO_LTP -o $@ $< ../libclone/libclone.a
+
+all: $(TARGETS)
+
+noltp: $(NOLTP_TARGETS)
+
+clean:
+ rm -f $(TARGETS) *.o $(NOLTP_TARGETS)
+
+install:
+ @set -e; for i in $(TARGETS) runutstest.sh check_utsns_enabled; do ln -f $$i ../../../bin/$$i ; chmod +x runutstest.sh ; done
+
+noltp_check: noltp
+ ./runtests_noltp.sh
Index: ltp-full-20070331/testcases/kernel/containers/utsname/README
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testcases/kernel/containers/utsname/README 2007-04-19 14:46:18.000000000 +0530
@@ -0,0 +1,34 @@
+This contains five tests for the uts namespace unsharing functionality.
+
+To enable this functionality, you currently must use a -mm kernel (see
+kernel.org), and enable CONFIG_UTS_NS=y (under 'GENERAL SETUP' in 'make
+menuconfig'). Then to run these tests, just type
+
+ sh runme.sh
+
+The tests are intended to do the following:
+
+test 1: check that after fork, two children see the same utsname
+ P1: A=gethostname
+ P2: B=gethostname
+ Ensure(A==B)
+test 2: check that after fork, two children are in the same utsname namespace.
+ P1: sethostname(newname); A=gethostname
+ P2: (wait); B=gethostname
+ Ensure (A==B)
+
+test 3: check that after unshare, processes are in different utsname namespaces.
+ P1: A=gethostname; unshare(utsname); sethostname(newname); C=gethostname
+ P2: B=gethostname; (wait); (wait); D=gethostname
+ Ensure (A==B && A==D && C!=D)
+
+test 4: similar to test 3, but other child changes hostname.
+ P1: A=gethostname; unshare(utsname); (wait); C=gethostname
+ P2: B=gethostname; (wait); sethostname(newname); D=gethostname
+ Ensure (A==B && A==C && C!=D)
+
+test 5: check that unsharing utsname without required permissions (CAP_SYS_AUDIT)
+ fails.
+ P1: A=gethostname; unshare(utsname) without suff. perms; (wait); C=gethostname
+ P2: B=gethostname; (wait); sethostname(newname); D=gethostname
+ Ensure (A==B==C==D) and state is ok.
Index: ltp-full-20070331/testcases/kernel/containers/utsname/runtests_noltp.sh
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testcases/kernel/containers/utsname/runtests_noltp.sh 2007-04-19 14:44:52.000000000 +0530
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+exit_code=0
+echo "unshare tests"
+for i in `seq 1 5`; do
+ echo "test $i (unshare)"
+ ./utstest_noltp unshare $i
+ if [ $? -ne 0 ]; then
+ exit_code=$?
+ fi
+done
+echo "clone tests"
+for i in `seq 1 5`; do
+ echo "test $i (clone)"
+ ./utstest_noltp clone $i
+ if [ $? -ne 0 ]; then
+ exit_code=$?
+ fi
+done
+exit $exit_code
Index: ltp-full-20070331/testcases/kernel/containers/utsname/runutstest.sh
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testcases/kernel/containers/utsname/runutstest.sh 2007-04-19 14:44:52.000000000 +0530
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+echo "unshare tests"
+for i in `seq 1 5`; do
+ echo "test $i (unshare)"
+ utstest unshare $i
+done
+echo "clone tests"
+for i in `seq 1 5`; do
+ echo "test $i (clone)"
+ utstest clone $i
+done
Index: ltp-full-20070331/testcases/kernel/containers/utsname/utstest.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testcases/kernel/containers/utsname/utstest.c 2007-04-19 14:44:52.000000000 +0530
@@ -0,0 +1,307 @@
+/*
+ * Copyright 2007 IBM
+ * Author: Serge Hallyn <[EMAIL PROTECTED]>
+ *
+ * test1:
+ P1: A=gethostname
+ P2: B=gethostname
+ Ensure(A==B)
+
+ * test2:
+ P1: sethostname(A);
+ P2: (wait); B=gethostname
+ Ensure (A==B)
+
+ * test3:
+ P1: A=gethostname; unshare(utsname); sethostname(newname); C=gethostname
+ P2: B=gethostname; (wait); (wait); D=gethostname
+ Ensure (A==B && A==D && C!=D)
+
+ * test4:
+ P1: A=gethostname; unshare(utsname); (wait); C=gethostname
+ P2: B=gethostname; (wait); sethostname(newname); D=gethostname
+ Ensure (A==B && A==C && C!=D)
+
+ * test5:
+ P1: drop_privs(); unshare(utsname); (wait); C=gethostname
+ P2: (wait); sethostname(B); D=gethostname
+ Ensure (B==C==D) and state is ok.
+ *
+ */
+
+#define _GNU_SOURCE 1
+#include <sys/wait.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#ifndef NO_LTP
+#include <test.h>
+#include <libclone.h>
+#else
+#include "../libclone/libclone.h"
+#endif
+
+char *TCID = "uts_namespace";
+int TST_TOTAL=1;
+
+#ifdef NO_LTP
+#define TFAIL "FAILURE: "
+#define TPASS "PASS: "
+#define tst_resm(x, format, arg...) printf("%s:" format, x, ## arg)
+#define tst_exit() exit(1)
+#endif
+
+int drop_root()
+{
+ int ret;
+ ret = setresuid(1000, 1000, 1000);
+ if (ret) {
+ perror("setresuid");
+ exit(4);
+ }
+ return 1;
+}
+
+int p1fd[2], p2fd[2];
+pid_t cpid;
+
+#define HLEN 100
+#define NAME1 "serge1"
+#define NAME2 "serge2"
+
+void picknewhostname(char *orig, char *new)
+{
+ memset(new, 0, HLEN);
+ if (strcmp(orig, NAME1) == 0)
+ strcpy(new, NAME2);
+ else
+ strcpy(new, NAME1);
+}
+
+void zeroize(char *s)
+{
+ memset(s, 0, HLEN);
+}
+
+char *tsttype;
+int P1(void *vtest)
+{
+ char hostname[HLEN], newhostname[HLEN], rhostname[HLEN];
+ int err;
+ int len;
+ int testnum;
+
+ testnum = atoi((char *)vtest);
+
+ close(p1fd[1]);
+ close(p2fd[0]);
+
+ switch(testnum) {
+ case 1:
+ gethostname(hostname, HLEN);
+ zeroize(rhostname);
+ len = read(p1fd[0], rhostname, HLEN);
+ if (strcmp(hostname, rhostname) == 0) {
+ tst_resm(TPASS, "test 1 (%s): success\n", tsttype);
+ tst_exit();
+ }
+ tst_resm(TFAIL, "test 1 (%s): hostname 1 %s, hostname 2 %s\n",
+ tsttype, hostname, rhostname);
+ tst_exit();
+ case 2:
+ gethostname(hostname, HLEN);
+ picknewhostname(hostname, newhostname);
+ err = sethostname(newhostname, strlen(newhostname));
+ write(p2fd[1], "1", 1);
+ if (err == -1) {
+ tst_resm(TFAIL, "test 2 (%s): failed to sethostname",
+ tsttype);
+ tst_exit();
+ }
+ zeroize(rhostname);
+ len = read(p1fd[0], rhostname, HLEN);
+ if (strcmp(newhostname, rhostname) == 0) {
+ tst_resm(TPASS, "test 2 (%s): success\n",
+ tsttype);
+ tst_exit();
+ }
+ tst_resm(TFAIL, "test 2 (%s) hostname 1 %s, hostname 2 %s\n",
+ tsttype, newhostname, rhostname);
+ tst_exit();
+ case 3:
+ gethostname(hostname, HLEN);
+ picknewhostname(hostname, newhostname);
+ err = sethostname(newhostname, strlen(newhostname));
+ write(p2fd[1], "1", 1);
+ if (err == -1) {
+ tst_resm(TFAIL, "test 3 (%s): failed to sethostname",
+ tsttype);
+ tst_exit();
+ }
+
+ zeroize(rhostname);
+ len = read(p1fd[0], rhostname, HLEN);
+ if (strcmp(newhostname, rhostname) == 0) {
+ tst_resm(TFAIL, "test 3 (%s): hostname 1 %s, hostname 2 %s, these should have been different\n",
+ tsttype, newhostname, rhostname);
+ tst_exit();
+ }
+ if (strcmp(hostname, rhostname) == 0) {
+ tst_resm(TPASS, "test 3 (%s): success\n", tsttype);
+ tst_exit();
+ }
+ tst_resm(TFAIL, "test 3 (%s): hostname 1 %s, hostname 2 %s, should have been same\n",
+ tsttype, hostname, rhostname);
+ tst_exit();
+
+ case 4:
+ gethostname(hostname, HLEN);
+ write(p2fd[1], "1", 1); /* tell p2 to go ahead and sethostname */
+ zeroize(rhostname);
+ len = read(p1fd[0], rhostname, HLEN);
+ gethostname(newhostname, HLEN);
+ if (strcmp(hostname, newhostname) != 0) {
+ tst_resm(TFAIL, "test 4 (%s): hostname 1 %s, hostname 2 %s, should be same\n",
+ tsttype, hostname, newhostname);
+ tst_exit();
+ }
+ if (strcmp(hostname, rhostname) == 0) {
+ tst_resm(TFAIL, "test 4 (%s): hostname 1 %s, hostname 2 %s, should be different",
+ tsttype, hostname, rhostname);
+ tst_exit();
+ }
+ tst_resm(TPASS, "test 4 (%s): successful\n", tsttype);
+ tst_exit();
+ case 5:
+ write(p2fd[1], "1", 1); /* tell p2 to go ahead and sethostname */
+ zeroize(rhostname);
+ len = read(p1fd[0], rhostname, HLEN);
+ gethostname(newhostname, HLEN);
+ if (strcmp(rhostname, newhostname) != 0) {
+ tst_resm(TFAIL, "test 5 (%s): hostnames %s and %s should be same\n",
+ tsttype, rhostname, newhostname);
+ tst_exit();
+ }
+ tst_resm(TPASS, "test 5 (%s): successful", tsttype);
+ tst_exit();
+ default:
+ break;
+ }
+ return -1;
+}
+
+int P2(void *vtest)
+{
+ char hostname[HLEN], newhostname[HLEN];
+ int len;
+ int testnum;
+
+ testnum = atoi((char *)vtest);
+
+ close(p1fd[0]);
+ close(p2fd[1]);
+
+ switch(testnum) {
+ case 1:
+ gethostname(hostname, HLEN);
+ write(p1fd[1], hostname, strlen(hostname));
+ break;
+ case 2:
+ case 3:
+ len = 0;
+ while (!len) {
+ len = read(p2fd[0], hostname, 1);
+ }
+ gethostname(hostname, HLEN);
+ write(p1fd[1], hostname, strlen(hostname));
+ break;
+ case 4:
+ case 5:
+ len = 0;
+ while (!len) {
+ len = read(p2fd[0], hostname, 1);
+ }
+ if (hostname[0] == '0') {
+ tst_resm(TPASS, "P2: P1 claims error\n");
+ tst_exit();
+ exit(0);
+ }
+ gethostname(hostname, HLEN);
+ picknewhostname(hostname, newhostname);
+ sethostname(newhostname, strlen(newhostname));
+ write(p1fd[1], newhostname, strlen(newhostname));
+ break;
+ default:
+ tst_resm(TFAIL, "undefined test: %d\n", testnum);
+ break;
+ }
+ tst_exit();
+ return 0;
+}
+
+#define UNSHARESTR "unshare"
+#define CLONESTR "clone"
+int main(int argc, char *argv[])
+{
+ int r, pid, use_clone = T_UNSHARE;
+ int testnum;
+ void *vtest;
+
+ if (argc != 3) {
+ tst_resm(TFAIL, "Usage: %s <clone|unshare> <testnum>\n", argv[0]);
+ tst_resm(TFAIL, " where clone or unshare specifies unshare method,");
+ tst_resm(TFAIL, " and testnum is between 1 and 5 inclusive\n");
+ exit(2);
+ }
+ if (pipe(p1fd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
+ if (pipe(p2fd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
+
+ tsttype = UNSHARESTR;
+ if (strcmp(argv[1], "clone") == 0) {
+ use_clone = T_CLONE;
+ tsttype = CLONESTR;
+ }
+
+ testnum = atoi(argv[2]);
+
+ vtest = (void *)argv[2];
+ switch(testnum) {
+ case 1:
+ case 2: r = do_clone_unshare_tests(T_NONE, 0,
+ P1, vtest, P2, vtest);
+ break;
+ case 3:
+ case 4:
+ r = do_clone_unshare_tests(use_clone, CLONE_NEWUTS,
+ P1, vtest, P2, vtest);
+ break;
+ case 5:
+ pid = fork();
+ if (pid == -1) {
+ perror("fork");
+ exit(2);
+ }
+ if (pid == 0) {
+ if (!drop_root()) {
+ tst_resm(TFAIL, "failed to drop root.\n");
+ tst_exit();
+ exit(1);
+ }
+ r = do_clone_unshare_test(use_clone, CLONE_NEWUTS,
+ P1, vtest);
+ write(p2fd[1], "0", 1); /* don't let p2 hang */
+ exit(0);
+ } else {
+ P2(vtest);
+ }
+ break;
+ default:
+ tst_resm(TFAIL, "testnum should be between 1 and 5 inclusive.\n");
+ break;
+ }
+
+ tst_exit();
+}
Index: ltp-full-20070331/testcases/kernel/Makefile
===================================================================
--- ltp-full-20070331.orig/testcases/kernel/Makefile 2007-04-19 14:44:47.000000000 +0530
+++ ltp-full-20070331/testcases/kernel/Makefile 2007-04-19 14:44:52.000000000 +0530
@@ -1,4 +1,4 @@
-SUBDIRS = include fs io ipc mem pty sched security syscalls
+SUBDIRS = containers include fs io ipc mem pty sched security syscalls
UCLINUX_SUBDIRS = syscalls
all:
Index: ltp-full-20070331/testscripts/test_containers.sh
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ ltp-full-20070331/testscripts/test_containers.sh 2007-04-19 14:44:52.000000000 +0530
@@ -0,0 +1,47 @@
+#!/bin/bash
+#
+# Copyright 2007 IBM
+#
+# 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.
+#
+# test_containers.sh - Run the containers test suite.
+
+# Must be root to run the containers testsuite
+if [ $UID != 0 ]
+then
+ echo "FAILED: Must be root to execute this script"
+ exit 1
+fi
+
+# set the LTPROOT directory
+cd `dirname $0`
+LTPROOT=${PWD}
+echo $LTPROOT | grep testscripts > /dev/null 2>&1
+if [ $? -eq 0 ]
+then
+ cd ..
+ LTPROOT=${PWD}
+fi
+
+# set the PATH to include testcase/bin
+
+export PATH=$PATH:/usr/sbin:$LTPROOT/testcases/bin
+export LTPBIN=$LTPROOT/testcases/bin
+
+# We will store the logfiles in $LTPROOT/results, so make sure
+# it exists.
+if [ ! -d $LTPROOT/results ]
+then
+ mkdir $LTPROOT/results
+fi
+
+# Check the role and mode testsuite is being executed under.
+echo "Running the containers testsuite..."
+
+$LTPROOT/pan/pan -S -a $LTPROOT/results/containers -n ltp-containers -l $LTPROOT/results/containers.logfile -o $LTPROOT/results/containers.outfile -p -f $LTPROOT/runtest/containers
+
+echo "Done."
+exit 0
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list