Path: blob/master/test/jdk/com/sun/jndi/dns/Parser.java
41153 views
/*1* Copyright (c) 2014, Red Hat, Inc.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 803510526* @summary DNS resource record parsing27* @modules jdk.naming.dns/com.sun.jndi.dns:+open28*/2930import com.sun.jndi.dns.ResourceRecord;31import javax.naming.CommunicationException;32import javax.naming.InvalidNameException;;3334import java.lang.reflect.Constructor;35import java.lang.reflect.InvocationTargetException;3637import java.io.IOException;3839import static java.nio.charset.StandardCharsets.ISO_8859_1;4041public class Parser {42static Constructor<ResourceRecord> rrConstructor;43static {44try {45rrConstructor = ResourceRecord.class.getDeclaredConstructor(46byte[].class, int.class, int.class, boolean.class,47boolean.class);48rrConstructor.setAccessible(true);49} catch (Exception e) {50throw new AssertionError(e);51}52}5354static ResourceRecord parse(String data, int offset, boolean qSection)55throws Throwable {56byte[] bytes = data.getBytes(ISO_8859_1);57try {58return rrConstructor.newInstance(59bytes, bytes.length, offset, qSection, !qSection);60} catch (InvocationTargetException e) {61throw e.getCause();62}63}6465public static void main(String[] args) throws Throwable {66ResourceRecord rr;6768rr = parse("\003www\007example\003com\000\000\002\000\001",690, true);70if (!rr.getName().toString().equals("www.example.com."))71throw new AssertionError(rr.getName().toString());72if (rr.getRrclass() != 1)73throw new AssertionError("RCLASS: " + rr.getRrclass());74if (rr.getType() != 2)75throw new AssertionError("RTYPE: " + rr.getType());7677String longLabel = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +78"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";7980rr = parse("\077" + longLabel + "\077" + longLabel +81"\077" + longLabel + "\061" + longLabel.substring(0, 49) +82"\007example\003com\000\000\002\000\001",830, true);84if (!rr.getName().toString().equals(longLabel +85'.' + longLabel + '.' + longLabel +86'.' + longLabel.substring(0, 49) + ".example.com."))87throw new AssertionError(rr.getName().toString());88if (rr.getRrclass() != 1)89throw new AssertionError("RCLASS: " + rr.getRrclass());90if (rr.getType() != 2)91throw new AssertionError("RTYPE: " + rr.getType());9293rr = parse("1-2-3-4-5-6-" +94"\003www\007example\003com\000\000\002\000\001" +95"\300\014\000\002\000\001\000\001\121\200" +96"\000\005\002ns\300\020",9733, false);98if (!rr.getName().toString().equals("www.example.com."))99throw new AssertionError(rr.getName().toString());100if (rr.getRrclass() != 1)101throw new AssertionError("RCLASS: " + rr.getRrclass());102if (rr.getType() != 2)103throw new AssertionError("RTYPE: " + rr.getType());104if (!rr.getRdata().toString().equals("ns.example.com."))105throw new AssertionError("RDATA: " + rr.getRdata());106107try {108parse("1-2-3-4-5-6-" +109"\003www\007example\003com\000\000\002\000\001" +110"\300\014\000\002\000\001\300\051\300\047" +111"\000\005\002ns\300\051",11233, false);113throw new AssertionError();114} catch (CommunicationException e) {115if (!e.getMessage().equals("DNS error: malformed packet")116|| e.getCause().getClass() != IOException.class117|| !e.getCause().getMessage().equals(118"Too many compression references"))119throw e;120}121122try {123String longLabel62 = "\076" + longLabel.substring(1);124parse(longLabel62 + longLabel62 + longLabel62 + longLabel62 +125"\002XX\000\000\002\000\001", 0, true);126throw new AssertionError();127} catch (CommunicationException e) {128if (!e.getMessage().equals("DNS error: malformed packet")129|| e.getCause().getClass() != InvalidNameException.class130|| !e.getCause().getMessage().equals("Name too long"))131throw e;132}133try {134parse("\100Y" + longLabel + "\000\000\002\000\001", 0, true);135throw new AssertionError();136} catch (CommunicationException e) {137if (!e.getMessage().equals("DNS error: malformed packet")138|| e.getCause().getClass() != IOException.class139|| !e.getCause().getMessage().equals("Invalid label type: 64"))140throw e;141}142}143}144145146