1 /*
   2  * Copyright (c) 2015, 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.
   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 
  24 import com.oracle.java.testlibrary.OutputAnalyzer;
  25 import com.oracle.java.testlibrary.dcmd.CommandExecutor;
  26 import com.oracle.java.testlibrary.dcmd.JMXExecutor;
  27 import org.testng.annotations.Test;
  28 import static org.testng.Assert.*;
  29 
  30 /*
  31  * @test
  32  * @bug 8054890
  33  * @summary Test of VM.set_flag diagnostic command
  34  * @library /testlibrary
  35  * @build com.oracle.java.testlibrary.*
  36  * @build com.oracle.java.testlibrary.dcmd.*
  37  * @run testng SetVMFlagTest
  38  */
  39 
  40 public class SetVMFlagTest {
  41     private static final String MANAGEABLE_PATTERN = "\\s*bool\\s+(\\S+)\\s+[\\:]?=\\s+" +
  42                                                      "(.*?)\\s+\\{manageable\\}";
  43     private static final String IMMUTABLE_PATTERN = "\\s*uintx\\s+(\\S+)\\s+[\\:]?=\\s+" +
  44                                                     "(.*?)\\s+\\{product\\}";
  45 
  46     public void run(CommandExecutor executor) {
  47         setMutableFlag(executor);
  48         setMutableFlagWithInvalidValue(executor);
  49         setImmutableFlag(executor);
  50         setNonExistingFlag(executor);
  51     }
  52 
  53     @Test
  54     public void jmx() {
  55         run(new JMXExecutor());
  56     }
  57 
  58     private void setMutableFlag(CommandExecutor executor) {
  59         OutputAnalyzer out = getAllFlags(executor);
  60         String flagName = out.firstMatch(MANAGEABLE_PATTERN, 1);
  61         String flagVal = out.firstMatch(MANAGEABLE_PATTERN, 2);
  62 
  63         System.out.println("### Setting a mutable flag '" + flagName + "'");
  64 
  65         if (flagVal == null) {
  66             System.err.println(out.getOutput());
  67             throw new Error("Can not find a boolean manageable flag");
  68         }
  69 
  70         Boolean blnVal = Boolean.parseBoolean(flagVal);
  71 
  72         out = executor.execute("VM.set_flag " + flagName + " " + (blnVal ? 0 : 1));
  73         out.stderrShouldBeEmpty();
  74 
  75         out = getAllFlags(executor);
  76 
  77         String newFlagVal = out.firstMatch(MANAGEABLE_PATTERN.replace("(\\S+)", flagName), 1);
  78 
  79         assertNotEquals(newFlagVal, flagVal);
  80     }
  81 
  82     private void setMutableFlagWithInvalidValue(CommandExecutor executor) {
  83         OutputAnalyzer out = getAllFlags(executor);
  84         String flagName = out.firstMatch(MANAGEABLE_PATTERN, 1);
  85         String flagVal = out.firstMatch(MANAGEABLE_PATTERN, 2);
  86 
  87         System.out.println("### Setting a mutable flag '" + flagName + "' to an invalid value");
  88 
  89         if (flagVal == null) {
  90             System.err.println(out.getOutput());
  91             throw new Error("Can not find a boolean manageable flag");
  92         }
  93 
  94         // a boolean flag accepts only 0/1 as its value
  95         out = executor.execute("VM.set_flag " + flagName + " unexpected_value");
  96         out.stderrShouldBeEmpty();
  97         out.stdoutShouldContain("flag value must be a boolean (1 or 0)");
  98 
  99         out = getAllFlags(executor);
 100 
 101         String newFlagVal = out.firstMatch(MANAGEABLE_PATTERN.replace("(\\S+)", flagName), 1);
 102 
 103         assertEquals(newFlagVal, flagVal);
 104     }
 105 
 106     private void setImmutableFlag(CommandExecutor executor) {
 107         OutputAnalyzer out = getAllFlags(executor);
 108         String flagName = out.firstMatch(IMMUTABLE_PATTERN, 1);
 109         String flagVal = out.firstMatch(IMMUTABLE_PATTERN, 2);
 110 
 111         System.out.println("### Setting an immutable flag '" + flagName + "'");
 112 
 113         if (flagVal == null) {
 114             System.err.println(out.getOutput());
 115             throw new Error("Can not find an immutable uintx flag");
 116         }
 117 
 118         Long numVal = Long.parseLong(flagVal);
 119 
 120         out = executor.execute("VM.set_flag " + flagName + " " + (numVal + 1));
 121         out.stderrShouldBeEmpty();
 122         out.stdoutShouldContain("only 'writeable' flags can be set");
 123 
 124         out = getAllFlags(executor);
 125 
 126         String newFlagVal = out.firstMatch(IMMUTABLE_PATTERN.replace("(\\S+)", flagName), 1);
 127 
 128         assertEquals(newFlagVal, flagVal);
 129     }
 130 
 131     private void setNonExistingFlag(CommandExecutor executor) {
 132         String unknownFlag = "ThisIsUnknownFlag";
 133         System.out.println("### Setting a non-existing flag '" + unknownFlag + "'");
 134         OutputAnalyzer out = executor.execute("VM.set_flag " + unknownFlag + " 1");
 135         out.stderrShouldBeEmpty();
 136         out.stdoutShouldContain("flag " + unknownFlag + " does not exist");
 137     }
 138 
 139     private OutputAnalyzer getAllFlags(CommandExecutor executor) {
 140         return executor.execute("VM.flags -all", true);
 141     }
 142 }