1 /*
   2  * Copyright 2015 SAP SE.  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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import jdk.testlibrary.OutputAnalyzer;
  24 import jdk.testlibrary.Platform;
  25 import jdk.testlibrary.ProcessTools;
  26 
  27 /*
  28  * @test
  29  * @bug 8132374
  30  * @summary Check that the value of the os.version property is equal
  31  *          to the value of the corresponding OS provided tools.
  32  * @library /lib/testlibrary
  33  * @run main OsVersionTest
  34  * @author Volker Simonis
  35  */
  36 public class OsVersionTest {
  37 
  38     public static void main(String args[]) throws Throwable {
  39         final String osVersion = System.getProperty("os.version");
  40         if (osVersion == null) {
  41             throw new Error("Cant query 'os.version' property!");
  42         }
  43         if (Platform.isLinux() || Platform.isSolaris()) {
  44             OutputAnalyzer output = ProcessTools.executeProcess("uname", "-r");
  45             if (!osVersion.equals(output.getOutput().trim())) {
  46                 throw new Error(osVersion + " != " + output.getOutput().trim());
  47             }
  48         }
  49         else if (Platform.isOSX()) {
  50             OutputAnalyzer output = ProcessTools.executeProcess("sw_vers", "-productVersion");
  51             if (!osVersion.equals(output.getOutput().trim())) {
  52                 throw new Error(osVersion + " != " + output.getOutput().trim());
  53             }
  54         }
  55         else if (Platform.isAix()) {
  56             OutputAnalyzer output1 = ProcessTools.executeProcess("uname", "-v");
  57             OutputAnalyzer output2 = ProcessTools.executeProcess("uname", "-r");
  58             String version = output1.getOutput().trim() + "." + output2.getOutput().trim();
  59             if (!osVersion.equals(version)) {
  60                 throw new Error(osVersion + " != " + version);
  61             }
  62         }
  63         else if (Platform.isWindows()) {
  64             OutputAnalyzer output = ProcessTools.executeProcess("cmd", "/c", "ver");
  65             String version = output.firstMatch(".+\\[Version ([0-9.]+)\\]", 1);
  66             if (version == null || !version.startsWith(osVersion)) {
  67                 throw new Error(osVersion + " != " + version);
  68             }
  69         }
  70         else {
  71             System.out.println("This test is currently not supported on " +
  72                                Platform.getOsName());
  73         }
  74     }
  75 }