1 /*
   2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug     6622366 7078024
  29  * @summary Basic Test for ServiceTag.getJavaServiceTag(String)
  30  *          to verify that the registration.xml and servicetag files
  31  *          are both created correctly.
  32  * @author  Mandy Chung
  33  *
  34  * @run build JavaServiceTagTest1 SvcTagClient Util
  35  * @run main JavaServiceTagTest1
  36  */
  37 
  38 import com.sun.servicetag.*;
  39 import java.io.*;
  40 import java.util.*;
  41 
  42 public class JavaServiceTagTest1 {
  43     private static String registrationDir = System.getProperty("test.classes");
  44     private static String servicetagDir = System.getProperty("test.src");
  45     private static File regFile;
  46     private static File svcTagFile;
  47     private static Registry registry;
  48     public static void main(String[] argv) throws Exception {
  49         try {
  50             registry = Util.getSvcTagClientRegistry();
  51             runTest();
  52         } finally {
  53             // restore empty registry file
  54             Util.emptyRegistryFile();
  55         }
  56     }
  57 
  58     private static void runTest() throws Exception {
  59         // cleanup the registration.xml and servicetag file in the test directory
  60         System.setProperty("servicetag.dir.path", registrationDir);
  61         regFile = new File(registrationDir, "registration.xml");
  62         regFile.delete();
  63 
  64         svcTagFile = new File(registrationDir, "servicetag");
  65         svcTagFile.delete();
  66 
  67         // verify that only one service tag is created
  68         ServiceTag st1 = testJavaServiceTag("Test1");
  69 
  70         // getJavaServiceTag method should create a new service tag
  71         // and delete the old one
  72         ServiceTag st2 = testJavaServiceTag("Test2");
  73         if (registry.getServiceTag(st1.getInstanceURN()) != null) {
  74             throw new RuntimeException("instance_urn: " + st1.getInstanceURN() +
  75                 " exists but expected to be removed");
  76         }
  77 
  78         // expected to have different instance_urn
  79         if (st1.getInstanceURN().equals(st2.getInstanceURN())) {
  80             throw new RuntimeException("instance_urn: " + st1.getInstanceURN() +
  81                 " == " + st2.getInstanceURN());
  82         }
  83 
  84         // Delete the service tag from the Registry and the servicetag file
  85         if (registry.removeServiceTag(st2.getInstanceURN()) == null) {
  86             throw new RuntimeException("Failed to remove " +
  87                 st1.getInstanceURN() + " from the registry");
  88         }
  89         svcTagFile.delete();
  90 
  91         // call the getJavaServiceTag(String) method again
  92         // should create the servicetag file.
  93         ServiceTag st3 = testJavaServiceTag("Test2");
  94         if (!Util.matches(st2, st3)) {
  95             System.out.println(st2);
  96             System.out.println(st3);
  97             throw new RuntimeException("Test Failed: Expected to be the same");
  98         }
  99 
 100     }
 101 
 102     private static ServiceTag testJavaServiceTag(String source) throws Exception {
 103         ServiceTag svctag = ServiceTag.getJavaServiceTag(source);
 104         checkServiceTag(svctag, source);
 105 
 106         // verify if registration.xml is created
 107         if (!regFile.exists()) {
 108             throw new RuntimeException(regFile + " not created.");
 109         }
 110 
 111         // verify the registration.xml content is the expected service tag
 112         BufferedInputStream in = new BufferedInputStream(new FileInputStream(regFile));
 113         RegistrationData registration = RegistrationData.loadFromXML(in);
 114         Set<ServiceTag> c = registration.getServiceTags();
 115         if (c.size() != 1) {
 116             throw new RuntimeException(regFile + " has " + c.size() +
 117                 " service tags. Expected 1.");
 118         }
 119         ServiceTag st = registration.getServiceTag(svctag.getInstanceURN());
 120         if (!Util.matches(st, svctag)) {
 121             throw new RuntimeException("RegistrationData ServiceTag " +
 122                 " doesn't match.");
 123         }
 124 
 125         // verify the service tag added in the registry
 126         st = registry.getServiceTag(svctag.getInstanceURN());
 127         if (!Util.matches(st, svctag)) {
 128             throw new RuntimeException("Registry ServiceTag " +
 129                 " doesn't match.");
 130         }
 131 
 132         // verify if servicetag file is created
 133         if (!svcTagFile.exists()) {
 134             throw new RuntimeException(svcTagFile + " not created.");
 135         }
 136 
 137         // verify that the servicetag file only contains one instance_urn
 138         BufferedReader reader = new BufferedReader(new FileReader(svcTagFile));
 139         int count = 0;
 140         try {
 141             String line;
 142             while ((line = reader.readLine()) != null) {
 143                 if (line.equals(svctag.getInstanceURN())) {
 144                     count++;
 145                 } else {
 146                     throw new RuntimeException("servicetag contains " +
 147                         " unexpected instance_urn " + line);
 148                 }
 149             }
 150         } finally {
 151             reader.close();
 152         }
 153         if (count != 1) {
 154             throw new RuntimeException("servicetag contains unexpected " +
 155                 "number of instance_urn = " + count);
 156         }
 157         return svctag;
 158     }
 159 
 160     /**
 161      * Tests if the running platform is a JDK.
 162      */
 163     static boolean isJDK() {
 164         // Determine the JRE path by checking the existence of
 165         // <HOME>/jre/lib and <HOME>/lib.
 166         String javaHome = System.getProperty("java.home");
 167         String jrepath = javaHome + File.separator + "jre";
 168         File f = new File(jrepath, "lib");
 169         if (!f.exists()) {
 170             // java.home usually points to the JRE path
 171             jrepath = javaHome;
 172         }
 173 
 174         return jrepath.endsWith(File.separator + "jre");
 175     }
 176 
 177     private static void checkServiceTag(ServiceTag st, String source)
 178             throws IOException {
 179         Properties props = loadServiceTagProps();
 180         // jdk 8 and later, JDK and JRE have the same product URN.
 181         String jdkUrn = props.getProperty("servicetag.jdk.urn");
 182         String jreUrn = props.getProperty("servicetag.jre.urn");
 183         boolean isJdk = isJDK();
 184 
 185         if (isJdk) {
 186             if (!st.getProductURN().equals(jdkUrn) ||
 187                     !st.getProductName().equals(
 188                          props.getProperty("servicetag.jdk.name"))) {
 189                 throw new RuntimeException("Product URN and name don't match.");
 190             }
 191         } else {
 192             if (!st.getProductURN().equals(jreUrn) ||
 193                     !st.getProductName().equals(
 194                         props.getProperty("servicetag.jre.name"))) {
 195                 throw new RuntimeException("Product URN and name don't match.");
 196             }
 197         }
 198 
 199         if (!st.getProductVersion().
 200                 equals(System.getProperty("java.version"))) {
 201             throw new RuntimeException("Unexpected product_version: " +
 202                 st.getProductVersion());
 203         }
 204         if (!st.getProductParent().
 205                 equals(props.getProperty("servicetag.parent.name"))) {
 206             throw new RuntimeException("Unexpected product_parent: " +
 207                 st.getProductParent());
 208         }
 209         if (!st.getProductParentURN().
 210                 equals(props.getProperty("servicetag.parent.urn"))) {
 211             throw new RuntimeException("Unexpected product_parent_urn: " +
 212                 st.getProductParentURN());
 213         }
 214         if (!st.getPlatformArch().
 215                 equals(System.getProperty("os.arch"))) {
 216             throw new RuntimeException("Unexpected platform_arch: " +
 217                 st.getPlatformArch());
 218         }
 219 
 220         String vendor = System.getProperty("java.vendor");
 221         if (!st.getProductVendor().
 222                 equals(vendor)) {
 223             throw new RuntimeException("Unexpected product_vendor: " +
 224                 st.getProductVendor());
 225         }
 226         if (!st.getSource().
 227                 equals(source)) {
 228             throw new RuntimeException("Unexpected source: " +
 229                 st.getSource() + " expected: " + source);
 230         }
 231         String[] ss = st.getProductDefinedInstanceID().split(",");
 232         boolean id = false;
 233         boolean dir = false;
 234         for (String s : ss) {
 235             String[] values = s.split("=");
 236             if (values[0].equals("id")) {
 237                 id = true;
 238                 String[] sss = values[1].split(" ");
 239                 if (!sss[0].equals(System.getProperty("java.runtime.version"))) {
 240                     throw new RuntimeException("Unexpected version in id: " +
 241                         sss[0]);
 242                 }
 243                 if (sss.length < 2) {
 244                     throw new RuntimeException("Unexpected id=" + values[1]);
 245                 }
 246             } else if (values[0].equals("dir")) {
 247                 dir = true;
 248             }
 249         }
 250         if (!id || !dir) {
 251             throw new RuntimeException("Unexpected product_defined_instance_id: " +
 252                 st.getProductDefinedInstanceID());
 253         }
 254     }
 255 
 256     private static Properties loadServiceTagProps()
 257            throws IOException {
 258         String filename = "/com/sun/servicetag/resources/javase_servicetag.properties";
 259         try (InputStream in = Installer.class.getClass().getResourceAsStream(filename)) {
 260             Properties props = new Properties();
 261             props.load(in);
 262             return props;
 263         }
 264     }
 265 }