blob: df6d056f70593a594aa19c6fac8d6d66e7de19c0 [file] [log] [blame]
Andrew Browndb457052015-02-21 15:41:58 -08001/*
andrewsbrown4feb2da2015-03-03 16:05:29 -08002 * 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 Browndb457052015-02-21 15:41:58 -080013 */
14package com.intel.jndn.utils;
15
16import com.intel.jndn.mock.MockFace;
andrewsbrownb005ee62015-03-31 14:45:54 -070017import com.intel.jndn.utils.client.SegmentedFutureData;
Andrew Browndb457052015-02-21 15:41:58 -080018import java.io.IOException;
andrewsbrown7d68e082015-04-20 13:34:51 -070019import java.util.ArrayList;
andrewsbrown69d53292015-03-17 19:37:34 +010020import java.util.List;
21import java.util.concurrent.ExecutionException;
22import java.util.concurrent.Future;
23import java.util.logging.Logger;
Andrew Browndb457052015-02-21 15:41:58 -080024import net.named_data.jndn.Data;
25import net.named_data.jndn.Interest;
26import net.named_data.jndn.Name;
27import net.named_data.jndn.Name.Component;
28import net.named_data.jndn.OnInterest;
29import net.named_data.jndn.transport.Transport;
30import net.named_data.jndn.util.Blob;
31import org.junit.Test;
32import static org.junit.Assert.*;
andrewsbrown629816c2015-04-07 09:04:21 -070033import org.junit.Before;
Andrew Browndb457052015-02-21 15:41:58 -080034
35/**
36 * Test SegmentedClient functionality.
37 *
38 * @author Andrew Brown <andrew.brown@intel.com>
39 */
40public class SegmentedClientTest {
41
andrewsbrown69d53292015-03-17 19:37:34 +010042 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
andrewsbrown629816c2015-04-07 09:04:21 -070043 private MockFace face;
andrewsbrown2d7ee8d2015-04-15 12:40:58 -070044
andrewsbrown629816c2015-04-07 09:04:21 -070045 @Before
andrewsbrown2d7ee8d2015-04-15 12:40:58 -070046 public void beforeTest() {
andrewsbrown629816c2015-04-07 09:04:21 -070047 face = new MockFace();
48 }
andrewsbrown69d53292015-03-17 19:37:34 +010049
Andrew Browndb457052015-02-21 15:41:58 -080050 /**
51 * Test of getSync method, of class SegmentedClient.
andrewsbrown69d53292015-03-17 19:37:34 +010052 *
53 * @throws java.lang.Exception
Andrew Browndb457052015-02-21 15:41:58 -080054 */
55 @Test
56 public void testGetSync() throws Exception {
Andrew Browndb457052015-02-21 15:41:58 -080057 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 }
andrewsbrown69d53292015-03-17 19:37:34 +010080
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 {
andrewsbrown69d53292015-03-17 19:37:34 +010088 // 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"));
andrewsbrownb005ee62015-03-31 14:45:54 -070091
andrewsbrown69d53292015-03-17 19:37:34 +010092 // 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 }
andrewsbrownb005ee62015-03-31 14:45:54 -070099
100 /**
andrewsbrown4dddd472015-04-01 14:28:46 -0700101 * Test that a sync failed request fails with an exception.
102 */
103 @Test(expected = IOException.class)
104 public void testSyncFailureToRetrieve() throws IOException {
andrewsbrown629816c2015-04-07 09:04:21 -0700105 SegmentedClient.getDefault().getSync(face, new Name("/test/no-data"));
andrewsbrown4dddd472015-04-01 14:28:46 -0700106 }
107
108 /**
andrewsbrown7d68e082015-04-20 13:34:51 -0700109 * Identifies bug where the last Name.Component was always cut off.
andrewsbrownb005ee62015-03-31 14:45:54 -0700110 *
111 * @throws InterruptedException
112 * @throws ExecutionException
113 */
114 @Test
115 public void testNameShorteningLogic() throws InterruptedException, ExecutionException {
andrewsbrown7d68e082015-04-20 13:34:51 -0700116 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);
andrewsbrownb005ee62015-03-31 14:45:54 -0700122
123 SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name);
andrewsbrown629816c2015-04-07 09:04:21 -0700124 assertEquals(name.getPrefix(-1).toUri(), future.get().getName().toUri());
125 }
126
127 /**
andrewsbrown7d68e082015-04-20 13:34:51 -0700128 * 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 /**
andrewsbrown629816c2015-04-07 09:04:21 -0700149 * 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 /**
andrewsbrown2d7ee8d2015-04-15 12:40:58 -0700166 * 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);
andrewsbrown7d68e082015-04-20 13:34:51 -0700177
andrewsbrown2d7ee8d2015-04-15 12:40:58 -0700178 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 /**
andrewsbrown629816c2015-04-07 09:04:21 -0700188 * 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;
andrewsbrownb005ee62015-03-31 14:45:54 -0700215 }
andrewsbrown4dddd472015-04-01 14:28:46 -0700216
andrewsbrown7d68e082015-04-20 13:34:51 -0700217 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 Browndb457052015-02-21 15:41:58 -0800231}