blob: 49d054fac1be25487d85b6f4d88077b64edd5f34 [file] [log] [blame]
andrewsbrown6f8f4692015-04-20 13:39:49 -07001/*
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.
13 */
14package com.intel.jndn.utils;
15
16import com.intel.jndn.mock.MockKeyChain;
17import java.io.IOException;
andrewsbrown6f8f4692015-04-20 13:39:49 -070018import java.util.logging.Logger;
19import net.named_data.jndn.Data;
20import net.named_data.jndn.Face;
andrewsbrown54cee482015-04-22 16:41:03 -070021import net.named_data.jndn.Interest;
andrewsbrown6f8f4692015-04-20 13:39:49 -070022import net.named_data.jndn.Name;
23import net.named_data.jndn.encoding.EncodingException;
24import net.named_data.jndn.security.KeyChain;
25import net.named_data.jndn.security.SecurityException;
26import net.named_data.jndn.util.Blob;
27import static org.junit.Assert.*;
28import org.junit.Test;
29
30/**
31 * Test the {@link SegmentedServer} on an actual NFD; to run this, pass the NFD
32 * IP/host name as a parameter like <code>-Dnfd.ip=10.1.1.1</code>.
33 *
34 * @author Andrew Brown <andrew.brown@intel.com>
35 */
36public class SegmentedServerTestIT {
37
38 private static final Logger logger = Logger.getLogger(SegmentedServerTestIT.class.getName());
39 Name prefix;
40 Face face;
41 SegmentedServer instance;
42 private String ip;
43
44 public SegmentedServerTestIT() throws SecurityException {
45 this.ip = System.getProperty("nfd.ip");
46 this.prefix = new Name("/test/for/segmented-server");
47 this.face = new Face(ip);
48 this.instance = new SegmentedServer(face, prefix);
49 KeyChain mockKeyChain = MockKeyChain.configure(new Name("/test/server"));
50 face.setCommandSigningInfo(mockKeyChain, mockKeyChain.getDefaultCertificateName());
51 }
52
53 @Test
54 public void testRegisterAndRetrieval() throws Exception {
andrewsbrown54cee482015-04-22 16:41:03 -070055 final Name name1 = new Name(prefix).append("1");
56 final Name name2 = new Name(prefix).append("2");
andrewsbrown6f8f4692015-04-20 13:39:49 -070057
58 // why a new thread? The server will only operate if someone turns the crank,
59 // i.e. someone calls face.processEvents() every so often.
60 new Thread(new Runnable() {
61 @Override
62 public void run() {
andrewsbrown6f8f4692015-04-20 13:39:49 -070063 try {
andrewsbrown54cee482015-04-22 16:41:03 -070064 instance.serve(buildDataPacket(name1));
65 instance.serve(buildDataPacket(name2));
andrewsbrown6f8f4692015-04-20 13:39:49 -070066 } catch (IOException ex) {
andrewsbrown54cee482015-04-22 16:41:03 -070067 logger.info("Failed to serve data.");
andrewsbrown6f8f4692015-04-20 13:39:49 -070068 }
69
70 while (true) {
71 try {
72 face.processEvents();
73 } catch (IOException | EncodingException ex) {
74 logger.info("Failed while processing events.");
75 }
76 }
77 }
78 }).start();
79
80 // why wait? we want to make sure the thread is running and that the prefix
81 // registration has succeeded on the NFD before we send interests
82 Thread.sleep(1000);
83
84 // why a different face? because we don't want the abover face.processEvents()
85 // to interfere with the SimpleClient's processEvents().
andrewsbrown54cee482015-04-22 16:41:03 -070086 logger.info("Retrieving data: " + prefix.toUri());
87 Interest interest = new Interest(new Name(prefix));
88 interest.setInterestLifetimeMilliseconds(2000);
89 interest.setMustBeFresh(true);
90 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
91
92 Data retrieved = SegmentedClient.getDefault().getSync(new Face(ip), interest);
andrewsbrown6f8f4692015-04-20 13:39:49 -070093 assertNotNull(retrieved);
94 assertEquals("...", retrieved.getContent().toString());
andrewsbrown54cee482015-04-22 16:41:03 -070095 logger.info("Retrieved data: " + retrieved.getName().toUri());
96 }
97
98 Data buildDataPacket(Name name) {
99 Data data = new Data(new Name(name).appendSegment(0));
100 data.setContent(new Blob("..."));
101 data.getMetaInfo().setFreshnessPeriod(30000);
102 return data;
andrewsbrown6f8f4692015-04-20 13:39:49 -0700103 }
104}