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 net.named_data.jndn.Data; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 28 | import net.named_data.jndn.Face; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 29 | import net.named_data.jndn.Name; |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 30 | import net.named_data.jndn.encoding.EncodingException; |
| 31 | import net.named_data.jndn.security.KeyChain; |
| 32 | import net.named_data.jndn.security.SecurityException; |
| 33 | import net.named_data.jndn.transport.UdpTransport; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 34 | import net.named_data.jndn.util.Blob; |
| 35 | |
| 36 | /** |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 37 | * Collect assorted methods to help with testing |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 38 | * |
| 39 | * @author Andrew Brown <andrew.brown@intel.com> |
| 40 | */ |
| 41 | public class TestHelper { |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 42 | |
| 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 | |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 51 | public static List<CompletableFuture<Data>> buildFutureSegments(Name name, int from, int to) { |
Alexander Afanasyev | bc30e65 | 2016-01-25 17:37:16 -0800 | [diff] [blame^] | 52 | List<CompletableFuture<Data>> list = new ArrayList<>(); |
| 53 | for (Data d : buildSegments(name, from, to)) { |
| 54 | list.add(CompletableFuture.completedFuture(d)); |
| 55 | } |
| 56 | return list; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public static List<Data> buildSegments(Name name, int from, int to) { |
Alexander Afanasyev | bc30e65 | 2016-01-25 17:37:16 -0800 | [diff] [blame^] | 60 | 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; |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 65 | } |
| 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 | } |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 73 | |
| 74 | public static Data buildData(Name name, String content, int finalBlockId) { |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 75 | Data data = buildData(name, content); |
| 76 | data.getMetaInfo().setFinalBlockId(Name.Component.fromNumberWithMarker(finalBlockId, 0x00)); |
| 77 | return data; |
| 78 | } |
andrewsbrown | 13b11c5 | 2015-07-02 14:23:15 -0700 | [diff] [blame] | 79 | |
| 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 | } |
andrewsbrown | 8d5ae29 | 2015-07-01 17:38:22 -0700 | [diff] [blame] | 133 | } |