2 * Copyright (c) 2015 Cable Television Laboratories, Inc and others. All rights reserved.
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
8 package org.pcmm.gates.impl;
10 import org.junit.Assert;
11 import org.junit.Test;
13 import java.net.Inet4Address;
14 import java.net.Inet6Address;
15 import java.net.InetAddress;
16 import java.net.UnknownHostException;
19 * Tests the data holder class SubscriberId to ensure both construction and byte parsing result in correct object
22 public class SubscriberIDTest {
24 @Test(expected = NullPointerException.class)
25 public void nullConstArg() {
26 new SubscriberID(null);
30 public void constructorIpv4() {
31 final InetAddress addr;
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");
38 final SubscriberID subId = new SubscriberID(addr);
39 Assert.assertEquals(addr, subId.getSourceIPAddress());
43 public void byteParsingIpv4() {
44 final InetAddress addr;
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");
51 final SubscriberID subId = new SubscriberID(addr);
52 final SubscriberID parsed = SubscriberID.parse(subId.getBytes());
53 Assert.assertEquals(subId, parsed);
57 public void constructorIpv6() {
58 final InetAddress addr;
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");
65 final SubscriberID subId = new SubscriberID(addr);
66 Assert.assertEquals(addr, subId.getSourceIPAddress());
70 public void byteParsingIpv6() {
71 final InetAddress addr;
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");
78 final SubscriberID subId = new SubscriberID(addr);
79 final SubscriberID parsed = SubscriberID.parse(subId.getBytes());
80 Assert.assertEquals(subId, parsed);