View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.greaterThan;
9   import static org.hamcrest.Matchers.greaterThanOrEqualTo;
10  import static org.hamcrest.Matchers.is;
11  import static org.hamcrest.Matchers.notNullValue;
12  
13  import java.io.IOException;
14  
15  import org.junit.jupiter.api.Test;
16  
17  import oshi.SystemInfo;
18  
19  /**
20   * Test Networks
21   */
22  class NetworksTest {
23  
24      /**
25       * Test inet network interfaces extraction.
26       *
27       * @throws IOException Signals that an I/O exception has occurred.
28       */
29      @Test
30      void testAllNetworkInterfaces() throws IOException {
31          SystemInfo si = new SystemInfo();
32  
33          for (NetworkIF net : si.getHardware().getNetworkIFs(true)) {
34              assertThat("NetworkIF should not be null", net.queryNetworkInterface(), is(notNullValue()));
35              assertThat("NetworkIF name should not be null", net.getName(), is(notNullValue()));
36              assertThat("NetworkIF display name should not be null", net.getDisplayName(), is(notNullValue()));
37              assertThat("NetworkIF ifAlias should not be null", net.getIfAlias(), is(notNullValue()));
38              assertThat("NetworkIF ifOperStatus should not be null", net.getIfOperStatus(), is(notNullValue()));
39              assertThat("NetworkIF IPv4 address should not be null", net.getIPv4addr(), is(notNullValue()));
40              assertThat("NetworkIF SubnetMasks should not be null", net.getSubnetMasks(), is(notNullValue()));
41              assertThat("NetworkIF IPv6 should not be null", net.getIPv6addr(), is(notNullValue()));
42              assertThat("NetworkIF prefix lengths should not be null", net.getPrefixLengths(), is(notNullValue()));
43              assertThat("NetworkIF type should not be negative", net.getIfType(), is(greaterThanOrEqualTo(0)));
44              assertThat("NetworkIF NDisPhysicalMediumType should not be negative", net.getNdisPhysicalMediumType(),
45                      is(greaterThanOrEqualTo(0)));
46              assertThat("NetworkIF bytes received should not be negative", net.getBytesRecv(),
47                      is(greaterThanOrEqualTo(0L)));
48              assertThat("NetworkIF bytes sent should not be negative", net.getBytesSent(), is(greaterThanOrEqualTo(0L)));
49              assertThat("NetworkIF packets received should not be negative", net.getPacketsRecv(),
50                      is(greaterThanOrEqualTo(0L)));
51              assertThat("NetworkIF packets sent should not be negative", net.getPacketsSent(),
52                      is(greaterThanOrEqualTo(0L)));
53              assertThat("NetworkIF InErrors should not be negative", net.getInErrors(), is(greaterThanOrEqualTo(0L)));
54              assertThat("NetworkIF out errors should not be negative", net.getOutErrors(), is(greaterThanOrEqualTo(0L)));
55              assertThat("NetworkIF in drops should not be negative", net.getInDrops(), is(greaterThanOrEqualTo(0L)));
56              assertThat("NetworkIF collisions should not be negative", net.getCollisions(),
57                      is(greaterThanOrEqualTo(0L)));
58              assertThat("NetworkIF speed should not be negative", net.getSpeed(), is(greaterThanOrEqualTo(0L)));
59              assertThat("NetworkIF time stamp should be positive", net.getTimeStamp(), is(greaterThan(0L)));
60              // MTU can be negative for non-local so test separately
61              testUpdateAttributes(net);
62  
63              if (net.getMacaddr().startsWith("00:00:00") || net.getMacaddr().length() < 8) {
64                  assertThat("Invalid MAC address corresponds to a known virtual machine", net.isKnownVmMacAddr(),
65                          is(false));
66              }
67  
68              assertThat("NetworkIF.toString() should not be null", net.toString(), is(notNullValue()));
69          }
70      }
71  
72      private void testUpdateAttributes(NetworkIF net) {
73          net.updateAttributes();
74          assertThat("NetworkIF bytes received after update attr should not be negative", net.getBytesRecv(),
75                  is(greaterThanOrEqualTo(0L)));
76          assertThat("NetworkIF bytes sent after update attr should not be negative", net.getBytesSent(),
77                  is(greaterThanOrEqualTo(0L)));
78          assertThat("NetworkIF packets received after update attr should not be negative", net.getPacketsRecv(),
79                  is(greaterThanOrEqualTo(0L)));
80          assertThat("NetworkIF packets sent after update attr should not be negative", net.getPacketsSent(),
81                  is(greaterThanOrEqualTo(0L)));
82          assertThat("NetworkIF in errors after update attr should not be negative", net.getInErrors(),
83                  is(greaterThanOrEqualTo(0L)));
84          assertThat("NetworkIF out errors after update attr should not be negative", net.getOutErrors(),
85                  is(greaterThanOrEqualTo(0L)));
86          assertThat("NetworkIF in drops after update attr should not be negative", net.getInDrops(),
87                  is(greaterThanOrEqualTo(0L)));
88          assertThat("NetworkIF collisions after update attr should not be negative", net.getCollisions(),
89                  is(greaterThanOrEqualTo(0L)));
90          assertThat("NetworkIF MTU should not be negative", net.getMTU(), is(greaterThanOrEqualTo(0L)));
91          assertThat("NetworkIF speed after update attr should not be negative", net.getSpeed(),
92                  is(greaterThanOrEqualTo(0L)));
93          assertThat("NetworkIF time stamp after update attr should not be negative", net.getTimeStamp(),
94                  is(greaterThan(0L)));
95      }
96  
97      /**
98       * Test all network interfaces extraction.
99       *
100      * @throws IOException Signals that an I/O exception has occurred.
101      */
102     @Test
103     void testNonLocalNetworkInterfaces() throws IOException {
104         SystemInfo si = new SystemInfo();
105 
106         for (NetworkIF net : si.getHardware().getNetworkIFs(false)) {
107             assertThat("NetworkIF should not be null", net.queryNetworkInterface(), is(notNullValue()));
108 
109             assertThat("Network interface is not localhost " + net.getDisplayName(),
110                     net.queryNetworkInterface().isLoopback(), is(false));
111             assertThat("Network interface has a hardware address", net.queryNetworkInterface().getHardwareAddress(),
112                     is(notNullValue()));
113 
114             assertThat("NetworkIF MacAddress should not be null", net.getMacaddr(), is(notNullValue()));
115         }
116     }
117 }