blob: 39750c0ea94aa0c3f36e5f00a88d14da51af98d9 [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
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 Afanasyeva8bc0d82016-01-25 17:25:30 -0800110 assertTrue(Nfdc.getForwarderStatus(face).getStartTimestamp() > 0);
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800111
112 exception.expect(ManagementException.class);
113 Nfdc.getForwarderStatus(mockFace);
114 }
115
116 @Test
117 public void testGetFaceList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800118 assertFalse(Nfdc.getFaceList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800119
120 exception.expect(ManagementException.class);
121 Nfdc.getFaceList(mockFace);
122 }
123
124 @Test
125 public void testGetFibList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800126 assertFalse(Nfdc.getFibList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800127
128 exception.expect(ManagementException.class);
129 Nfdc.getFibList(mockFace);
130 }
131
132 @Test
133 public void testGetRouteList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800134 assertFalse(Nfdc.getRouteList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800135
136 exception.expect(ManagementException.class);
137 Nfdc.getRouteList(mockFace);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800138 }
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 Afanasyev3c5ae7c2016-02-19 19:33:21 -0800166
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 Afanasyeva8bc0d82016-01-25 17:25:30 -0800197 }
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 Afanasyev3c5ae7c2016-02-19 19:33:21 -0800213
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 Afanasyeva8bc0d82016-01-25 17:25:30 -0800250 }
251
252 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800253 * LocalControlHeader works only with NFD < 0.3.4, broken otherwise.
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800254 */
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 Afanasyev7ac3e392016-02-19 23:21:01 -0800264
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 Afanasyeva8bc0d82016-01-25 17:25:30 -0800272}