blob: 144a05e3b30bef2c576ede3ab1992e89387ef2e3 [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 {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080032 @Before
33 public void setup() throws SecurityException {
34 face = new MockFace();
35 counter = 0;
36 recvData = null;
37 isTimeout = false;
38 exception = null;
39 }
Andrew Brown3831baf2015-01-19 13:38:52 -080040
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080041 /////////////////////////////////////////////////////////////////////////////
Andrew Brown3831baf2015-01-19 13:38:52 -080042
Andrew Brown3831baf2015-01-19 13:38:52 -080043 @Test
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080044 public void ExpressInterest() throws IOException, EncodingException, InterruptedException {
45 // make request
46 expressInterest("/test/with/responses");
Andrew Brown3831baf2015-01-19 13:38:52 -080047
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080048 run(2);
49
50 // add response (after face is connectd)
Andrew Brown3831baf2015-01-19 13:38:52 -080051 Data response = new Data(new Name("/test/with/responses"));
52 response.setContent(new Blob("..."));
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080053 face.receive(response);
Andrew Brown3831baf2015-01-19 13:38:52 -080054
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080055 run(20);
Andrew Brown3831baf2015-01-19 13:38:52 -080056
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080057 assertNotEquals(recvData, null);
58 assertEquals(isTimeout, false);
59 assertEquals(recvData.getName().toString(), "/test/with/responses");
60 assertEquals(recvData.getContent().buf(), new Blob("...").buf());
Andrew Brown3831baf2015-01-19 13:38:52 -080061 }
62
Andrew Brown3831baf2015-01-19 13:38:52 -080063 @Test
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080064 public void ExpressInterest2() throws IOException, EncodingException, InterruptedException {
65 // add response (before face is connected)
66 Data response = new Data(new Name("/test/with/responses"));
67 response.setContent(new Blob("..."));
68 face.receive(response);
Andrew Brown3831baf2015-01-19 13:38:52 -080069
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080070 // make request
71 expressInterest("/test/with/responses");
72
73 run(20);
74
75 assertNotEquals(recvData, null);
76 assertEquals(isTimeout, false);
77 assertEquals(recvData.getName().toString(), "/test/with/responses");
78 assertEquals(recvData.getContent().buf(), new Blob("...").buf());
79 }
80
81 @Test
82 public void ExpressInterestTimeout() throws IOException, EncodingException, InterruptedException {
83 // make request
84 expressInterest("/some/name");
85
86 run(20);
87
88 assertEquals(recvData, null);
89 assertEquals(isTimeout, true);
90 }
91
92 @Test
93 public void RegisterPrefix() throws IOException, SecurityException, EncodingException, InterruptedException {
94 class State {
95 boolean regFailed = false;
96 boolean regSucceed = false;
97 }
98 final State state = new State();
99
100 logger.info("Register prefix: /test/with/handlers");
101 face.registerPrefix(new Name("/test/with/handlers"),
102 new OnInterestCallback() {
103 @Override
104 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
105 logger.info("Received interest, responding: " + interest.getName().toUri());
106 Data response = new Data(new Name("/test/with/handlers"));
107 response.setContent(new Blob("..."));
108 try {
109 face.putData(response);
110 } catch (IOException e) {
111 exception = e;
112 }
113 counter++;
114 }
115 },
116 new OnRegisterFailed() {
117 @Override
118 public void onRegisterFailed(Name prefix) {
119 logger.info("Prefix registration fails: " + prefix);
120 state.regFailed = true;
121 counter++;
122 }
123 },
124 new OnRegisterSuccess() {
125 @Override
126 public void onRegisterSuccess(Name prefix, long registeredPrefixId) {
127 logger.info("Prefix registration succeed: " + prefix);
128 state.regSucceed = true;
129 counter++;
Andrew Brown3831baf2015-01-19 13:38:52 -0800130 }
131 }
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800132 );
133
134 run(100, 1);
135 assertTrue(state.regSucceed);
136 assertFalse(state.regFailed);
Andrew Brown3831baf2015-01-19 13:38:52 -0800137
138 // make request
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800139 face.receive(new Interest(new Name("/test/with/handlers")));
Andrew Brown3831baf2015-01-19 13:38:52 -0800140
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800141 run(100, 2);
142
143 assertNull(exception);
144
145 assertEquals(face.sentData.size(), 1);
146 assertFalse(isTimeout);
147 assertEquals("/test/with/handlers", face.sentData.get(0).getName().toString());
148 assertEquals(new Blob("...").buf(), face.sentData.get(0).getContent().buf());
Andrew Brown3831baf2015-01-19 13:38:52 -0800149 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800150
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700151 @Test
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800152 public void RegistrationPrefixConnectTransport() throws IOException, SecurityException {
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700153 assertFalse(face.getTransport().getIsConnected());
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800154 face.registerPrefix(new Name("/fake/prefix"), (OnInterestCallback) null, (OnRegisterFailed) null,
155 (OnRegisterSuccess) null);
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700156 assertTrue(face.getTransport().getIsConnected());
157 }
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800158
andrewsbrown0f36eee2015-05-07 01:37:48 +0100159 @Test
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800160 public void SetInterestFilter() throws IOException, SecurityException, EncodingException, InterruptedException {
161 class State {
162 boolean regFailed = false;
163 boolean regSucceed = false;
164 }
165 final State state = new State();
166
167 // connect transport
168 face.registerPrefix(new Name("/fake/prefix"), (OnInterestCallback) null, new OnRegisterFailed() {
andrewsbrown0f36eee2015-05-07 01:37:48 +0100169 @Override
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800170 public void onRegisterFailed(Name prefix) {
171 state.regFailed = true;
172 counter++;
173 }
174 }, new OnRegisterSuccess() {
175 @Override
176 public void onRegisterSuccess(Name prefix, long registeredPrefixId) {
177 state.regSucceed = true;
178 counter++;
andrewsbrown0f36eee2015-05-07 01:37:48 +0100179 }
180 });
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800181
182 // set filter
andrewsbrown0f36eee2015-05-07 01:37:48 +0100183 face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() {
184 @Override
185 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800186 counter++;
187 }
188 });
189
190 face.receive(new Interest(new Name("/a/b")).setInterestLifetimeMilliseconds(100));
191
192 run(10, 2);
193
194 assertEquals(2, counter);
195 assertTrue(state.regSucceed);
196 assertFalse(state.regFailed);
197 }
198
199 /////////////////////////////////////////////////////////////////////////////
200
201 private void
202 run(int limit, int maxCounter) throws IOException, EncodingException, InterruptedException {
203 // process face until a response is received
204 int allowedLoops = limit;
205 while (counter < maxCounter && allowedLoops > 0) {
206 allowedLoops--;
207 face.processEvents();
208 Thread.sleep(100);
209 }
210 }
211
212 private void
213 run(int limit) throws IOException, EncodingException, InterruptedException {
214 run(limit, 1);
215 }
216
217 private void
218 expressInterest(String name) throws IOException {
219 logger.info("Express interest: " + name);
220 face.expressInterest(new Interest(new Name(name)).setInterestLifetimeMilliseconds(1000), new
221 OnData() {
222 @Override
223 public void onData(Interest interest, Data data) {
224 counter++;
225 logger.fine("Received data");
226 recvData = data;
227 }
228 },
229 new OnTimeout() {
230 @Override
231 public void onTimeout(Interest interest) {
232 logger.fine("Received timeout");
233 counter++;
234 isTimeout = true;
andrewsbrown0f36eee2015-05-07 01:37:48 +0100235 }
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800236 });
andrewsbrown0f36eee2015-05-07 01:37:48 +0100237 }
Alexander Afanasyev83a26d32016-01-26 01:04:32 -0800238
239 /////////////////////////////////////////////////////////////////////////////
240
241 private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName());
242
243 private MockFace face;
244 private int counter;
245 private Data recvData = null;
246 private boolean isTimeout = false;
247 private Exception exception = null;
Andrew Brown3831baf2015-01-19 13:38:52 -0800248}