blob: 823223da2c22c9f362f0c99cb85c80258067e1f7 [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");
andrewsbrownd403db82015-05-11 13:02:28 -070090 Future<Data> futureData = SegmentedClient.getDefault().getAsync(face, new Name("/test/no-data"));
91 futureData.get();
andrewsbrown69d53292015-03-17 19:37:34 +010092 }
andrewsbrownb005ee62015-03-31 14:45:54 -070093
94 /**
andrewsbrown4dddd472015-04-01 14:28:46 -070095 * Test that a sync failed request fails with an exception.
96 */
97 @Test(expected = IOException.class)
98 public void testSyncFailureToRetrieve() throws IOException {
andrewsbrown629816c2015-04-07 09:04:21 -070099 SegmentedClient.getDefault().getSync(face, new Name("/test/no-data"));
andrewsbrown4dddd472015-04-01 14:28:46 -0700100 }
101
102 /**
andrewsbrown7d68e082015-04-20 13:34:51 -0700103 * Identifies bug where the last Name.Component was always cut off.
andrewsbrownb005ee62015-03-31 14:45:54 -0700104 *
105 * @throws InterruptedException
106 * @throws ExecutionException
107 */
108 @Test
109 public void testNameShorteningLogic() throws InterruptedException, ExecutionException {
andrewsbrown7d68e082015-04-20 13:34:51 -0700110 final List<Data> segments = buildSegmentedData("/test/name", 10);
111 for (Data segment : segments) {
112 face.addResponse(segment.getName(), segment);
113 }
114
115 Name name = new Name("/test/name").appendSegment(0);
andrewsbrownb005ee62015-03-31 14:45:54 -0700116
117 SegmentedFutureData future = (SegmentedFutureData) SegmentedClient.getDefault().getAsync(face, name);
andrewsbrown629816c2015-04-07 09:04:21 -0700118 assertEquals(name.getPrefix(-1).toUri(), future.get().getName().toUri());
119 }
120
121 /**
andrewsbrown7d68e082015-04-20 13:34:51 -0700122 * Verify that Data returned with a different Name than the Interest is still
123 * segmented correctly.
124 *
125 * @throws Exception
126 */
127 @Test
128 public void testWhenDataNameIsLongerThanInterestName() throws Exception {
129 final List<Data> segments = buildSegmentedData("/a/b/c/d", 10);
130 for (Data segment : segments) {
131 face.addResponse(segment.getName(), segment);
132 }
133
134 Name name = new Name("/a/b");
135 face.addResponse(name, segments.get(0));
136
137 Data data = SegmentedClient.getDefault().getSync(face, name);
138 assertNotNull(data);
139 assertEquals("/a/b/c/d", data.getName().toUri());
140 }
141
142 /**
andrewsbrown629816c2015-04-07 09:04:21 -0700143 * Verify that Data packets with no content do not cause errors; identifies
144 * bug.
145 *
146 * @throws Exception
147 */
148 @Test
149 public void testNoContent() throws Exception {
150 Name name = new Name("/test/no-content").appendSegment(0);
151 Data data = buildSegmentedData(name);
152 face.addResponse(name, data);
153
154 Future<Data> result = SegmentedClient.getDefault().getAsync(face, name);
155 assertEquals("/test/no-content", result.get().getName().toUri());
156 assertEquals("", result.get().getContent().toString());
157 }
158
159 /**
andrewsbrown2d7ee8d2015-04-15 12:40:58 -0700160 * Verify that segmented content is the correct length when retrieved by the
161 * client.
162 *
163 * @throws Exception
164 */
165 @Test
166 public void testContentLength() throws Exception {
167 Data data1 = new Data(new Name("/test/content-length").appendSegment(0));
168 data1.setContent(new Blob("0123456789"));
169 data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00));
170 face.addResponse(data1.getName(), data1);
andrewsbrown7d68e082015-04-20 13:34:51 -0700171
andrewsbrown2d7ee8d2015-04-15 12:40:58 -0700172 Data data2 = new Data(new Name("/test/content-length").appendSegment(1));
173 data2.setContent(new Blob("0123456789"));
174 data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00));
175 face.addResponse(data2.getName(), data2);
176
177 Future<Data> result = SegmentedClient.getDefault().getAsync(face, new Name("/test/content-length").appendSegment(0));
178 assertEquals(20, result.get().getContent().size());
179 }
180
181 /**
andrewsbrown629816c2015-04-07 09:04:21 -0700182 * If a Data packet does not have a FinalBlockId, the SegmentedClient should
183 * just return the packet.
184 *
185 * @throws Exception
186 */
187 @Test
188 public void testNoFinalBlockId() throws Exception {
189 Name name = new Name("/test/no-final-block-id");
190 Data data = new Data(name);
191 data.setContent(new Blob("1"));
192 face.addResponse(name, data);
193
194 Future<Data> result = SegmentedClient.getDefault().getAsync(face, name);
195 assertEquals("/test/no-final-block-id", result.get().getName().toUri());
196 assertEquals("1", result.get().getContent().toString());
197 }
198
199 /**
200 * Helper method, sets FinalBlockId from last Name component
201 *
202 * @param name
203 * @return
204 */
205 private Data buildSegmentedData(Name name) {
206 Data data = new Data(name);
207 data.getMetaInfo().setFinalBlockId(name.get(-1));
208 return data;
andrewsbrownb005ee62015-03-31 14:45:54 -0700209 }
andrewsbrown4dddd472015-04-01 14:28:46 -0700210
andrewsbrown7d68e082015-04-20 13:34:51 -0700211 private List<Data> buildSegmentedData(String name, int numSegments) {
212 Name.Component finalBlockId = Name.Component.fromNumberWithMarker(numSegments - 1, 0x00);
213 List<Data> segments = new ArrayList<>(numSegments);
214
215 for (int i = 0; i < numSegments; i++) {
216 Data data = new Data(new Name(name).appendSegment(i));
217 data.setContent(new Blob("0123456789"));
218 data.getMetaInfo().setFinalBlockId(finalBlockId);
219 segments.add(data);
220 }
221
222 return segments;
223 }
224
Andrew Browndb457052015-02-21 15:41:58 -0800225}