blob: 350c79a1947f5a01504eb1c142614048d4fbf86e [file] [log] [blame]
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -08001/*
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 */
14package com.intel.jndn.management;
15
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080016import com.intel.jndn.management.enums.LocalControlHeader;
17import com.intel.jndn.management.enums.Strategies;
18import com.intel.jndn.management.types.RibEntry;
19import com.intel.jndn.management.types.StrategyChoice;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080020import com.intel.jndn.mock.MockFace;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080021import com.intel.jndn.mock.MockKeyChain;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080022import net.named_data.jndn.ControlResponse;
23import net.named_data.jndn.Data;
24import net.named_data.jndn.DigestSha256Signature;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080025import net.named_data.jndn.Face;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080026import net.named_data.jndn.Interest;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080027import net.named_data.jndn.KeyLocator;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080028import net.named_data.jndn.MetaInfo;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080029import net.named_data.jndn.Name;
30import net.named_data.jndn.encoding.EncodingException;
31import net.named_data.jndn.security.KeyChain;
32import net.named_data.jndn.security.SecurityException;
33import org.junit.Before;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080034import org.junit.Rule;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080035import org.junit.Test;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080036import org.junit.rules.ExpectedException;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080037
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080038import java.io.IOException;
39import java.util.List;
40import java.util.Random;
41import java.util.logging.Logger;
42
43import static org.junit.Assert.assertEquals;
44import static org.junit.Assert.assertFalse;
45import static org.junit.Assert.assertNotNull;
46import static org.junit.Assert.assertTrue;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080047
48/**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080049 * Testing Nfdc with real NFD instance (NFD must be run locally while executing the test).
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080050 *
51 * @author Andrew Brown <andrew.brown@intel.com>
52 */
53public class NfdcIT {
54 private static final Logger LOG = Logger.getLogger(NfdcIT.class.getName());
55 private Face face;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080056 private MockFace mockFace;
57 private Face noKeyChainFace;
58
59 @Rule
60 public final ExpectedException exception = ExpectedException.none();
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080061
62 @Before
63 public void setUp() throws SecurityException {
64 face = new Face("localhost");
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080065 mockFace = new MockFace();
66 noKeyChainFace = new Face("localhost"); // don't set command signing info
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080067 KeyChain keyChain = MockKeyChain.configure(new Name("/tmp/identity"));
68 face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName());
69 }
70
71 @Test
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080072 public void testGetKeyLocator() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080073 KeyLocator keyLocator = Nfdc.getKeyLocator(face);
74 assertNotNull(keyLocator);
75 LOG.info("Connected to NFD with key locator: " + keyLocator.getKeyName().toUri());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080076
77 exception.expect(ManagementException.class);
78 Nfdc.getKeyLocator(mockFace);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080079 }
80
81 @Test
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080082 public void testFailOfGetKeyLocator() throws Exception {
83 mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
84 @Override
andrewsbrown6df23ad2016-04-22 14:24:12 -070085 public void emit(final Interest interest) {
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080086 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
andrewsbrown6df23ad2016-04-22 14:24:12 -070098 try {
99 mockFace.receive(data);
100 } catch (EncodingException e) {
101 LOG.severe("Failed to set receive data: " + e);
102 }
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800103 }
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 Afanasyeva8bc0d82016-01-25 17:25:30 -0800113 assertTrue(Nfdc.getForwarderStatus(face).getStartTimestamp() > 0);
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800114
115 exception.expect(ManagementException.class);
116 Nfdc.getForwarderStatus(mockFace);
117 }
118
119 @Test
120 public void testGetFaceList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800121 assertFalse(Nfdc.getFaceList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800122
123 exception.expect(ManagementException.class);
124 Nfdc.getFaceList(mockFace);
125 }
126
127 @Test
128 public void testGetFibList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800129 assertFalse(Nfdc.getFibList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800130
131 exception.expect(ManagementException.class);
132 Nfdc.getFibList(mockFace);
133 }
134
135 @Test
136 public void testGetRouteList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800137 assertFalse(Nfdc.getRouteList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800138
139 exception.expect(ManagementException.class);
140 Nfdc.getRouteList(mockFace);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800141 }
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 Afanasyev3c5ae7c2016-02-19 19:33:21 -0800169
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 Afanasyeva8bc0d82016-01-25 17:25:30 -0800200 }
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 Afanasyev3c5ae7c2016-02-19 19:33:21 -0800216
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
andrewsbrown6df23ad2016-04-22 14:24:12 -0700240 public void emit(final Interest interest) {
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800241 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
andrewsbrown6df23ad2016-04-22 14:24:12 -0700249 try {
250 mockFace.receive(data);
251 } catch (EncodingException e) {
252 LOG.severe("Failed to set receive data: " + e);
253 }
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800254 }
255 });
256 Nfdc.setStrategy(mockFace, new Name("/"), Strategies.BROADCAST);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800257 }
258
259 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800260 * LocalControlHeader works only with NFD < 0.3.4, broken otherwise.
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800261 */
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 Afanasyev7ac3e392016-02-19 23:21:01 -0800271
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 Afanasyeva8bc0d82016-01-25 17:25:30 -0800279}