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 com.intel.jndn.mock.MockKeyChain; |
Andrew Brown | 85234ea | 2016-09-07 13:41:29 -0700 | [diff] [blame] | 17 | import net.named_data.jndn.Data; |
| 18 | import net.named_data.jndn.Face; |
| 19 | import net.named_data.jndn.Name; |
| 20 | import net.named_data.jndn.encoding.EncodingException; |
| 21 | import net.named_data.jndn.security.KeyChain; |
| 22 | import net.named_data.jndn.security.SecurityException; |
| 23 | import net.named_data.jndn.transport.UdpTransport; |
| 24 | import net.named_data.jndn.util.Blob; |
| 25 | |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.util.ArrayList; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 28 | import java.util.List; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 29 | import java.util.Random; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 30 | import java.util.concurrent.CompletableFuture; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 31 | import java.util.concurrent.Executors; |
| 32 | import java.util.concurrent.ScheduledExecutorService; |
| 33 | import java.util.concurrent.TimeUnit; |
| 34 | import java.util.logging.Level; |
| 35 | import java.util.logging.Logger; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 36 | import java.util.stream.Collectors; |
| 37 | import java.util.stream.IntStream; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 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 | * |
Andrew Brown | 85234ea | 2016-09-07 13:41:29 -0700 | [diff] [blame] | 42 | * @author Andrew Brown, andrew.brown@intel.com |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 43 | */ |
| 44 | public class TestHelper { |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 45 | |
| 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 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 54 | 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) { |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 61 | return IntStream.range(from, to).boxed() |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 62 | .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 | } |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 72 | |
| 73 | public static Data buildData(Name name, String content, int finalBlockId) { |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 74 | Data data = buildData(name, content); |
| 75 | data.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(finalBlockId, 0x00)); |
| 76 | return data; |
| 77 | } |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 78 | |
| 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 Brown | 0f5a9c1 | 2016-09-12 21:06:39 -0700 | [diff] [blame] | 108 | public final List<Face> faces = new ArrayList<>(); |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 109 | } |
| 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 | } |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 132 | } |