blob: 2ee0ae42b37a6e5758ff35e7a4e21089b5aaecdf [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;
17import java.io.IOException;
andrewsbrown69d53292015-03-17 19:37:34 +010018import java.util.List;
andrewsbrown69d53292015-03-17 19:37:34 +010019import java.util.concurrent.Future;
20import java.util.logging.Logger;
Andrew Browndb457052015-02-21 15:41:58 -080021import net.named_data.jndn.Data;
andrewsbrown8d5ae292015-07-01 17:38:22 -070022import net.named_data.jndn.Face;
Andrew Browndb457052015-02-21 15:41:58 -080023import net.named_data.jndn.Interest;
andrewsbrown8d5ae292015-07-01 17:38:22 -070024import net.named_data.jndn.InterestFilter;
Andrew Browndb457052015-02-21 15:41:58 -080025import net.named_data.jndn.Name;
26import net.named_data.jndn.Name.Component;
andrewsbrown8d5ae292015-07-01 17:38:22 -070027import net.named_data.jndn.OnInterestCallback;
Andrew Browndb457052015-02-21 15:41:58 -080028import net.named_data.jndn.util.Blob;
29import org.junit.Test;
30import static org.junit.Assert.*;
andrewsbrown629816c2015-04-07 09:04:21 -070031import org.junit.Before;
Andrew Browndb457052015-02-21 15:41:58 -080032
33/**
34 * Test SegmentedClient functionality.
35 *
36 * @author Andrew Brown <andrew.brown@intel.com>
37 */
38public class SegmentedClientTest {
39
andrewsbrown69d53292015-03-17 19:37:34 +010040 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
andrewsbrown8d5ae292015-07-01 17:38:22 -070041 private SegmentedClient instance;
andrewsbrown629816c2015-04-07 09:04:21 -070042 private MockFace face;
andrewsbrown2d7ee8d2015-04-15 12:40:58 -070043
andrewsbrown629816c2015-04-07 09:04:21 -070044 @Before
andrewsbrown2d7ee8d2015-04-15 12:40:58 -070045 public void beforeTest() {
andrewsbrown629816c2015-04-07 09:04:21 -070046 face = new MockFace();
andrewsbrown8d5ae292015-07-01 17:38:22 -070047 instance = new SegmentedClient(1, 100); // warning: setting an interest
48 // lifetime that is too low will cause the getSync() tests to fail due to
49 // Thread.sleep()
andrewsbrown629816c2015-04-07 09:04:21 -070050 }
andrewsbrown69d53292015-03-17 19:37:34 +010051
Andrew Browndb457052015-02-21 15:41:58 -080052 @Test
53 public void testGetSync() throws Exception {
andrewsbrown8d5ae292015-07-01 17:38:22 -070054 Name name = new Name("/segmented/data");
55 face.registerPrefix(name, new OnInterestCallback() {
Andrew Browndb457052015-02-21 15:41:58 -080056 private int count = 0;
57 private int max = 9;
58
59 @Override
andrewsbrown8d5ae292015-07-01 17:38:22 -070060 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
Andrew Browndb457052015-02-21 15:41:58 -080061 Data data = new Data(interest.getName());
62 if (!SegmentedClient.hasSegment(data.getName())) {
63 data.getName().appendSegment(0);
64 }
65 data.getMetaInfo().setFinalBlockId(Component.fromNumberWithMarker(max, 0x00));
66 data.setContent(new Blob("."));
67 try {
andrewsbrown8d5ae292015-07-01 17:38:22 -070068 face.putData(data);
Andrew Browndb457052015-02-21 15:41:58 -080069 } catch (IOException e) {
70 fail(e.getMessage());
71 }
72 }
73 }, null);
74
andrewsbrown8d5ae292015-07-01 17:38:22 -070075 logger.info("Client retrieving segments synchronously: " + name.toUri());
76 Data data = instance.getSync(face, new Name(name).appendSegment(0));
Andrew Browndb457052015-02-21 15:41:58 -080077 assertEquals(10, data.getContent().size());
78 }
andrewsbrown69d53292015-03-17 19:37:34 +010079
80 /**
81 * Test that a failed request fails with an exception.
82 *
83 * @throws java.lang.Exception
84 */
andrewsbrown8d5ae292015-07-01 17:38:22 -070085// @Test(expected = ExecutionException.class)
86// public void testFailureToRetrieve() throws Exception {
87// // retrieve non-existent data, should timeout
88// logger.info("Client retrieving segments asynchronously: /test/no-data");
89// Future<Data> futureData = instance.getAsync(face, new Name("/test/no-data"));
90// face.processEvents();
91// futureData.get();
92// }
andrewsbrownb005ee62015-03-31 14:45:54 -070093
andrewsbrown8d5ae292015-07-01 17:38:22 -070094// /**
95// * Test that a sync failed request fails with an exception.
96// */
97// @Test(expected = IOException.class)
98// public void testSyncFailureToRetrieve() throws IOException {
99// logger.info("Client retrieving segments synchronously: /test/no-data");
100// instance.getSync(face, new Name("/test/no-data"));
101// }
andrewsbrown629816c2015-04-07 09:04:21 -0700102
103 /**
andrewsbrown7d68e082015-04-20 13:34:51 -0700104 * Verify that Data returned with a different Name than the Interest is still
105 * segmented correctly.
106 *
107 * @throws Exception
108 */
109 @Test
110 public void testWhenDataNameIsLongerThanInterestName() throws Exception {
andrewsbrown8d5ae292015-07-01 17:38:22 -0700111 final List<Data> segments = TestHelper.buildSegments(new Name("/a/b/c/d"), 0, 10);
andrewsbrown7d68e082015-04-20 13:34:51 -0700112 for (Data segment : segments) {
113 face.addResponse(segment.getName(), segment);
114 }
115
116 Name name = new Name("/a/b");
117 face.addResponse(name, segments.get(0));
118
andrewsbrown8d5ae292015-07-01 17:38:22 -0700119 logger.info("Client retrieving segments synchronously: " + name.toUri());
120 Data data = instance.getSync(face, name);
andrewsbrown7d68e082015-04-20 13:34:51 -0700121 assertNotNull(data);
122 assertEquals("/a/b/c/d", data.getName().toUri());
123 }
124
125 /**
andrewsbrown629816c2015-04-07 09:04:21 -0700126 * Verify that Data packets with no content do not cause errors; identifies
127 * bug.
128 *
129 * @throws Exception
130 */
131 @Test
132 public void testNoContent() throws Exception {
133 Name name = new Name("/test/no-content").appendSegment(0);
andrewsbrown8d5ae292015-07-01 17:38:22 -0700134 Data data = TestHelper.buildData(name, "", 0);
andrewsbrown629816c2015-04-07 09:04:21 -0700135 face.addResponse(name, data);
136
andrewsbrown8d5ae292015-07-01 17:38:22 -0700137 logger.info("Client retrieving segments asynchronously: /test/no-content");
138 Future<Data> result = instance.getAsync(face, name);
139 face.processEvents();
andrewsbrown629816c2015-04-07 09:04:21 -0700140 assertEquals("/test/no-content", result.get().getName().toUri());
141 assertEquals("", result.get().getContent().toString());
142 }
143
144 /**
andrewsbrown2d7ee8d2015-04-15 12:40:58 -0700145 * Verify that segmented content is the correct length when retrieved by the
146 * client.
147 *
148 * @throws Exception
149 */
150 @Test
151 public void testContentLength() throws Exception {
152 Data data1 = new Data(new Name("/test/content-length").appendSegment(0));
153 data1.setContent(new Blob("0123456789"));
154 data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00));
155 face.addResponse(data1.getName(), data1);
andrewsbrown7d68e082015-04-20 13:34:51 -0700156
andrewsbrown2d7ee8d2015-04-15 12:40:58 -0700157 Data data2 = new Data(new Name("/test/content-length").appendSegment(1));
158 data2.setContent(new Blob("0123456789"));
159 data1.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(1, 0x00));
160 face.addResponse(data2.getName(), data2);
161
andrewsbrown8d5ae292015-07-01 17:38:22 -0700162 logger.info("Client retrieving segments asynchronously: /test/content-length");
163 Future<Data> result = instance.getAsync(face, new Name("/test/content-length").appendSegment(0));
164 face.processEvents();
165 face.processEvents();
andrewsbrown2d7ee8d2015-04-15 12:40:58 -0700166 assertEquals(20, result.get().getContent().size());
167 }
168
169 /**
andrewsbrown629816c2015-04-07 09:04:21 -0700170 * If a Data packet does not have a FinalBlockId, the SegmentedClient should
171 * just return the packet.
172 *
173 * @throws Exception
174 */
175 @Test
176 public void testNoFinalBlockId() throws Exception {
177 Name name = new Name("/test/no-final-block-id");
178 Data data = new Data(name);
179 data.setContent(new Blob("1"));
180 face.addResponse(name, data);
181
andrewsbrown8d5ae292015-07-01 17:38:22 -0700182 logger.info("Client retrieving segments asynchronously: /test/no-final-block-id");
183 Future<Data> result = instance.getAsync(face, name);
184 face.processEvents();
andrewsbrown629816c2015-04-07 09:04:21 -0700185 assertEquals("/test/no-final-block-id", result.get().getName().toUri());
186 assertEquals("1", result.get().getContent().toString());
187 }
Andrew Browndb457052015-02-21 15:41:58 -0800188}