/
NetworkInterfacesTest.java
130 lines (119 loc) · 6.14 KB
/
NetworkInterfacesTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Copyright (C) 2016 RasPi Check Contributors
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package de.eidottermihi.rpicheck.test;
import org.junit.Test;
import java.util.List;
import de.eidottermihi.rpicheck.ssh.beans.NetworkInterfaceInformation;
import de.eidottermihi.rpicheck.ssh.beans.WlanBean;
import de.eidottermihi.rpicheck.ssh.impl.RaspiQueryException;
import de.eidottermihi.rpicheck.test.mocks.CommandMocker;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
public class NetworkInterfacesTest extends AbstractMockedQueryTest {
@Test
public void eth0() throws RaspiQueryException {
sessionMocker.withCommand("ls -1 /sys/class/net", new CommandMocker()
.withResponse("lo\neth0").mock());
sessionMocker.withCommand("cat /sys/class/net/eth0/carrier",
new CommandMocker().withResponse("1").mock());
sessionMocker.withCommand("ip -f inet addr show dev eth0 | sed -n 2p",
new CommandMocker().withResponse("192.168.0.9").mock());
List<NetworkInterfaceInformation> interfaces = raspiQuery
.queryNetworkInformation();
assertEquals(1, interfaces.size());
NetworkInterfaceInformation eth0 = interfaces.get(0);
assertEquals("eth0", eth0.getName());
assertEquals(true, eth0.isHasCarrier());
assertEquals("192.168.0.9", eth0.getIpAdress());
assertNull(eth0.getWlanInfo());
}
@Test
public void wlan() throws RaspiQueryException {
sessionMocker.withCommand("ls -1 /sys/class/net", new CommandMocker()
.withResponse("lo\nwlan0").mock());
sessionMocker.withCommand("cat /sys/class/net/wlan0/carrier",
new CommandMocker().withResponse("1").mock());
sessionMocker.withCommand("ip -f inet addr show dev wlan0 | sed -n 2p",
new CommandMocker().withResponse("192.168.0.9").mock());
sessionMocker.withCommand("cat /proc/net/wireless", new CommandMocker()
.withResponse(
"Inter-| sta-| Quality | Discarded packets | Missed | WE"
+ "\n"
+ " face | tus | link level noise | nwid crypt frag retry misc | beacon | 22"
+ "\n"
+ " wlan0: 0000 100. 95. 0. 0 0 0 0 0 0\n").mock());
List<NetworkInterfaceInformation> interfaces = raspiQuery
.queryNetworkInformation();
assertEquals(1, interfaces.size());
NetworkInterfaceInformation wlan0 = interfaces.get(0);
assertEquals("wlan0", wlan0.getName());
assertEquals(true, wlan0.isHasCarrier());
assertEquals("192.168.0.9", wlan0.getIpAdress());
WlanBean wlanInfo = wlan0.getWlanInfo();
assertNotNull(wlanInfo);
assertEquals(100, wlanInfo.getLinkQuality().intValue());
assertEquals(95, wlanInfo.getSignalLevel().intValue());
}
@Test
public void eth0_no_ip_cmd_available() throws RaspiQueryException {
sessionMocker.withCommand("ls -1 /sys/class/net", new CommandMocker()
.withResponse("lo\neth0").mock());
sessionMocker.withCommand("cat /sys/class/net/eth0/carrier",
new CommandMocker().withResponse("1").mock());
sessionMocker.withCommand("ip -f inet addr show dev eth0 | sed -n 2p",
new CommandMocker().withResponse("ip: command not found")
.withExitStatus(1).mock());
sessionMocker
.withCommand(
"/sbin/ifconfig eth0 | grep \"inet addr\"",
new CommandMocker()
.withResponse(
" inet addr:192.168.0.9 Bcast:192.168.0.255 Mask:255.255.255.0")
.withExitStatus(0).mock());
List<NetworkInterfaceInformation> interfaces = raspiQuery
.queryNetworkInformation();
assertEquals(1, interfaces.size());
NetworkInterfaceInformation eth0 = interfaces.get(0);
assertEquals("eth0", eth0.getName());
assertEquals(true, eth0.isHasCarrier());
assertEquals("192.168.0.9", eth0.getIpAdress());
assertNull(eth0.getWlanInfo());
}
@Test
public void eth0_no_ip_and_ifconfig_available() throws RaspiQueryException {
sessionMocker.withCommand("ls -1 /sys/class/net", new CommandMocker()
.withResponse("lo\neth0").mock());
sessionMocker.withCommand("cat /sys/class/net/eth0/carrier",
new CommandMocker().withResponse("1").mock());
sessionMocker.withCommand("ip -f inet addr show dev eth0 | sed -n 2p",
new CommandMocker().withResponse("ip: command not found")
.withExitStatus(1).mock());
sessionMocker.withCommand("/sbin/ifconfig eth0 | grep \"inet addr\"",
new CommandMocker().withResponse("ifconfig: command not found")
.withExitStatus(1).mock());
List<NetworkInterfaceInformation> interfaces = raspiQuery
.queryNetworkInformation();
assertEquals(1, interfaces.size());
NetworkInterfaceInformation eth0 = interfaces.get(0);
assertEquals("eth0", eth0.getName());
assertEquals(true, eth0.isHasCarrier());
assertNull(eth0.getIpAdress());
assertNull(eth0.getWlanInfo());
}
}