1#!/bin/sh
2#==============================================================================
3# Copyright (c) 2015 Red Hat, Inc.
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of version 2 the GNU General Public License as
7# published by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16#
17# Written by Matus Marhefka <mmarhefk@redhat.com>
18#
19#==============================================================================
20#
21# Tests that a separate network namespace cannot affect sysfs contents
22# of the main namespace.
23#==============================================================================
24
25TCID="netns_sysfs"
26TST_TOTAL=3
27NS_TYPE="net,mnt"
28DUMMYDEV_HOST="dummy_test0"
29DUMMYDEV="dummy_test1"
30. test.sh
31
32setns_check
33if [ $? -eq 32 ]; then
34	tst_brkm TCONF "setns not supported"
35fi
36
37cleanup()
38{
39	tst_rmdir
40	ip link del $DUMMYDEV_HOST 2>/dev/null
41	ip link del $DUMMYDEV 2>/dev/null
42	kill -9 $NS_HANDLE 2>/dev/null
43}
44
45tst_tmpdir
46NS_HANDLE=$(ns_create $NS_TYPE)
47if [ $? -eq 1 ]; then
48	tst_resm TINFO "$NS_HANDLE"
49	tst_brkm TBROK "unable to create a new network namespace"
50fi
51TST_CLEANUP=cleanup
52
53ip link add $DUMMYDEV_HOST type dummy || \
54	tst_brkm TBROK "failed to add a new (host) dummy device"
55
56ns_exec $NS_HANDLE $NS_TYPE mount --make-rprivate /sys
57ns_exec $NS_HANDLE $NS_TYPE ip link add $DUMMYDEV type dummy || \
58	tst_brkm TBROK "failed to add a new dummy device"
59ns_exec $NS_HANDLE $NS_TYPE mount -t sysfs none /sys 2>/dev/null
60
61
62# TEST CASE #1
63ns_exec $NS_HANDLE $NS_TYPE test -e /sys/class/net/$DUMMYDEV
64if [ $? -eq 0 ]; then
65	tst_resm TPASS "sysfs in new namespace has $DUMMYDEV interface"
66else
67	tst_resm TFAIL "sysfs in new namespace does not have $DUMMYDEV interface"
68fi
69
70
71# TEST CASE #2
72ns_exec $NS_HANDLE $NS_TYPE test -e /sys/class/net/$DUMMYDEV_HOST
73if [ $? -ne 0 ]; then
74	tst_resm TPASS "sysfs in new namespace does not have $DUMMYDEV_HOST interface"
75else
76	tst_resm TFAIL "sysfs in new namespace contains $DUMMYDEV_HOST interface"
77fi
78
79
80# TEST CASE #3
81test -e /sys/class/net/$DUMMYDEV
82if [ $? -ne 0 ]; then
83	tst_resm TPASS "sysfs not affected by a separate namespace"
84else
85	tst_resm TFAIL "sysfs affected by a separate namespace"
86fi
87
88
89tst_exit
90