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; |
| 17 | import com.intel.jndn.management.types.RibEntry; |
| 18 | import com.intel.jndn.management.types.StrategyChoice; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 19 | import com.intel.jndn.mock.MockFace; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 20 | import com.intel.jndn.mock.MockKeyChain; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 21 | import net.named_data.jndn.ControlResponse; |
| 22 | import net.named_data.jndn.Data; |
| 23 | import net.named_data.jndn.DigestSha256Signature; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 24 | import net.named_data.jndn.Face; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 25 | import net.named_data.jndn.Interest; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 26 | import net.named_data.jndn.KeyLocator; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 27 | import net.named_data.jndn.MetaInfo; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 28 | import net.named_data.jndn.Name; |
| 29 | import net.named_data.jndn.encoding.EncodingException; |
| 30 | import net.named_data.jndn.security.KeyChain; |
| 31 | import net.named_data.jndn.security.SecurityException; |
| 32 | import org.junit.Before; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 33 | import org.junit.Rule; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 34 | import org.junit.Test; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 35 | import org.junit.rules.ExpectedException; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 37 | import java.io.IOException; |
| 38 | import java.util.List; |
| 39 | import java.util.Random; |
| 40 | import java.util.logging.Logger; |
| 41 | |
| 42 | import static org.junit.Assert.assertEquals; |
| 43 | import static org.junit.Assert.assertFalse; |
| 44 | import static org.junit.Assert.assertNotNull; |
| 45 | import static org.junit.Assert.assertTrue; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 48 | * 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] | 49 | * |
| 50 | * @author Andrew Brown <andrew.brown@intel.com> |
| 51 | */ |
| 52 | public class NfdcIT { |
| 53 | private static final Logger LOG = Logger.getLogger(NfdcIT.class.getName()); |
| 54 | private Face face; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 55 | private MockFace mockFace; |
| 56 | private Face noKeyChainFace; |
| 57 | |
| 58 | @Rule |
| 59 | public final ExpectedException exception = ExpectedException.none(); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 60 | |
| 61 | @Before |
| 62 | public void setUp() throws SecurityException { |
| 63 | face = new Face("localhost"); |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame] | 64 | mockFace = new MockFace(new MockFace.Options().setEnablePacketLogging(false).setEnableRegistrationReply(false)); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 65 | noKeyChainFace = new Face("localhost"); // don't set command signing info |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 66 | KeyChain keyChain = MockKeyChain.configure(new Name("/tmp/identity")); |
| 67 | face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName()); |
| 68 | } |
| 69 | |
| 70 | @Test |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 71 | public void testGetKeyLocator() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 72 | KeyLocator keyLocator = Nfdc.getKeyLocator(face); |
| 73 | assertNotNull(keyLocator); |
| 74 | LOG.info("Connected to NFD with key locator: " + keyLocator.getKeyName().toUri()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 75 | |
| 76 | exception.expect(ManagementException.class); |
| 77 | Nfdc.getKeyLocator(mockFace); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 81 | public void testFailOfGetKeyLocator() throws Exception { |
| 82 | mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() { |
| 83 | @Override |
andrewsbrown | 6df23ad | 2016-04-22 14:24:12 -0700 | [diff] [blame] | 84 | public void emit(final Interest interest) { |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 85 | Data data = new Data(); |
| 86 | data.setName(new Name(interest.getName()).appendVersion(0).appendSegment(0)); |
| 87 | |
| 88 | MetaInfo meta = new MetaInfo(); |
| 89 | meta.setFinalBlockId(data.getName().get(-1)); |
| 90 | data.setMetaInfo(meta); |
| 91 | |
| 92 | data.setSignature(new DigestSha256Signature()); |
| 93 | |
| 94 | LOG.info(data.getSignature().toString()); |
| 95 | |
| 96 | // don't set anything else |
andrewsbrown | 6df23ad | 2016-04-22 14:24:12 -0700 | [diff] [blame] | 97 | try { |
| 98 | mockFace.receive(data); |
| 99 | } catch (EncodingException e) { |
| 100 | LOG.severe("Failed to set receive data: " + e); |
| 101 | } |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 102 | } |
| 103 | }); |
| 104 | |
| 105 | exception.expect(ManagementException.class); |
| 106 | exception.expectMessage("No key locator available."); |
| 107 | Nfdc.getKeyLocator(mockFace); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testGetForwarderStatus() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 112 | assertTrue(Nfdc.getForwarderStatus(face).getStartTimestamp() > 0); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 113 | |
| 114 | exception.expect(ManagementException.class); |
| 115 | Nfdc.getForwarderStatus(mockFace); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testGetFaceList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 120 | assertFalse(Nfdc.getFaceList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 121 | |
| 122 | exception.expect(ManagementException.class); |
| 123 | Nfdc.getFaceList(mockFace); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testGetFibList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 128 | assertFalse(Nfdc.getFibList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 129 | |
| 130 | exception.expect(ManagementException.class); |
| 131 | Nfdc.getFibList(mockFace); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testGetRouteList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 136 | assertFalse(Nfdc.getRouteList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 137 | |
| 138 | exception.expect(ManagementException.class); |
| 139 | Nfdc.getRouteList(mockFace); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testRoutes() throws EncodingException, IOException, ManagementException, InterruptedException { |
| 144 | Nfdc.register(face, new Name("/my/route/to/app/face"), 333); |
| 145 | int faceId = Nfdc.createFace(face, "udp4://127.0.0.1:56363"); |
| 146 | Nfdc.register(face, "udp4://127.0.0.1:56363", new Name("/my/test/route"), 999); |
| 147 | Nfdc.register(face, faceId, new Name("/"), 555); |
| 148 | |
| 149 | // check that route is created |
| 150 | Thread.sleep(1000); // NFD registers the route asynchronously |
| 151 | |
| 152 | boolean found = false; |
| 153 | for (RibEntry route : Nfdc.getRouteList(face)) { |
| 154 | LOG.info("Found route: " + route.getName().toUri()); |
| 155 | if (route.getName().equals(new Name("/my/test/route"))) { |
| 156 | found = true; |
| 157 | } |
| 158 | } |
| 159 | assertTrue(found); |
| 160 | |
| 161 | Nfdc.unregister(face, new Name("/my/route/to/app/face")); |
| 162 | |
| 163 | // remove the route |
| 164 | Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363"); |
| 165 | |
| 166 | // remove face |
| 167 | Nfdc.destroyFace(face, faceId); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 168 | |
| 169 | Thread.sleep(1000); // wait for face to be destroyed |
| 170 | |
| 171 | exception.expect(ManagementException.class); |
| 172 | exception.expectMessage("Face not found: udp4://127.0.0.1:56363"); |
| 173 | Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363"); |
| 174 | } |
| 175 | |
Davide Pesavento | 6346313 | 2020-11-03 20:37:23 -0500 | [diff] [blame^] | 176 | @Test |
| 177 | public void testFailOfRegister() throws Exception { |
| 178 | exception.expect(ManagementException.class); |
| 179 | Nfdc.register(mockFace, new Name("/my/route/to/app/face"), 333); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testFailOfUnregister() throws Exception { |
| 184 | exception.expect(ManagementException.class); |
| 185 | Nfdc.unregister(mockFace, new Name("/my/route/to/app/face")); |
| 186 | } |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 187 | |
| 188 | @Test |
| 189 | public void testFailOfCreateFace() throws Exception { |
| 190 | exception.expect(ManagementException.class); |
| 191 | Nfdc.createFace(mockFace, "udp4://127.0.0.1:56363"); |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void testFailOfDestroyFace() throws Exception { |
| 196 | exception.expect(ManagementException.class); |
| 197 | Nfdc.destroyFace(mockFace, 1); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void testStrategies() throws Exception { |
| 202 | Name prefix = new Name("/test/strategy").append("random:" + new Random().nextInt()); |
| 203 | |
| 204 | List<StrategyChoice> choices = Nfdc.getStrategyList(face); |
| 205 | int oldSize = choices.size(); |
| 206 | |
Davide Pesavento | 6346313 | 2020-11-03 20:37:23 -0500 | [diff] [blame^] | 207 | Nfdc.setStrategy(face, prefix, Strategies.RANDOM); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 208 | Thread.sleep(1000); // strategy takes a while to register |
| 209 | |
| 210 | choices = Nfdc.getStrategyList(face); |
| 211 | assertEquals(oldSize + 1, choices.size()); |
| 212 | |
| 213 | Nfdc.unsetStrategy(face, prefix); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 214 | |
| 215 | exception.expect(ManagementException.class); |
| 216 | Nfdc.getStrategyList(mockFace); |
| 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void testFailOfUnsetStrategy() throws Exception { |
| 221 | exception.expect(ManagementException.class); |
| 222 | Nfdc.unsetStrategy(mockFace, new Name("/")); |
| 223 | } |
| 224 | |
| 225 | @Test |
| 226 | public void testFailOfSetStrategyWithoutKeychain() throws Exception { |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame] | 227 | exception.expect(NullPointerException.class); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 228 | Nfdc.setStrategy(noKeyChainFace, new Name("/test"), Strategies.BEST_ROUTE); |
| 229 | } |
| 230 | |
| 231 | @Test |
| 232 | public void testFailOfSetStrategyWithNon200Code() throws Exception { |
| 233 | exception.expect(ManagementException.class); |
| 234 | exception.expectMessage("Action failed, forwarder returned: 300 Test FAIL"); |
| 235 | |
| 236 | mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() { |
| 237 | @Override |
andrewsbrown | 6df23ad | 2016-04-22 14:24:12 -0700 | [diff] [blame] | 238 | public void emit(final Interest interest) { |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 239 | ControlResponse response = new ControlResponse(); |
| 240 | response.setStatusCode(300); |
| 241 | response.setStatusText("Test FAIL"); |
| 242 | |
| 243 | Data data = new Data(); |
| 244 | data.setName(interest.getName()); |
| 245 | data.setContent(response.wireEncode()); |
| 246 | |
andrewsbrown | 6df23ad | 2016-04-22 14:24:12 -0700 | [diff] [blame] | 247 | try { |
| 248 | mockFace.receive(data); |
| 249 | } catch (EncodingException e) { |
| 250 | LOG.severe("Failed to set receive data: " + e); |
| 251 | } |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 252 | } |
| 253 | }); |
Davide Pesavento | 6346313 | 2020-11-03 20:37:23 -0500 | [diff] [blame^] | 254 | Nfdc.setStrategy(mockFace, new Name("/"), Strategies.MULTICAST); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Alexander Afanasyev | 7ac3e39 | 2016-02-19 23:21:01 -0800 | [diff] [blame] | 257 | @Test |
| 258 | public void testGetChannelStatus() throws Exception { |
| 259 | assertFalse(Nfdc.getChannelStatusList(face).isEmpty()); |
| 260 | |
| 261 | exception.expect(ManagementException.class); |
| 262 | Nfdc.getChannelStatusList(mockFace); |
| 263 | } |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 264 | } |