Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 4feb2da | 2015-03-03 16:05:29 -0800 | [diff] [blame] | 2 | * jndn-utils |
| 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 | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.utils; |
| 15 | |
| 16 | import com.intel.jndn.mock.MockFace; |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 17 | import com.intel.jndn.utils.client.SegmentedFutureData; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 18 | import java.io.IOException; |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame^] | 19 | import java.util.ArrayList; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 20 | import java.util.List; |
| 21 | import java.util.concurrent.ExecutionException; |
| 22 | import java.util.concurrent.Future; |
| 23 | import java.util.logging.Logger; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 24 | import net.named_data.jndn.Data; |
| 25 | import net.named_data.jndn.Interest; |
| 26 | import net.named_data.jndn.Name; |
| 27 | import net.named_data.jndn.Name.Component; |
| 28 | import net.named_data.jndn.OnInterest; |
| 29 | import net.named_data.jndn.transport.Transport; |
| 30 | import net.named_data.jndn.util.Blob; |
| 31 | import org.junit.Test; |
| 32 | import static org.junit.Assert.*; |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 33 | import org.junit.Before; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Test SegmentedClient functionality. |
| 37 | * |
| 38 | * @author Andrew Brown <andrew.brown@intel.com> |
| 39 | */ |
| 40 | public class SegmentedClientTest { |
| 41 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 42 | private static final Logger logger = Logger.getLogger(SimpleClient.class.getName()); |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 43 | private MockFace face; |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 44 | |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 45 | @Before |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 46 | public void beforeTest() { |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 47 | face = new MockFace(); |
| 48 | } |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 49 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Test of getSync method, of class SegmentedClient. |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 52 | * |
| 53 | * @throws java.lang.Exception |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 54 | */ |
| 55 | @Test |
| 56 | public void testGetSync() throws Exception { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 57 | face.registerPrefix(new Name("/segmented/data"), new OnInterest() { |
| 58 | private int count = 0; |
| 59 | private int max = 9; |
| 60 | |
| 61 | @Override |
| 62 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
| 63 | Data data = new Data(interest.getName()); |
| 64 | if (!SegmentedClient.hasSegment(data.getName())) { |
| 65 | data.getName().appendSegment(0); |
| 66 | } |
| 67 | data.getMetaInfo().setFinalBlockId(Component.fromNumberWithMarker(max, 0x00)); |
| 68 | data.setContent(new Blob(".")); |
| 69 | try { |
| 70 | transport.send(data.wireEncode().buf()); |
| 71 | } catch (IOException e) { |
| 72 | fail(e.getMessage()); |
| 73 | } |
| 74 | } |
| 75 | }, null); |
| 76 | |
| 77 | Data data = SegmentedClient.getDefault().getSync(face, new Name("/segmented/data").appendSegment(0)); |
| 78 | assertEquals(10, data.getContent().size()); |
| 79 | } |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Test that a failed request fails with an exception. |
| 83 | * |
| 84 | * @throws java.lang.Exception |
| 85 | */ |
| 86 | @Test(expected = ExecutionException.class) |
| 87 | public void testFailureToRetrieve() throws Exception { |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 88 | // retrieve non-existent data, should timeout |
| 89 | logger.info("Client expressing interest asynchronously: /test/no-data"); |
| 90 | List<Future<Data>> futureSegments = SegmentedClient.getDefault().getAsyncList(face, new Name("/test/no-data")); |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 91 | |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 92 | // the list of future packets should be initialized |
| 93 | assertEquals(1, futureSegments.size()); |
| 94 | assertTrue(futureSegments.get(0).isDone()); |
| 95 | |
| 96 | // should throw error |
| 97 | futureSegments.get(0).get(); |
| 98 | } |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 101 | * Test that a sync failed request fails with an exception. |
| 102 | */ |
| 103 | @Test(expected = IOException.class) |
| 104 | public void testSyncFailureToRetrieve() throws IOException { |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 105 | SegmentedClient.getDefault().getSync(face, new Name("/test/no-data")); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame^] | 109 | * Identifies bug where the last Name.Component was always cut off. |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 110 | * |
| 111 | * @throws InterruptedException |
| 112 | * @throws ExecutionException |
| 113 | */ |
| 114 | @Test |
| 115 | public void testNameShorteningLogic() throws InterruptedException, ExecutionException { |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame^] | 116 | final List<Data> segments = buildSegmentedData("/test/name", 10); |
| 117 | for (Data segment : segments) { |
| 118 | face.addResponse(segment.getName(), segment); |
| 119 | } |
| 120 | |
| 121 | Name name = new Name("/test/name").appendSegment(0); |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 122 | |
| 123 | SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name); |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 124 | assertEquals(name.getPrefix(-1).toUri(), future.get().getName().toUri()); |
| 125 | } |
| 126 | |
| 127 | /** |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame^] | 128 | * Verify that Data returned with a different Name than the Interest is still |
| 129 | * segmented correctly. |
| 130 | * |
| 131 | * @throws Exception |
| 132 | */ |
| 133 | @Test |
| 134 | public void testWhenDataNameIsLongerThanInterestName() throws Exception { |
| 135 | final List<Data> segments = buildSegmentedData("/a/b/c/d", 10); |
| 136 | for (Data segment : segments) { |
| 137 | face.addResponse(segment.getName(), segment); |
| 138 | } |
| 139 | |
| 140 | Name name = new Name("/a/b"); |
| 141 | face.addResponse(name, segments.get(0)); |
| 142 | |
| 143 | Data data = SegmentedClient.getDefault().getSync(face, name); |
| 144 | assertNotNull(data); |
| 145 | assertEquals("/a/b/c/d", data.getName().toUri()); |
| 146 | } |
| 147 | |
| 148 | /** |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 149 | * Verify that Data packets with no content do not cause errors; identifies |
| 150 | * bug. |
| 151 | * |
| 152 | * @throws Exception |
| 153 | */ |
| 154 | @Test |
| 155 | public void testNoContent() throws Exception { |
| 156 | Name name = new Name("/test/no-content").appendSegment(0); |
| 157 | Data data = buildSegmentedData(name); |
| 158 | face.addResponse(name, data); |
| 159 | |
| 160 | Future<Data> result = SegmentedClient.getDefault().getAsync(face, name); |
| 161 | assertEquals("/test/no-content", result.get().getName().toUri()); |
| 162 | assertEquals("", result.get().getContent().toString()); |
| 163 | } |
| 164 | |
| 165 | /** |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 166 | * Verify that segmented content is the correct length when retrieved by the |
| 167 | * client. |
| 168 | * |
| 169 | * @throws Exception |
| 170 | */ |
| 171 | @Test |
| 172 | public void testContentLength() throws Exception { |
| 173 | Data data1 = new Data(new Name("/test/content-length").appendSegment(0)); |
| 174 | data1.setContent(new Blob("0123456789")); |
| 175 | data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00)); |
| 176 | face.addResponse(data1.getName(), data1); |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame^] | 177 | |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 178 | Data data2 = new Data(new Name("/test/content-length").appendSegment(1)); |
| 179 | data2.setContent(new Blob("0123456789")); |
| 180 | data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00)); |
| 181 | face.addResponse(data2.getName(), data2); |
| 182 | |
| 183 | Future<Data> result = SegmentedClient.getDefault().getAsync(face, new Name("/test/content-length").appendSegment(0)); |
| 184 | assertEquals(20, result.get().getContent().size()); |
| 185 | } |
| 186 | |
| 187 | /** |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 188 | * If a Data packet does not have a FinalBlockId, the SegmentedClient should |
| 189 | * just return the packet. |
| 190 | * |
| 191 | * @throws Exception |
| 192 | */ |
| 193 | @Test |
| 194 | public void testNoFinalBlockId() throws Exception { |
| 195 | Name name = new Name("/test/no-final-block-id"); |
| 196 | Data data = new Data(name); |
| 197 | data.setContent(new Blob("1")); |
| 198 | face.addResponse(name, data); |
| 199 | |
| 200 | Future<Data> result = SegmentedClient.getDefault().getAsync(face, name); |
| 201 | assertEquals("/test/no-final-block-id", result.get().getName().toUri()); |
| 202 | assertEquals("1", result.get().getContent().toString()); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Helper method, sets FinalBlockId from last Name component |
| 207 | * |
| 208 | * @param name |
| 209 | * @return |
| 210 | */ |
| 211 | private Data buildSegmentedData(Name name) { |
| 212 | Data data = new Data(name); |
| 213 | data.getMetaInfo().setFinalBlockId(name.get(-1)); |
| 214 | return data; |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 215 | } |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 216 | |
andrewsbrown | 7d68e08 | 2015-04-20 13:34:51 -0700 | [diff] [blame^] | 217 | private List<Data> buildSegmentedData(String name, int numSegments) { |
| 218 | Name.Component finalBlockId = Name.Component.fromNumberWithMarker(numSegments - 1, 0x00); |
| 219 | List<Data> segments = new ArrayList<>(numSegments); |
| 220 | |
| 221 | for (int i = 0; i < numSegments; i++) { |
| 222 | Data data = new Data(new Name(name).appendSegment(i)); |
| 223 | data.setContent(new Blob("0123456789")); |
| 224 | data.getMetaInfo().setFinalBlockId(finalBlockId); |
| 225 | segments.add(data); |
| 226 | } |
| 227 | |
| 228 | return segments; |
| 229 | } |
| 230 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 231 | } |