#!/bin/bash
# Copyright 2014 Cynthia Rempel <cynthia@rtems.org>
#
# Brief: Some cursery coverage tests of ifconfig...
# Note: requires permissions to run modprobe and all ifconfig options
# Commands used: grep, grep -i, ip link, ip tuntap, wc -l
#
# Possible improvements:
# 1. Verify the dummy interface actually has the modified characteristics
#    instead of relying on ifconfig output
# 2. Introduce more error cases, to verify ifconfig fails gracefully
# 3. Do more complex calls to ifconfig, while mixing the order of the
#    arguments
# 4. Cover more ifconfig options:
#    hw ether|infiniband ADDRESS - set LAN hardware address (AA:BB:CC...)
#    txqueuelen LEN - number of buffered packets before output blocks
#    Obsolete fields included for historical purposes:
#    irq|io_addr|mem_start ADDR - micromanage obsolete hardware
#    outfill|keepalive INTEGER - SLIP analog dialup line quality monitoring
#    metric INTEGER - added to Linux 0.9.10 with comment "never used", still true

[ -f testing.sh ] && . testing.sh

if [ "$(id -u)" -ne 0 ]
then
  echo "$SHOWSKIP: ifconfig (not root)"
  continue 2>/dev/null
  exit
fi

#testing "name" "command" "result" "infile" "stdin"

# Add a dummy interface to test with
ifconfig dummy0 up

# Test Description: Disable the dummy0 interface
# Results Expected: After calling ifconfig, no lines with dummy0 are displayed
testing "ifconfig dummy0 down and if config /-only" \
"ifconfig dummy0 down && ifconfig dummy0 | grep dummy | wc -l" \
"0\n" "" ""

# Test Description: Enable the dummy0 interface
# Results Expected: After calling ifconfig, one line with dummy0 is displayed
testing "ifconfig dummy0 up" \
"ifconfig dummy0 up && ifconfig dummy0 | grep dummy | wc -l" \
"1\n" "" ""

# Test Description: Set the ip address of the dummy0 interface
# Results Expected: After calling ifconfig dummy0, one line displays the ip
#                   address selected
testing "ifconfig dummy0 10.240.240.240" \
"ifconfig dummy0 10.240.240.240 && ifconfig dummy0 | grep 10\.240\.240\.240 | wc -l" \
"1\n" "" ""

# Test Description: Change the netmask to the interface
# Results Expected: After calling ifconfig dummy0, one line displays the
#                   netmask selected
testing "ifconfig dummy0 netmask 255.255.240.0" \
"ifconfig dummy0 netmask 255.255.240.0 && ifconfig dummy0 | grep 255\.255\.240\.0 | wc -l" \
"1\n" "" ""

# Test Description: Change the broadcast address to the interface
# Results Expected: After calling ifconfig dummy0, one line displays the
#                   broadcast address selected
testing "ifconfig dummy0 broadcast 10.240.240.255" \
"ifconfig dummy0 broadcast 10.240.240.255 && ifconfig dummy0 | grep 10\.240\.240\.255 | wc -l" \
"1\n" "" ""

# Test Description: Revert to the default ip address
# Results Expected: After calling ifconfig dummy0, there are no lines
#                   displaying the ip address previously selected
testing "ifconfig dummy0 default" \
"ifconfig dummy0 default && ifconfig dummy0 | grep 10\.240\.240\.240 | wc -l" \
"0\n" "" ""

# Test Description: Change the Maximum transmission unit (MTU) of the interface
# Results Expected: After calling ifconfig dummy0, there is one line with the
#                   selected MTU
testing "ifconfig dummy0 mtu 1269" \
"ifconfig dummy0 mtu 1269 && ifconfig dummy0 | grep 1269 | wc -l" \
"1\n" "" ""

# Test Description: Verify ifconfig add fails with such a small mtu
# Results Expected: There is one line of error message containing
#                   "No buffer space available"
testing "ifconfig dummy0 add ::2 -- too small mtu" \
"ifconfig dummy0 add ::2 2>&1 | grep No\ buffer\ space\ available | wc -l" \
"1\n" "" ""

# Test Description: Change the Maximum transmission unit (MTU) of the interface
# Results Expected: After calling ifconfig dummy0, there is one line with the
#                   selected MTU
testing "ifconfig dummy0 mtu 2000" \
"ifconfig dummy0 mtu 2000 && ifconfig dummy0 | grep 2000 | wc -l" \
"1\n" "" ""

