blob: f68c3313339e5e73d05791befc7c2c96da194704 [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.transport.Transport;
23import net.named_data.jndn.util.Blob;
Andrew Brown3831baf2015-01-19 13:38:52 -080024import org.junit.Test;
25import static org.junit.Assert.*;
26
27/**
Andrew Brownec1b0d02015-02-21 13:11:42 -080028 * Test MockFace functionality
Andrew Brown3831baf2015-01-19 13:38:52 -080029 *
30 * @author Andrew Brown <andrew.brown@intel.com>
31 */
32public class MockFaceTest {
33
34 /**
35 * Setup logging
36 */
Andrew Brownec1b0d02015-02-21 13:11:42 -080037 private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName());
Andrew Brown3831baf2015-01-19 13:38:52 -080038
39 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080040 * Test setting responses for specific names
Andrew Brownec1b0d02015-02-21 13:11:42 -080041 *
Andrew Brown3831baf2015-01-19 13:38:52 -080042 * @throws java.io.IOException
43 * @throws net.named_data.jndn.encoding.EncodingException
44 */
45 @Test
46 public void testWithResponses() throws IOException, EncodingException {
47 MockFace face = new MockFace();
48
49 // add response
50 Data response = new Data(new Name("/test/with/responses"));
51 response.setContent(new Blob("..."));
52 face.addResponse(new Name("/test/with/responses"), response);
53
54 // make request
andrewsbrown0f36eee2015-05-07 01:37:48 +010055 final TestCounter count = new TestCounter();
Andrew Brown3831baf2015-01-19 13:38:52 -080056 logger.info("Express interest: /test/with/responses");
57 face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() {
58 @Override
59 public void onData(Interest interest, Data data) {
60 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -080061 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -080062 assertEquals(data.getContent().buf(), new Blob("...").buf());
63 }
64 });
65
66 // process face until a response is received
67 int allowedLoops = 100;
68 while (count.get() == 0 && allowedLoops > 0) {
69 allowedLoops--;
70 face.processEvents();
71 }
72 assertEquals(1, count.get());
73 }
74
75 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080076 * Test serving data dynamically with OnInterest handlers
Andrew Brownec1b0d02015-02-21 13:11:42 -080077 *
Andrew Brown3831baf2015-01-19 13:38:52 -080078 * @throws net.named_data.jndn.encoding.EncodingException
79 * @throws java.io.IOException
80 * @throws net.named_data.jndn.security.SecurityException
81 */
82 @Test
83 public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException {
84 MockFace face = new MockFace();
85
86 // add interest handler
87 logger.info("Register prefix: /test/with/responses");
Alexander Afanasyev8e9330f2016-01-25 19:13:40 -080088 face.registerPrefix(new Name("/test/with/handlers"), new OnInterestCallback() {
Andrew Brown3831baf2015-01-19 13:38:52 -080089 @Override
Alexander Afanasyev8e9330f2016-01-25 19:13:40 -080090 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080091 logger.fine("Received interest, responding: " + interest.getName().toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080092 Data response = new Data(new Name("/test/with/handlers"));
93 response.setContent(new Blob("..."));
94 try {
Alexander Afanasyev8e9330f2016-01-25 19:13:40 -080095 face.putData(response);
Andrew Brown3831baf2015-01-19 13:38:52 -080096 } catch (IOException e) {
97 fail("Failed to send encoded data packet.");
98 }
99 }
100 }, null);
101
102 // make request
andrewsbrown0f36eee2015-05-07 01:37:48 +0100103 final TestCounter count = new TestCounter();
Andrew Brown3831baf2015-01-19 13:38:52 -0800104 logger.info("Express interest: /test/with/responses");
105 face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() {
106 @Override
107 public void onData(Interest interest, Data data) {
108 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -0800109 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -0800110 assertEquals(data.getContent().buf(), new Blob("...").buf());
111 }
112 });
113
114 // process faces until a response is received
115 int allowedLoops = 100;
116 while (count.get() == 0 && allowedLoops > 0) {
117 allowedLoops--;
118 face.processEvents();
119 }
120 assertEquals(1, count.get());
121 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800122
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700123 /**
124 * Ensure registering a prefix connects the underlying transport
125 *
126 * @throws IOException
127 * @throws SecurityException
128 */
129 @Test
130 public void testRegistrationConnectsTransport() throws IOException, SecurityException {
131 MockFace face = new MockFace();
132 assertFalse(face.getTransport().getIsConnected());
andrewsbrown1f28bcf2015-04-20 13:29:20 -0700133 face.registerPrefix(new Name("/fake/prefix"), (OnInterest) null, null);
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700134 assertTrue(face.getTransport().getIsConnected());
135 }
andrewsbrown0f36eee2015-05-07 01:37:48 +0100136
137 /**
138 * Test that interest filters work as expected
139 */
140 @Test
141 public void testInterestFilters() throws IOException, SecurityException, EncodingException {
142 MockFace face = new MockFace();
143
144 final TestCounter count = new TestCounter();
145 face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() {
146 @Override
147 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
148 count.inc();
149 }
150 });
151
152 face.expressInterest(new Interest(new Name("/a/b")).setInterestLifetimeMilliseconds(100), null);
153 face.processEvents();
154
155 assertEquals(1, count.get());
156 }
157
158 @Test
159 public void testResponseFromInsideElementReader() throws IOException, SecurityException, EncodingException{
160 MockFace face = new MockFace();
161 face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() {
162 @Override
163 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
164 try {
165 face.putData(new Data(interest.getName()).setContent(new Blob("......")));
166 } catch (IOException ex) {
167 fail("Failed to put data.");
168 }
169 }
170 });
171
172 final TestCounter count = new TestCounter();
173 face.expressInterest(new Interest(new Name("/a/b/c")), new OnData() {
174 @Override
175 public void onData(Interest interest, Data data) {
176 logger.info("Data returned: " + data.getContent().toString());
177 count.inc();
178 }
179 });
180 assertEquals(0, count.get());
181
182 face.processEvents();
183 face.processEvents(); // the second processEvents() is required because the InterestFilter sends data from within the first processEvents loop
184 assertEquals(1, count.get());
185 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800186}