blob: 32a8d350cf977840d529cfbbbd06a5e8184d422c [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
16import java.io.IOException;
Andrew Brownec1b0d02015-02-21 13:11:42 -080017import java.util.logging.Logger;
Alexander Afanasyev8e9330f2016-01-25 19:13:40 -080018
19import net.named_data.jndn.*;
Andrew Brown3831baf2015-01-19 13:38:52 -080020import net.named_data.jndn.encoding.EncodingException;
andrewsbrowna52bf7d2015-04-06 13:51:53 -070021import net.named_data.jndn.security.SecurityException;
Andrew Brown3831baf2015-01-19 13:38:52 -080022import net.named_data.jndn.util.Blob;
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080023import org.junit.Before;
Andrew Brown3831baf2015-01-19 13:38:52 -080024import org.junit.Test;
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080025
Andrew Brown3831baf2015-01-19 13:38:52 -080026import static org.junit.Assert.*;
27
28/**
Andrew Brownec1b0d02015-02-21 13:11:42 -080029 * Test MockFace functionality
Andrew Brown3831baf2015-01-19 13:38:52 -080030 */
31public class MockFaceTest {
Andrew Brown58191872016-02-05 22:16:57 -080032
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080033 @Before
34 public void setup() throws SecurityException {
35 face = new MockFace();
36 counter = 0;
37 recvData = null;
38 isTimeout = false;
39 exception = null;
40 }
Andrew Brown3831baf2015-01-19 13:38:52 -080041
Andrew Brown3831baf2015-01-19 13:38:52 -080042 @Test
Andrew Brown58191872016-02-05 22:16:57 -080043 public void testExpressingAnInterest() throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080044 // make request
45 expressInterest("/test/with/responses");
Andrew Brown3831baf2015-01-19 13:38:52 -080046
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080047 run(2);
48
49 // add response (after face is connectd)
Andrew Brown3831baf2015-01-19 13:38:52 -080050 Data response = new Data(new Name("/test/with/responses"));
51 response.setContent(new Blob("..."));
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080052 face.receive(response);
Andrew Brown3831baf2015-01-19 13:38:52 -080053
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080054 run(20);
Andrew Brown3831baf2015-01-19 13:38:52 -080055
Andrew Brown58191872016-02-05 22:16:57 -080056 assertNotNull(recvData);
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080057 assertEquals(isTimeout, false);
58 assertEquals(recvData.getName().toString(), "/test/with/responses");
59 assertEquals(recvData.getContent().buf(), new Blob("...").buf());
Andrew Brown3831baf2015-01-19 13:38:52 -080060 }
61
Andrew Brown3831baf2015-01-19 13:38:52 -080062 @Test
Andrew Brown58191872016-02-05 22:16:57 -080063 public void testExpressingAnInterestAfterConfiguration() throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080064 // add response (before face is connected)
65 Data response = new Data(new Name("/test/with/responses"));
66 response.setContent(new Blob("..."));
67 face.receive(response);
Andrew Brown3831baf2015-01-19 13:38:52 -080068
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080069 // make request
70 expressInterest("/test/with/responses");
71
72 run(20);
73
Andrew Brown58191872016-02-05 22:16:57 -080074 assertNotNull(recvData);
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080075 assertEquals(isTimeout, false);
76 assertEquals(recvData.getName().toString(), "/test/with/responses");
77 assertEquals(recvData.getContent().buf(), new Blob("...").buf());
78 }
79
80 @Test
Andrew Brown58191872016-02-05 22:16:57 -080081 public void testInterestTimeouts() throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080082 // make request
83 expressInterest("/some/name");
84
85 run(20);
86
87 assertEquals(recvData, null);
88 assertEquals(isTimeout, true);
89 }
90
91 @Test
Andrew Brown58191872016-02-05 22:16:57 -080092 public void testPrefixRegistration() throws IOException, SecurityException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080093 class State {
94 boolean regFailed = false;
95 boolean regSucceed = false;
96 }
97 final State state = new State();
98
99 logger.info("Register prefix: /test/with/handlers");
Andrew Brown58191872016-02-05 22:16:57 -0800100 face.registerPrefix(new Name("/test/with/handlers"), new OnInterestCallback() {
101 @Override
102 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
103 logger.info("Received interest, responding: " + interest.getName().toUri());
104 Data response = new Data(new Name("/test/with/handlers"));
105 response.setContent(new Blob("..."));
106 try {
107 face.putData(response);
108 } catch (IOException e) {
109 exception = e;
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800110 }
Andrew Brown58191872016-02-05 22:16:57 -0800111 counter++;
Andrew Brown3831baf2015-01-19 13:38:52 -0800112 }
Andrew Brown58191872016-02-05 22:16:57 -0800113 }, new OnRegisterFailed() {
114 @Override
115 public void onRegisterFailed(Name prefix) {
116 logger.info("Prefix registration fails: " + prefix);
117 state.regFailed = true;
118 counter++;
119 }
120 }, new OnRegisterSuccess() {
121 @Override
122 public void onRegisterSuccess(Name prefix, long registeredPrefixId) {
123 logger.info("Prefix registration succeed: " + prefix);
124 state.regSucceed = true;
125 counter++;
126 }
127 });
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800128
129 run(100, 1);
130 assertTrue(state.regSucceed);
131 assertFalse(state.regFailed);
Andrew Brown3831baf2015-01-19 13:38:52 -0800132
133 // make request
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800134 face.receive(new Interest(new Name("/test/with/handlers")));
Andrew Brown3831baf2015-01-19 13:38:52 -0800135
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800136 run(100, 2);
137
138 assertNull(exception);
139
140 assertEquals(face.sentData.size(), 1);
141 assertFalse(isTimeout);
142 assertEquals("/test/with/handlers", face.sentData.get(0).getName().toString());
143 assertEquals(new Blob("...").buf(), face.sentData.get(0).getContent().buf());
Andrew Brown3831baf2015-01-19 13:38:52 -0800144 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800145
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700146 @Test
Andrew Brown58191872016-02-05 22:16:57 -0800147 public void testThatTransportConnectsOnPrefixRegistration() throws IOException, SecurityException {
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700148 assertFalse(face.getTransport().getIsConnected());
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800149 face.registerPrefix(new Name("/fake/prefix"), (OnInterestCallback) null, (OnRegisterFailed) null,
Andrew Brown58191872016-02-05 22:16:57 -0800150 (OnRegisterSuccess) null);
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700151 assertTrue(face.getTransport().getIsConnected());
152 }
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800153
andrewsbrown0f36eee2015-05-07 01:37:48 +0100154 @Test
Andrew Brown58191872016-02-05 22:16:57 -0800155 public void testInterestFilters() throws IOException, SecurityException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800156 class State {
157 boolean regFailed = false;
158 boolean regSucceed = false;
159 }
160 final State state = new State();
161
162 // connect transport
163 face.registerPrefix(new Name("/fake/prefix"), (OnInterestCallback) null, new OnRegisterFailed() {
andrewsbrown0f36eee2015-05-07 01:37:48 +0100164 @Override
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800165 public void onRegisterFailed(Name prefix) {
166 state.regFailed = true;
167 counter++;
168 }
169 }, new OnRegisterSuccess() {
170 @Override
171 public void onRegisterSuccess(Name prefix, long registeredPrefixId) {
172 state.regSucceed = true;
173 counter++;
andrewsbrown0f36eee2015-05-07 01:37:48 +0100174 }
175 });
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800176
177 // set filter
andrewsbrown0f36eee2015-05-07 01:37:48 +0100178 face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() {
179 @Override
180 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800181 counter++;
182 }
183 });
184
185 face.receive(new Interest(new Name("/a/b")).setInterestLifetimeMilliseconds(100));
186
187 run(10, 2);
188
189 assertEquals(2, counter);
190 assertTrue(state.regSucceed);
191 assertFalse(state.regFailed);
192 }
193
194 /////////////////////////////////////////////////////////////////////////////
195
andrewsbrown8e517fa2016-02-12 15:51:19 -0800196 private void run(int limit, int maxCounter) throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800197 // process face until a response is received
198 int allowedLoops = limit;
199 while (counter < maxCounter && allowedLoops > 0) {
200 allowedLoops--;
201 face.processEvents();
202 Thread.sleep(100);
203 }
204 }
205
andrewsbrown8e517fa2016-02-12 15:51:19 -0800206 private void run(int limit) throws IOException, EncodingException, InterruptedException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800207 run(limit, 1);
208 }
209
Andrew Brown58191872016-02-05 22:16:57 -0800210 private void expressInterest(String name) throws IOException {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800211 logger.info("Express interest: " + name);
Andrew Brown58191872016-02-05 22:16:57 -0800212 face.expressInterest(new Interest(new Name(name)).setInterestLifetimeMilliseconds(1000), new OnData() {
213 @Override
214 public void onData(Interest interest, Data data) {
215 counter++;
216 logger.fine("Received data");
217 recvData = data;
218 }
219 }, new OnTimeout() {
220 @Override
221 public void onTimeout(Interest interest) {
222 logger.fine("Received timeout");
223 counter++;
224 isTimeout = true;
225 }
226 });
andrewsbrown0f36eee2015-05-07 01:37:48 +0100227 }
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800228
229 /////////////////////////////////////////////////////////////////////////////
230
231 private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName());
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800232 private MockFace face;
233 private int counter;
234 private Data recvData = null;
235 private boolean isTimeout = false;
236 private Exception exception = null;
Andrew Brown3831baf2015-01-19 13:38:52 -0800237}