1 /*
   2  * Copyright (c) 2018, 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 /**
  25  * @test
  26  * @summary DNSName parsing tests
  27  * @bug 8213952
  28  * @modules java.base/sun.security.x509
  29  * @run testng DNSNameTest
  30  */
  31 
  32 import java.io.IOException;
  33 import sun.security.x509.DNSName;
  34 
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 
  38 import static org.testng.Assert.*;
  39 
  40 public class DNSNameTest {
  41     @DataProvider(name = "goodNames")
  42     public Object[][] goodNames() {
  43         Object[][] data = {
  44                 {"abc.com"},
  45                 {"ABC.COM"},
  46                 {"a12.com"},
  47                 {"a1b2c3.com"},
  48                 {"1abc.com"},
  49                 {"123.com"},
  50                 {"abc.com-"}, // end with hyphen
  51                 {"a-b-c.com"}, // hyphens
  52         };
  53         return data;
  54     }
  55 
  56     @DataProvider(name = "badNames")
  57     public Object[][] badNames() {
  58         Object[][] data = {
  59                 {" 1abc.com"}, // begin with space
  60                 {"1abc.com "}, // end with space
  61                 {"1a bc.com "}, // no space allowed
  62                 {"-abc.com"}, // begin with hyphen
  63                 {"a..b"}, // ..
  64                 {".a"}, // begin with .
  65                 {"a."}, // end with .
  66                 {""}, // empty
  67                 {"  "},  // space only
  68         };
  69         return data;
  70     }
  71 
  72     @Test(dataProvider = "goodNames")
  73     public void testGoodDNSName(String dnsNameString) {
  74         try {
  75             DNSName dn = new DNSName(dnsNameString);
  76         } catch (IOException e) {
  77             fail("Unexpected IOException");
  78         }
  79     }
  80 
  81     @Test(dataProvider = "badNames")
  82     public void testBadDNSName(String dnsNameString) {
  83         try {
  84             DNSName dn = new DNSName(dnsNameString);
  85             fail("IOException expected");
  86         } catch (IOException e) {
  87             if (!e.getMessage().contains("DNSName"))
  88                 fail("Unexpeceted message: " + e);
  89         }
  90     }
  91 }