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 |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
andrewsbrown | 533c6ef | 2015-03-03 16:08:41 -0800 | [diff] [blame] | 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 | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame^] | 16 | import net.named_data.jndn.ControlParameters; |
| 17 | import net.named_data.jndn.ControlResponse; |
| 18 | import net.named_data.jndn.Data; |
| 19 | import net.named_data.jndn.Face; |
| 20 | import net.named_data.jndn.Interest; |
| 21 | import net.named_data.jndn.Name; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 22 | import net.named_data.jndn.encoding.EncodingException; |
| 23 | import net.named_data.jndn.encoding.TlvWireFormat; |
| 24 | import net.named_data.jndn.encoding.tlv.Tlv; |
| 25 | import net.named_data.jndn.encoding.tlv.TlvDecoder; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 26 | import net.named_data.jndn.security.KeyChain; |
| 27 | import net.named_data.jndn.security.SecurityException; |
| 28 | import net.named_data.jndn.transport.Transport; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 29 | |
andrewsbrown | 1f28bcf | 2015-04-20 13:29:20 -0700 | [diff] [blame] | 30 | import java.nio.ByteBuffer; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 31 | import java.util.ArrayList; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 32 | import java.util.List; |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 33 | import java.util.logging.Level; |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 34 | import java.util.logging.Logger; |
Alexander Afanasyev | 8e9330f | 2016-01-25 19:13:40 -0800 | [diff] [blame] | 35 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 36 | /** |
| 37 | * A client-side face for unit testing |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 38 | * |
| 39 | * @author Alexander Afanasyev, <aa@cs.ucla.edu> |
| 40 | * @author Andrew Brown <andrew.brown@intel.com> |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 41 | */ |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 42 | public class MockFace extends Face { |
| 43 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 44 | /** |
| 45 | * API for handling {@link Interest}s |
| 46 | */ |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 47 | public interface SignalOnSendInterest { |
| 48 | void emit(Interest interest) throws EncodingException, SecurityException; |
| 49 | } |
| 50 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 51 | /** |
| 52 | * API for handling {@link Data}s |
| 53 | */ |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 54 | public interface SignalOnSendData { |
| 55 | void emit(Data data); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Options for MockFace |
| 60 | */ |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 61 | public static class Options { |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame^] | 62 | private boolean enablePacketLogging = false; |
| 63 | private boolean enableRegistrationReply = false; |
| 64 | |
| 65 | public boolean isEnablePacketLogging() { |
| 66 | return enablePacketLogging; |
| 67 | } |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame^] | 70 | * Enable/disable packet logging |
| 71 | * |
| 72 | * @param enablePacketLogging If true, packets sent out of MockFace will be appended to a container |
| 73 | * @return this |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 74 | */ |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame^] | 75 | public Options setEnablePacketLogging(boolean enablePacketLogging) { |
| 76 | this.enablePacketLogging = enablePacketLogging; |
| 77 | return this; |
| 78 | } |
| 79 | |
| 80 | public boolean isEnableRegistrationReply() { |
| 81 | return enableRegistrationReply; |
| 82 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame^] | 85 | * Enable/disable prefix registration mocking |
| 86 | * |
| 87 | * @param enableRegistrationReply If true, prefix registration command will be automatically replied with a |
| 88 | * successful response |
| 89 | * @return this |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 90 | */ |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame^] | 91 | public Options setEnableRegistrationReply(boolean enableRegistrationReply) { |
| 92 | this.enableRegistrationReply = enableRegistrationReply; |
| 93 | return this; |
| 94 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 95 | } |
| 96 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 97 | /** |
| 98 | * Default options |
| 99 | */ |
Alexander Afanasyev | 522327e | 2016-02-19 19:40:31 -0800 | [diff] [blame^] | 100 | public static final Options DEFAULT_OPTIONS = new Options() |
| 101 | .setEnablePacketLogging(true) |
| 102 | .setEnableRegistrationReply(true); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 105 | * Create MockFace that logs packets in {@link #sentInterests} and |
| 106 | * {@link #sentData} and emulates NFD prefix registration |
| 107 | * |
| 108 | * @throws SecurityException should not be thrown by this test class |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 109 | */ |
| 110 | public MockFace() throws SecurityException { |
| 111 | this(DEFAULT_OPTIONS); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Create MockFace with the specified options |
| 116 | * <p> |
| 117 | * To create Face that does not log packets: |
| 118 | * <pre> |
| 119 | * new MockFace(new Options()); |
| 120 | * // use onSendInterest.add(handler) and onSendData.add(handler) |
| 121 | * // to add custom logic when Interest or Data packet are sent |
| 122 | * // from the upper level (to transport) |
| 123 | * </pre> |
| 124 | * |
| 125 | * To create Face that just logs packets in sentInterests and sentData: |
| 126 | * <pre> |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 127 | * new MockFace(new Options(){ enablePacketLogging = true; }); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 128 | * </pre> |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 129 | * |
| 130 | * @param options see {@link Options} |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 131 | */ |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 132 | public MockFace(Options options) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 133 | super(new MockFaceTransport(), null); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 134 | transport = (MockFaceTransport) node_.getTransport(); |
| 135 | transport.setOnSendBlock(new OnIncomingPacket()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 136 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 137 | try { |
| 138 | keyChain = MockKeyChain.configure(new Name("/mock/key")); |
| 139 | setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName()); |
| 140 | } catch (SecurityException ex) { |
| 141 | LOGGER.log(Level.SEVERE, "Unexpected error in MockKeyChain; this class should never throw", ex); |
| 142 | throw new Error(ex); |
| 143 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 144 | |
| 145 | if (options.enablePacketLogging) { |
| 146 | onSendInterest.add(new SignalOnSendInterest() { |
| 147 | @Override |
| 148 | public void emit(Interest interest) { |
| 149 | sentInterests.add(interest); |
| 150 | } |
| 151 | }); |
| 152 | |
| 153 | onSendData.add(new SignalOnSendData() { |
| 154 | @Override |
| 155 | public void emit(Data data) { |
| 156 | sentData.add(data); |
| 157 | } |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | if (options.enableRegistrationReply) { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 162 | onSendInterest.add(new OnPrefixRegistration()); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Route incoming packets to the correct callbacks |
| 168 | */ |
| 169 | private class OnIncomingPacket implements MockFaceTransport.OnSendBlockSignal { |
| 170 | |
| 171 | @Override |
| 172 | public void emit(ByteBuffer buffer) throws EncodingException, SecurityException { |
| 173 | // @todo Implement NDNLP processing |
| 174 | |
| 175 | if (buffer.get(0) == Tlv.Interest || buffer.get(0) == Tlv.Data) { |
| 176 | TlvDecoder decoder = new TlvDecoder(buffer); |
| 177 | if (decoder.peekType(Tlv.Interest, buffer.remaining())) { |
| 178 | Interest interest = new Interest(); |
| 179 | interest.wireDecode(buffer, TlvWireFormat.get()); |
| 180 | |
| 181 | for (SignalOnSendInterest signal : onSendInterest) { |
| 182 | signal.emit(interest); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 183 | } |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 184 | } else if (decoder.peekType(Tlv.Data, buffer.remaining())) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 185 | Data data = new Data(); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 186 | data.wireDecode(buffer, TlvWireFormat.get()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 187 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 188 | for (SignalOnSendData signal : onSendData) { |
| 189 | signal.emit(data); |
| 190 | } |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 191 | } |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 192 | } else { |
| 193 | LOGGER.info("Received an unknown packet"); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Handle prefix registration requests |
| 200 | */ |
| 201 | private class OnPrefixRegistration implements SignalOnSendInterest { |
| 202 | |
| 203 | @Override |
| 204 | public void emit(Interest interest) throws EncodingException, SecurityException { |
| 205 | final Name localhostRegistration = new Name("/localhost/nfd/rib"); |
| 206 | if (!interest.getName().getPrefix(3).equals(localhostRegistration)) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | ControlParameters params = new ControlParameters(); |
| 211 | params.wireDecode(interest.getName().get(-5).getValue()); |
| 212 | params.setFaceId(1); |
| 213 | params.setOrigin(0); |
| 214 | |
| 215 | if ("register".equals(interest.getName().get(3).toString())) { |
| 216 | params.setCost(0); |
| 217 | } |
| 218 | |
andrewsbrown | 6d70028 | 2016-02-16 18:29:46 -0800 | [diff] [blame] | 219 | ControlResponse response = new ControlResponse(); |
| 220 | response.setStatusCode(200); |
| 221 | response.setStatusText("OK"); |
| 222 | response.setBodyAsControlParameters(params); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 223 | |
| 224 | Data data = new Data(); |
| 225 | data.setName(interest.getName()); |
andrewsbrown | 6d70028 | 2016-02-16 18:29:46 -0800 | [diff] [blame] | 226 | data.setContent(response.wireEncode()); |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 227 | keyChain.sign(data); |
| 228 | |
| 229 | receive(data); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Mock reception of the Interest packet on the Face (from transport) |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 235 | * |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 236 | * @param interest the mock-remote interest to add to the PIT |
| 237 | * @throws EncodingException if packet encoding fails (it should not) |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 238 | */ |
| 239 | public void receive(Interest interest) throws EncodingException { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 240 | transport.receive(interest.wireEncode().buf()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Mock reception of the Data packet on the Face (from transport) |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 245 | * |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 246 | * @param data the mock-remote data to add to the CS |
| 247 | * @throws EncodingException if packet encoding fails (it should not) |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 248 | */ |
| 249 | public void receive(Data data) throws EncodingException { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 250 | transport.receive(data.wireEncode().buf()); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 253 | /** |
| 254 | * @return the transport for this face |
| 255 | */ |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 256 | public Transport getTransport() { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 257 | return transport; |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 260 | /** |
| 261 | * Interests sent out of this MockFace |
| 262 | * <p> |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 263 | * Sent Interests are appended to this container if options.enablePacketLogger |
| 264 | * is true. User of this class is responsible for cleaning up the container, |
| 265 | * if necessary. After .expressInterest, .processEvents must be called before |
| 266 | * the Interest would show up here. |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 267 | */ |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 268 | public final List<Interest> sentInterests = new ArrayList<>(); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * Data sent out of this MockFace |
| 272 | * <p> |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 273 | * Sent Data are appended to this container if options.enablePacketLogger is |
| 274 | * true. User of this class is responsible for cleaning up the container, if |
| 275 | * necessary. After .put, .processEvents must be called before the Data would |
| 276 | * show up here. |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 277 | */ |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 278 | public final List<Data> sentData = new ArrayList<>(); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * Emits whenever an Interest is sent |
| 282 | * <p> |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 283 | * After .expressInterest, .processEvents must be called before this signal |
| 284 | * would be emitted. |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 285 | */ |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 286 | public final List<SignalOnSendInterest> onSendInterest = new ArrayList<>(); |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * Emits whenever a Data packet is sent |
| 290 | * <p> |
Andrew Brown | 5819187 | 2016-02-05 22:16:57 -0800 | [diff] [blame] | 291 | * After .putData, .processEvents must be called before this signal would be |
| 292 | * emitted. |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 293 | */ |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 294 | public final List<SignalOnSendData> onSendData = new ArrayList<>(); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 295 | |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 296 | private static final Logger LOGGER = Logger.getLogger(MockFace.class.getName()); |
| 297 | private MockFaceTransport transport; |
| 298 | private KeyChain keyChain; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 299 | } |