1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4set -o pipefail
5
6NS=netns-name-test
7DEV=dummy-dev0
8DEV2=dummy-dev1
9ALT_NAME=some-alt-name
10
11RET_CODE=0
12
13cleanup() {
14    ip netns del $NS
15}
16
17trap cleanup EXIT
18
19fail() {
20    echo "ERROR: ${1:-unexpected return code} (ret: $_)" >&2
21    RET_CODE=1
22}
23
24ip netns add $NS
25
26#
27# Test basic move without a rename
28#
29ip -netns $NS link add name $DEV type dummy || fail
30ip -netns $NS link set dev $DEV netns 1 ||
31    fail "Can't perform a netns move"
32ip link show dev $DEV >> /dev/null || fail "Device not found after move"
33ip link del $DEV || fail
34
35#
36# Test move with a conflict
37#
38ip link add name $DEV type dummy
39ip -netns $NS link add name $DEV type dummy || fail
40ip -netns $NS link set dev $DEV netns 1 2> /dev/null &&
41    fail "Performed a netns move with a name conflict"
42ip link show dev $DEV >> /dev/null || fail "Device not found after move"
43ip -netns $NS link del $DEV || fail
44ip link del $DEV || fail
45
46#
47# Test move with a conflict and rename
48#
49ip link add name $DEV type dummy
50ip -netns $NS link add name $DEV type dummy || fail
51ip -netns $NS link set dev $DEV netns 1 name $DEV2 ||
52    fail "Can't perform a netns move with rename"
53ip link del $DEV2 || fail
54ip link del $DEV || fail
55
56#
57# Test dup alt-name with netns move
58#
59ip link add name $DEV type dummy || fail
60ip link property add dev $DEV altname $ALT_NAME || fail
61ip -netns $NS link add name $DEV2 type dummy || fail
62ip -netns $NS link property add dev $DEV2 altname $ALT_NAME || fail
63
64ip -netns $NS link set dev $DEV2 netns 1 2> /dev/null &&
65    fail "Moved with alt-name dup"
66
67ip link del $DEV || fail
68ip -netns $NS link del $DEV2 || fail
69
70#
71# Test creating alt-name in one net-ns and using in another
72#
73ip -netns $NS link add name $DEV type dummy || fail
74ip -netns $NS link property add dev $DEV altname $ALT_NAME || fail
75ip -netns $NS link set dev $DEV netns 1 || fail
76ip link show dev $ALT_NAME >> /dev/null || fail "Can't find alt-name after move"
77ip  -netns $NS link show dev $ALT_NAME 2> /dev/null &&
78    fail "Can still find alt-name after move"
79ip link del $DEV || fail
80
81echo -ne "$(basename $0) \t\t\t\t"
82if [ $RET_CODE -eq 0 ]; then
83    echo "[  OK  ]"
84else
85    echo "[ FAIL ]"
86fi
87exit $RET_CODE
88