blob: ea46687f595f51d300f196ae6ddf96738a4d5a5b [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;
Andrew Brown3831baf2015-01-19 13:38:52 -080018import net.named_data.jndn.Data;
andrewsbrown0f36eee2015-05-07 01:37:48 +010019import net.named_data.jndn.Face;
Andrew Brown3831baf2015-01-19 13:38:52 -080020import net.named_data.jndn.Interest;
andrewsbrown0f36eee2015-05-07 01:37:48 +010021import net.named_data.jndn.InterestFilter;
Andrew Brown3831baf2015-01-19 13:38:52 -080022import net.named_data.jndn.Name;
23import net.named_data.jndn.OnData;
24import net.named_data.jndn.OnInterest;
andrewsbrown0f36eee2015-05-07 01:37:48 +010025import net.named_data.jndn.OnInterestCallback;
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.transport.Transport;
29import net.named_data.jndn.util.Blob;
Andrew Brown3831baf2015-01-19 13:38:52 -080030import org.junit.Test;
31import static org.junit.Assert.*;
32
33/**
Andrew Brownec1b0d02015-02-21 13:11:42 -080034 * Test MockFace functionality
Andrew Brown3831baf2015-01-19 13:38:52 -080035 *
36 * @author Andrew Brown <andrew.brown@intel.com>
37 */
38public class MockFaceTest {
39
40 /**
41 * Setup logging
42 */
Andrew Brownec1b0d02015-02-21 13:11:42 -080043 private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName());
Andrew Brown3831baf2015-01-19 13:38:52 -080044
45 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080046 * Test setting responses for specific names
Andrew Brownec1b0d02015-02-21 13:11:42 -080047 *
Andrew Brown3831baf2015-01-19 13:38:52 -080048 * @throws java.io.IOException
49 * @throws net.named_data.jndn.encoding.EncodingException
50 */
51 @Test
52 public void testWithResponses() throws IOException, EncodingException {
53 MockFace face = new MockFace();
54
55 // add response
56 Data response = new Data(new Name("/test/with/responses"));
57 response.setContent(new Blob("..."));
58 face.addResponse(new Name("/test/with/responses"), response);
59
60 // make request
andrewsbrown0f36eee2015-05-07 01:37:48 +010061 final TestCounter count = new TestCounter();
Andrew Brown3831baf2015-01-19 13:38:52 -080062 logger.info("Express interest: /test/with/responses");
63 face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() {
64 @Override
65 public void onData(Interest interest, Data data) {
66 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -080067 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -080068 assertEquals(data.getContent().buf(), new Blob("...").buf());
69 }
70 });
71
72 // process face until a response is received
73 int allowedLoops = 100;
74 while (count.get() == 0 && allowedLoops > 0) {
75 allowedLoops--;
76 face.processEvents();
77 }
78 assertEquals(1, count.get());
79 }
80
81 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080082 * Test serving data dynamically with OnInterest handlers
Andrew Brownec1b0d02015-02-21 13:11:42 -080083 *
Andrew Brown3831baf2015-01-19 13:38:52 -080084 * @throws net.named_data.jndn.encoding.EncodingException
85 * @throws java.io.IOException
86 * @throws net.named_data.jndn.security.SecurityException
87 */
88 @Test
89 public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException {
90 MockFace face = new MockFace();
91
92 // add interest handler
93 logger.info("Register prefix: /test/with/responses");
94 face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() {
95 @Override
96 public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080097 logger.fine("Received interest, responding: " + interest.getName().toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080098 Data response = new Data(new Name("/test/with/handlers"));
99 response.setContent(new Blob("..."));
100 try {
101 transport.send(response.wireEncode().buf());
102 } catch (IOException e) {
103 fail("Failed to send encoded data packet.");
104 }
105 }
106 }, null);
107
108 // make request
andrewsbrown0f36eee2015-05-07 01:37:48 +0100109 final TestCounter count = new TestCounter();
Andrew Brown3831baf2015-01-19 13:38:52 -0800110 logger.info("Express interest: /test/with/responses");
111 face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() {
112 @Override
113 public void onData(Interest interest, Data data) {
114 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -0800115 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -0800116 assertEquals(data.getContent().buf(), new Blob("...").buf());
117 }
118 });
119
120 // process faces until a response is received
121 int allowedLoops = 100;
122 while (count.get() == 0 && allowedLoops > 0) {
123 allowedLoops--;
124 face.processEvents();
125 }
126 assertEquals(1, count.get());
127 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800128
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700129 /**
130 * Ensure registering a prefix connects the underlying transport
131 *
132 * @throws IOException
133 * @throws SecurityException
134 */
135 @Test
136 public void testRegistrationConnectsTransport() throws IOException, SecurityException {
137 MockFace face = new MockFace();
138 assertFalse(face.getTransport().getIsConnected());
andrewsbrown1f28bcf2015-04-20 13:29:20 -0700139 face.registerPrefix(new Name("/fake/prefix"), (OnInterest) null, null);
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700140 assertTrue(face.getTransport().getIsConnected());
141 }
andrewsbrown0f36eee2015-05-07 01:37:48 +0100142
143 /**
144 * Test that interest filters work as expected
145 */
146 @Test
147 public void testInterestFilters() throws IOException, SecurityException, EncodingException {
148 MockFace face = new MockFace();
149
150 final TestCounter count = new TestCounter();
151 face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() {
152 @Override
153 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
154 count.inc();
155 }
156 });
157
158 face.expressInterest(new Interest(new Name("/a/b")).setInterestLifetimeMilliseconds(100), null);
159 face.processEvents();
160
161 assertEquals(1, count.get());
162 }
163
164 @Test
165 public void testResponseFromInsideElementReader() throws IOException, SecurityException, EncodingException{
166 MockFace face = new MockFace();
167 face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() {
168 @Override
169 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
170 try {
171 face.putData(new Data(interest.getName()).setContent(new Blob("......")));
172 } catch (IOException ex) {
173 fail("Failed to put data.");
174 }
175 }
176 });
177
178 final TestCounter count = new TestCounter();
179 face.expressInterest(new Interest(new Name("/a/b/c")), new OnData() {
180 @Override
181 public void onData(Interest interest, Data data) {
182 logger.info("Data returned: " + data.getContent().toString());
183 count.inc();
184 }
185 });
186 assertEquals(0, count.get());
187
188 face.processEvents();
189 face.processEvents(); // the second processEvents() is required because the InterestFilter sends data from within the first processEvents loop
190 assertEquals(1, count.get());
191 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800192}