blob: fe9966c18541a737ff4ddc2b9b865a547d27e694 [file] [log] [blame]
Davide Pesaventodf179552020-11-07 02:00:19 -05001/*
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
16import com.intel.jndn.management.enums.Strategies;
17import com.intel.jndn.mock.MockFace;
18import com.intel.jndn.mock.MockKeyChain;
19import net.named_data.jndn.ControlResponse;
20import net.named_data.jndn.Data;
21import net.named_data.jndn.Interest;
22import net.named_data.jndn.KeyLocator;
23import net.named_data.jndn.KeyLocatorType;
24import net.named_data.jndn.MetaInfo;
25import net.named_data.jndn.Name;
26import net.named_data.jndn.encoding.Tlv0_3WireFormat;
27import net.named_data.jndn.encoding.WireFormat;
28import net.named_data.jndn.security.KeyChain;
29import net.named_data.jndn.security.SecurityException;
30import net.named_data.jndn.security.SigningInfo;
31
32import org.junit.Before;
33import org.junit.Test;
34import static org.junit.Assert.assertEquals;
35import static org.junit.Assert.assertNotEquals;
36import static org.junit.Assert.assertThrows;
37import static org.junit.Assert.assertTrue;
38import static org.junit.Assert.fail;
39
40/**
41 * Nfdc unit tests.
42 */
43public class NfdcTest {
44 private MockFace mockFace;
45 private KeyChain keyChain;
46 private MockFace.SignalOnSendInterest replyWithEmptyData;
47
48 @Before
49 public void setUp() throws SecurityException {
50 WireFormat.setDefaultWireFormat(Tlv0_3WireFormat.get());
51
52 mockFace = new MockFace(new MockFace.Options());
53 keyChain = MockKeyChain.configure(new Name("/tmp/identity"));
54
55 replyWithEmptyData = new MockFace.SignalOnSendInterest() {
56 @Override
57 public void emit(final Interest interest) {
58 Data data = new Data();
59 data.setName(new Name(interest.getName()).appendVersion(0).appendSegment(0));
60 MetaInfo meta = new MetaInfo();
61 meta.setFinalBlockId(data.getName().get(-1));
62 data.setMetaInfo(meta);
63
64 try {
65 keyChain.sign(data);
66 } catch (Exception e) {
67 fail("Failed to sign data: " + e);
68 }
69
70 try {
71 mockFace.receive(data);
72 } catch (Exception e) {
73 fail("Failed to receive data on mock face: " + e);
74 }
75 }
76 };
77 }
78
79 @Test
80 public void testGetKeyLocator() throws Exception {
81 mockFace.onSendInterest.add(replyWithEmptyData);
82
83 KeyLocator keyLocator = Nfdc.getKeyLocator(mockFace);
84 assertEquals(KeyLocatorType.KEYNAME, keyLocator.getType());
85 assertNotEquals(0, keyLocator.getKeyName().size());
86 }
87
88 @Test
89 public void testGetKeyLocatorWithDigestSha256() throws Exception {
90 mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
91 @Override
92 public void emit(final Interest interest) {
93 Data data = new Data();
94 data.setName(new Name(interest.getName()).appendVersion(0).appendSegment(0));
95 MetaInfo meta = new MetaInfo();
96 meta.setFinalBlockId(data.getName().get(-1));
97 data.setMetaInfo(meta);
98
99 try {
100 keyChain.sign(data, new SigningInfo(SigningInfo.SignerType.SHA256));
101 } catch (Exception e) {
102 fail("Failed to sign data: " + e);
103 }
104
105 try {
106 mockFace.receive(data);
107 } catch (Exception e) {
108 fail("Failed to receive data on mock face: " + e);
109 }
110 }
111 });
112
113 Exception exception = assertThrows(ManagementException.class, () -> Nfdc.getKeyLocator(mockFace));
114 assertEquals("No key locator available.", exception.getMessage());
115 }
116
117 @Test
118 public void testGetChannelStatusList() throws Exception {
119 assertThrows(ManagementException.class, () -> Nfdc.getChannelStatusList(mockFace));
120
121 mockFace.onSendInterest.add(replyWithEmptyData);
122 assertTrue(Nfdc.getChannelStatusList(mockFace).isEmpty());
123 }
124
125 @Test
126 public void testGetFaceList() throws Exception {
127 assertThrows(ManagementException.class, () -> Nfdc.getFaceList(mockFace));
128
129 mockFace.onSendInterest.add(replyWithEmptyData);
130 assertTrue(Nfdc.getFaceList(mockFace).isEmpty());
131 }
132
133 @Test
134 public void testFailOfCreateFace() throws Exception {
135 mockFace.onSendInterest.add(replyWithEmptyData);
136 assertThrows(ManagementException.class, () -> Nfdc.createFace(mockFace, "udp4://127.0.0.1:56363"));
137 }
138
139 @Test
140 public void testFailOfDestroyFace() throws Exception {
141 mockFace.onSendInterest.add(replyWithEmptyData);
142 assertThrows(ManagementException.class, () -> Nfdc.destroyFace(mockFace, 1));
143 }
144
145 @Test
146 public void testGetFibList() throws Exception {
147 assertThrows(ManagementException.class, () -> Nfdc.getFibList(mockFace));
148
149 mockFace.onSendInterest.add(replyWithEmptyData);
150 assertTrue(Nfdc.getFibList(mockFace).isEmpty());
151 }
152
153 @Test
154 public void testGetRouteList() throws Exception {
155 assertThrows(ManagementException.class, () -> Nfdc.getRouteList(mockFace));
156
157 mockFace.onSendInterest.add(replyWithEmptyData);
158 assertTrue(Nfdc.getRouteList(mockFace).isEmpty());
159 }
160
161 @Test
162 public void testFailOfRegister() throws Exception {
163 mockFace.onSendInterest.add(replyWithEmptyData);
164 assertThrows(ManagementException.class, () -> Nfdc.register(mockFace, new Name("/my/route/to/app/face"), 333));
165 }
166
167 @Test
168 public void testFailOfUnregister() throws Exception {
169 mockFace.onSendInterest.add(replyWithEmptyData);
170 assertThrows(ManagementException.class, () -> Nfdc.unregister(mockFace, new Name("/my/route/to/app/face")));
171 }
172
173 @Test
174 public void testGetStrategyList() throws Exception {
175 assertThrows(ManagementException.class, () -> Nfdc.getStrategyList(mockFace));
176
177 mockFace.onSendInterest.add(replyWithEmptyData);
178 assertTrue(Nfdc.getStrategyList(mockFace).isEmpty());
179 }
180
181 @Test
182 public void testFailOfSetStrategyWithNon200Code() throws Exception {
183 mockFace.onSendInterest.add(new MockFace.SignalOnSendInterest() {
184 @Override
185 public void emit(final Interest interest) {
186 ControlResponse response = new ControlResponse();
187 response.setStatusCode(400);
188 response.setStatusText("test error");
189
190 Data data = new Data();
191 data.setName(interest.getName());
192 data.setContent(response.wireEncode());
193
194 try {
195 mockFace.receive(data);
196 } catch (Exception e) {
197 fail("Failed to receive data on mock face: " + e);
198 }
199 }
200 });
201
202 Exception exception = assertThrows(ManagementException.class, () -> {
203 Nfdc.setStrategy(mockFace, new Name("/"), Strategies.MULTICAST);
204 });
205 assertEquals("Action failed, forwarder returned: 400 test error", exception.getMessage());
206 }
207
208 @Test
209 public void testFailOfUnsetStrategy() throws Exception {
210 mockFace.onSendInterest.add(replyWithEmptyData);
211 assertThrows(ManagementException.class, () -> Nfdc.unsetStrategy(mockFace, new Name("/")));
212 }
213}