blob: ec6ad5ca82106d89035ff125059cf7d39b454a59 [file] [log] [blame]
Yi Tsengb8e19f12017-06-07 15:47:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengb8e19f12017-06-07 15:47:23 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.dhcprelay.store;
17
18import org.junit.Test;
19import org.onlab.junit.TestUtils;
20import org.onlab.packet.DHCP;
21import org.onlab.packet.DHCP6;
22import org.onlab.packet.Ip4Address;
23import org.onlab.packet.Ip6Address;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.HostId;
28import org.onosproject.net.HostLocation;
29
30import java.util.Optional;
31
32import static org.hamcrest.Matchers.containsInAnyOrder;
33import static org.hamcrest.Matchers.equalTo;
34import static org.hamcrest.Matchers.is;
35import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertThat;
37
38/**
39 * Unit test for DHCP record.
40 */
41public class DhcpRecordTest {
42 private static final MacAddress MAC = MacAddress.valueOf("1a:1a:1a:1a:1a:1a");
43 private static final VlanId VLAN = VlanId.vlanId("100");
44 private static final HostId HOST_ID = HostId.hostId(MAC, VLAN);
45 private static final HostLocation HL1 =
46 new HostLocation(ConnectPoint.deviceConnectPoint("of:0000000000000001/1"), 0);
47 private static final HostLocation HL2 =
48 new HostLocation(ConnectPoint.deviceConnectPoint("of:0000000000000001/2"), 0);
49 private static final Ip4Address IP4ADDR = Ip4Address.valueOf("10.0.2.1");
50 private static final MacAddress GW_MAC = MacAddress.valueOf("00:00:00:00:04:01");
51 private static final Ip6Address IP6ADDR = Ip6Address.valueOf("2001::1");
52
53 /**
54 * Test creating a DHCP relay record.
55 */
56 @Test
57 public void testCreateRecord() {
58 DhcpRecord record = new DhcpRecord(HOST_ID)
59 .addLocation(HL1)
60 .addLocation(HL2)
61 .ip4Address(IP4ADDR)
62 .nextHop(GW_MAC)
63 .ip4Status(DHCP.MsgType.DHCPACK)
64 .ip6Address(IP6ADDR)
65 .ip6Status(DHCP6.MsgType.REPLY)
66 .setDirectlyConnected(true);
67
68 assertThat(record.locations().size(), is(2));
69 assertThat(record.locations(), containsInAnyOrder(HL1, HL2));
70 assertThat(record.ip4Address(), is(Optional.of(IP4ADDR)));
71 assertThat(record.nextHop(), is(Optional.of(GW_MAC)));
72 assertThat(record.ip4Status(), is(Optional.of(DHCP.MsgType.DHCPACK)));
73 assertThat(record.ip6Address(), is(Optional.of(IP6ADDR)));
74 assertThat(record.ip6Status(), is(Optional.of(DHCP6.MsgType.REPLY)));
75 assertThat(record.directlyConnected(), is(true));
76
77 DhcpRecord record2 = new DhcpRecord(HOST_ID)
78 .nextHop(GW_MAC)
79 .addLocation(HL2)
80 .ip6Address(IP6ADDR)
81 .addLocation(HL1)
82 .ip6Status(DHCP6.MsgType.REPLY)
83 .ip4Address(IP4ADDR)
84 .ip4Status(DHCP.MsgType.DHCPACK)
85 .setDirectlyConnected(true);
86
87 TestUtils.setField(record, "lastSeen", 0);
88 TestUtils.setField(record2, "lastSeen", 0);
89
90 assertThat(record, equalTo(record2));
91 assertThat(record.hashCode(), equalTo(record2.hashCode()));
92 }
93
94 /**
95 * Test clone a DHCP record.
96 */
97 @Test
98 public void testCloneRecord() {
99 DhcpRecord record = new DhcpRecord(HOST_ID)
100 .addLocation(HL1)
101 .addLocation(HL2)
102 .ip4Address(IP4ADDR)
103 .nextHop(GW_MAC)
104 .ip4Status(DHCP.MsgType.DHCPACK)
105 .ip6Address(IP6ADDR)
106 .ip6Status(DHCP6.MsgType.REPLY)
107 .setDirectlyConnected(true);
108 DhcpRecord clonedRecord = record.clone();
109 assertEquals(record, clonedRecord);
110 }
111}