blob: 72a96d7646c44d9e5401c92d7dd832025bfbc001 [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 com.intel.jndn.mock.MockKeyChain;
Andrew Brown85234ea2016-09-07 13:41:29 -070017import net.named_data.jndn.Data;
18import net.named_data.jndn.Face;
19import net.named_data.jndn.Name;
20import net.named_data.jndn.encoding.EncodingException;
21import net.named_data.jndn.security.KeyChain;
22import net.named_data.jndn.security.SecurityException;
23import net.named_data.jndn.transport.UdpTransport;
24import net.named_data.jndn.util.Blob;
25
andrewsbrown13b11c52015-07-02 14:23:15 -070026import java.io.IOException;
27import java.util.ArrayList;
andrewsbrown8d5ae292015-07-01 17:38:22 -070028import java.util.List;
andrewsbrown13b11c52015-07-02 14:23:15 -070029import java.util.Random;
andrewsbrown8d5ae292015-07-01 17:38:22 -070030import java.util.concurrent.CompletableFuture;
andrewsbrown13b11c52015-07-02 14:23:15 -070031import java.util.concurrent.Executors;
32import java.util.concurrent.ScheduledExecutorService;
33import java.util.concurrent.TimeUnit;
34import java.util.logging.Level;
35import java.util.logging.Logger;
andrewsbrown8d5ae292015-07-01 17:38:22 -070036import java.util.stream.Collectors;
37import java.util.stream.IntStream;
andrewsbrown8d5ae292015-07-01 17:38:22 -070038
39/**
andrewsbrown13b11c52015-07-02 14:23:15 -070040 * Collect assorted methods to help with testing
andrewsbrown8d5ae292015-07-01 17:38:22 -070041 *
Andrew Brown85234ea2016-09-07 13:41:29 -070042 * @author Andrew Brown, andrew.brown@intel.com
andrewsbrown8d5ae292015-07-01 17:38:22 -070043 */
44public class TestHelper {
andrewsbrown13b11c52015-07-02 14:23:15 -070045
46 public static Data retrieve(CompletableFuture<Data> future) {
47 try {
48 return future.get(4000, TimeUnit.MILLISECONDS);
49 } catch (Exception ex) {
50 throw new RuntimeException(ex);
51 }
52 }
53
andrewsbrown8d5ae292015-07-01 17:38:22 -070054 public static List<CompletableFuture<Data>> buildFutureSegments(Name name, int from, int to) {
55 return buildSegments(name, from, to).stream()
56 .map((d) -> CompletableFuture.completedFuture(d))
57 .collect(Collectors.toList());
58 }
59
60 public static List<Data> buildSegments(Name name, int from, int to) {
andrewsbrown13b11c52015-07-02 14:23:15 -070061 return IntStream.range(from, to).boxed()
andrewsbrown8d5ae292015-07-01 17:38:22 -070062 .map((i) -> buildData(new Name(name).appendSegment(i), i.toString(), to - 1))
63 .collect(Collectors.toList());
64 }
65
66 public static Data buildData(Name name, String content) {
67 Data data = new Data(name);
68 data.setContent(new Blob(content));
69
70 return data;
71 }
andrewsbrown13b11c52015-07-02 14:23:15 -070072
73 public static Data buildData(Name name, String content, int finalBlockId) {
andrewsbrown8d5ae292015-07-01 17:38:22 -070074 Data data = buildData(name, content);
75 data.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(finalBlockId, 0x00));
76 return data;
77 }
andrewsbrown13b11c52015-07-02 14:23:15 -070078
79 public static NdnEnvironment buildTestEnvironment(String host, int numFaces) throws SecurityException {
80 NdnEnvironment environment = new NdnEnvironment();
81 environment.executor = Executors.newScheduledThreadPool(numFaces);
82 environment.keyChain = MockKeyChain.configure(new Name("/test/identity").append(buildRandomString(10)));
83
84 for (int i = 0; i < numFaces; i++) {
85 Face face = new Face(new UdpTransport(), new UdpTransport.ConnectionInfo(host));
86 face.setCommandSigningInfo(environment.keyChain, environment.keyChain.getDefaultCertificateName());
87 environment.executor.scheduleAtFixedRate(new EventProcessor(face), 0, 20, TimeUnit.MILLISECONDS);
88 environment.faces.add(i, face);
89 }
90
91 return environment;
92 }
93
94 public static String buildRandomString(int length) {
95 return new String(buildRandomBytes(length));
96 }
97
98 public static byte[] buildRandomBytes(int length){
99 byte[] bytes = new byte[length];
100 new Random().nextBytes(bytes);
101 return bytes;
102 }
103
104 public static class NdnEnvironment {
105
106 public ScheduledExecutorService executor;
107 public KeyChain keyChain;
Andrew Brown0f5a9c12016-09-12 21:06:39 -0700108 public final List<Face> faces = new ArrayList<>();
andrewsbrown13b11c52015-07-02 14:23:15 -0700109 }
110
111 public static class EventProcessor implements Runnable {
112
113 private final Face face;
114
115 public EventProcessor(Face face) {
116 this.face = face;
117 }
118
119 @Override
120 public void run() {
121 try {
122 face.processEvents();
123 } catch (IOException | EncodingException ex) {
124 Logger.getLogger(TestHelper.class.getName()).log(Level.SEVERE, null, ex);
125 }
126 }
127 }
128
129 public static class TestCounter{
130 public int count = 0;
131 }
andrewsbrown8d5ae292015-07-01 17:38:22 -0700132}