1 /*
   2  * Copyright (c) 2014, Red Hat, Inc.
   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  * @bug 8035105
  27  * @summary DNS resource record parsing
  28  * @modules jdk.naming.dns/com.sun.jndi.dns:+open
  29  *
  30  * @compile --add-modules jdk.naming.dns Parser.java
  31  * @run main Parser
  32  */
  33 
  34 import com.sun.jndi.dns.ResourceRecord;
  35 import javax.naming.CommunicationException;
  36 import javax.naming.InvalidNameException;;
  37 
  38 import java.lang.reflect.Constructor;
  39 import java.lang.reflect.InvocationTargetException;
  40 
  41 import java.io.IOException;
  42 
  43 import static java.nio.charset.StandardCharsets.ISO_8859_1;
  44 
  45 public class Parser {
  46     static Constructor<ResourceRecord> rrConstructor;
  47     static {
  48         try {
  49             rrConstructor = ResourceRecord.class.getDeclaredConstructor(
  50                 byte[].class, int.class, int.class, boolean.class,
  51                 boolean.class);
  52             rrConstructor.setAccessible(true);
  53         } catch (Exception e) {
  54             throw new AssertionError(e);
  55         }
  56     }
  57 
  58     static ResourceRecord parse(String data, int offset, boolean qSection)
  59         throws Throwable {
  60         byte[] bytes = data.getBytes(ISO_8859_1);
  61         try {
  62             return rrConstructor.newInstance(
  63                 bytes, bytes.length, offset, qSection, !qSection);
  64         } catch (InvocationTargetException e) {
  65             throw e.getCause();
  66         }
  67     }
  68 
  69     public static void main(String[] args) throws Throwable {
  70         ResourceRecord rr;
  71 
  72         rr = parse("\003www\007example\003com\000\000\002\000\001",
  73             0, true);
  74         if (!rr.getName().toString().equals("www.example.com."))
  75             throw new AssertionError(rr.getName().toString());
  76         if (rr.getRrclass() != 1)
  77             throw new AssertionError("RCLASS: " + rr.getRrclass());
  78         if (rr.getType() != 2)
  79             throw new AssertionError("RTYPE: " + rr.getType());
  80 
  81         String longLabel = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +
  82             "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  83 
  84         rr = parse("\077" + longLabel + "\077" + longLabel +
  85             "\077" + longLabel + "\061" + longLabel.substring(0, 49) +
  86             "\007example\003com\000\000\002\000\001",
  87              0, true);
  88         if (!rr.getName().toString().equals(longLabel +
  89             '.' + longLabel + '.' + longLabel +
  90             '.' + longLabel.substring(0, 49) + ".example.com."))
  91             throw new AssertionError(rr.getName().toString());
  92         if (rr.getRrclass() != 1)
  93             throw new AssertionError("RCLASS: " + rr.getRrclass());
  94         if (rr.getType() != 2)
  95             throw new AssertionError("RTYPE: " + rr.getType());
  96 
  97         rr = parse("1-2-3-4-5-6-" +
  98             "\003www\007example\003com\000\000\002\000\001" +
  99             "\300\014\000\002\000\001\000\001\121\200" +
 100             "\000\005\002ns\300\020",
 101             33, false);
 102         if (!rr.getName().toString().equals("www.example.com."))
 103             throw new AssertionError(rr.getName().toString());
 104         if (rr.getRrclass() != 1)
 105             throw new AssertionError("RCLASS: " + rr.getRrclass());
 106         if (rr.getType() != 2)
 107             throw new AssertionError("RTYPE: " + rr.getType());
 108         if (!rr.getRdata().toString().equals("ns.example.com."))
 109             throw new AssertionError("RDATA: " + rr.getRdata());
 110 
 111         try {
 112             parse("1-2-3-4-5-6-" +
 113                 "\003www\007example\003com\000\000\002\000\001" +
 114                 "\300\014\000\002\000\001\300\051\300\047" +
 115                 "\000\005\002ns\300\051",
 116                 33, false);
 117             throw new AssertionError();
 118         } catch (CommunicationException e) {
 119             if (!e.getMessage().equals("DNS error: malformed packet")
 120                 || e.getCause().getClass() != IOException.class
 121                 || !e.getCause().getMessage().equals(
 122                     "Too many compression references"))
 123                 throw e;
 124         }
 125 
 126         try {
 127             String longLabel62 = "\076" + longLabel.substring(1);
 128             parse(longLabel62 + longLabel62 + longLabel62 + longLabel62 +
 129                 "\002XX\000\000\002\000\001", 0, true);
 130             throw new AssertionError();
 131         } catch (CommunicationException e) {
 132             if (!e.getMessage().equals("DNS error: malformed packet")
 133                 || e.getCause().getClass() != InvalidNameException.class
 134                 || !e.getCause().getMessage().equals("Name too long"))
 135                 throw e;
 136         }
 137         try {
 138             parse("\100Y" + longLabel + "\000\000\002\000\001", 0, true);
 139             throw new AssertionError();
 140         } catch (CommunicationException e) {
 141             if (!e.getMessage().equals("DNS error: malformed packet")
 142                 || e.getCause().getClass() != IOException.class
 143                 || !e.getCause().getMessage().equals("Invalid label type: 64"))
 144                 throw e;
 145         }
 146     }
 147 }