blob: b90e770ab1027ac9591c21e0c752fa2657e82f18 [file] [log] [blame]
Jian Li9b199162019-02-10 18:00:35 +09001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16package org.onosproject.k8snetworking.util;
17
18import org.junit.Test;
19import org.onlab.packet.IpAddress;
20
21import java.util.Set;
22
23import static junit.framework.TestCase.assertEquals;
Jian Li3bc3d5b2020-09-23 16:46:12 +090024import static junit.framework.TestCase.assertFalse;
25import static junit.framework.TestCase.assertTrue;
26import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.existingContainerPortByMac;
Jian Li9b199162019-02-10 18:00:35 +090027import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getSubnetIps;
28
29/**
30 * Unit tests for kubernetes networking utils.
31 */
32public final class K8sNetworkUtilTest {
33
34 /**
35 * Tests the getSubnetIps method.
36 */
37 @Test
38 public void testGetSubnetIps() {
39 String bClassCidr = "10.10.0.0/16";
40 Set<IpAddress> bClassIps = getSubnetIps(bClassCidr);
Jian Li7970b712019-05-03 20:58:21 +090041 assertEquals(((Double) Math.pow(2, 16)).intValue() - 4, bClassIps.size());
Jian Li9b199162019-02-10 18:00:35 +090042
43 String cClassCidr = "10.10.10.0/24";
44 Set<IpAddress> cClassIps = getSubnetIps(cClassCidr);
Jian Li7970b712019-05-03 20:58:21 +090045 assertEquals(((Double) Math.pow(2, 8)).intValue() - 4, cClassIps.size());
Jian Li9b199162019-02-10 18:00:35 +090046
47 String dClassCidr = "10.10.10.10/32";
48 Set<IpAddress> dClassIps = getSubnetIps(dClassCidr);
Jian Li7970b712019-05-03 20:58:21 +090049 assertEquals(0, dClassIps.size());
Jian Li9b199162019-02-10 18:00:35 +090050 }
Jian Li3bc3d5b2020-09-23 16:46:12 +090051
52 /**
53 * Tests the existing container port by MAC.
54 */
55 @Test
56 public void testExistingContainerPortByMac() {
57 String sourceMacStr = "fe:85:5a:d8:68:1d";
58 String comparedMacStr = "8A:85:5A:D8:68:1D";
59
60 boolean result1 = existingContainerPortByMac(sourceMacStr, comparedMacStr);
61 boolean result2 = existingContainerPortByMac(comparedMacStr, sourceMacStr);
62
63 assertTrue(result1);
64 assertTrue(result2);
65
66 String wrongMacStr = "8A:85:5A:D8:68:1F";
67 boolean result3 = existingContainerPortByMac(sourceMacStr, wrongMacStr);
68 boolean result4 = existingContainerPortByMac(wrongMacStr, sourceMacStr);
69
70 assertFalse(result3);
71 assertFalse(result4);
72 }
Jian Li9b199162019-02-10 18:00:35 +090073}