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