Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/dns/Parser.java
38855 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*/2829import com.sun.jndi.dns.ResourceRecord;30import javax.naming.CommunicationException;31import javax.naming.InvalidNameException;;3233import java.lang.reflect.Constructor;34import java.lang.reflect.InvocationTargetException;3536import java.io.IOException;3738import static java.nio.charset.StandardCharsets.ISO_8859_1;3940public class Parser {41static Constructor<ResourceRecord> rrConstructor;42static {43try {44rrConstructor = ResourceRecord.class.getDeclaredConstructor(45byte[].class, int.class, int.class, boolean.class,46boolean.class);47rrConstructor.setAccessible(true);48} catch (Exception e) {49throw new AssertionError(e);50}51}5253static ResourceRecord parse(String data, int offset, boolean qSection)54throws Throwable {55byte[] bytes = data.getBytes(ISO_8859_1);56try {57return rrConstructor.newInstance(58bytes, bytes.length, offset, qSection, !qSection);59} catch (InvocationTargetException e) {60throw e.getCause();61}62}6364public static void main(String[] args) throws Throwable {65ResourceRecord rr;6667rr = parse("\003www\007example\003com\000\000\002\000\001",680, true);69if (!rr.getName().toString().equals("www.example.com."))70throw new AssertionError(rr.getName().toString());71if (rr.getRrclass() != 1)72throw new AssertionError("RCLASS: " + rr.getRrclass());73if (rr.getType() != 2)74throw new AssertionError("RTYPE: " + rr.getType());7576String longLabel = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" +77"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";7879rr = parse("\077" + longLabel + "\077" + longLabel +80"\077" + longLabel + "\061" + longLabel.substring(0, 49) +81"\007example\003com\000\000\002\000\001",820, true);83if (!rr.getName().toString().equals(longLabel +84'.' + longLabel + '.' + longLabel +85'.' + longLabel.substring(0, 49) + ".example.com."))86throw new AssertionError(rr.getName().toString());87if (rr.getRrclass() != 1)88throw new AssertionError("RCLASS: " + rr.getRrclass());89if (rr.getType() != 2)90throw new AssertionError("RTYPE: " + rr.getType());9192rr = parse("1-2-3-4-5-6-" +93"\003www\007example\003com\000\000\002\000\001" +94"\300\014\000\002\000\001\000\001\121\200" +95"\000\005\002ns\300\020",9633, false);97if (!rr.getName().toString().equals("www.example.com."))98throw new AssertionError(rr.getName().toString());99if (rr.getRrclass() != 1)100throw new AssertionError("RCLASS: " + rr.getRrclass());101if (rr.getType() != 2)102throw new AssertionError("RTYPE: " + rr.getType());103if (!rr.getRdata().toString().equals("ns.example.com."))104throw new AssertionError("RDATA: " + rr.getRdata());105106try {107parse("1-2-3-4-5-6-" +108"\003www\007example\003com\000\000\002\000\001" +109"\300\014\000\002\000\001\300\051\300\047" +110"\000\005\002ns\300\051",11133, false);112throw new AssertionError();113} catch (CommunicationException e) {114if (!e.getMessage().equals("DNS error: malformed packet")115|| e.getCause().getClass() != IOException.class116|| !e.getCause().getMessage().equals(117"Too many compression references"))118throw e;119}120121try {122String longLabel62 = "\076" + longLabel.substring(1);123parse(longLabel62 + longLabel62 + longLabel62 + longLabel62 +124"\002XX\000\000\002\000\001", 0, true);125throw new AssertionError();126} catch (CommunicationException e) {127if (!e.getMessage().equals("DNS error: malformed packet")128|| e.getCause().getClass() != InvalidNameException.class129|| !e.getCause().getMessage().equals("Name too long"))130throw e;131}132try {133parse("\100Y" + longLabel + "\000\000\002\000\001", 0, true);134throw new AssertionError();135} catch (CommunicationException e) {136if (!e.getMessage().equals("DNS error: malformed packet")137|| e.getCause().getClass() != IOException.class138|| !e.getCause().getMessage().equals("Invalid label type: 64"))139throw e;140}141}142}143144145