blob: 7b79bf4b8a3d88364e610365cb54fdf27effc33d [file] [log] [blame]
andrewsbrown8d5ae292015-07-01 17:38:22 -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
andrewsbrown13b11c52015-07-02 14:23:15 -070016import java.io.IOException;
17import java.util.ArrayList;
andrewsbrown8d5ae292015-07-01 17:38:22 -070018import java.util.List;
andrewsbrown13b11c52015-07-02 14:23:15 -070019import java.util.Random;
andrewsbrown8d5ae292015-07-01 17:38:22 -070020import java.util.concurrent.CompletableFuture;
andrewsbrown13b11c52015-07-02 14:23:15 -070021import java.util.concurrent.Executors;
22import java.util.concurrent.ScheduledExecutorService;
23import java.util.concurrent.TimeUnit;
24import java.util.logging.Level;
25import java.util.logging.Logger;
Alexander Afanasyevc898bca2016-01-28 11:18:51 -080026
27import com.intel.jndn.mock.MockFace;
28import com.intel.jndn.mock.MockKeyChain;
andrewsbrown8d5ae292015-07-01 17:38:22 -070029import net.named_data.jndn.Data;
andrewsbrown13b11c52015-07-02 14:23:15 -070030import net.named_data.jndn.Face;
Alexander Afanasyevc898bca2016-01-28 11:18:51 -080031import net.named_data.jndn.Interest;
andrewsbrown8d5ae292015-07-01 17:38:22 -070032import net.named_data.jndn.Name;
andrewsbrown13b11c52015-07-02 14:23:15 -070033import net.named_data.jndn.encoding.EncodingException;
34import net.named_data.jndn.security.KeyChain;
35import net.named_data.jndn.security.SecurityException;
36import net.named_data.jndn.transport.UdpTransport;
andrewsbrown8d5ae292015-07-01 17:38:22 -070037import net.named_data.jndn.util.Blob;
38
39/**
andrewsbrown13b11c52015-07-02 14:23:15 -070040 * Collect assorted methods to help with testing
andrewsbrown8d5ae292015-07-01 17:38:22 -070041 *
42 * @author Andrew Brown <andrew.brown@intel.com>
43 */
44public class TestHelper {
andrewsbrown13b11c52015-07-02 14:23:15 -070045
Alexander Afanasyevc898bca2016-01-28 11:18:51 -080046 public interface Tester {
47 boolean test();
48 }
49
50 public static void run(Face face, int limit, Tester t) throws IOException, EncodingException, InterruptedException {
51 while (t.test() && limit > 0) {
52 face.processEvents();
53 Thread.sleep(500);
54 limit--;
55 }
56 }
57
58 public static void run(Face face, int limit) throws IOException, EncodingException, InterruptedException {
59 run(face, limit, new Tester() {
60 @Override
61 public boolean test() {
62 return true;
63 }
64 });
65 }
66
67 public static void addDataPublisher(final MockFace face, final int finalBlockId) {
68 face.onSendInterest.add(new MockFace.SignalOnSendInterest() {
69 @Override
70 public void emit(Interest interest) throws EncodingException, SecurityException {
71 if (finalBlockId < 0) {
72 face.receive(TestHelper.buildData(interest.getName(), "..."));
73 }
74 else {
75 face.receive(TestHelper.buildData(interest.getName(), "...", finalBlockId));
76 }
77 }
78 });
79 }
80
81
andrewsbrown13b11c52015-07-02 14:23:15 -070082 public static Data retrieve(CompletableFuture<Data> future) {
83 try {
84 return future.get(4000, TimeUnit.MILLISECONDS);
85 } catch (Exception ex) {
86 throw new RuntimeException(ex);
87 }
88 }
89
andrewsbrown8d5ae292015-07-01 17:38:22 -070090 public static List<CompletableFuture<Data>> buildFutureSegments(Name name, int from, int to) {
Alexander Afanasyevbc30e652016-01-25 17:37:16 -080091 List<CompletableFuture<Data>> list = new ArrayList<>();
92 for (Data d : buildSegments(name, from, to)) {
93 list.add(CompletableFuture.completedFuture(d));
94 }
95 return list;
andrewsbrown8d5ae292015-07-01 17:38:22 -070096 }
97
98 public static List<Data> buildSegments(Name name, int from, int to) {
Alexander Afanasyevbc30e652016-01-25 17:37:16 -080099 List<Data> list = new ArrayList<>();
100 for (Integer i = from; i < to; i++) {
101 list.add(buildData(new Name(name).appendSegment(i), i.toString(), to - 1));
102 }
103 return list;
andrewsbrown8d5ae292015-07-01 17:38:22 -0700104 }
105
106 public static Data buildData(Name name, String content) {
107 Data data = new Data(name);
108 data.setContent(new Blob(content));
109
110 return data;
111 }
andrewsbrown13b11c52015-07-02 14:23:15 -0700112
113 public static Data buildData(Name name, String content, int finalBlockId) {
andrewsbrown8d5ae292015-07-01 17:38:22 -0700114 Data data = buildData(name, content);
115 data.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(finalBlockId, 0x00));
116 return data;
117 }
andrewsbrown13b11c52015-07-02 14:23:15 -0700118
119 public static NdnEnvironment buildTestEnvironment(String host, int numFaces) throws SecurityException {
120 NdnEnvironment environment = new NdnEnvironment();
121 environment.executor = Executors.newScheduledThreadPool(numFaces);
122 environment.keyChain = MockKeyChain.configure(new Name("/test/identity").append(buildRandomString(10)));
123
124 for (int i = 0; i < numFaces; i++) {
125 Face face = new Face(new UdpTransport(), new UdpTransport.ConnectionInfo(host));
126 face.setCommandSigningInfo(environment.keyChain, environment.keyChain.getDefaultCertificateName());
127 environment.executor.scheduleAtFixedRate(new EventProcessor(face), 0, 20, TimeUnit.MILLISECONDS);
128 environment.faces.add(i, face);
129 }
130
131 return environment;
132 }
133
134 public static String buildRandomString(int length) {
135 return new String(buildRandomBytes(length));
136 }
137
138 public static byte[] buildRandomBytes(int length){
139 byte[] bytes = new byte[length];
140 new Random().nextBytes(bytes);
141 return bytes;
142 }
143
144 public static class NdnEnvironment {
145
146 public ScheduledExecutorService executor;
147 public KeyChain keyChain;
148 public List<Face> faces = new ArrayList<>();
149 }
150
151 public static class EventProcessor implements Runnable {
152
153 private final Face face;
154
155 public EventProcessor(Face face) {
156 this.face = face;
157 }
158
159 @Override
160 public void run() {
161 try {
162 face.processEvents();
163 } catch (IOException | EncodingException ex) {
164 Logger.getLogger(TestHelper.class.getName()).log(Level.SEVERE, null, ex);
165 }
166 }
167 }
andrewsbrown8d5ae292015-07-01 17:38:22 -0700168}