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