26
26
import static com .sun .jna .platform .win32 .IPHlpAPI .AF_INET ;
27
27
import static com .sun .jna .platform .win32 .IPHlpAPI .AF_INET6 ;
28
28
import static org .junit .Assert .assertEquals ;
29
+ import static org .junit .Assert .assertNotEquals ;
29
30
import static org .junit .Assert .assertTrue ;
30
31
31
32
import java .math .BigInteger ;
@@ -66,25 +67,18 @@ public void testGetIfEntry() throws SocketException {
66
67
MIB_IFROW ifRow = new MIB_IFROW ();
67
68
ifRow .dwIndex = netint .getIndex ();
68
69
assertEquals (WinError .NO_ERROR , IPHLP .GetIfEntry (ifRow ));
69
- // Bytes should exceed packets
70
70
// These originate from unsigned ints. Use standard Java
71
71
// widening conversion to long which does sign-extension,
72
72
// then drop any copies of the sign bit, to prevent the value
73
73
// being considered a negative one by Java if it is set
74
- long bytesSent = (ifRow .dwOutOctets ) & 0xffffffffL ;
75
- long packetsSent = (ifRow .dwOutUcastPkts ) & 0xffffffffL ;
76
- if (packetsSent > 0 ) {
77
- assertTrue (bytesSent > packetsSent );
78
- } else {
79
- assertEquals (0 , bytesSent );
80
- }
81
- long bytesRecv = (ifRow .dwInOctets ) & 0xffffffffL ;
82
- long packetsRecv = (ifRow .dwInUcastPkts ) & 0xffffffffL ;
83
- if (packetsRecv > 0 ) {
84
- assertTrue (bytesRecv > packetsRecv );
85
- } else {
86
- assertEquals (0 , bytesRecv );
87
- }
74
+ long bytesSent = ifRow .dwOutOctets & 0xffffffffL ;
75
+ long packetsSent = ifRow .dwOutUcastPkts & 0xffffffffL ;
76
+ long bytesRecv = ifRow .dwInOctets & 0xffffffffL ;
77
+ long packetsRecv = ifRow .dwInUcastPkts & 0xffffffffL ;
78
+ // Bytes should match or exceed minimum packet size of 20.
79
+ // It is possible to have bytes not part of a packet but not vice versa.
80
+ assertTrue (bytesSent >= packetsSent * 20L );
81
+ assertTrue (bytesRecv >= packetsRecv * 20L );
88
82
}
89
83
}
90
84
@@ -105,18 +99,11 @@ public void testGetIfEntry2() throws SocketException {
105
99
// These originate from unsigned longs.
106
100
BigInteger bytesSent = new BigInteger (Long .toHexString (ifRow .OutOctets ), 16 );
107
101
BigInteger packetsSent = new BigInteger (Long .toHexString (ifRow .OutUcastPkts ), 16 );
108
- if (packetsSent .longValue () > 0 ) {
109
- assertEquals (1 , bytesSent .compareTo (packetsSent ));
110
- } else {
111
- assertEquals (0 , bytesSent .compareTo (packetsSent ));
112
- }
113
102
BigInteger bytesRecv = new BigInteger (Long .toHexString (ifRow .InOctets ), 16 );
114
103
BigInteger packetsRecv = new BigInteger (Long .toHexString (ifRow .InUcastPkts ), 16 );
115
- if (packetsRecv .longValue () > 0 ) {
116
- assertEquals (1 , bytesRecv .compareTo (packetsRecv ));
117
- } else {
118
- assertEquals (0 , bytesRecv .compareTo (packetsRecv ));
119
- }
104
+ BigInteger minPacketSize = BigInteger .valueOf (20 );
105
+ assertNotEquals (-1 , bytesSent .compareTo (packetsSent .multiply (minPacketSize )));
106
+ assertNotEquals (-1 , bytesRecv .compareTo (packetsRecv .multiply (minPacketSize )));
120
107
}
121
108
}
122
109
} else {
0 commit comments