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