andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 14 | package com.intel.jndn.utils; |
| 15 | |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 16 | import java.io.IOException; |
| 17 | import java.util.ArrayList; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 18 | import java.util.List; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 19 | import java.util.Random; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 20 | import java.util.concurrent.CompletableFuture; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 21 | import java.util.concurrent.Executors; |
| 22 | import java.util.concurrent.ScheduledExecutorService; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | import java.util.logging.Level; |
| 25 | import java.util.logging.Logger; |
Alexander Afanasyev | c898bca | 2016-01-28 11:18:51 -0800 | [diff] [blame] | 26 | |
| 27 | import com.intel.jndn.mock.MockFace; |
| 28 | import com.intel.jndn.mock.MockKeyChain; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 29 | import net.named_data.jndn.Data; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 30 | import net.named_data.jndn.Face; |
Alexander Afanasyev | c898bca | 2016-01-28 11:18:51 -0800 | [diff] [blame] | 31 | import net.named_data.jndn.Interest; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 32 | import net.named_data.jndn.Name; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 33 | import net.named_data.jndn.encoding.EncodingException; |
| 34 | import net.named_data.jndn.security.KeyChain; |
| 35 | import net.named_data.jndn.security.SecurityException; |
| 36 | import net.named_data.jndn.transport.UdpTransport; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 37 | import net.named_data.jndn.util.Blob; |
| 38 | |
| 39 | /** |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 40 | * Collect assorted methods to help with testing |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 41 | * |
| 42 | * @author Andrew Brown <andrew.brown@intel.com> |
| 43 | */ |
| 44 | public class TestHelper { |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | c898bca | 2016-01-28 11:18:51 -0800 | [diff] [blame] | 46 | 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 | |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 82 | 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 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 90 | public static List<CompletableFuture<Data>> buildFutureSegments(Name name, int from, int to) { |
Alexander Afanasyev | bc30e65 | 2016-01-25 17:37:16 -0800 | [diff] [blame] | 91 | List<CompletableFuture<Data>> list = new ArrayList<>(); |
| 92 | for (Data d : buildSegments(name, from, to)) { |
| 93 | list.add(CompletableFuture.completedFuture(d)); |
| 94 | } |
| 95 | return list; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | public static List<Data> buildSegments(Name name, int from, int to) { |
Alexander Afanasyev | bc30e65 | 2016-01-25 17:37:16 -0800 | [diff] [blame] | 99 | 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; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 104 | } |
| 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 | } |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 112 | |
| 113 | public static Data buildData(Name name, String content, int finalBlockId) { |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 114 | Data data = buildData(name, content); |
| 115 | data.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(finalBlockId, 0x00)); |
| 116 | return data; |
| 117 | } |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 118 | |
| 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 | } |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 168 | } |