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