Initial commit
diff --git a/src/main/java/com/intel/jndn/mock/MockTransport.java b/src/main/java/com/intel/jndn/mock/MockTransport.java
new file mode 100644
index 0000000..ee9c4e3
--- /dev/null
+++ b/src/main/java/com/intel/jndn/mock/MockTransport.java
@@ -0,0 +1,174 @@
+/*
+ * File name: MockTransport.java
+ *
+ * Purpose: Provide testing functionality for running NDN unit tests without
+ * connecting to the network or requiring an NFD installed.
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.mock;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.encoding.ElementListener;
+import net.named_data.jndn.encoding.ElementReader;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.transport.Transport;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ * Mock the transport class Example: ...
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class MockTransport extends Transport {
+
+ public final static int BUFFER_CAPACITY = 8000;
+ private static final Logger logger = LogManager.getLogger();
+ protected boolean connected;
+ protected ByteBuffer inputBuffer = ByteBuffer.allocate(BUFFER_CAPACITY);
+ protected ByteBuffer outputBuffer = ByteBuffer.allocate(BUFFER_CAPACITY);
+ protected ElementReader elementReader;
+ protected List<Data> outputPackets = new ArrayList<>();
+
+ /**
+ * Retrieve sent bytes
+ *
+ * @return
+ */
+ public ByteBuffer getSentBuffer() {
+ return outputBuffer;
+ }
+
+ /**
+ *
+ */
+ public void clear() {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
+ /**
+ *
+ * @return
+ */
+ public List<Data> getSentDataPackets() {
+ return outputPackets;
+ }
+
+ /**
+ * Retrieve received bytes; this is mocked by adding bytes to the buffer with
+ * respondWith()
+ *
+ * @return
+ */
+ public ByteBuffer getReceivedBuffer() {
+ return inputBuffer;
+ }
+
+ /**
+ *
+ * @param response
+ */
+ public void respondWith(ByteBuffer response) {
+ inputBuffer.put(response);
+ }
+
+ /**
+ *
+ * @param response
+ */
+ public void respondWith(Data response) {
+ respondWith(response.wireEncode().buf());
+ }
+
+ /**
+ * Mock connection
+ *
+ * @param connectionInfo
+ * @param elementListener
+ * @throws IOException
+ */
+ @Override
+ public void connect(Transport.ConnectionInfo connectionInfo,
+ ElementListener elementListener) throws IOException {
+ logger.debug("Connecting...");
+ connected = true;
+ elementReader = new ElementReader(elementListener);
+ }
+
+ /**
+ * Mock sending data to the host; access the data as bytes using
+ * getSentBuffer() or as packets with getSentDataPackets()
+ *
+ * @param data The buffer of data to send. This reads from position() to
+ * limit(), but does not change the position.
+ * @throws IOException For I/O error.
+ */
+ @Override
+ public void send(ByteBuffer data) throws IOException {
+ logger.debug("Sending " + (data.capacity() - data.position()) + " bytes");
+
+ // add to sent bytes
+ outputBuffer.put(data);
+ data.flip();
+
+ // add to sent packets
+ try {
+ Data packet = new Data();
+ packet.wireDecode(data);
+ outputPackets.add(new Data());
+ } catch (EncodingException e) {
+ logger.warn("Failed to parse bytes into a data packet");
+ }
+ }
+
+ /**
+ * Process any data to receive and clear the input buffer; to mock incoming
+ * Data packets, add data to the buffer with respondWith().
+ *
+ * @throws IOException For I/O error.
+ * @throws EncodingException For invalid encoding.
+ */
+ @Override
+ public void processEvents() throws IOException, EncodingException {
+ if (!getIsConnected()) {
+ logger.warn("Not connnected, aborting...");
+ return;
+ }
+
+ // pass data up to face
+ inputBuffer.limit(inputBuffer.capacity());
+ inputBuffer.position(0);
+ elementReader.onReceivedData(inputBuffer);
+
+ // reset buffer
+ inputBuffer = ByteBuffer.allocate(BUFFER_CAPACITY);
+ }
+
+ /**
+ * Check if the transport is connected.
+ *
+ * @return true if connected.
+ */
+ @Override
+ public boolean getIsConnected() {
+ return connected;
+ }
+
+ /**
+ * Close the connection.
+ *
+ * @throws IOException For I/O error.
+ */
+ @Override
+ public void close() throws IOException {
+ logger.debug("Closing...");
+ connected = false;
+ }
+}
diff --git a/src/main/resources/log4j2-test.xml b/src/main/resources/log4j2-test.xml
new file mode 100644
index 0000000..9e29cc3
--- /dev/null
+++ b/src/main/resources/log4j2-test.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="WARN">
+ <Appenders>
+ <Console name="Console" target="SYSTEM_OUT">
+ <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Root level="trace">
+ <AppenderRef ref="Console"/>
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml
new file mode 100644
index 0000000..8f7a167
--- /dev/null
+++ b/src/main/resources/log4j2.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="WARN">
+ <Appenders>
+ <Console name="Console" target="SYSTEM_OUT">
+ <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Root level="error">
+ <AppenderRef ref="Console"/>
+ </Root>
+ </Loggers>
+</Configuration>
\ No newline at end of file
diff --git a/src/test/java/com/intel/jndn/mock/MockTransportTest.java b/src/test/java/com/intel/jndn/mock/MockTransportTest.java
new file mode 100644
index 0000000..137717f
--- /dev/null
+++ b/src/test/java/com/intel/jndn/mock/MockTransportTest.java
@@ -0,0 +1,133 @@
+/*
+ * File name: MockTransportTest.java
+ *
+ * Purpose: Test the MockTransport
+ *
+ * © Copyright Intel Corporation. All rights reserved.
+ * Intel Corporation, 2200 Mission College Boulevard,
+ * Santa Clara, CA 95052-8119, USA
+ */
+package com.intel.jndn.mock;
+
+import java.io.IOException;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Face;
+import net.named_data.jndn.Interest;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.OnData;
+import net.named_data.jndn.OnInterest;
+import net.named_data.jndn.OnRegisterFailed;
+import net.named_data.jndn.encoding.EncodingException;
+import net.named_data.jndn.security.SecurityException;
+import net.named_data.jndn.transport.Transport;
+import net.named_data.jndn.util.Blob;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * Mock the transport class Example: ...
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class MockTransportTest {
+
+ /**
+ * Setup logging
+ */
+ private static final Logger logger = LogManager.getLogger();
+
+ /**
+ * Test of getSentBuffer method, of class MockTransport.
+ * @throws java.io.IOException
+ * @throws net.named_data.jndn.encoding.EncodingException
+ */
+ @Test
+ public void testGetSentBuffer() throws IOException, EncodingException {
+ MockTransport transport = new MockTransport();
+ Face face = new Face(transport, null);
+
+ // setup return data
+ Data response = new Data(new Name("/a/b/c"));
+ response.setContent(new Blob("..."));
+ transport.respondWith(response);
+
+ // express interest on the face
+ final Counter count = new Counter();
+ face.expressInterest(new Interest(new Name("/a/b/c")), new OnData() {
+ @Override
+ public void onData(Interest interest, Data data) {
+ count.inc();
+ logger.debug("Received data");
+ assertEquals(data.getContent().buf(), new Blob("...").buf());
+ }
+ });
+
+ while(count.get() == 0){
+ face.processEvents();
+ }
+ }
+
+ class Counter{
+ int count = 0;
+ public void inc(){
+ count++;
+ }
+ public int get(){
+ return count;
+ }
+ }
+
+ /**
+ * Test of getSentBuffer method, of class MockTransport.
+ */
+// @Test
+// public void testGetSentBuffer() {
+// MockTransport transport = new MockTransport();
+// Face face = new Face(transport, null);
+//
+// // setup return data
+// int typeCode = 101;
+// int[] tlvBytes = new int[]{1, 200};
+//
+// // register prefix
+// try {
+// Name prefix = new Name("/a/b/c");
+// logger.info("Registering prefix: " + prefix.toUri());
+// long id = face.registerPrefix(prefix, new TestOnInterest(), new TestOnRegisterFailed());
+// assertTrue(id > 0);
+// } catch (IOException | SecurityException e) {
+// fail("Failed to register prefix.");
+// }
+// }
+// class TestOnData implements OnData {
+//
+// @Override
+// public void onData(Interest interest, Data data) {
+// assertEquals(data.getName(), new Name("/a/b/c"));
+// assertEquals(data.getContent(), new Blob("..."));
+// }
+// }
+ class TestOnInterest implements OnInterest {
+
+ @Override
+ public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
+ Data data = new Data(interest.getName());
+ data.setContent(new Blob("..."));
+ try {
+ transport.send(data.wireEncode().buf());
+ } catch (IOException e) {
+ fail("Failed to send packet.");
+ }
+ }
+ }
+
+ class TestOnRegisterFailed implements OnRegisterFailed {
+
+ @Override
+ public void onRegisterFailed(Name prefix) {
+ fail("Failed to register prefix.");
+ }
+ }
+}