Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.dhcprelay.store; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import org.onlab.junit.TestUtils; |
| 20 | import org.onlab.packet.DHCP; |
| 21 | import org.onlab.packet.DHCP6; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.Ip6Address; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.HostId; |
| 28 | import org.onosproject.net.HostLocation; |
| 29 | |
| 30 | import java.util.Optional; |
| 31 | |
| 32 | import static org.hamcrest.Matchers.containsInAnyOrder; |
| 33 | import static org.hamcrest.Matchers.equalTo; |
| 34 | import static org.hamcrest.Matchers.is; |
| 35 | import static org.junit.Assert.assertEquals; |
| 36 | import static org.junit.Assert.assertThat; |
| 37 | |
| 38 | /** |
| 39 | * Unit test for DHCP record. |
| 40 | */ |
| 41 | public 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); |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 89 | TestUtils.setField(record, "addrPrefTime", 0); |
| 90 | TestUtils.setField(record2, "addrPrefTime", 0); |
| 91 | TestUtils.setField(record, "pdPrefTime", 0); |
| 92 | TestUtils.setField(record2, "pdPrefTime", 0); |
| 93 | TestUtils.setField(record, "v6Counter", null); |
| 94 | TestUtils.setField(record2, "v6Counter", null); |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 95 | |
| 96 | assertThat(record, equalTo(record2)); |
| 97 | assertThat(record.hashCode(), equalTo(record2.hashCode())); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Test clone a DHCP record. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testCloneRecord() { |
| 105 | DhcpRecord record = new DhcpRecord(HOST_ID) |
| 106 | .addLocation(HL1) |
| 107 | .addLocation(HL2) |
| 108 | .ip4Address(IP4ADDR) |
| 109 | .nextHop(GW_MAC) |
| 110 | .ip4Status(DHCP.MsgType.DHCPACK) |
| 111 | .ip6Address(IP6ADDR) |
| 112 | .ip6Status(DHCP6.MsgType.REPLY) |
| 113 | .setDirectlyConnected(true); |
| 114 | DhcpRecord clonedRecord = record.clone(); |
| 115 | assertEquals(record, clonedRecord); |
| 116 | } |
| 117 | } |