blob: fbf931e1d76116052b2ce9fa03e671f30d861762 [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 net.named_data.jndn.Data;
andrewsbrown13b11c52015-07-02 14:23:15 -070028import net.named_data.jndn.Face;
andrewsbrown8d5ae292015-07-01 17:38:22 -070029import net.named_data.jndn.Name;
andrewsbrown13b11c52015-07-02 14:23:15 -070030import net.named_data.jndn.encoding.EncodingException;
31import net.named_data.jndn.security.KeyChain;
32import net.named_data.jndn.security.SecurityException;
33import net.named_data.jndn.transport.UdpTransport;
andrewsbrown8d5ae292015-07-01 17:38:22 -070034import net.named_data.jndn.util.Blob;
35
36/**
andrewsbrown13b11c52015-07-02 14:23:15 -070037 * Collect assorted methods to help with testing
andrewsbrown8d5ae292015-07-01 17:38:22 -070038 *
39 * @author Andrew Brown <andrew.brown@intel.com>
40 */
41public class TestHelper {
andrewsbrown13b11c52015-07-02 14:23:15 -070042
43 public static Data retrieve(CompletableFuture<Data> future) {
44 try {
45 return future.get(4000, TimeUnit.MILLISECONDS);
46 } catch (Exception ex) {
47 throw new RuntimeException(ex);
48 }
49 }
50
andrewsbrown8d5ae292015-07-01 17:38:22 -070051 public static List<CompletableFuture<Data>> buildFutureSegments(Name name, int from, int to) {
Alexander Afanasyevbc30e652016-01-25 17:37:16 -080052 List<CompletableFuture<Data>> list = new ArrayList<>();
53 for (Data d : buildSegments(name, from, to)) {
54 list.add(CompletableFuture.completedFuture(d));
55 }
56 return list;
andrewsbrown8d5ae292015-07-01 17:38:22 -070057 }
58
59 public static List<Data> buildSegments(Name name, int from, int to) {
Alexander Afanasyevbc30e652016-01-25 17:37:16 -080060 List<Data> list = new ArrayList<>();
61 for (Integer i = from; i < to; i++) {
62 list.add(buildData(new Name(name).appendSegment(i), i.toString(), to - 1));
63 }
64 return list;
andrewsbrown8d5ae292015-07-01 17:38:22 -070065 }
66
67 public static Data buildData(Name name, String content) {
68 Data data = new Data(name);
69 data.setContent(new Blob(content));
70
71 return data;
72 }
andrewsbrown13b11c52015-07-02 14:23:15 -070073
74 public static Data buildData(Name name, String content, int finalBlockId) {
andrewsbrown8d5ae292015-07-01 17:38:22 -070075 Data data = buildData(name, content);
76 data.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(finalBlockId, 0x00));
77 return data;
78 }
andrewsbrown13b11c52015-07-02 14:23:15 -070079
80 public static NdnEnvironment buildTestEnvironment(String host, int numFaces) throws SecurityException {
81 NdnEnvironment environment = new NdnEnvironment();
82 environment.executor = Executors.newScheduledThreadPool(numFaces);
83 environment.keyChain = MockKeyChain.configure(new Name("/test/identity").append(buildRandomString(10)));
84
85 for (int i = 0; i < numFaces; i++) {
86 Face face = new Face(new UdpTransport(), new UdpTransport.ConnectionInfo(host));
87 face.setCommandSigningInfo(environment.keyChain, environment.keyChain.getDefaultCertificateName());
88 environment.executor.scheduleAtFixedRate(new EventProcessor(face), 0, 20, TimeUnit.MILLISECONDS);
89 environment.faces.add(i, face);
90 }
91
92 return environment;
93 }
94
95 public static String buildRandomString(int length) {
96 return new String(buildRandomBytes(length));
97 }
98
99 public static byte[] buildRandomBytes(int length){
100 byte[] bytes = new byte[length];
101 new Random().nextBytes(bytes);
102 return bytes;
103 }
104
105 public static class NdnEnvironment {
106
107 public ScheduledExecutorService executor;
108 public KeyChain keyChain;
109 public List<Face> faces = new ArrayList<>();
110 }
111
112 public static class EventProcessor implements Runnable {
113
114 private final Face face;
115
116 public EventProcessor(Face face) {
117 this.face = face;
118 }
119
120 @Override
121 public void run() {
122 try {
123 face.processEvents();
124 } catch (IOException | EncodingException ex) {
125 Logger.getLogger(TestHelper.class.getName()).log(Level.SEVERE, null, ex);
126 }
127 }
128 }
129
130 public static class TestCounter{
131 public int count = 0;
132 }
andrewsbrown8d5ae292015-07-01 17:38:22 -0700133}