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 |
| 85 | public void emit(final Interest interest) throws EncodingException, SecurityException { |
| 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 |
| 98 | |
| 99 | mockFace.receive(data); |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | exception.expect(ManagementException.class); |
| 104 | exception.expectMessage("No key locator available."); |
| 105 | Nfdc.getKeyLocator(mockFace); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testGetForwarderStatus() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 110 | assertTrue(Nfdc.getForwarderStatus(face).getStartTimestamp() > 0); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 111 | |
| 112 | exception.expect(ManagementException.class); |
| 113 | Nfdc.getForwarderStatus(mockFace); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testGetFaceList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 118 | assertFalse(Nfdc.getFaceList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 119 | |
| 120 | exception.expect(ManagementException.class); |
| 121 | Nfdc.getFaceList(mockFace); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testGetFibList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 126 | assertFalse(Nfdc.getFibList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 127 | |
| 128 | exception.expect(ManagementException.class); |
| 129 | Nfdc.getFibList(mockFace); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void testGetRouteList() throws Exception { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 134 | assertFalse(Nfdc.getRouteList(face).isEmpty()); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 135 | |
| 136 | exception.expect(ManagementException.class); |
| 137 | Nfdc.getRouteList(mockFace); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testRoutes() throws EncodingException, IOException, ManagementException, InterruptedException { |
| 142 | Nfdc.register(face, new Name("/my/route/to/app/face"), 333); |
| 143 | int faceId = Nfdc.createFace(face, "udp4://127.0.0.1:56363"); |
| 144 | Nfdc.register(face, "udp4://127.0.0.1:56363", new Name("/my/test/route"), 999); |
| 145 | Nfdc.register(face, faceId, new Name("/"), 555); |
| 146 | |
| 147 | // check that route is created |
| 148 | Thread.sleep(1000); // NFD registers the route asynchronously |
| 149 | |
| 150 | boolean found = false; |
| 151 | for (RibEntry route : Nfdc.getRouteList(face)) { |
| 152 | LOG.info("Found route: " + route.getName().toUri()); |
| 153 | if (route.getName().equals(new Name("/my/test/route"))) { |
| 154 | found = true; |
| 155 | } |
| 156 | } |
| 157 | assertTrue(found); |
| 158 | |
| 159 | Nfdc.unregister(face, new Name("/my/route/to/app/face")); |
| 160 | |
| 161 | // remove the route |
| 162 | Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363"); |
| 163 | |
| 164 | // remove face |
| 165 | Nfdc.destroyFace(face, faceId); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 166 | |
| 167 | Thread.sleep(1000); // wait for face to be destroyed |
| 168 | |
| 169 | exception.expect(ManagementException.class); |
| 170 | exception.expectMessage("Face not found: udp4://127.0.0.1:56363"); |
| 171 | Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363"); |
| 172 | } |
| 173 | |
| 174 | // TODO: restore after fixed bug in MockFace |
| 175 | // @Test |
| 176 | // public void testFailOfRegister() throws Exception { |
| 177 | // exception.expect(ManagementException.class); |
| 178 | // Nfdc.register(mockFace, new Name("/my/route/to/app/face"), 333); |
| 179 | // } |
| 180 | // |
| 181 | // @Test |
| 182 | // public void testFailOfUnregister() throws Exception { |
| 183 | // exception.expect(ManagementException.class); |
| 184 | // Nfdc.unregister(mockFace, new Name("/my/route/to/app/face")); |
| 185 | // } |
| 186 | |
| 187 | @Test |
| 188 | public void testFailOfCreateFace() throws Exception { |
| 189 | exception.expect(ManagementException.class); |
| 190 | Nfdc.createFace(mockFace, "udp4://127.0.0.1:56363"); |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void testFailOfDestroyFace() throws Exception { |
| 195 | exception.expect(ManagementException.class); |
| 196 | Nfdc.destroyFace(mockFace, 1); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Test |
| 200 | public void testStrategies() throws Exception { |
| 201 | Name prefix = new Name("/test/strategy").append("random:" + new Random().nextInt()); |
| 202 | |
| 203 | List<StrategyChoice> choices = Nfdc.getStrategyList(face); |
| 204 | int oldSize = choices.size(); |
| 205 | |
| 206 | Nfdc.setStrategy(face, prefix, Strategies.CLIENT_CONTROL); |
| 207 | Thread.sleep(1000); // strategy takes a while to register |
| 208 | |
| 209 | choices = Nfdc.getStrategyList(face); |
| 210 | assertEquals(oldSize + 1, choices.size()); |
| 211 | |
| 212 | Nfdc.unsetStrategy(face, prefix); |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 213 | |
| 214 | exception.expect(ManagementException.class); |
| 215 | Nfdc.getStrategyList(mockFace); |
| 216 | } |
| 217 | |
| 218 | @Test |
| 219 | public void testFailOfUnsetStrategy() throws Exception { |
| 220 | exception.expect(ManagementException.class); |
| 221 | Nfdc.unsetStrategy(mockFace, new Name("/")); |
| 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public void testFailOfSetStrategyWithoutKeychain() throws Exception { |
| 226 | exception.expect(IllegalArgumentException.class); |
| 227 | Nfdc.setStrategy(noKeyChainFace, new Name("/test"), Strategies.BEST_ROUTE); |
| 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void testFailOfSetStrategyWithNon200Code() throws Exception { |
| 232 | exception.expect(ManagementException.class); |
| 233 | exception.expectMessage("Action failed, forwarder returned: 300 Test FAIL"); |
| 234 | |
| 235 | mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() { |
| 236 | @Override |
| 237 | public void emit(final Interest interest) throws EncodingException, SecurityException { |
| 238 | ControlResponse response = new ControlResponse(); |
| 239 | response.setStatusCode(300); |
| 240 | response.setStatusText("Test FAIL"); |
| 241 | |
| 242 | Data data = new Data(); |
| 243 | data.setName(interest.getName()); |
| 244 | data.setContent(response.wireEncode()); |
| 245 | |
| 246 | mockFace.receive(data); |
| 247 | } |
| 248 | }); |
| 249 | Nfdc.setStrategy(mockFace, new Name("/"), Strategies.BROADCAST); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 253 | * LocalControlHeader works only with NFD < 0.3.4, broken otherwise. |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 254 | */ |
| 255 | @Test(expected = ManagementException.class) |
| 256 | public void testLocalControlHeader() throws Exception { |
| 257 | Nfdc.enableLocalControlHeader(face, LocalControlHeader.INCOMING_FACE_ID); |
| 258 | Thread.sleep(1000); // strategy takes a while to register |
| 259 | |
| 260 | // TODO: add asserts |
| 261 | |
| 262 | Nfdc.disableLocalControlHeader(face, LocalControlHeader.INCOMING_FACE_ID); |
| 263 | } |
Alexander Afanasyev | 7ac3e39 | 2016-02-19 23:21:01 -0800 | [diff] [blame^] | 264 | |
| 265 | @Test |
| 266 | public void testGetChannelStatus() throws Exception { |
| 267 | assertFalse(Nfdc.getChannelStatusList(face).isEmpty()); |
| 268 | |
| 269 | exception.expect(ManagementException.class); |
| 270 | Nfdc.getChannelStatusList(mockFace); |
| 271 | } |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 272 | } |