blob: 3b78b745969d60859103f3ae9c009d066bf3730d [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;
18import java.util.logging.Level;
19import java.util.logging.Logger;
20import net.named_data.jndn.Data;
21import net.named_data.jndn.Face;
22import 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 {
55 final Name name = new Name(prefix).append("1");
56
57 // why a new thread? The server will only operate if someone turns the crank,
58 // i.e. someone calls face.processEvents() every so often.
59 new Thread(new Runnable() {
60 @Override
61 public void run() {
62 Data served = new Data(name);
63 served.setContent(new Blob("..."));
64 served.getMetaInfo().setFreshnessPeriod(30000);
65
66 try {
67 instance.serve(served);
68 logger.info("Serving data: " + name.toUri());
69 } catch (IOException ex) {
70 logger.info("Failed to serve data: " + name.toUri());
71 }
72
73 while (true) {
74 try {
75 face.processEvents();
76 } catch (IOException | EncodingException ex) {
77 logger.info("Failed while processing events.");
78 }
79 }
80 }
81 }).start();
82
83 // why wait? we want to make sure the thread is running and that the prefix
84 // registration has succeeded on the NFD before we send interests
85 Thread.sleep(1000);
86
87 // why a different face? because we don't want the abover face.processEvents()
88 // to interfere with the SimpleClient's processEvents().
89 logger.info("Retrieving data: " + name.toUri());
90 Data retrieved = SegmentedClient.getDefault().getSync(new Face(ip), name);
91 assertNotNull(retrieved);
92 assertEquals("...", retrieved.getContent().toString());
93 }
94}