1 
   2 /*
   3  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 import java.awt.Button;
  26 import java.awt.Dialog;
  27 import java.awt.Frame;
  28 import java.awt.Panel;
  29 import java.awt.TextArea;
  30 import java.awt.event.ActionEvent;
  31 import java.awt.event.ActionListener;
  32 
  33 /*
  34  * @test
  35  * @bug 6991580 8080108 8133035
  36  * @requires os.family != "windows"
  37  * @summary IPv6 Nameservers in resolv.conf throws NumberFormatException
  38  * @modules java.desktop
  39  *          jdk.naming.dns/com.sun.jndi.dns
  40  * @run main/manual Test6991580
  41  */
  42 
  43 
  44 public class Test6991580 {
  45 
  46    private static void init() throws Exception {
  47     //*** Create instructions for the user here ***
  48 
  49     String[] instructions =
  50         {
  51          "This test should only be run on non-Windows systems.",
  52          "If your system doesn't meet this condition, press PASS.",
  53          "To run the test follow these instructions:",
  54          "1. Open a terminal window.",
  55          "2. Make sure you have, for example, the following snippet "
  56             + "into your platform's /etc/resolv.conf:",
  57          "nameserver 127.0.0.1",
  58          "nameserver 2001:4860:4860::8888",
  59          "nameserver [::1]:5353",
  60          "nameserver 127.0.0.1:5353",
  61          "Modify the /etc/resolv.conf file if needed. "
  62             + "Don't forget to save the original content of the file.",
  63          "3. Type \"cd " + System.getProperty("test.classes") + "\".",
  64          "4. Type \"" + System.getProperty("java.home") +
  65                 "/bin/java IPv6NameserverPlatformParsingTest\".",
  66          "5. If you see",
  67          "\"PASS: Found IPv6 address and DnsClient parsed it correctly.\"",
  68          ", press PASS else press FAIL.",
  69          "6. If you modified /etc/resolv.conf on the step #2, "
  70             + "please, restore the original content of the file."
  71         };
  72 
  73     Sysout.createDialog( );
  74     Sysout.printInstructions( instructions );
  75    }
  76 
  77  /*****************************************************
  78      Standard Test Machinery Section
  79       DO NOT modify anything in this section -- it's a
  80       standard chunk of code which has all of the
  81       synchronisation necessary for the test harness.
  82       By keeping it the same in all tests, it is easier
  83       to read and understand someone else's test, as
  84       well as insuring that all tests behave correctly
  85       with the test harness.
  86      There is a section following this for test-defined
  87       classes
  88   ******************************************************/
  89    private static boolean theTestPassed = false;
  90    private static boolean testGeneratedInterrupt = false;
  91    private static String failureMessage = "";
  92 
  93    private static Thread mainThread = null;
  94 
  95    private static int sleepTime = 300000;
  96 
  97    public static void main( String args[] ) throws Exception
  98     {
  99       mainThread = Thread.currentThread();
 100       try
 101        {
 102          init();
 103        }
 104       catch( TestPassedException e )
 105        {
 106          //The test passed, so just return from main and harness will
 107          // interepret this return as a pass
 108          return;
 109        }
 110       //At this point, neither test passed nor test failed has been
 111       // called -- either would have thrown an exception and ended the
 112       // test, so we know we have multiple threads.
 113 
 114       //Test involves other threads, so sleep and wait for them to
 115       // called pass() or fail()
 116       try
 117        {
 118          Thread.sleep( sleepTime );
 119          //Timed out, so fail the test
 120          throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 121        }
 122       catch (InterruptedException e)
 123        {
 124          if( ! testGeneratedInterrupt ) throw e;
 125 
 126          //reset flag in case hit this code more than once for some reason (just safety)
 127          testGeneratedInterrupt = false;
 128          if ( theTestPassed == false )
 129           {
 130             throw new RuntimeException( failureMessage );
 131           }
 132        }
 133 
 134     }//main
 135 
 136    public static synchronized void setTimeoutTo( int seconds )
 137     {
 138       sleepTime = seconds * 1000;
 139     }
 140 
 141    public static synchronized void pass()
 142     {
 143       Sysout.println( "The test passed." );
 144       Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 145       //first check if this is executing in main thread
 146       if ( mainThread == Thread.currentThread() )
 147        {
 148          //Still in the main thread, so set the flag just for kicks,
 149          // and throw a test passed exception which will be caught
 150          // and end the test.
 151          theTestPassed = true;
 152          throw new TestPassedException();
 153        }
 154       //pass was called from a different thread, so set the flag and interrupt
 155       // the main thead.
 156       theTestPassed = true;
 157       testGeneratedInterrupt = true;
 158       mainThread.interrupt();
 159     }//pass()
 160 
 161    public static synchronized void fail()
 162     {
 163       //test writer didn't specify why test failed, so give generic
 164       fail( "it just plain failed! :-)" );
 165     }
 166 
 167    public static synchronized void fail( String whyFailed )
 168     {
 169       Sysout.println( "The test failed: " + whyFailed );
 170       Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 171       //check if this called from main thread
 172       if ( mainThread == Thread.currentThread() )
 173        {
 174          //If main thread, fail now 'cause not sleeping
 175          throw new RuntimeException( whyFailed );
 176        }
 177       theTestPassed = false;
 178       testGeneratedInterrupt = true;
 179       failureMessage = whyFailed;
 180       mainThread.interrupt();
 181     }//fail()
 182 
 183  }
 184 
 185 //This exception is used to exit from any level of call nesting
 186 // when it's determined that the test has passed, and immediately
 187 // end the test.
 188 class TestPassedException extends RuntimeException
 189  {
 190  }
 191 
 192 //*********** End Standard Test Machinery Section **********
 193 
 194 
 195 /****************************************************
 196  Standard Test Machinery
 197  DO NOT modify anything below -- it's a standard
 198   chunk of code whose purpose is to make user
 199   interaction uniform, and thereby make it simpler
 200   to read and understand someone else's test.
 201  ****************************************************/
 202 
 203 /**
 204  This is part of the standard test machinery.
 205  It creates a dialog (with the instructions), and is the interface
 206   for sending text messages to the user.
 207  To print the instructions, send an array of strings to Sysout.createDialog
 208   WithInstructions method.  Put one line of instructions per array entry.
 209  To display a message for the tester to see, simply call Sysout.println
 210   with the string to be displayed.
 211  This mimics System.out.println but works within the test harness as well
 212   as standalone.
 213  */
 214 
 215 class Sysout
 216  {
 217    private static TestDialog dialog;
 218 
 219    public static void createDialogWithInstructions( String[] instructions )
 220     {
 221       dialog = new TestDialog( new Frame(), "Instructions" );
 222       dialog.printInstructions( instructions );
 223       dialog.show();
 224       println( "Any messages for the tester will display here." );
 225     }
 226 
 227    public static void createDialog( )
 228     {
 229       dialog = new TestDialog( new Frame(), "Instructions" );
 230       String[] defInstr = { "Instructions will appear here. ", "" } ;
 231       dialog.printInstructions( defInstr );
 232       dialog.show();
 233       println( "Any messages for the tester will display here." );
 234     }
 235 
 236 
 237    public static void printInstructions( String[] instructions )
 238     {
 239       dialog.printInstructions( instructions );
 240     }
 241 
 242 
 243    public static void println( String messageIn )
 244     {
 245       dialog.displayMessage( messageIn );
 246     }
 247 
 248  }// Sysout  class
 249 
 250 /**
 251   This is part of the standard test machinery.  It provides a place for the
 252    test instructions to be displayed, and a place for interactive messages
 253    to the user to be displayed.
 254   To have the test instructions displayed, see Sysout.
 255   To have a message to the user be displayed, see Sysout.
 256   Do not call anything in this dialog directly.
 257   */
 258 class TestDialog extends Dialog implements ActionListener
 259  {
 260 
 261    TextArea instructionsText;
 262    TextArea messageText;
 263    int maxStringLength = 120;
 264    Panel  buttonP = new Panel();
 265    Button passB = new Button( "pass" );
 266    Button failB = new Button( "fail" );
 267 
 268    //DO NOT call this directly, go through Sysout
 269    public TestDialog( Frame frame, String name )
 270     {
 271       super( frame, name );
 272       int scrollBoth = TextArea.SCROLLBARS_BOTH;
 273       instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 274       add( "North", instructionsText );
 275 
 276       messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 277       add("Center", messageText);
 278 
 279       passB = new Button( "pass" );
 280       passB.setActionCommand( "pass" );
 281       passB.addActionListener( this );
 282       buttonP.add( "East", passB );
 283 
 284       failB = new Button( "fail" );
 285       failB.setActionCommand( "fail" );
 286       failB.addActionListener( this );
 287       buttonP.add( "West", failB );
 288 
 289       add( "South", buttonP );
 290       pack();
 291 
 292       show();
 293     }// TestDialog()
 294 
 295    //DO NOT call this directly, go through Sysout
 296    public void printInstructions( String[] instructions )
 297     {
 298       //Clear out any current instructions
 299       instructionsText.setText( "" );
 300 
 301       //Go down array of instruction strings
 302 
 303       String printStr, remainingStr;
 304       for( int i=0; i < instructions.length; i++ )
 305        {
 306          //chop up each into pieces maxSringLength long
 307          remainingStr = instructions[ i ];
 308          while( remainingStr.length() > 0 )
 309           {
 310             //if longer than max then chop off first max chars to print
 311             if( remainingStr.length() >= maxStringLength )
 312              {
 313                //Try to chop on a word boundary
 314                int posOfSpace = remainingStr.
 315                   lastIndexOf( ' ', maxStringLength - 1 );
 316 
 317                if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 318 
 319                printStr = remainingStr.substring( 0, posOfSpace + 1 );
 320                remainingStr = remainingStr.substring( posOfSpace + 1 );
 321              }
 322             //else just print
 323             else
 324              {
 325                printStr = remainingStr;
 326                remainingStr = "";
 327              }
 328 
 329             instructionsText.append( printStr + "\n" );
 330 
 331           }// while
 332 
 333        }// for
 334 
 335     }//printInstructions()
 336 
 337    //DO NOT call this directly, go through Sysout
 338    public void displayMessage( String messageIn )
 339     {
 340       messageText.append( messageIn + "\n" );
 341     }
 342 
 343    //catch presses of the passed and failed buttons.
 344    //simply call the standard pass() or fail() static methods of
 345    //DialogOrient
 346    @Override
 347    public void actionPerformed( ActionEvent e )
 348     {
 349       if( "pass".equals(e.getActionCommand()) )
 350        {
 351          Test6991580.pass();
 352        }
 353       else
 354        {
 355          Test6991580.fail();
 356        }
 357     }
 358 
 359  }