mirror of
https://github.com/romkatv/powerlevel10k.git
synced 2024-12-19 14:01:57 +00:00
Merge pull request #1126 from Shini31/master
Use ip command for VPN segment - fix #1125
This commit is contained in:
commit
613b798bb3
5 changed files with 612 additions and 205 deletions
|
@ -373,3 +373,58 @@ function upsearch () {
|
||||||
popd > /dev/null
|
popd > /dev/null
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Parse IP address from ifconfig on OSX and from IP on Linux
|
||||||
|
# Parameters:
|
||||||
|
# $1 - string The desired Interface
|
||||||
|
# $2 - string A root prefix for testing purposes
|
||||||
|
function p9k::parseIp() {
|
||||||
|
local desiredInterface="${1}"
|
||||||
|
|
||||||
|
if [[ -z "${desiredInterface}" ]]; then
|
||||||
|
desiredInterface="^[^ ]+"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local ROOT_PREFIX="${2}"
|
||||||
|
if [[ "$OS" == "OSX" ]]; then
|
||||||
|
# Get a plain list of all interfaces
|
||||||
|
local rawInterfaces="$(${ROOT_PREFIX}/sbin/ifconfig -l 2>/dev/null)"
|
||||||
|
# Parse into array (split by whitespace)
|
||||||
|
local -a interfaces
|
||||||
|
interfaces=(${=rawInterfaces})
|
||||||
|
# Parse only relevant interface names
|
||||||
|
local pattern="${desiredInterface}[^ ]?"
|
||||||
|
local -a relevantInterfaces
|
||||||
|
for rawInterface in $interfaces; do
|
||||||
|
[[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=( $MATCH )
|
||||||
|
done
|
||||||
|
local newline=$'\n'
|
||||||
|
for interfaceName in $relevantInterfaces; do
|
||||||
|
local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName 2>/dev/null)"
|
||||||
|
if [[ "${interface}" =~ "lo[0-9]*" ]]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
# Check if interface is UP.
|
||||||
|
if [[ "${interface//${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then
|
||||||
|
local ipFound="${match[3]}"
|
||||||
|
local -a interfaceStates=(${(s:,:)match[1]})
|
||||||
|
if [[ "${interfaceStates[(r)UP]}" == "UP" ]]; then
|
||||||
|
echo "${ipFound}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
else
|
||||||
|
local -a interfaces
|
||||||
|
interfaces=( "${(f)$(${ROOT_PREFIX}/sbin/ip -brief -4 a show 2>/dev/null)}" )
|
||||||
|
local pattern="^${desiredInterface}[ ]+UP[ ]+([^/ ]+)"
|
||||||
|
for interface in "${(@)interfaces}"; do
|
||||||
|
if [[ "$interface" =~ $pattern ]]; then
|
||||||
|
echo "${match[1]}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
|
@ -534,7 +534,9 @@ prompt_battery() {
|
||||||
# * $1 Alignment: string - left|right
|
# * $1 Alignment: string - left|right
|
||||||
# * $2 Index: integer
|
# * $2 Index: integer
|
||||||
# * $3 Joined: bool - If the segment should be joined
|
# * $3 Joined: bool - If the segment should be joined
|
||||||
|
# * $4 Root Prefix: string - Root prefix for testing purposes
|
||||||
prompt_public_ip() {
|
prompt_public_ip() {
|
||||||
|
local ROOT_PREFIX="${4}"
|
||||||
# set default values for segment
|
# set default values for segment
|
||||||
set_default POWERLEVEL9K_PUBLIC_IP_TIMEOUT "300"
|
set_default POWERLEVEL9K_PUBLIC_IP_TIMEOUT "300"
|
||||||
set_default POWERLEVEL9K_PUBLIC_IP_NONE ""
|
set_default POWERLEVEL9K_PUBLIC_IP_NONE ""
|
||||||
|
@ -562,7 +564,7 @@ prompt_public_ip() {
|
||||||
|
|
||||||
# grab a fresh IP if needed
|
# grab a fresh IP if needed
|
||||||
local fresh_ip
|
local fresh_ip
|
||||||
if [[ $refresh_ip =~ true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
|
if [[ $refresh_ip == true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
|
||||||
for method in "${POWERLEVEL9K_PUBLIC_IP_METHODS[@]}"; do
|
for method in "${POWERLEVEL9K_PUBLIC_IP_METHODS[@]}"; do
|
||||||
case $method in
|
case $method in
|
||||||
'dig')
|
'dig')
|
||||||
|
@ -598,11 +600,10 @@ prompt_public_ip() {
|
||||||
icon='PUBLIC_IP_ICON'
|
icon='PUBLIC_IP_ICON'
|
||||||
# Check VPN is on if VPN interface is set
|
# Check VPN is on if VPN interface is set
|
||||||
if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then
|
if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then
|
||||||
for vpn_iface in $(/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1)
|
local vpnIp="$(p9k::parseIp "${POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE}" "${ROOT_PREFIX}")"
|
||||||
do
|
if [[ -n "$vpnIp" ]]; then
|
||||||
icon='VPN_ICON'
|
icon='VPN_ICON'
|
||||||
break
|
fi
|
||||||
done
|
|
||||||
fi
|
fi
|
||||||
$1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" "$icon"
|
$1_prompt_segment "$0" "$2" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "${public_ip}" "$icon"
|
||||||
fi
|
fi
|
||||||
|
@ -1099,31 +1100,8 @@ prompt_icons_test() {
|
||||||
################################################################
|
################################################################
|
||||||
# Segment to display the current IP address
|
# Segment to display the current IP address
|
||||||
prompt_ip() {
|
prompt_ip() {
|
||||||
if [[ "$OS" == "OSX" ]]; then
|
local ROOT_PREFIX="${4}"
|
||||||
if defined POWERLEVEL9K_IP_INTERFACE; then
|
local ip=$(p9k::parseIp "${POWERLEVEL9K_IP_INTERFACE}" "${ROOT_PREFIX}")
|
||||||
# Get the IP address of the specified interface.
|
|
||||||
ip=$(ipconfig getifaddr "$POWERLEVEL9K_IP_INTERFACE")
|
|
||||||
else
|
|
||||||
local interfaces callback
|
|
||||||
# Get network interface names ordered by service precedence.
|
|
||||||
interfaces=$(networksetup -listnetworkserviceorder | grep -o "Device:\s*[a-z0-9]*" | grep -o -E '[a-z0-9]*$')
|
|
||||||
callback='ipconfig getifaddr $item'
|
|
||||||
|
|
||||||
ip=$(getRelevantItem "$interfaces" "$callback")
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if defined POWERLEVEL9K_IP_INTERFACE; then
|
|
||||||
# Get the IP address of the specified interface.
|
|
||||||
ip=$(ip -4 a show "$POWERLEVEL9K_IP_INTERFACE" | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+")
|
|
||||||
else
|
|
||||||
local interfaces callback
|
|
||||||
# Get all network interface names that are up
|
|
||||||
interfaces=$(ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o -E "[a-z0-9]+")
|
|
||||||
callback='ip -4 a show $item | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+"'
|
|
||||||
|
|
||||||
ip=$(getRelevantItem "$interfaces" "$callback")
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "$ip" ]]; then
|
if [[ -n "$ip" ]]; then
|
||||||
"$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'NETWORK_ICON'
|
"$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'NETWORK_ICON'
|
||||||
|
@ -1135,11 +1113,12 @@ prompt_ip() {
|
||||||
set_default POWERLEVEL9K_VPN_IP_INTERFACE "tun"
|
set_default POWERLEVEL9K_VPN_IP_INTERFACE "tun"
|
||||||
# prompt if vpn active
|
# prompt if vpn active
|
||||||
prompt_vpn_ip() {
|
prompt_vpn_ip() {
|
||||||
for vpn_iface in $(/sbin/ifconfig | grep -e "^${POWERLEVEL9K_VPN_IP_INTERFACE}" | cut -d":" -f1)
|
local ROOT_PREFIX="${4}"
|
||||||
do
|
local ip=$(p9k::parseIp "${POWERLEVEL9K_VPN_IP_INTERFACE}" "${ROOT_PREFIX}")
|
||||||
ip=$(/sbin/ifconfig "$vpn_iface" | grep -o "inet\s.*" | cut -d' ' -f2)
|
|
||||||
|
if [[ -n "${ip}" ]]; then
|
||||||
"$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON'
|
"$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON'
|
||||||
done
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
################################################################
|
################################################################
|
||||||
|
|
|
@ -7,225 +7,239 @@ SHUNIT_PARENT=$0
|
||||||
|
|
||||||
function setUp() {
|
function setUp() {
|
||||||
export TERM="xterm-256color"
|
export TERM="xterm-256color"
|
||||||
|
|
||||||
|
# Test specific
|
||||||
|
P9K_HOME=$(pwd)
|
||||||
|
FOLDER=/tmp/powerlevel9k-test
|
||||||
|
mkdir -p $FOLDER
|
||||||
|
mkdir $FOLDER/sbin
|
||||||
|
}
|
||||||
|
|
||||||
|
function tearDown() {
|
||||||
|
# Go back to powerlevel9k folder
|
||||||
|
cd "${P9K_HOME}"
|
||||||
|
# Remove eventually created test-specific folder
|
||||||
|
rm -fr "${FOLDER}"
|
||||||
|
|
||||||
|
unset FOLDER
|
||||||
|
unset P9K_HOME
|
||||||
|
}
|
||||||
|
|
||||||
|
function fakeIfconfig() {
|
||||||
|
local INTERFACE1="${1}"
|
||||||
|
[[ -z "${INTERFACE1}" ]] && INTERFACE1="eth0"
|
||||||
|
local INTERFACE1_IP="1.2.3.4"
|
||||||
|
local INTERFACE2="${2}"
|
||||||
|
[[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2"
|
||||||
|
local INTERFACE2_IP="5.6.7.8"
|
||||||
|
# Fake ifconfig
|
||||||
|
cat > $FOLDER/sbin/ifconfig <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
if [[ "\$*" =~ '-l' ]]; then
|
||||||
|
echo "docker0 tun1 ${INTERFACE1} ${INTERFACE2} lo"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ '${INTERFACE1}' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet ${INTERFACE1_IP} txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
INNER
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ '${INTERFACE2}' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet ${INTERFACE2_IP} txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
INNER
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# If neither INTERFACE1 nor INTERFACE2 is queried, fake a offline (DOWN) interface.
|
||||||
|
# We assume if there is at least one argument, we queried for a specific interface.
|
||||||
|
if [[ "\$#" -gt 0 ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
tun1: flags=4099<DOWN,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 5.5.5.5 txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
INNER
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$#" -eq 0 ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
docker0: flags=4099<DOWN,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
|
||||||
|
ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
|
||||||
|
tun1: flags=4099<DOWN,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 10.20.30.40 txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
|
||||||
|
${INTERFACE1}: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet ${INTERFACE1_IP} txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
|
||||||
|
${INTERFACE2}: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet ${INTERFACE2_IP} txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
|
||||||
|
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
|
||||||
|
inet 127.0.0.1 netmask 255.0.0.0
|
||||||
|
inet6 ::1 prefixlen 128 scopeid 0x10<host>
|
||||||
|
loop txqueuelen 1000 (Local Loopback)
|
||||||
|
RX packets 5136 bytes 328651 (320.9 KiB)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 5136 bytes 328651 (320.9 KiB)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
INNER
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
EOF
|
||||||
|
chmod +x $FOLDER/sbin/ifconfig
|
||||||
|
}
|
||||||
|
|
||||||
|
function fakeIp() {
|
||||||
|
local INTERFACE1="${1}"
|
||||||
|
[[ -z "${INTERFACE1}" ]] && INTERFACE1="eth0"
|
||||||
|
local INTERFACE2="${2}"
|
||||||
|
[[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2"
|
||||||
|
cat > $FOLDER/sbin/ip <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
if [[ "\$*" =~ '-brief.*show' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
lo UNKNOWN 127.0.0.1/8
|
||||||
|
${INTERFACE1} UP 1.2.3.4/24
|
||||||
|
${INTERFACE2} UP 5.4.3.2/16
|
||||||
|
docker0 DOWN 172.17.0.1/16
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
2: ${INTERFACE1}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
||||||
|
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
|
||||||
|
valid_lft forever preferred_lft forever
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ 'show ${INTERFACE2}' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
3: ${INTERFACE2}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
||||||
|
inet 1.2.3.4 brd 10.0.2.255 scope global eth0
|
||||||
|
valid_lft forever preferred_lft forever
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
EOF
|
||||||
|
|
||||||
|
chmod +x $FOLDER/sbin/ip
|
||||||
}
|
}
|
||||||
|
|
||||||
function testIpSegmentPrintsNothingOnOsxIfNotConnected() {
|
function testIpSegmentPrintsNothingOnOsxIfNotConnected() {
|
||||||
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
cat > $FOLDER/sbin/ifconfig <<EOF
|
||||||
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip custom_world)
|
#!/usr/bin/env zsh
|
||||||
alias networksetup='echo "not connected"'
|
|
||||||
local POWERLEVEL9K_CUSTOM_WORLD='echo world'
|
echo "not connected"
|
||||||
|
EOF
|
||||||
|
|
||||||
# Load Powerlevel9k
|
# Load Powerlevel9k
|
||||||
source powerlevel9k.zsh-theme
|
source powerlevel9k.zsh-theme
|
||||||
local OS="OSX" # Fake OSX
|
local OS="OSX" # Fake OSX
|
||||||
|
|
||||||
assertEquals "%K{007} %F{000}world %k%F{007}%f " "$(build_left_prompt)"
|
assertEquals "" "$(prompt_ip left 1 false "$FOLDER")"
|
||||||
|
|
||||||
unalias networksetup
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testIpSegmentPrintsNothingOnLinuxIfNotConnected() {
|
function testIpSegmentPrintsNothingOnLinuxIfNotConnected() {
|
||||||
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
|
||||||
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip custom_world)
|
|
||||||
alias ip='echo "not connected"'
|
|
||||||
local POWERLEVEL9K_CUSTOM_WORLD='echo world'
|
|
||||||
|
|
||||||
# Load Powerlevel9k
|
# Load Powerlevel9k
|
||||||
source powerlevel9k.zsh-theme
|
source powerlevel9k.zsh-theme
|
||||||
local OS="Linux" # Fake Linux
|
local OS="Linux" # Fake Linux
|
||||||
|
|
||||||
assertEquals "%K{007} %F{000}world %k%F{007}%f " "$(build_left_prompt)"
|
cat > $FOLDER/sbin/ip <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
unalias ip
|
echo "not connected"
|
||||||
|
EOF
|
||||||
|
chmod +x $FOLDER/sbin/ip
|
||||||
|
|
||||||
|
assertEquals "" "$(prompt_ip left 1 false "$FOLDER")"
|
||||||
}
|
}
|
||||||
|
|
||||||
function testIpSegmentWorksOnOsxWithNoInterfaceSpecified() {
|
function testIpSegmentWorksOnOsxWithNoInterfaceSpecified() {
|
||||||
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
|
||||||
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
|
|
||||||
alias networksetup="echo 'An asterisk (*) denotes that a network service is disabled.
|
|
||||||
(1) Ethernet
|
|
||||||
(Hardware Port: Ethernet, Device: en0)
|
|
||||||
|
|
||||||
(2) FireWire
|
|
||||||
(Hardware Port: FireWire, Device: fw0)
|
|
||||||
|
|
||||||
(3) Wi-Fi
|
|
||||||
(Hardware Port: Wi-Fi, Device: en1)
|
|
||||||
|
|
||||||
(4) Bluetooth PAN
|
|
||||||
(Hardware Port: Bluetooth PAN, Device: en3)
|
|
||||||
|
|
||||||
(5) Thunderbolt Bridge
|
|
||||||
(Hardware Port: Thunderbolt Bridge, Device: bridge0)
|
|
||||||
|
|
||||||
(6) Apple USB Ethernet Adapter
|
|
||||||
(Hardware Port: Apple USB Ethernet Adapter, Device: en4)
|
|
||||||
'"
|
|
||||||
|
|
||||||
alias ipconfig="_(){ echo '1.2.3.4'; };_"
|
|
||||||
|
|
||||||
# Load Powerlevel9k
|
# Load Powerlevel9k
|
||||||
source powerlevel9k.zsh-theme
|
source powerlevel9k.zsh-theme
|
||||||
local OS='OSX' # Fake OSX
|
local OS='OSX' # Fake OSX
|
||||||
|
|
||||||
assertEquals "%K{006} %F{000}IP %F{000}1.2.3.4 %k%F{006}%f " "$(build_left_prompt)"
|
fakeIfconfig "eth1" "eth2"
|
||||||
|
|
||||||
unalias ipconfig
|
assertEquals "%K{006} %F{000}IP %F{000}1.2.3.4 " "$(prompt_ip left 1 false "$FOLDER")"
|
||||||
unalias networksetup
|
|
||||||
}
|
|
||||||
|
|
||||||
# There could be more than one confiured network interfaces.
|
|
||||||
# `networksetup -listnetworkserviceorder` lists the interfaces
|
|
||||||
# in hierarchical order, but from outside this is not obvious
|
|
||||||
# (implementation detail). So we need a test for this case.
|
|
||||||
function testIpSegmentWorksOnOsxWithMultipleInterfacesSpecified() {
|
|
||||||
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
|
||||||
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
|
|
||||||
alias networksetup="echo 'An asterisk (*) denotes that a network service is disabled.
|
|
||||||
(1) Ethernet
|
|
||||||
(Hardware Port: Ethernet, Device: en0)
|
|
||||||
|
|
||||||
(2) FireWire
|
|
||||||
(Hardware Port: FireWire, Device: fw0)
|
|
||||||
|
|
||||||
(3) Wi-Fi
|
|
||||||
(Hardware Port: Wi-Fi, Device: en1)
|
|
||||||
|
|
||||||
(4) Bluetooth PAN
|
|
||||||
(Hardware Port: Bluetooth PAN, Device: en3)
|
|
||||||
|
|
||||||
(5) Thunderbolt Bridge
|
|
||||||
(Hardware Port: Thunderbolt Bridge, Device: bridge0)
|
|
||||||
|
|
||||||
(6) Apple USB Ethernet Adapter
|
|
||||||
(Hardware Port: Apple USB Ethernet Adapter, Device: en4)
|
|
||||||
'"
|
|
||||||
|
|
||||||
# Return a unique IP address for every interface
|
|
||||||
ipconfig() {
|
|
||||||
case "${2}" {
|
|
||||||
en0)
|
|
||||||
echo 1.2.3.4
|
|
||||||
;;
|
|
||||||
fw0)
|
|
||||||
echo 2.3.4.5
|
|
||||||
;;
|
|
||||||
en1)
|
|
||||||
echo 3.4.5.6
|
|
||||||
;;
|
|
||||||
en3)
|
|
||||||
echo 4.5.6.7
|
|
||||||
;;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Load Powerlevel9k
|
|
||||||
source powerlevel9k.zsh-theme
|
|
||||||
local OS='OSX' # Fake OSX
|
|
||||||
|
|
||||||
assertEquals "%K{006} %F{000}IP %F{000}1.2.3.4 %k%F{006}%f " "$(build_left_prompt)"
|
|
||||||
|
|
||||||
unfunction ipconfig
|
|
||||||
unalias networksetup
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testIpSegmentWorksOnOsxWithInterfaceSpecified() {
|
function testIpSegmentWorksOnOsxWithInterfaceSpecified() {
|
||||||
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
fakeIfconfig "eth1"
|
||||||
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
|
|
||||||
local POWERLEVEL9K_IP_INTERFACE='xxx'
|
local POWERLEVEL9K_IP_INTERFACE="eth1"
|
||||||
alias ipconfig="_(){ echo '1.2.3.4'; };_"
|
|
||||||
|
|
||||||
# Load Powerlevel9k
|
# Load Powerlevel9k
|
||||||
source powerlevel9k.zsh-theme
|
source powerlevel9k.zsh-theme
|
||||||
local OS='OSX' # Fake OSX
|
local OS='OSX' # Fake OSX
|
||||||
|
|
||||||
assertEquals "%K{006} %F{000}IP %F{000}1.2.3.4 %k%F{006}%f " "$(build_left_prompt)"
|
assertEquals "%K{006} %F{000}IP %F{000}1.2.3.4 " "$(prompt_ip left 1 false "$FOLDER")"
|
||||||
|
|
||||||
unalias ipconfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testIpSegmentWorksOnLinuxWithNoInterfaceSpecified() {
|
function testIpSegmentWorksOnLinuxWithNoInterfaceSpecified() {
|
||||||
setopt aliases
|
|
||||||
local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
|
|
||||||
# That command is harder to test, as it is used at first
|
|
||||||
# to get all relevant network interfaces and then for
|
|
||||||
# getting the configuration of that segment..
|
|
||||||
ip(){
|
|
||||||
if [[ "$*" == 'link ls up' ]]; then
|
|
||||||
echo "1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
|
|
||||||
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
|
|
||||||
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
|
||||||
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff";
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$*" == '-4 a show eth0' ]]; then
|
|
||||||
echo '2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
|
||||||
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
|
|
||||||
valid_lft forever preferred_lft forever';
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Load Powerlevel9k
|
# Load Powerlevel9k
|
||||||
source powerlevel9k.zsh-theme
|
source powerlevel9k.zsh-theme
|
||||||
local OS='Linux' # Fake Linux
|
local OS='Linux' # Fake Linux
|
||||||
|
|
||||||
assertEquals "%K{006} %F{000}IP %F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)"
|
fakeIp "eth0"
|
||||||
|
|
||||||
unfunction ip
|
assertEquals "%K{006} %F{000}IP %F{000}1.2.3.4 " "$(prompt_ip left 1 false "$FOLDER")"
|
||||||
}
|
|
||||||
|
|
||||||
function testIpSegmentWorksOnLinuxWithMultipleInterfacesSpecified() {
|
|
||||||
setopt aliases
|
|
||||||
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
|
||||||
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
|
|
||||||
# That command is harder to test, as it is used at first
|
|
||||||
# to get all relevant network interfaces and then for
|
|
||||||
# getting the configuration of that segment..
|
|
||||||
ip(){
|
|
||||||
if [[ "$*" == 'link ls up' ]]; then
|
|
||||||
echo "1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
|
|
||||||
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
|
|
||||||
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
|
||||||
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
|
|
||||||
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
|
||||||
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
|
|
||||||
4: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
|
||||||
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff";
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$*" == '-4 a show eth1' ]]; then
|
|
||||||
echo '3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
|
||||||
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
|
|
||||||
valid_lft forever preferred_lft forever';
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Load Powerlevel9k
|
|
||||||
source powerlevel9k.zsh-theme
|
|
||||||
local OS='Linux' # Fake Linux
|
|
||||||
|
|
||||||
assertEquals "%K{006} %F{000}IP %F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)"
|
|
||||||
|
|
||||||
unfunction ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testIpSegmentWorksOnLinuxWithInterfaceSpecified() {
|
function testIpSegmentWorksOnLinuxWithInterfaceSpecified() {
|
||||||
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
fakeIp "eth3"
|
||||||
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
|
|
||||||
local POWERLEVEL9K_IP_INTERFACE='xxx'
|
local POWERLEVEL9K_IP_INTERFACE="eth3"
|
||||||
ip(){
|
|
||||||
echo '2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
|
||||||
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
|
|
||||||
valid_lft forever preferred_lft forever';
|
|
||||||
}
|
|
||||||
|
|
||||||
# Load Powerlevel9k
|
# Load Powerlevel9k
|
||||||
source powerlevel9k.zsh-theme
|
source powerlevel9k.zsh-theme
|
||||||
local OS='Linux' # Fake Linux
|
local OS='Linux' # Fake Linux
|
||||||
|
|
||||||
assertEquals "%K{006} %F{000}IP %F{000}10.0.2.15 %k%F{006}%f " "$(build_left_prompt)"
|
assertEquals "%K{006} %F{000}IP %F{000}1.2.3.4 " "$(prompt_ip left 1 false "$FOLDER")"
|
||||||
|
|
||||||
unfunction ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
source shunit2/shunit2
|
source shunit2/shunit2
|
|
@ -12,6 +12,7 @@ function setUp() {
|
||||||
P9K_HOME=$(pwd)
|
P9K_HOME=$(pwd)
|
||||||
FOLDER=/tmp/powerlevel9k-test
|
FOLDER=/tmp/powerlevel9k-test
|
||||||
mkdir -p $FOLDER
|
mkdir -p $FOLDER
|
||||||
|
mkdir $FOLDER/sbin
|
||||||
cd $FOLDER
|
cd $FOLDER
|
||||||
|
|
||||||
# Change cache file, so that the users environment don't
|
# Change cache file, so that the users environment don't
|
||||||
|
@ -33,6 +34,113 @@ function tearDown() {
|
||||||
unset POWERLEVEL9K_PUBLIC_IP_FILE
|
unset POWERLEVEL9K_PUBLIC_IP_FILE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fakeIfconfig() {
|
||||||
|
local INTERFACE="${1}"
|
||||||
|
[[ -z "${INTERFACE}" ]] && INTERFACE="tun0"
|
||||||
|
# Fake ifconfig
|
||||||
|
cat > $FOLDER/sbin/ifconfig <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
if [[ "\$*" == "-l" ]]; then
|
||||||
|
echo "lo0 gif0 stf0 EHC250 EHC253 tun1 tun0 ${INTERFACE} fw0 en0 en2 en1 p2p0 bridge0 utun0"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" == "${INTERFACE}" ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
${INTERFACE}: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 1.2.3.4 txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
INNER
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$#" == "0" ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
|
||||||
|
ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
|
||||||
|
${INTERFACE}: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 1.2.3.4 txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
|
||||||
|
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
|
||||||
|
inet 127.0.0.1 netmask 255.0.0.0
|
||||||
|
inet6 ::1 prefixlen 128 scopeid 0x10<host>
|
||||||
|
loop txqueuelen 1000 (Local Loopback)
|
||||||
|
RX packets 5136 bytes 328651 (320.9 KiB)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 5136 bytes 328651 (320.9 KiB)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
EOF
|
||||||
|
chmod +x $FOLDER/sbin/ifconfig
|
||||||
|
}
|
||||||
|
|
||||||
|
function fakeIp() {
|
||||||
|
local INTERFACE1="${1}"
|
||||||
|
[[ -z "${INTERFACE1}" ]] && INTERFACE1="tun0"
|
||||||
|
local INTERFACE2="${2}"
|
||||||
|
[[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2"
|
||||||
|
cat > $FOLDER/sbin/ip <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
if [[ "\$*" == 'link ls up' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
|
||||||
|
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
|
||||||
|
2: ${INTERFACE1}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
||||||
|
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
|
||||||
|
3: ${INTERFACE2}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
||||||
|
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
|
||||||
|
4: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
||||||
|
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ '-brief.*show' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
lo UNKNOWN 127.0.0.1/8
|
||||||
|
${INTERFACE1} UP 1.2.3.4/24
|
||||||
|
${INTERFACE2} UP 5.4.3.2/16
|
||||||
|
docker0 DOWN 172.17.0.1/16
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
2: ${INTERFACE1}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
||||||
|
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
|
||||||
|
valid_lft forever preferred_lft forever
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ 'show ${INTERFACE2}' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
3: ${INTERFACE2}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
||||||
|
inet 1.2.3.4 brd 10.0.2.255 scope global eth0
|
||||||
|
valid_lft forever preferred_lft forever
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
EOF
|
||||||
|
|
||||||
|
chmod +x $FOLDER/sbin/ip
|
||||||
|
}
|
||||||
|
|
||||||
function testPublicIpSegmentPrintsNothingByDefaultIfHostIsNotAvailable() {
|
function testPublicIpSegmentPrintsNothingByDefaultIfHostIsNotAvailable() {
|
||||||
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
|
||||||
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip custom_world)
|
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip custom_world)
|
||||||
|
@ -219,4 +327,60 @@ function testPublicIpSegmentWhenGoingOnline() {
|
||||||
unfunction dig
|
unfunction dig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testPublicIpSegmentWithVPNTurnedOnLinux() {
|
||||||
|
local OS='linux'
|
||||||
|
|
||||||
|
echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE
|
||||||
|
local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun1"
|
||||||
|
|
||||||
|
# Fake ip command
|
||||||
|
fakeIp "tun1"
|
||||||
|
|
||||||
|
assertEquals "%K{000} %F{007}(vpn) %F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"
|
||||||
|
}
|
||||||
|
|
||||||
|
function testPublicIpSegmentWithVPNTurnedOnOsx() {
|
||||||
|
typeset -F now
|
||||||
|
now=$(date +%s)
|
||||||
|
|
||||||
|
local OS='OSX'
|
||||||
|
|
||||||
|
echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE
|
||||||
|
local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun1"
|
||||||
|
|
||||||
|
# Fake stat call
|
||||||
|
function stat() {
|
||||||
|
echo $now
|
||||||
|
}
|
||||||
|
|
||||||
|
# Fake ifconfig
|
||||||
|
fakeIfconfig "tun1"
|
||||||
|
|
||||||
|
assertEquals "%K{000} %F{007}(vpn) %F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"
|
||||||
|
|
||||||
|
unfunction stat
|
||||||
|
}
|
||||||
|
|
||||||
|
function testPublicIpSegmentWithVPNTurnedOnAndFuzzyMatchingOnOsx() {
|
||||||
|
typeset -F now
|
||||||
|
now=$(date +%s)
|
||||||
|
|
||||||
|
local OS='OSX'
|
||||||
|
|
||||||
|
echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE
|
||||||
|
local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun"
|
||||||
|
|
||||||
|
# Fake stat call
|
||||||
|
function stat() {
|
||||||
|
echo $now
|
||||||
|
}
|
||||||
|
|
||||||
|
# Fake ifconfig
|
||||||
|
fakeIfconfig "tun3"
|
||||||
|
|
||||||
|
assertEquals "%K{000} %F{007}(vpn) %F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"
|
||||||
|
|
||||||
|
unfunction stat
|
||||||
|
}
|
||||||
|
|
||||||
source shunit2/shunit2
|
source shunit2/shunit2
|
195
test/segments/vpn_ip.spec
Executable file
195
test/segments/vpn_ip.spec
Executable file
|
@ -0,0 +1,195 @@
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
|
||||||
|
|
||||||
|
# Required for shunit2 to run correctly
|
||||||
|
setopt shwordsplit
|
||||||
|
SHUNIT_PARENT=$0
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
export TERM="xterm-256color"
|
||||||
|
|
||||||
|
# Test specific
|
||||||
|
P9K_HOME=$(pwd)
|
||||||
|
FOLDER=/tmp/powerlevel9k-test
|
||||||
|
mkdir -p $FOLDER
|
||||||
|
mkdir $FOLDER/sbin
|
||||||
|
}
|
||||||
|
|
||||||
|
function tearDown() {
|
||||||
|
# Go back to powerlevel9k folder
|
||||||
|
cd "${P9K_HOME}"
|
||||||
|
# Remove eventually created test-specific folder
|
||||||
|
rm -fr "${FOLDER}"
|
||||||
|
|
||||||
|
unset FOLDER
|
||||||
|
unset P9K_HOME
|
||||||
|
}
|
||||||
|
|
||||||
|
function fakeIfconfig() {
|
||||||
|
# Fake ifconfig
|
||||||
|
cat > $FOLDER/sbin/ifconfig <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
if [[ "\$#" -gt 0 ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 1.2.3.4 txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
INNER
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat <<INNER
|
||||||
|
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
|
||||||
|
ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
|
||||||
|
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
|
||||||
|
inet 1.2.3.4 txqueuelen 1000 (Ethernet)
|
||||||
|
RX packets 0 bytes 0 (0.0 B)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 0 bytes 0 (0.0 B)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
device interrupt 16 memory 0xe8200000-e8220000
|
||||||
|
|
||||||
|
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
|
||||||
|
inet 127.0.0.1 netmask 255.0.0.0
|
||||||
|
inet6 ::1 prefixlen 128 scopeid 0x10<host>
|
||||||
|
loop txqueuelen 1000 (Local Loopback)
|
||||||
|
RX packets 5136 bytes 328651 (320.9 KiB)
|
||||||
|
RX errors 0 dropped 0 overruns 0 frame 0
|
||||||
|
TX packets 5136 bytes 328651 (320.9 KiB)
|
||||||
|
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|
||||||
|
INNER
|
||||||
|
EOF
|
||||||
|
chmod +x $FOLDER/sbin/ifconfig
|
||||||
|
}
|
||||||
|
|
||||||
|
function fakeIp() {
|
||||||
|
local INTERFACE1="${1}"
|
||||||
|
[[ -z "${INTERFACE1}" ]] && INTERFACE1="tun0"
|
||||||
|
local INTERFACE2="${2}"
|
||||||
|
[[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2"
|
||||||
|
cat > $FOLDER/sbin/ip <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
if [[ "\$*" == 'link ls up' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
|
||||||
|
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
|
||||||
|
2: ${INTERFACE1}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
||||||
|
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
|
||||||
|
3: ${INTERFACE2}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
||||||
|
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
|
||||||
|
4: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
|
||||||
|
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ '-brief.*show' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
lo UNKNOWN 127.0.0.1/8
|
||||||
|
${INTERFACE1} UP 1.2.3.4/24
|
||||||
|
${INTERFACE2} UP 5.4.3.2/16
|
||||||
|
docker0 DOWN 172.17.0.1/16
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
2: ${INTERFACE1}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
||||||
|
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
|
||||||
|
valid_lft forever preferred_lft forever
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "\$*" =~ 'show ${INTERFACE2}' ]]; then
|
||||||
|
cat <<INNER
|
||||||
|
3: ${INTERFACE2}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
|
||||||
|
inet 1.2.3.4 brd 10.0.2.255 scope global eth0
|
||||||
|
valid_lft forever preferred_lft forever
|
||||||
|
INNER
|
||||||
|
fi
|
||||||
|
EOF
|
||||||
|
|
||||||
|
chmod +x $FOLDER/sbin/ip
|
||||||
|
}
|
||||||
|
|
||||||
|
function testVpnIpSegmentPrintsNothingOnOsxIfNotConnected() {
|
||||||
|
# Load Powerlevel9k
|
||||||
|
source powerlevel9k.zsh-theme
|
||||||
|
local OS="OSX" # Fake OSX
|
||||||
|
|
||||||
|
cat > $FOLDER/sbin/ifconfig <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
echo "not connected"
|
||||||
|
EOF
|
||||||
|
chmod +x $FOLDER/sbin/ifconfig
|
||||||
|
|
||||||
|
assertEquals "" "$(prompt_vpn_ip left 1 false "$FOLDER")"
|
||||||
|
}
|
||||||
|
|
||||||
|
function testVpnIpSegmentPrintsNothingOnLinuxIfNotConnected() {
|
||||||
|
# Load Powerlevel9k
|
||||||
|
source powerlevel9k.zsh-theme
|
||||||
|
local OS="Linux" # Fake Linux
|
||||||
|
|
||||||
|
cat > $FOLDER/sbin/ip <<EOF
|
||||||
|
#!/usr/bin/env zsh
|
||||||
|
|
||||||
|
echo "not connected"
|
||||||
|
EOF
|
||||||
|
chmod +x $FOLDER/sbin/ip
|
||||||
|
|
||||||
|
assertEquals "" "$(prompt_vpn_ip left 1 false "$FOLDER")"
|
||||||
|
}
|
||||||
|
|
||||||
|
function testVpnIpSegmentWorksOnOsxWithInterfaceSpecified() {
|
||||||
|
local POWERLEVEL9K_VPN_IP_INTERFACE='tun1'
|
||||||
|
|
||||||
|
fakeIfconfig
|
||||||
|
|
||||||
|
# Load Powerlevel9k
|
||||||
|
source powerlevel9k.zsh-theme
|
||||||
|
local OS='OSX' # Fake OSX
|
||||||
|
|
||||||
|
assertEquals "%K{006} %F{000}(vpn) %F{000}1.2.3.4 " "$(prompt_vpn_ip left 1 false "$FOLDER")"
|
||||||
|
}
|
||||||
|
|
||||||
|
function testVpnIpSegmentWorksOnLinuxWithInterfaceSpecified() {
|
||||||
|
local POWERLEVEL9K_VPN_IP_INTERFACE='tun1'
|
||||||
|
|
||||||
|
fakeIp "tun1"
|
||||||
|
|
||||||
|
# Load Powerlevel9k
|
||||||
|
source powerlevel9k.zsh-theme
|
||||||
|
local OS='Linux' # Fake Linux
|
||||||
|
|
||||||
|
assertEquals "%K{006} %F{000}(vpn) %F{000}1.2.3.4 " "$(prompt_vpn_ip left 1 false "$FOLDER")"
|
||||||
|
}
|
||||||
|
|
||||||
|
# vpn_ip is not capable of handling multiple vpn interfaces ATM.
|
||||||
|
# function testVpnIpSegmentWorksOnLinuxWithMultipleInterfacesSpecified() {
|
||||||
|
# local POWERLEVEL9K_VPN_IP_INTERFACE=(tun0 tun1)
|
||||||
|
|
||||||
|
# fakeIp "tun0" "tun1"
|
||||||
|
|
||||||
|
# # Load Powerlevel9k
|
||||||
|
# source powerlevel9k.zsh-theme
|
||||||
|
# local OS='Linux' # Fake Linux
|
||||||
|
|
||||||
|
# setopt xtrace
|
||||||
|
# assertEquals "%K{006} %F{000}(vpn) %F{000}10.0.2.15 " "$(prompt_vpn_ip left 1 false "$FOLDER")"
|
||||||
|
# unsetopt xtrace
|
||||||
|
# }
|
||||||
|
|
||||||
|
source shunit2/shunit2
|
Loading…
Reference in a new issue