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