Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * jndn-management |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 16 | import com.intel.jndn.management.enums.Strategies; |
Davide Pesavento | df17955 | 2020-11-07 02:00:19 -0500 | [diff] [blame^] | 17 | import com.intel.jndn.management.types.ForwarderStatus; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 18 | import com.intel.jndn.management.types.RibEntry; |
| 19 | import com.intel.jndn.management.types.StrategyChoice; |
| 20 | import com.intel.jndn.mock.MockKeyChain; |
| 21 | import net.named_data.jndn.Face; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 22 | import net.named_data.jndn.Name; |
Davide Pesavento | dd1fe9f | 2020-11-03 20:48:24 -0500 | [diff] [blame] | 23 | import net.named_data.jndn.encoding.Tlv0_3WireFormat; |
| 24 | import net.named_data.jndn.encoding.WireFormat; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 25 | import net.named_data.jndn.security.KeyChain; |
| 26 | import net.named_data.jndn.security.SecurityException; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 28 | import java.util.List; |
| 29 | import java.util.Random; |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 30 | |
Davide Pesavento | df17955 | 2020-11-07 02:00:19 -0500 | [diff] [blame^] | 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 33 | import static org.junit.Assert.assertEquals; |
| 34 | import static org.junit.Assert.assertFalse; |
Davide Pesavento | dd1fe9f | 2020-11-03 20:48:24 -0500 | [diff] [blame] | 35 | import static org.junit.Assert.assertThrows; |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 36 | import static org.junit.Assert.assertTrue; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 39 | * Testing Nfdc with real NFD instance (NFD must be run locally while executing the test). |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 40 | * |
| 41 | * @author Andrew Brown <andrew.brown@intel.com> |
| 42 | */ |
| 43 | public class NfdcIT { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 44 | private Face face; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 45 | private Face noKeyChainFace; |
| 46 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 47 | @Before |
| 48 | public void setUp() throws SecurityException { |
Davide Pesavento | dd1fe9f | 2020-11-03 20:48:24 -0500 | [diff] [blame] | 49 | WireFormat.setDefaultWireFormat(Tlv0_3WireFormat.get()); |
| 50 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 51 | face = new Face("localhost"); |
| 52 | KeyChain keyChain = MockKeyChain.configure(new Name("/tmp/identity")); |
| 53 | face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName()); |
Davide Pesavento | df17955 | 2020-11-07 02:00:19 -0500 | [diff] [blame^] | 54 | noKeyChainFace = new Face("localhost"); // don't set command signing info |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testGetForwarderStatus() throws Exception { |
Davide Pesavento | df17955 | 2020-11-07 02:00:19 -0500 | [diff] [blame^] | 59 | ForwarderStatus status = Nfdc.getForwarderStatus(face); |
| 60 | assertTrue(status.getStartTimestamp() > 0); |
| 61 | assertTrue(status.getCurrentTimestamp() > 0); |
| 62 | assertTrue(status.getNFibEntries() > 0); |
| 63 | assertTrue(status.getNInInterests() > 0); |
| 64 | assertTrue(status.getNOutData() > 0); |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testGetChannelStatusList() throws Exception { |
| 69 | assertFalse(Nfdc.getChannelStatusList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testGetFaceList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 74 | assertFalse(Nfdc.getFaceList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testGetFibList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 79 | assertFalse(Nfdc.getFibList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testGetRouteList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 84 | assertFalse(Nfdc.getRouteList(face).isEmpty()); |
| 85 | } |
| 86 | |
| 87 | @Test |
Davide Pesavento | df17955 | 2020-11-07 02:00:19 -0500 | [diff] [blame^] | 88 | public void testRoutes() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 89 | Nfdc.register(face, new Name("/my/route/to/app/face"), 333); |
| 90 | int faceId = Nfdc.createFace(face, "udp4://127.0.0.1:56363"); |
| 91 | Nfdc.register(face, "udp4://127.0.0.1:56363", new Name("/my/test/route"), 999); |
| 92 | Nfdc.register(face, faceId, new Name("/"), 555); |
| 93 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 94 | Thread.sleep(1000); // NFD registers the route asynchronously |
| 95 | |
Davide Pesavento | df17955 | 2020-11-07 02:00:19 -0500 | [diff] [blame^] | 96 | // check that route is created |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 97 | boolean found = false; |
| 98 | for (RibEntry route : Nfdc.getRouteList(face)) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 99 | if (route.getName().equals(new Name("/my/test/route"))) { |
| 100 | found = true; |
| 101 | } |
| 102 | } |
| 103 | assertTrue(found); |
| 104 | |
| 105 | Nfdc.unregister(face, new Name("/my/route/to/app/face")); |
| 106 | |
| 107 | // remove the route |
| 108 | Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363"); |
| 109 | |
| 110 | // remove face |
| 111 | Nfdc.destroyFace(face, faceId); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 112 | Thread.sleep(1000); // wait for face to be destroyed |
| 113 | |
Davide Pesavento | dd1fe9f | 2020-11-03 20:48:24 -0500 | [diff] [blame] | 114 | Exception exception = assertThrows(ManagementException.class, () -> { |
| 115 | Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363"); |
| 116 | }); |
| 117 | assertEquals("Face not found: udp4://127.0.0.1:56363", exception.getMessage()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Davide Pesavento | 6346313 | 2020-11-03 20:37:23 -0500 | [diff] [blame] | 120 | @Test |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 121 | public void testStrategies() throws Exception { |
Davide Pesavento | dd1fe9f | 2020-11-03 20:48:24 -0500 | [diff] [blame] | 122 | Name prefix = new Name("/test/strategy").append("random" + new Random().nextInt()); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 123 | List<StrategyChoice> choices = Nfdc.getStrategyList(face); |
Davide Pesavento | df17955 | 2020-11-07 02:00:19 -0500 | [diff] [blame^] | 124 | assertFalse(choices.isEmpty()); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 125 | int oldSize = choices.size(); |
| 126 | |
Davide Pesavento | 6346313 | 2020-11-03 20:37:23 -0500 | [diff] [blame] | 127 | Nfdc.setStrategy(face, prefix, Strategies.RANDOM); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 128 | Thread.sleep(1000); // strategy takes a while to register |
| 129 | |
| 130 | choices = Nfdc.getStrategyList(face); |
| 131 | assertEquals(oldSize + 1, choices.size()); |
| 132 | |
| 133 | Nfdc.unsetStrategy(face, prefix); |
Davide Pesavento | dd1fe9f | 2020-11-03 20:48:24 -0500 | [diff] [blame] | 134 | Thread.sleep(1000); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 135 | |
Davide Pesavento | dd1fe9f | 2020-11-03 20:48:24 -0500 | [diff] [blame] | 136 | choices = Nfdc.getStrategyList(face); |
| 137 | assertEquals(oldSize, choices.size()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testFailOfSetStrategyWithoutKeychain() throws Exception { |
Davide Pesavento | dd1fe9f | 2020-11-03 20:48:24 -0500 | [diff] [blame] | 142 | assertThrows(NullPointerException.class, () -> { |
| 143 | Nfdc.setStrategy(noKeyChainFace, new Name("/test"), Strategies.BEST_ROUTE); |
| 144 | }); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 145 | } |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 146 | } |