Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 533c6ef | 2015-03-03 16:08:41 -0800 | [diff] [blame] | 2 | * 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.mock; |
| 15 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 16 | import net.named_data.jndn.Data; |
| 17 | import net.named_data.jndn.Face; |
| 18 | import net.named_data.jndn.Interest; |
| 19 | import net.named_data.jndn.InterestFilter; |
| 20 | import net.named_data.jndn.Name; |
| 21 | import net.named_data.jndn.OnData; |
| 22 | import net.named_data.jndn.OnInterestCallback; |
| 23 | import net.named_data.jndn.OnRegisterFailed; |
| 24 | import net.named_data.jndn.OnRegisterSuccess; |
| 25 | import net.named_data.jndn.OnTimeout; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 26 | import net.named_data.jndn.encoding.EncodingException; |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 27 | import net.named_data.jndn.security.SecurityException; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 28 | import net.named_data.jndn.util.Blob; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 29 | import org.junit.Before; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 30 | import org.junit.Test; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 32 | import java.io.IOException; |
| 33 | import java.util.logging.Logger; |
| 34 | |
| 35 | import static org.junit.Assert.assertEquals; |
| 36 | import static org.junit.Assert.assertFalse; |
| 37 | import static org.junit.Assert.assertNotNull; |
| 38 | import static org.junit.Assert.assertNull; |
| 39 | import static org.junit.Assert.assertTrue; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 42 | * Test MockFace functionality. |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 43 | */ |
| 44 | public class MockFaceTest { |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 45 | 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 Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 54 | @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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 62 | |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 63 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 64 | public void testExpressingAnInterest() throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 65 | // make request |
| 66 | expressInterest("/test/with/responses"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 68 | run(2); |
| 69 | |
| 70 | // add response (after face is connectd) |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 71 | Data response = new Data(new Name("/test/with/responses")); |
| 72 | response.setContent(new Blob("...")); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 73 | face.receive(response); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 75 | run(20); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 76 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 77 | assertNotNull(recvData); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 78 | assertEquals(isTimeout, false); |
| 79 | assertEquals(recvData.getName().toString(), "/test/with/responses"); |
| 80 | assertEquals(recvData.getContent().buf(), new Blob("...").buf()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 83 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 84 | public void testExpressingAnInterestAfterConfiguration() throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 85 | // 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 90 | // make request |
| 91 | expressInterest("/test/with/responses"); |
| 92 | |
| 93 | run(20); |
| 94 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 95 | assertNotNull(recvData); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 96 | assertEquals(isTimeout, false); |
| 97 | assertEquals(recvData.getName().toString(), "/test/with/responses"); |
| 98 | assertEquals(recvData.getContent().buf(), new Blob("...").buf()); |
| 99 | } |
| 100 | |
| 101 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 102 | public void testInterestTimeouts() throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 103 | // make request |
| 104 | expressInterest("/some/name"); |
| 105 | |
| 106 | run(20); |
| 107 | |
| 108 | assertEquals(recvData, null); |
| 109 | assertEquals(isTimeout, true); |
| 110 | } |
| 111 | |
| 112 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 113 | public void testPrefixRegistration() throws IOException, SecurityException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 114 | class State { |
| 115 | boolean regFailed = false; |
| 116 | boolean regSucceed = false; |
| 117 | } |
| 118 | final State state = new State(); |
| 119 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 120 | LOG.info("Register prefix: /test/with/handlers"); |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 121 | face.registerPrefix(new Name("/test/with/handlers"), new OnInterestCallback() { |
| 122 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 123 | 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 Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 126 | 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 Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 132 | } |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 133 | counter++; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 134 | } |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 135 | }, new OnRegisterFailed() { |
| 136 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 137 | public void onRegisterFailed(final Name prefix) { |
| 138 | LOG.info("Prefix registration fails: " + prefix); |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 139 | state.regFailed = true; |
| 140 | counter++; |
| 141 | } |
| 142 | }, new OnRegisterSuccess() { |
| 143 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 144 | public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) { |
| 145 | LOG.info("Prefix registration succeed: " + prefix); |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 146 | state.regSucceed = true; |
| 147 | counter++; |
| 148 | } |
| 149 | }); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 150 | |
| 151 | run(100, 1); |
| 152 | assertTrue(state.regSucceed); |
| 153 | assertFalse(state.regFailed); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 154 | |
| 155 | // make request |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 156 | face.receive(new Interest(new Name("/test/with/handlers"))); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 157 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 158 | 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 166 | } |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 167 | |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 168 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 169 | public void testThatTransportConnectsOnPrefixRegistration() throws IOException, SecurityException { |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 170 | assertFalse(face.getTransport().getIsConnected()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 171 | face.registerPrefix(new Name("/fake/prefix"), (OnInterestCallback) null, (OnRegisterFailed) null, |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 172 | (OnRegisterSuccess) null); |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 173 | assertTrue(face.getTransport().getIsConnected()); |
| 174 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 175 | |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 176 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 177 | public void testInterestFilters() throws IOException, SecurityException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 178 | 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() { |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 186 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 187 | public void onRegisterFailed(final Name prefix) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 188 | state.regFailed = true; |
| 189 | counter++; |
| 190 | } |
| 191 | }, new OnRegisterSuccess() { |
| 192 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 193 | public void onRegisterSuccess(final Name prefix, final long registeredPrefixId) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 194 | state.regSucceed = true; |
| 195 | counter++; |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 196 | } |
| 197 | }); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 198 | |
| 199 | // set filter |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 200 | face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() { |
| 201 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 202 | public void onInterest(final Name prefix, final Interest interest, final Face face, final long interestFilterId, |
| 203 | final InterestFilter filter) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 204 | 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 Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 219 | private void run(final int limit, final int maxCounter) throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 220 | // 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 Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 228 | |
| 229 | private void run(final int limit) throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 230 | run(limit, 1); |
| 231 | } |
| 232 | |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 233 | private void expressInterest(final String name) throws IOException { |
| 234 | LOG.info("Express interest: " + name); |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 235 | face.expressInterest(new Interest(new Name(name)).setInterestLifetimeMilliseconds(1000), new OnData() { |
| 236 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 237 | public void onData(final Interest interest, final Data data) { |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 238 | counter++; |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 239 | LOG.fine("Received data"); |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 240 | recvData = data; |
| 241 | } |
| 242 | }, new OnTimeout() { |
| 243 | @Override |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 244 | public void onTimeout(final Interest interest) { |
| 245 | LOG.fine("Received timeout"); |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 246 | counter++; |
| 247 | isTimeout = true; |
| 248 | } |
| 249 | }); |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 250 | } |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 251 | } |