1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.jdp.client;
  34 
  35 import java.io.UnsupportedEncodingException;
  36 import java.net.InetAddress;
  37 import java.net.MalformedURLException;
  38 import java.net.UnknownHostException;
  39 import java.security.SecureRandom;
  40 import java.util.logging.Level;
  41 
  42 import javax.management.remote.JMXServiceURL;
  43 
  44 import org.openjdk.jmc.jdp.common.Configuration;
  45 import org.openjdk.jmc.jdp.server.JDPServer;
  46 import org.openjdk.jmc.jdp.server.jmx.JMXJDPServer;
  47 
  48 @SuppressWarnings("nls")
  49 public final class TestToolkit {
  50         private static final String HEXES = "0123456789ABCDEF";
  51         private final static SecureRandom RND = new SecureRandom();
  52         public static final int TEST_MULTICAST_PORT = 7711;
  53         private static final String TEST_MULTICAST_ADDRESS_STRING = "224.0.23.177";
  54         public static final InetAddress TEST_MULTICAST_ADDRESS;
  55 
  56         static {
  57                 InetAddress tmp = null;
  58                 try {
  59                         tmp = InetAddress.getByName(TEST_MULTICAST_ADDRESS_STRING);
  60                 } catch (UnknownHostException e) {
  61                         // Multicast address by IP, should never happen!
  62                         JDPClientTest.LOGGER.log(Level.SEVERE, "Could not create test multicast address!", e);
  63                 }
  64                 TEST_MULTICAST_ADDRESS = tmp;
  65         }
  66 
  67         private TestToolkit() {
  68                 throw new AssertionError("Nope!");
  69         }
  70 
  71         public static String toHexString(byte[] raw) {
  72                 if (raw == null) {
  73                         return null;
  74                 }
  75                 final StringBuilder hex = new StringBuilder(2 * raw.length);
  76                 for (final byte b : raw) {
  77                         hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
  78                 }
  79                 return hex.toString();
  80         }
  81 
  82         public static long nextLong() {
  83                 return RND.nextLong();
  84         }
  85 
  86         public static String generateNewID(String prefix) {
  87                 return String.format("%s %X", prefix, RND.nextLong());
  88         }
  89 
  90         public static void printServerSettings(JDPServer server) {
  91                 System.out.println(
  92                                 String.format("JDP Server created at %s:%d", server.getConfiguration().getMulticastAddress().toString(),
  93                                                 server.getConfiguration().getMulticastPort()));
  94         }
  95 
  96         public static JDPServer createDefaultJMXJDPServer(String discoverableID) throws MalformedURLException {
  97                 return new JMXJDPServer(discoverableID, createConfiguration(), createServiceURL("localhost", 7091), null);
  98         }
  99 
 100         public static JMXServiceURL createServiceURL(String host, int port) throws MalformedURLException {
 101                 return new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", host, port));
 102         }
 103 
 104         public static String parseCommaSeparatedByteString(String str) {
 105                 String[] tmp = str.split(", ");
 106                 byte[] bytes = toBytes(tmp);
 107                 try {
 108                         return new String(bytes, "UTF-8");
 109                 } catch (UnsupportedEncodingException e) {
 110                         return null;
 111                 }
 112         }
 113 
 114         public static Configuration createConfiguration() {
 115                 return new Configuration() {
 116 
 117                         @Override
 118                         public short getTTL() {
 119                                 return 1;
 120                         }
 121 
 122                         @Override
 123                         public int getMulticastPort() {
 124                                 return TEST_MULTICAST_PORT;
 125                         }
 126 
 127                         @Override
 128                         public InetAddress getMulticastAddress() {
 129                                 return TEST_MULTICAST_ADDRESS;
 130                         }
 131 
 132                         @Override
 133                         public int getBroadcastPeriod() {
 134                                 return 1000;
 135                         }
 136 
 137                         @Override
 138                         public int getMaxHeartBeatTimeout() {
 139                                 return Configuration.DEFAULT_MAX_HEART_BEAT_TIMEOUT;
 140                         }
 141                 };
 142         }
 143 
 144         private static byte[] toBytes(String[] tmp) {
 145                 byte[] bytes = new byte[tmp.length];
 146                 for (int i = 0; i < tmp.length; i++) {
 147                         bytes[i] = Byte.parseByte(tmp[i]);
 148                 }
 149                 return bytes;
 150         }
 151 }