# Test Description: Verify ifconfig add succeeds with a larger mtu
# Results Expected: after calling ifconfig dummy0, there is one line with the
#                   selected ip address
testing "ifconfig dummy0 add ::2" \
"ifconfig dummy0 add ::2/126 && ifconfig dummy0 | grep \:\:2\/126 | wc -l" \
"1\n" "" ""

# Test Description: Verify ifconfig del removes the selected ip6 address
# Results Expected: after calling ifconfig dummy0, there are no lines with the
#                   selected ip address
testing "ifconfig dummy0 del ::2" \
"ifconfig dummy0 del ::2/126 && ifconfig dummy0 | grep \:\:2 | wc -l" \
"0\n" "" ""

# Test Description: Remove the noarp flag and bring the interface down in
#                   preparation for the next test
# Results Expected: After calling ifconfig dummy0, there are no lines with the
#                   NOARP flag
testing "ifconfig dummy0 arp down" \
"ifconfig dummy0 arp down && ifconfig dummy0 | grep -i NOARP | wc -l" \
"0\n" "" ""

# Test Description: Call the pointtopoint option with no argument
# Results Expected: After calling ifconfig dummy0, there is one line with the
#                   NOARP and UP flags
testing "ifconfig dummy0 pointtopoint" \
"ifconfig dummy0 pointtopoint && ifconfig dummy0 | grep -i NOARP | grep -i UP | wc -l" \
"1\n" "" ""

# Test Description: Test the pointtopoint option and set the ipaddress
# Results Expected: After calling ifconfig dummy0, there is one line with the
#                   word inet and the selected ip address
testing "ifconfig dummy0 pointtopoint 127.0.0.2" \
"ifconfig dummy0 pointtopoint 127.0.0.2 && ifconfig dummy0 | grep -i inet | grep -i 127\.0\.0\.2 | wc -l" \
"1\n" "" ""

####### Flags you can set on an interface (or -remove by prefixing with -): ###############

# Test Description: Enable allmulti mode on the interface
# Results Expected: After calling ifconfig dummy0, there is one line with the
#                   allmulti flag
testing "ifconfig dummy0 allmulti" \
"ifconfig dummy0 allmulti && ifconfig dummy0 | grep -i allmulti | wc -l" "1\n" \
"" ""

# Test Description: Disable multicast mode the interface
# Results Expected: After calling ifconfig dummy0, there are no lines with the
#                   allmulti flag
testing "ifconfig dummy0 -allmulti" \
"ifconfig dummy0 -allmulti && ifconfig dummy0 | grep -i allmulti | wc -l" "0\n" \
"" ""

# Test Description: Disable NOARP mode on the interface
# Results Expected: After calling ifconfig dummy0, there are no lines with the
#                   NOARP flag
testing "ifconfig dummy0 arp" \
"ifconfig dummy0 arp && ifconfig dummy0 | grep -i NOARP | wc -l" "0\n" \
"" ""

# Test Description: Enable NOARP mode on the interface
# Results Expected: After calling ifconfig dummy0, there is one line with the
#                   NOARP flag
testing "ifconfig dummy0 -arp" \
"ifconfig dummy0 -arp && ifconfig dummy0 | grep -i NOARP | wc -l" "1\n" \
"" ""

# Test Description: Enable multicast mode on the interface
# Results Expected: After calling ifconfig dummy0, there is one line with the
#                   multicast flag
testing "ifconfig dummy0 multicast" \
"ifconfig dummy0 multicast && ifconfig dummy0 | grep -i multicast | wc -l" \
"1\n" "" ""

# Test Description: Disable multicast mode the interface
# Results Expected: After calling ifconfig dummy0, there are no lines with the
#                   multicast flag
testing "ifconfig dummy0 -multicast" \
"ifconfig dummy0 -multicast && ifconfig dummy0 | grep -i multicast | wc -l" \
"0\n" "" ""

# Test Description: Enable promiscuous mode the interface
# Results Expected: After calling ifconfig dummy0, there is one line with the
#                   promisc flag
testing "ifconfig dummy0 promisc" \
"ifconfig dummy0 promisc && ifconfig dummy0 | grep -i promisc | wc -l" "1\n" \
"" ""

# Disable promiscuous mode the interface
# Results Expected: After calling ifconfig dummy0, there are no lines with the
#                   promisc flag
testing "ifconfig dummy0 -promisc" \
"ifconfig dummy0 -promisc && ifconfig dummy0 | grep -i promisc | wc -l" "0\n" \
"" ""

# Disable the dummy interface
ifconfig dummy0 down