1
2
3
4
5 package oshi.software.os;
6
7 import static org.hamcrest.MatcherAssert.assertThat;
8 import static org.hamcrest.Matchers.allOf;
9 import static org.hamcrest.Matchers.anyOf;
10 import static org.hamcrest.Matchers.emptyString;
11 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
12 import static org.hamcrest.Matchers.is;
13 import static org.hamcrest.Matchers.lessThanOrEqualTo;
14 import static org.hamcrest.Matchers.not;
15 import static org.hamcrest.Matchers.notNullValue;
16
17 import org.junit.jupiter.api.BeforeAll;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.TestInstance;
20 import org.junit.jupiter.api.TestInstance.Lifecycle;
21
22 import oshi.SystemInfo;
23 import oshi.software.os.InternetProtocolStats.IPConnection;
24 import oshi.software.os.InternetProtocolStats.TcpStats;
25 import oshi.software.os.InternetProtocolStats.UdpStats;
26
27
28
29
30 @TestInstance(Lifecycle.PER_CLASS)
31 class InternetProtocolStatsTest {
32
33 private InternetProtocolStats ipStats = null;
34
35 @BeforeAll
36 void setUp() {
37 SystemInfo si = new SystemInfo();
38 this.ipStats = si.getOperatingSystem().getInternetProtocolStats();
39 }
40
41 @Test
42 void testTCPStats() {
43 TcpStats tcp4 = ipStats.getTCPv4Stats();
44 assertThat("IPV4 TCP connections should be 0 or higher", tcp4.getConnectionsEstablished(),
45 is(greaterThanOrEqualTo(0L)));
46 assertThat("IPV4 TCP active connections should be 0 or higher", tcp4.getConnectionsActive(),
47 is(greaterThanOrEqualTo(0L)));
48 assertThat("IPV4 TCP passive connections should be 0 or higher", tcp4.getConnectionsPassive(),
49 is(greaterThanOrEqualTo(0L)));
50 assertThat("IPV4 TCP connection failures should be 0 or higher", tcp4.getConnectionFailures(),
51 is(greaterThanOrEqualTo(0L)));
52 assertThat("IPV4 TCP connections reset should be 0 or higher", tcp4.getConnectionsReset(),
53 is(greaterThanOrEqualTo(0L)));
54 assertThat("IPV4 TCP segments received should be 0 or higher", tcp4.getSegmentsReceived(),
55 is(greaterThanOrEqualTo(0L)));
56 assertThat("IPV4 TCP segments sent should be 0 or higher", tcp4.getSegmentsSent(),
57 is(greaterThanOrEqualTo(0L)));
58 assertThat("IPV4 TCP segments retransmitted should be 0 or higher", tcp4.getSegmentsRetransmitted(),
59 is(greaterThanOrEqualTo(0L)));
60 assertThat("IPV4 TCP errors should be or higher ", tcp4.getInErrors(), is(greaterThanOrEqualTo(0L)));
61 assertThat("IPV4 TCP segments transmitted with the reset flag should be 0 or higher", tcp4.getOutResets(),
62 is(greaterThanOrEqualTo(0L)));
63
64 TcpStats tcp6 = ipStats.getTCPv6Stats();
65 assertThat("IPV6 TCP connections should be 0 or higher", tcp6.getConnectionsEstablished(),
66 is(greaterThanOrEqualTo(0L)));
67 assertThat("IPV6 TCP active connections should be 0 or higher", tcp6.getConnectionsActive(),
68 is(greaterThanOrEqualTo(0L)));
69 assertThat("IPV6 TCP passive connections should be 0 or higher", tcp6.getConnectionsPassive(),
70 is(greaterThanOrEqualTo(0L)));
71 assertThat("IPV6 TCP connection failures should be 0 or higher", tcp6.getConnectionFailures(),
72 is(greaterThanOrEqualTo(0L)));
73 assertThat("IPV6 TCP connections reset should be 0 or higher", tcp6.getConnectionsReset(),
74 is(greaterThanOrEqualTo(0L)));
75 assertThat("IPV6 TCP segments received should be 0 or higher", tcp6.getSegmentsReceived(),
76 is(greaterThanOrEqualTo(0L)));
77 assertThat("IPV6 TCP segments sent should be 0 or higher", tcp6.getSegmentsSent(),
78 is(greaterThanOrEqualTo(0L)));
79 assertThat("IPV6 TCP segments retransmitted should be 0 or higher", tcp6.getSegmentsRetransmitted(),
80 is(greaterThanOrEqualTo(0L)));
81 assertThat("IPV6 TCP errors should be or higher ", tcp6.getInErrors(), is(greaterThanOrEqualTo(0L)));
82 assertThat("IPV6 TCP segments transmitted with the reset flag should be 0 or higher", tcp6.getOutResets(),
83 is(greaterThanOrEqualTo(0L)));
84
85 assertThat("IPV4 TCP stats shouldn't be null", tcp4.toString(), is(notNullValue()));
86 assertThat("IPV6 TCP stats shouldn't be null", tcp6.toString(), is(notNullValue()));
87 }
88
89 @Test
90 void testUDPStats() {
91 UdpStats udp4 = ipStats.getUDPv4Stats();
92 assertThat("IPV4 UDP datagrams delivered to UDP users should be 0 or higher", udp4.getDatagramsReceived(),
93 is(greaterThanOrEqualTo(0L)));
94 assertThat("IPV4 UDP datagrams sent should be 0 or higher", udp4.getDatagramsSent(),
95 is(greaterThanOrEqualTo(0L)));
96 assertThat("IPV4 UDP datagrams received with no application at the destination port should be 0 or higher",
97 udp4.getDatagramsNoPort(), is(greaterThanOrEqualTo(0L)));
98 assertThat(
99 "IPV4 UDP datagrams received that could not be delivered for reasons other than lack of application at the destination port should be 0 or higher",
100 udp4.getDatagramsReceivedErrors(), is(greaterThanOrEqualTo(0L)));
101
102 UdpStats udp6 = ipStats.getUDPv6Stats();
103 assertThat("IPV6 UDP datagrams delivered to UDP users should be 0 or higher", udp6.getDatagramsReceived(),
104 is(greaterThanOrEqualTo(0L)));
105 assertThat("IPV6 UDP datagrams sent should be 0 or higher", udp6.getDatagramsSent(),
106 is(greaterThanOrEqualTo(0L)));
107 assertThat("IPV6 UDP datagrams received with no application at the destination port should be 0 or higher",
108 udp6.getDatagramsNoPort(), is(greaterThanOrEqualTo(0L)));
109 assertThat(
110 "IPV6 UDP datagrams received that could not be delivered for reasons other than lack of application at the destination port should be 0 or higher",
111 udp6.getDatagramsReceivedErrors(), is(greaterThanOrEqualTo(0L)));
112
113 assertThat("IPV4 UDP stats shouldn't be null", udp4.toString(), is(notNullValue()));
114 assertThat("IPV6 UDP stats shouldn't be null", udp6.toString(), is(notNullValue()));
115 }
116
117 @Test
118 void testGetConnections() {
119 for (IPConnection conn : ipStats.getConnections()) {
120 assertThat("Protocol name is not null or empty", conn.getType(),
121 allOf(is(not(emptyString())), is(notNullValue())));
122 if (conn.getType().contains("46")) {
123 assertThat("Local address array size should be 0, or 4", conn.getLocalAddress().length,
124 anyOf(is(0), is(4)));
125 assertThat("Foreign address rray size should be 0, or 4", conn.getForeignAddress().length,
126 anyOf(is(0), is(4)));
127 } else if (conn.getType().contains("6")) {
128 assertThat("Local address array size should be 0, or 16", conn.getLocalAddress().length,
129 anyOf(is(0), is(16)));
130 assertThat("Foreign address rray size should be 0, or 16", conn.getForeignAddress().length,
131 anyOf(is(0), is(16)));
132 } else {
133 assertThat("Local address array size should be 0, or 4", conn.getLocalAddress().length,
134 anyOf(is(0), is(4)));
135 assertThat("Foreign address rray size should be 0, or 4", conn.getForeignAddress().length,
136 anyOf(is(0), is(4)));
137 }
138 assertThat("Local port must be a 16 bit value", conn.getLocalPort(),
139 allOf(greaterThanOrEqualTo(0), lessThanOrEqualTo(0xffff)));
140 assertThat("Foreign port must be a 16 bit value", conn.getLocalPort(),
141 allOf(greaterThanOrEqualTo(0), lessThanOrEqualTo(0xffff)));
142 assertThat("Transmit queue msut be nonnegative", conn.getTransmitQueue(), greaterThanOrEqualTo(0));
143 assertThat("Receive queue msut be nonnegative", conn.getReceiveQueue(), greaterThanOrEqualTo(0));
144 assertThat("Connection state must not be null", conn.getState(), is(notNullValue()));
145 assertThat("Owning Process ID must be -1 or higher", conn.getowningProcessId(), greaterThanOrEqualTo(-1));
146 assertThat("Connection toString must not be null", conn.toString(), is(notNullValue()));
147 }
148 }
149 }