blob: fc7a935a8100031fee11668e413c5d19874dbe54 [file] [log] [blame]
Andrew Brown3831baf2015-01-19 13:38:52 -08001/*
andrewsbrown533c6ef2015-03-03 16:08:41 -08002 * jndn-mock
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.
Andrew Brown3831baf2015-01-19 13:38:52 -080013 */
14package com.intel.jndn.mock;
15
Alexander Afanasyevcbc41012016-02-19 20:10:57 -080016import net.named_data.jndn.Data;
17import net.named_data.jndn.Face;
18import net.named_data.jndn.Interest;
19import net.named_data.jndn.InterestFilter;
20import net.named_data.jndn.Name;
21import net.named_data.jndn.OnData;
22import net.named_data.jndn.OnInterestCallback;
23import net.named_data.jndn.OnRegisterFailed;
24import net.named_data.jndn.OnRegisterSuccess;
25import net.named_data.jndn.OnTimeout;
Andrew Brown3831baf2015-01-19 13:38:52 -080026import net.named_data.jndn.encoding.EncodingException;
andrewsbrowna52bf7d2015-04-06 13:51:53 -070027import net.named_data.jndn.security.SecurityException;
Andrew Brown3831baf2015-01-19 13:38:52 -080028import net.named_data.jndn.util.Blob;
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080029import org.junit.Before;
Andrew Brown3831baf2015-01-19 13:38:52 -080030import org.junit.Test;
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080031
Alexander Afanasyevcbc41012016-02-19 20:10:57 -080032import java.io.IOException;
33import java.util.logging.Logger;
34
35import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertFalse;
37import static org.junit.Assert.assertNotNull;
38import static org.junit.Assert.assertNull;
39import static org.junit.Assert.assertTrue;
Andrew Brown3831baf2015-01-19 13:38:52 -080040
41/**
Alexander Afanasyevcbc41012016-02-19 20:10:57 -080042 * Test MockFace functionality.
Andrew Brown3831baf2015-01-19 13:38:52 -080043 */
44public class MockFaceTest {
Alexander Afanasyevcbc41012016-02-19 20:10:57 -080045 private static final Logger LOG = Logger.getLogger(MockFaceTest.class.getName());
46 private MockFace face;
47 private int counter;
48 private Data recvData = null;
49 private boolean isTimeout = false;
50 private Exception exception = null;
51
52 /////////////////////////////////////////////////////////////////////////////
Andrew Brown58191872016-02-05 22:16:57 -080053
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080054 @Before
55 public void setup() throws SecurityException {
56 face = new MockFace();
57 counter = 0;
58 recvData = null;
59 isTimeout = false;
60 exception = null;
61 }
Andrew Brown3831baf2015-01-19 13:38:52 -080062
Andrew Brown3831baf2015-01-19 13:38:52 -080063 @Test
Andrew Brown58191872016-02-05 22:16:57 -080064 public void testExpressingAnInterest() throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080065 // make request
66 expressInterest("/test/with/responses");
Andrew Brown3831baf2015-01-19 13:38:52 -080067
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080068 run(2);
69
70 // add response (after face is connectd)
Andrew Brown3831baf2015-01-19 13:38:52 -080071 Data response = new Data(new Name("/test/with/responses"));
72 response.setContent(new Blob("..."));
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080073 face.receive(response);
Andrew Brown3831baf2015-01-19 13:38:52 -080074
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080075 run(20);
Andrew Brown3831baf2015-01-19 13:38:52 -080076
Andrew Brown58191872016-02-05 22:16:57 -080077 assertNotNull(recvData);
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080078 assertEquals(isTimeout, false);
79 assertEquals(recvData.getName().toString(), "/test/with/responses");
80 assertEquals(recvData.getContent().buf(), new Blob("...").buf());
Andrew Brown3831baf2015-01-19 13:38:52 -080081 }
82
Andrew Brown3831baf2015-01-19 13:38:52 -080083 @Test
Andrew Brown58191872016-02-05 22:16:57 -080084 public void testExpressingAnInterestAfterConfiguration() throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080085 // add response (before face is connected)
86 Data response = new Data(new Name("/test/with/responses"));
87 response.setContent(new Blob("..."));
88 face.receive(response);
Andrew Brown3831baf2015-01-19 13:38:52 -080089
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080090 // make request
91 expressInterest("/test/with/responses");
92
93 run(20);
94
Andrew Brown58191872016-02-05 22:16:57 -080095 assertNotNull(recvData);
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080096 assertEquals(isTimeout, false);
97 assertEquals(recvData.getName().toString(), "/test/with/responses");
98 assertEquals(recvData.getContent().buf(), new Blob("...").buf());
99 }
100
101 @Test
Andrew Brown58191872016-02-05 22:16:57 -0800102 public void testInterestTimeouts() throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800103 // make request
104 expressInterest("/some/name");
105
106 run(20);
107
108 assertEquals(recvData, null);
109 assertEquals(isTimeout, true);
110 }
111
112 @Test
Andrew Brown58191872016-02-05 22:16:57 -0800113 public void testPrefixRegistration() throws IOException, SecurityException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800114 class State {
115 boolean regFailed = false;
116 boolean regSucceed = false;
117 }
118 final State state = new State();
119
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800120 LOG.info("Register prefix: /test/with/handlers");
Andrew Brown58191872016-02-05 22:16:57 -0800121 face.registerPrefix(new Name("/test/with/handlers"), new OnInterestCallback() {
122 @Override
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800123 public void onInterest(final Name prefix, final Interest interest, final Face face, final long interestFilterId,
124 final InterestFilter filter) {
125 LOG.info("Received interest, responding: " + interest.getName().toUri());
Andrew Brown58191872016-02-05 22:16:57 -0800126 Data response = new Data(new Name("/test/with/handlers"));
127 response.setContent(new Blob("..."));
128 try {
129 face.putData(response);
130 } catch (IOException e) {
131 exception = e;
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800132 }
Andrew Brown58191872016-02-05 22:16:57 -0800133 counter++;
Andrew Brown3831baf2015-01-19 13:38:52 -0800134 }
Andrew Brown58191872016-02-05 22:16:57 -0800135 }, new OnRegisterFailed() {
136 @Override
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800137 public void onRegisterFailed(final Name prefix) {
138 LOG.info("Prefix registration fails: " + prefix);
Andrew Brown58191872016-02-05 22:16:57 -0800139 state.regFailed = true;
140 counter++;
141 }
142 }, new OnRegisterSuccess() {
143 @Override
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800144 public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) {
145 LOG.info("Prefix registration succeed: " + prefix);
Andrew Brown58191872016-02-05 22:16:57 -0800146 state.regSucceed = true;
147 counter++;
148 }
149 });
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800150
151 run(100, 1);
152 assertTrue(state.regSucceed);
153 assertFalse(state.regFailed);
Andrew Brown3831baf2015-01-19 13:38:52 -0800154
155 // make request
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800156 face.receive(new Interest(new Name("/test/with/handlers")));
Andrew Brown3831baf2015-01-19 13:38:52 -0800157
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800158 run(100, 2);
159
160 assertNull(exception);
161
162 assertEquals(face.sentData.size(), 1);
163 assertFalse(isTimeout);
164 assertEquals("/test/with/handlers", face.sentData.get(0).getName().toString());
165 assertEquals(new Blob("...").buf(), face.sentData.get(0).getContent().buf());
Andrew Brown3831baf2015-01-19 13:38:52 -0800166 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800167
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700168 @Test
Andrew Brown58191872016-02-05 22:16:57 -0800169 public void testThatTransportConnectsOnPrefixRegistration() throws IOException, SecurityException {
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700170 assertFalse(face.getTransport().getIsConnected());
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800171 face.registerPrefix(new Name("/fake/prefix"), (OnInterestCallback) null, (OnRegisterFailed) null,
Andrew Brown58191872016-02-05 22:16:57 -0800172 (OnRegisterSuccess) null);
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700173 assertTrue(face.getTransport().getIsConnected());
174 }
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800175
andrewsbrown0f36eee2015-05-07 01:37:48 +0100176 @Test
Andrew Brown58191872016-02-05 22:16:57 -0800177 public void testInterestFilters() throws IOException, SecurityException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800178 class State {
179 boolean regFailed = false;
180 boolean regSucceed = false;
181 }
182 final State state = new State();
183
184 // connect transport
185 face.registerPrefix(new Name("/fake/prefix"), (OnInterestCallback) null, new OnRegisterFailed() {
andrewsbrown0f36eee2015-05-07 01:37:48 +0100186 @Override
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800187 public void onRegisterFailed(final Name prefix) {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800188 state.regFailed = true;
189 counter++;
190 }
191 }, new OnRegisterSuccess() {
192 @Override
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800193 public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800194 state.regSucceed = true;
195 counter++;
andrewsbrown0f36eee2015-05-07 01:37:48 +0100196 }
197 });
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800198
199 // set filter
andrewsbrown0f36eee2015-05-07 01:37:48 +0100200 face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() {
201 @Override
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800202 public void onInterest(final Name prefix, final Interest interest, final Face face, final long interestFilterId,
203 final InterestFilter filter) {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800204 counter++;
205 }
206 });
207
208 face.receive(new Interest(new Name("/a/b")).setInterestLifetimeMilliseconds(100));
209
210 run(10, 2);
211
212 assertEquals(2, counter);
213 assertTrue(state.regSucceed);
214 assertFalse(state.regFailed);
215 }
216
217 /////////////////////////////////////////////////////////////////////////////
218
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800219 private void run(final int limit, final int maxCounter) throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800220 // process face until a response is received
221 int allowedLoops = limit;
222 while (counter < maxCounter && allowedLoops > 0) {
223 allowedLoops--;
224 face.processEvents();
225 Thread.sleep(100);
226 }
227 }
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800228
229 private void run(final int limit) throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800230 run(limit, 1);
231 }
232
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800233 private void expressInterest(final String name) throws IOException {
234 LOG.info("Express interest: " + name);
Andrew Brown58191872016-02-05 22:16:57 -0800235 face.expressInterest(new Interest(new Name(name)).setInterestLifetimeMilliseconds(1000), new OnData() {
236 @Override
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800237 public void onData(final Interest interest, final Data data) {
Andrew Brown58191872016-02-05 22:16:57 -0800238 counter++;
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800239 LOG.fine("Received data");
Andrew Brown58191872016-02-05 22:16:57 -0800240 recvData = data;
241 }
242 }, new OnTimeout() {
243 @Override
Alexander Afanasyevcbc41012016-02-19 20:10:57 -0800244 public void onTimeout(final Interest interest) {
245 LOG.fine("Received timeout");
Andrew Brown58191872016-02-05 22:16:57 -0800246 counter++;
247 isTimeout = true;
248 }
249 });
andrewsbrown0f36eee2015-05-07 01:37:48 +0100250 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800251}