| 1 |
|
| 2 |
package jnr.netdb; |
| 3 |
|
| 4 |
import org.junit.After; |
| 5 |
import org.junit.AfterClass; |
| 6 |
import org.junit.Before; |
| 7 |
import org.junit.BeforeClass; |
| 8 |
import org.junit.Test; |
| 9 |
import static org.junit.Assert.*; |
| 10 |
|
| 11 |
/** |
| 12 |
* |
| 13 |
*/ |
| 14 |
public class NativeProtocolsDBTest { |
| 15 |
|
| 16 |
public NativeProtocolsDBTest() { |
| 17 |
} |
| 18 |
|
| 19 |
@BeforeClass |
| 20 |
public static void setUpClass() throws Exception { |
| 21 |
} |
| 22 |
|
| 23 |
@AfterClass |
| 24 |
public static void tearDownClass() throws Exception { |
| 25 |
} |
| 26 |
|
| 27 |
@Before |
| 28 |
public void setUp() { |
| 29 |
} |
| 30 |
|
| 31 |
@After |
| 32 |
public void tearDown() { |
| 33 |
} |
| 34 |
|
| 35 |
@Test public void canLookupIpProtocolByName() { |
| 36 |
ProtocolsDB db = NativeProtocolsDB.getInstance(); |
| 37 |
// we try ip first and then ipv4 due to jnr/jnr-netdb#4 |
| 38 |
Protocol p = Protocol.getProtocolByName("ip"); |
| 39 |
if (p != null) { |
| 40 |
assertEquals("incorrect proto number", 0, p.getProto()); |
| 41 |
assertEquals("incorrect name", "ip", p.getName()); |
| 42 |
} else { |
| 43 |
p = Protocol.getProtocolByName("ipv4"); |
| 44 |
assertNotNull("could not lookup ipv4 protocol", p); |
| 45 |
assertEquals("incorrect proto number", 4, p.getProto()); |
| 46 |
assertEquals("incorrect name", "ipv4", p.getName()); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
@Test public void canLookupIpProtocolByNumber() { |
| 51 |
ProtocolsDB db = NativeProtocolsDB.getInstance(); |
| 52 |
// we try ip first and then ipv4 due to jnr/jnr-netdb#4 |
| 53 |
Protocol p = Protocol.getProtocolByName("ip"); |
| 54 |
if (p != null) { |
| 55 |
p = Protocol.getProtocolByNumber(0); |
| 56 |
assertEquals("incorrect proto number", 0, p.getProto()); |
| 57 |
assertEquals("incorrect name", "ip", p.getName()); |
| 58 |
} else { |
| 59 |
p = Protocol.getProtocolByNumber(4); |
| 60 |
assertNotNull("could not lookup ip protocol", p); |
| 61 |
assertEquals("incorrect proto number", 4, p.getProto()); |
| 62 |
assertEquals("incorrect name", "ipv4", p.getName()); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
@Test public void canLookupTcpProtocolByName() { |
| 67 |
ProtocolsDB db = NativeProtocolsDB.getInstance(); |
| 68 |
Protocol p = db.getProtocolByName("tcp"); |
| 69 |
assertNotNull("could not lookup tcp protocol", p); |
| 70 |
assertEquals("incorrect proto number", 6, p.getProto()); |
| 71 |
assertEquals("incorrect name", "tcp", p.getName()); |
| 72 |
} |
| 73 |
|
| 74 |
@Test public void canLookupTcpProtocolByNumber() { |
| 75 |
ProtocolsDB db = NativeProtocolsDB.getInstance(); |
| 76 |
Protocol p = db.getProtocolByNumber(6); |
| 77 |
assertNotNull("could not lookup tcp protocol", p); |
| 78 |
assertEquals("incorrect proto number", 6, p.getProto()); |
| 79 |
assertEquals("incorrect name", "tcp", p.getName()); |
| 80 |
} |
| 81 |
|
| 82 |
@Test public void getAllProtocolsReturnsNonEmptyList() { |
| 83 |
ProtocolsDB db = NativeProtocolsDB.getInstance(); |
| 84 |
assertFalse(db.getAllProtocols().isEmpty()); |
| 85 |
} |
| 86 |
|
| 87 |
@Test public void getAllProtocolsContainsTcp() { |
| 88 |
ProtocolsDB db = NativeProtocolsDB.getInstance(); |
| 89 |
boolean tcpFound = false; |
| 90 |
for (Protocol p : db.getAllProtocols()) { |
| 91 |
if (p.getName().equals("tcp") || p.getAliases().contains("TCP")) { |
| 92 |
tcpFound = true; |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
assertTrue(tcpFound); |
| 97 |
} |
| 98 |
} |