Upgrade ietf-{inet,yang}-types to 2013-07-15
[packetcable.git] / packetcable-driver / src / test / java / org / pcmm / gates / impl / SubscriberIDTest.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.gates.impl;
6
7 import org.junit.Assert;
8 import org.junit.Test;
9
10 import java.net.Inet4Address;
11 import java.net.Inet6Address;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14
15 /**
16  * Tests the data holder class SubscriberId to ensure both construction and byte parsing result in correct object
17  * creation.
18  */
19 public class SubscriberIDTest {
20
21     @Test(expected = NullPointerException.class)
22     public void nullConstArg() {
23         new SubscriberID(null);
24     }
25
26     @Test
27     public void constructorIpv4() {
28         final InetAddress addr;
29         try {
30             addr = InetAddress.getByName("127.0.0.1");
31             if (addr instanceof Inet6Address) Assert.fail("Address should be IPv4");
32         } catch (UnknownHostException e) {
33             throw new RuntimeException("host not found");
34         }
35         final SubscriberID subId = new SubscriberID(addr);
36         Assert.assertEquals(addr, subId.getSourceIPAddress());
37     }
38
39     @Test
40     public void byteParsingIpv4() {
41         final InetAddress addr;
42         try {
43             addr = InetAddress.getByName("127.0.0.1");
44             if (addr instanceof Inet6Address) Assert.fail("Address should be IPv4");
45         } catch (UnknownHostException e) {
46             throw new RuntimeException("host not found");
47         }
48         final SubscriberID subId = new SubscriberID(addr);
49         final SubscriberID parsed = SubscriberID.parse(subId.getBytes());
50         Assert.assertEquals(subId, parsed);
51     }
52
53     @Test
54     public void constructorIpv6() {
55         final InetAddress addr;
56         try {
57             addr = InetAddress.getByName("00:00:00:00:00:00:00:01");
58             if (addr instanceof Inet4Address) Assert.fail("Address should be IPv6");
59         } catch (UnknownHostException e) {
60             throw new RuntimeException("host not found");
61         }
62         final SubscriberID subId = new SubscriberID(addr);
63         Assert.assertEquals(addr, subId.getSourceIPAddress());
64     }
65
66     @Test
67     public void byteParsingIpv6() {
68         final InetAddress addr;
69         try {
70             addr = InetAddress.getByName("00:00:00:00:00:00:00:01");
71             if (addr instanceof Inet4Address) Assert.fail("Address should be IPv6");
72         } catch (UnknownHostException e) {
73             throw new RuntimeException("host not found");
74         }
75         final SubscriberID subId = new SubscriberID(addr);
76         final SubscriberID parsed = SubscriberID.parse(subId.getBytes());
77         Assert.assertEquals(subId, parsed);
78     }
79
80 }