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  * @summary DNS resource record parsing
  27  */
  28 
  29 import com.sun.jndi.dns.ResourceRecord;
  30 import javax.naming.InvalidNameException;
  31 
  32 import java.lang.reflect.Constructor;
  33 import java.lang.reflect.InvocationTargetException;
  34 import static java.nio.charset.StandardCharsets.ISO_8859_1;
  35 
  36 public class Parser {
  37     static Constructor<ResourceRecord> rrConstructor;
  38     static {
  39         try {
  40             rrConstructor = ResourceRecord.class.getDeclaredConstructor(
  41                 byte[].class, int.class, int.class, boolean.class,
  42                 boolean.class);
  43             rrConstructor.setAccessible(true);
  44         } catch (Exception e) {
  45             throw new AssertionError(e);
  46         }
  47     }
  48 
  49     static ResourceRecord parse(String data, int offset, boolean qSection)
  50         throws Throwable {
  51         byte[] bytes = data.getBytes(ISO_8859_1);
  52         try {
  53             return rrConstructor.newInstance(
  54                 bytes, bytes.length, offset, qSection, !qSection);
  55         } catch (InvocationTargetException e) {
  56             throw e.getCause();
  57         }
  58     }
  59 
  60     public static void main(String[] args) throws Throwable {
  61         ResourceRecord rr;
  62 
  63         rr = parse("\003www\007example\003com\000\000\002\000\001",
  64                    0, true);
  65         if (!rr.getName().toString().equals("www.example.com.")) {
  66             throw new AssertionError(rr.getName().toString());
  67         }
  68         if (rr.getRrclass() != 1) {
  69             throw new AssertionError("RCLASS: " + rr.getRrclass());
  70         }
  71         if (rr.getType() != 2) {
  72             throw new AssertionError("RTYPE: " + rr.getType());
  73         }
  74 
  75         rr = parse("1-2-3-4-5-6-" +
  76                    "\003www\007example\003com\000\000\002\000\001" +
  77                    "\300\014\000\002\000\001\000\001\121\200" +
  78                    "\000\005\002ns\300\020",
  79                    33, false);
  80         if (!rr.getName().toString().equals("www.example.com.")) {
  81             throw new AssertionError(rr.getName().toString());
  82         }
  83         if (rr.getRrclass() != 1) {
  84             throw new AssertionError("RCLASS: " + rr.getRrclass());
  85         }
  86         if (rr.getType() != 2) {
  87             throw new AssertionError("RTYPE: " + rr.getType());
  88         }
  89         if (!rr.getRdata().toString().equals("ns.example.com.")) {
  90             throw new AssertionError("RDATA: " + rr.getRdata());
  91         }
  92 
  93         try {
  94             parse("1-2-3-4-5-6-" +
  95                   "\003www\007example\003com\000\000\002\000\001" +
  96                   "\300\014\000\002\000\001\300\051\300\047" +
  97                   "\000\005\002ns\300\051",
  98                   33, false);
  99             throw new AssertionError();
 100         } catch (InvalidNameException e) {
 101             if (!e.getMessage().equals("Too many compression references")) {
 102                 throw e;
 103             }
 104         }
 105     }
 106 }