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.transport.Transport; |
| 23 | import net.named_data.jndn.util.Blob; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import static org.junit.Assert.*; |
| 26 | |
| 27 | /** |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 28 | * Test MockFace functionality |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 29 | * |
| 30 | * @author Andrew Brown <andrew.brown@intel.com> |
| 31 | */ |
| 32 | public class MockFaceTest { |
| 33 | |
| 34 | /** |
| 35 | * Setup logging |
| 36 | */ |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 37 | private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame] | 40 | * Test setting responses for specific names |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 41 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 42 | * @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 |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 55 | final TestCounter count = new TestCounter(); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 56 | 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 Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 61 | logger.fine("Received data"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 62 | 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 Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame] | 76 | * Test serving data dynamically with OnInterest handlers |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 77 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 78 | * @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 Afanasyev | 8e9330f | 2016-01-25 19:13:40 -0800 | [diff] [blame^] | 88 | face.registerPrefix(new Name("/test/with/handlers"), new OnInterestCallback() { |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 89 | @Override |
Alexander Afanasyev | 8e9330f | 2016-01-25 19:13:40 -0800 | [diff] [blame^] | 90 | public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) { |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 91 | logger.fine("Received interest, responding: " + interest.getName().toUri()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 92 | Data response = new Data(new Name("/test/with/handlers")); |
| 93 | response.setContent(new Blob("...")); |
| 94 | try { |
Alexander Afanasyev | 8e9330f | 2016-01-25 19:13:40 -0800 | [diff] [blame^] | 95 | face.putData(response); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 96 | } catch (IOException e) { |
| 97 | fail("Failed to send encoded data packet."); |
| 98 | } |
| 99 | } |
| 100 | }, null); |
| 101 | |
| 102 | // make request |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 103 | final TestCounter count = new TestCounter(); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 104 | 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 Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 109 | logger.fine("Received data"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 110 | 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 122 | |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 123 | /** |
| 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()); |
andrewsbrown | 1f28bcf | 2015-04-20 13:29:20 -0700 | [diff] [blame] | 133 | face.registerPrefix(new Name("/fake/prefix"), (OnInterest) null, null); |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 134 | assertTrue(face.getTransport().getIsConnected()); |
| 135 | } |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame] | 136 | |
| 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 186 | } |