blob: 98e11cfa35abc87d4d94de2138fb15bf95adfa8e [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;
Davide Pesaventodf179552020-11-07 02:00:19 -050017import com.intel.jndn.management.types.ForwarderStatus;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080018import com.intel.jndn.management.types.RibEntry;
19import com.intel.jndn.management.types.StrategyChoice;
20import com.intel.jndn.mock.MockKeyChain;
21import net.named_data.jndn.Face;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080022import net.named_data.jndn.Name;
Davide Pesaventodd1fe9f2020-11-03 20:48:24 -050023import net.named_data.jndn.encoding.Tlv0_3WireFormat;
24import net.named_data.jndn.encoding.WireFormat;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080025import net.named_data.jndn.security.KeyChain;
26import net.named_data.jndn.security.SecurityException;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080027
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080028import java.util.List;
29import java.util.Random;
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080030
Davide Pesaventodf179552020-11-07 02:00:19 -050031import org.junit.Before;
32import org.junit.Test;
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080033import static org.junit.Assert.assertEquals;
34import static org.junit.Assert.assertFalse;
Davide Pesaventodd1fe9f2020-11-03 20:48:24 -050035import static org.junit.Assert.assertThrows;
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080036import static org.junit.Assert.assertTrue;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080037
38/**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080039 * Testing Nfdc with real NFD instance (NFD must be run locally while executing the test).
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080040 *
41 * @author Andrew Brown <andrew.brown@intel.com>
42 */
43public class NfdcIT {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080044 private Face face;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080045 private Face noKeyChainFace;
46
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080047 @Before
48 public void setUp() throws SecurityException {
Davide Pesaventodd1fe9f2020-11-03 20:48:24 -050049 WireFormat.setDefaultWireFormat(Tlv0_3WireFormat.get());
50
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080051 face = new Face("localhost");
52 KeyChain keyChain = MockKeyChain.configure(new Name("/tmp/identity"));
53 face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName());
Davide Pesaventodf179552020-11-07 02:00:19 -050054 noKeyChainFace = new Face("localhost"); // don't set command signing info
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080055 }
56
57 @Test
58 public void testGetForwarderStatus() throws Exception {
Davide Pesaventodf179552020-11-07 02:00:19 -050059 ForwarderStatus status = Nfdc.getForwarderStatus(face);
60 assertTrue(status.getStartTimestamp() > 0);
61 assertTrue(status.getCurrentTimestamp() > 0);
62 assertTrue(status.getNFibEntries() > 0);
63 assertTrue(status.getNInInterests() > 0);
64 assertTrue(status.getNOutData() > 0);
65 }
66
67 @Test
68 public void testGetChannelStatusList() throws Exception {
69 assertFalse(Nfdc.getChannelStatusList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080070 }
71
72 @Test
73 public void testGetFaceList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080074 assertFalse(Nfdc.getFaceList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080075 }
76
77 @Test
78 public void testGetFibList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080079 assertFalse(Nfdc.getFibList(face).isEmpty());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080080 }
81
82 @Test
83 public void testGetRouteList() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080084 assertFalse(Nfdc.getRouteList(face).isEmpty());
85 }
86
87 @Test
Davide Pesaventodf179552020-11-07 02:00:19 -050088 public void testRoutes() throws Exception {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080089 Nfdc.register(face, new Name("/my/route/to/app/face"), 333);
90 int faceId = Nfdc.createFace(face, "udp4://127.0.0.1:56363");
91 Nfdc.register(face, "udp4://127.0.0.1:56363", new Name("/my/test/route"), 999);
92 Nfdc.register(face, faceId, new Name("/"), 555);
93
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080094 Thread.sleep(1000); // NFD registers the route asynchronously
95
Davide Pesaventodf179552020-11-07 02:00:19 -050096 // check that route is created
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080097 boolean found = false;
98 for (RibEntry route : Nfdc.getRouteList(face)) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080099 if (route.getName().equals(new Name("/my/test/route"))) {
100 found = true;
101 }
102 }
103 assertTrue(found);
104
105 Nfdc.unregister(face, new Name("/my/route/to/app/face"));
106
107 // remove the route
108 Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363");
109
110 // remove face
111 Nfdc.destroyFace(face, faceId);
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800112 Thread.sleep(1000); // wait for face to be destroyed
113
Davide Pesaventodd1fe9f2020-11-03 20:48:24 -0500114 Exception exception = assertThrows(ManagementException.class, () -> {
115 Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363");
116 });
117 assertEquals("Face not found: udp4://127.0.0.1:56363", exception.getMessage());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800118 }
119
Davide Pesavento63463132020-11-03 20:37:23 -0500120 @Test
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800121 public void testStrategies() throws Exception {
Davide Pesaventodd1fe9f2020-11-03 20:48:24 -0500122 Name prefix = new Name("/test/strategy").append("random" + new Random().nextInt());
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800123 List<StrategyChoice> choices = Nfdc.getStrategyList(face);
Davide Pesaventodf179552020-11-07 02:00:19 -0500124 assertFalse(choices.isEmpty());
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800125 int oldSize = choices.size();
126
Davide Pesavento63463132020-11-03 20:37:23 -0500127 Nfdc.setStrategy(face, prefix, Strategies.RANDOM);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800128 Thread.sleep(1000); // strategy takes a while to register
129
130 choices = Nfdc.getStrategyList(face);
131 assertEquals(oldSize + 1, choices.size());
132
133 Nfdc.unsetStrategy(face, prefix);
Davide Pesaventodd1fe9f2020-11-03 20:48:24 -0500134 Thread.sleep(1000);
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800135
Davide Pesaventodd1fe9f2020-11-03 20:48:24 -0500136 choices = Nfdc.getStrategyList(face);
137 assertEquals(oldSize, choices.size());
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800138 }
139
140 @Test
141 public void testFailOfSetStrategyWithoutKeychain() throws Exception {
Davide Pesaventodd1fe9f2020-11-03 20:48:24 -0500142 assertThrows(NullPointerException.class, () -> {
143 Nfdc.setStrategy(noKeyChainFace, new Name("/test"), Strategies.BEST_ROUTE);
144 });
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -0800145 }
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800146}