blob: f1deef186bc36746afbe7de945bb94b254d2ed1c [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.Strategies;
17import com.intel.jndn.management.types.RibEntry;
18import com.intel.jndn.management.types.StrategyChoice;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080019import com.intel.jndn.mock.MockFace;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080020import com.intel.jndn.mock.MockKeyChain;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080021import net.named_data.jndn.ControlResponse;
22import net.named_data.jndn.Data;
23import net.named_data.jndn.DigestSha256Signature;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080024import net.named_data.jndn.Face;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080025import net.named_data.jndn.Interest;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080026import net.named_data.jndn.KeyLocator;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080027import net.named_data.jndn.MetaInfo;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080028import net.named_data.jndn.Name;
29import net.named_data.jndn.encoding.EncodingException;
30import net.named_data.jndn.security.KeyChain;
31import net.named_data.jndn.security.SecurityException;
32import org.junit.Before;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080033import org.junit.Rule;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080034import org.junit.Test;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080035import org.junit.rules.ExpectedException;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080036
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080037import java.io.IOException;
38import java.util.List;
39import java.util.Random;
40import java.util.logging.Logger;
41
42import static org.junit.Assert.assertEquals;
43import static org.junit.Assert.assertFalse;
44import static org.junit.Assert.assertNotNull;
45import static org.junit.Assert.assertTrue;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080046
47/**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080048 * Testing Nfdc with real NFD instance (NFD must be run locally while executing the test).
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080049 *
50 * @author Andrew Brown <andrew.brown@intel.com>
51 */
52public class NfdcIT {
53 private static final Logger LOG = Logger.getLogger(NfdcIT.class.getName());
54 private Face face;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080055 private MockFace mockFace;
56 private Face noKeyChainFace;
57
58 @Rule
59 public final ExpectedException exception = ExpectedException.none();
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080060
61 @Before
62 public void setUp() throws SecurityException {
63 face = new Face("localhost");
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -040064 mockFace = new MockFace(new MockFace.Options().setEnablePacketLogging(false).setEnableRegistrationReply(false));
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080065 noKeyChainFace = new Face("localhost"); // don't set command signing info
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080066 KeyChain keyChain = MockKeyChain.configure(new Name("/tmp/identity"));
67 face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName());
68 }
69
70 @Test
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080071 public void testGetKeyLocator() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080072 KeyLocator keyLocator = Nfdc.getKeyLocator(face);
73 assertNotNull(keyLocator);
74 LOG.info("Connected to NFD with key locator: " + keyLocator.getKeyName().toUri());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080075
76 exception.expect(ManagementException.class);
77 Nfdc.getKeyLocator(mockFace);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080078 }
79
80 @Test
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080081 public void testFailOfGetKeyLocator() throws Exception {
82 mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
83 @Override
andrewsbrown6df23ad2016-04-22 14:24:12 -070084 public void emit(final Interest interest) {
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080085 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
andrewsbrown6df23ad2016-04-22 14:24:12 -070097 try {
98 mockFace.receive(data);
99 } catch (EncodingException e) {
100 LOG.severe("Failed to set receive data: " + e);
101 }
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800102 }
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 Afanasyeva8bc0d82016-01-25 17:25:30 -0800112 assertTrue(Nfdc.getForwarderStatus(face).getStartTimestamp() > 0);
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800113
114 exception.expect(ManagementException.class);
115 Nfdc.getForwarderStatus(mockFace);
116 }
117
118 @Test
119 public void testGetFaceList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800120 assertFalse(Nfdc.getFaceList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800121
122 exception.expect(ManagementException.class);
123 Nfdc.getFaceList(mockFace);
124 }
125
126 @Test
127 public void testGetFibList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800128 assertFalse(Nfdc.getFibList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800129
130 exception.expect(ManagementException.class);
131 Nfdc.getFibList(mockFace);
132 }
133
134 @Test
135 public void testGetRouteList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800136 assertFalse(Nfdc.getRouteList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800137
138 exception.expect(ManagementException.class);
139 Nfdc.getRouteList(mockFace);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800140 }
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 Afanasyev3c5ae7c2016-02-19 19:33:21 -0800168
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
176 // TODO: restore after fixed bug in MockFace
177// @Test
178// public void testFailOfRegister() throws Exception {
179// exception.expect(ManagementException.class);
180// Nfdc.register(mockFace, new Name("/my/route/to/app/face"), 333);
181// }
182//
183// @Test
184// public void testFailOfUnregister() throws Exception {
185// exception.expect(ManagementException.class);
186// Nfdc.unregister(mockFace, new Name("/my/route/to/app/face"));
187// }
188
189 @Test
190 public void testFailOfCreateFace() throws Exception {
191 exception.expect(ManagementException.class);
192 Nfdc.createFace(mockFace, "udp4://127.0.0.1:56363");
193 }
194
195 @Test
196 public void testFailOfDestroyFace() throws Exception {
197 exception.expect(ManagementException.class);
198 Nfdc.destroyFace(mockFace, 1);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800199 }
200
201 @Test
202 public void testStrategies() throws Exception {
203 Name prefix = new Name("/test/strategy").append("random:" + new Random().nextInt());
204
205 List<StrategyChoice> choices = Nfdc.getStrategyList(face);
206 int oldSize = choices.size();
207
208 Nfdc.setStrategy(face, prefix, Strategies.CLIENT_CONTROL);
209 Thread.sleep(1000); // strategy takes a while to register
210
211 choices = Nfdc.getStrategyList(face);
212 assertEquals(oldSize + 1, choices.size());
213
214 Nfdc.unsetStrategy(face, prefix);
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800215
216 exception.expect(ManagementException.class);
217 Nfdc.getStrategyList(mockFace);
218 }
219
220 @Test
221 public void testFailOfUnsetStrategy() throws Exception {
222 exception.expect(ManagementException.class);
223 Nfdc.unsetStrategy(mockFace, new Name("/"));
224 }
225
226 @Test
227 public void testFailOfSetStrategyWithoutKeychain() throws Exception {
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400228 exception.expect(NullPointerException.class);
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800229 Nfdc.setStrategy(noKeyChainFace, new Name("/test"), Strategies.BEST_ROUTE);
230 }
231
232 @Test
233 public void testFailOfSetStrategyWithNon200Code() throws Exception {
234 exception.expect(ManagementException.class);
235 exception.expectMessage("Action failed, forwarder returned: 300 Test FAIL");
236
237 mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
238 @Override
andrewsbrown6df23ad2016-04-22 14:24:12 -0700239 public void emit(final Interest interest) {
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800240 ControlResponse response = new ControlResponse();
241 response.setStatusCode(300);
242 response.setStatusText("Test FAIL");
243
244 Data data = new Data();
245 data.setName(interest.getName());
246 data.setContent(response.wireEncode());
247
andrewsbrown6df23ad2016-04-22 14:24:12 -0700248 try {
249 mockFace.receive(data);
250 } catch (EncodingException e) {
251 LOG.severe("Failed to set receive data: " + e);
252 }
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800253 }
254 });
255 Nfdc.setStrategy(mockFace, new Name("/"), Strategies.BROADCAST);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800256 }
257
Alexander Afanasyev7ac3e392016-02-19 23:21:01 -0800258 @Test
259 public void testGetChannelStatus() throws Exception {
260 assertFalse(Nfdc.getChannelStatusList(face).isEmpty());
261
262 exception.expect(ManagementException.class);
263 Nfdc.getChannelStatusList(mockFace);
264 }
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800265}