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; |
| 17 | import java.io.IOException; |
| 18 | import java.util.ArrayList; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 19 | import java.util.List; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 20 | import java.util.Random; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 21 | import java.util.concurrent.CompletableFuture; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 22 | import java.util.concurrent.Executors; |
| 23 | import java.util.concurrent.ScheduledExecutorService; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | import java.util.logging.Level; |
| 26 | import java.util.logging.Logger; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 27 | import java.util.stream.Collectors; |
| 28 | import java.util.stream.IntStream; |
| 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; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 31 | import net.named_data.jndn.Name; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 32 | import net.named_data.jndn.encoding.EncodingException; |
| 33 | import net.named_data.jndn.security.KeyChain; |
| 34 | import net.named_data.jndn.security.SecurityException; |
| 35 | import net.named_data.jndn.transport.UdpTransport; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 36 | import net.named_data.jndn.util.Blob; |
| 37 | |
| 38 | /** |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 39 | * Collect assorted methods to help with testing |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 40 | * |
| 41 | * @author Andrew Brown <andrew.brown@intel.com> |
| 42 | */ |
| 43 | public class TestHelper { |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 44 | |
| 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 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 53 | 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) { |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 60 | return IntStream.range(from, to).boxed() |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 61 | .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 | } |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 71 | |
| 72 | public static Data buildData(Name name, String content, int finalBlockId) { |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 73 | Data data = buildData(name, content); |
| 74 | data.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(finalBlockId, 0x00)); |
| 75 | return data; |
| 76 | } |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 77 | |
| 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 | } |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 131 | } |