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 | |
| 16 | import java.io.IOException; |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 17 | import java.util.logging.Logger; |
Alexander Afanasyev | 8e9330f | 2016-01-25 19:13:40 -0800 | [diff] [blame] | 18 | |
| 19 | import net.named_data.jndn.*; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 20 | import net.named_data.jndn.encoding.EncodingException; |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 21 | import net.named_data.jndn.security.SecurityException; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 22 | import net.named_data.jndn.util.Blob; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 23 | import org.junit.Before; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 24 | import org.junit.Test; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 25 | |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 26 | import static org.junit.Assert.*; |
| 27 | |
| 28 | /** |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 29 | * Test MockFace functionality |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 30 | */ |
| 31 | public class MockFaceTest { |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 33 | @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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 41 | |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 42 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 43 | public void testExpressingAnInterest() throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 44 | // make request |
| 45 | expressInterest("/test/with/responses"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 47 | run(2); |
| 48 | |
| 49 | // add response (after face is connectd) |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 50 | Data response = new Data(new Name("/test/with/responses")); |
| 51 | response.setContent(new Blob("...")); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 52 | face.receive(response); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 54 | run(20); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 55 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 56 | assertNotNull(recvData); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 57 | assertEquals(isTimeout, false); |
| 58 | assertEquals(recvData.getName().toString(), "/test/with/responses"); |
| 59 | assertEquals(recvData.getContent().buf(), new Blob("...").buf()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 62 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 63 | public void testExpressingAnInterestAfterConfiguration() throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 64 | // 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 69 | // make request |
| 70 | expressInterest("/test/with/responses"); |
| 71 | |
| 72 | run(20); |
| 73 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 74 | assertNotNull(recvData); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 75 | assertEquals(isTimeout, false); |
| 76 | assertEquals(recvData.getName().toString(), "/test/with/responses"); |
| 77 | assertEquals(recvData.getContent().buf(), new Blob("...").buf()); |
| 78 | } |
| 79 | |
| 80 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 81 | public void testInterestTimeouts() throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 82 | // make request |
| 83 | expressInterest("/some/name"); |
| 84 | |
| 85 | run(20); |
| 86 | |
| 87 | assertEquals(recvData, null); |
| 88 | assertEquals(isTimeout, true); |
| 89 | } |
| 90 | |
| 91 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 92 | public void testPrefixRegistration() throws IOException, SecurityException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 93 | 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 Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 100 | 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 Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 110 | } |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 111 | counter++; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 112 | } |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 113 | }, 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 Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 128 | |
| 129 | run(100, 1); |
| 130 | assertTrue(state.regSucceed); |
| 131 | assertFalse(state.regFailed); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 132 | |
| 133 | // make request |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 134 | face.receive(new Interest(new Name("/test/with/handlers"))); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 136 | 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 144 | } |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 145 | |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 146 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 147 | public void testThatTransportConnectsOnPrefixRegistration() throws IOException, SecurityException { |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 148 | assertFalse(face.getTransport().getIsConnected()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 149 | face.registerPrefix(new Name("/fake/prefix"), (OnInterestCallback) null, (OnRegisterFailed) null, |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 150 | (OnRegisterSuccess) null); |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 151 | assertTrue(face.getTransport().getIsConnected()); |
| 152 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 153 | |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 154 | @Test |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 155 | public void testInterestFilters() throws IOException, SecurityException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 156 | 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() { |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 164 | @Override |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 165 | 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++; |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 174 | } |
| 175 | }); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 176 | |
| 177 | // set filter |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 178 | 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 Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 181 | 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 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 196 | private void run(int limit, int maxCounter) throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 197 | // 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 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 206 | private void run(int limit) throws IOException, EncodingException, InterruptedException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 207 | run(limit, 1); |
| 208 | } |
| 209 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 210 | private void expressInterest(String name) throws IOException { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 211 | logger.info("Express interest: " + name); |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 212 | 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 | }); |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 227 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 228 | |
| 229 | ///////////////////////////////////////////////////////////////////////////// |
| 230 | |
| 231 | private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 232 | private MockFace face; |
| 233 | private int counter; |
| 234 | private Data recvData = null; |
| 235 | private boolean isTimeout = false; |
| 236 | private Exception exception = null; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 237 | } |