Author: erodriguez
Date: Fri Jan  7 15:16:33 2005
New Revision: 124588

URL: http://svn.apache.org/viewcvs?view=rev&rev=124588
Log:
Basic DNS message decoder testing.
Added:
   
incubator/directory/dns/trunk/core/src/test/org/apache/dns/DnsMessageDecoderTest.java
   incubator/directory/dns/trunk/core/src/test/org/apache/dns/DnsTestCase.java

Added: 
incubator/directory/dns/trunk/core/src/test/org/apache/dns/DnsMessageDecoderTest.java
Url: 
http://svn.apache.org/viewcvs/incubator/directory/dns/trunk/core/src/test/org/apache/dns/DnsMessageDecoderTest.java?view=auto&rev=124588
==============================================================================
--- (empty file)
+++ 
incubator/directory/dns/trunk/core/src/test/org/apache/dns/DnsMessageDecoderTest.java
       Fri Jan  7 15:16:33 2005
@@ -0,0 +1,50 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.dns;
+
+import java.nio.ByteBuffer;
+
+import org.apache.dns.io.DnsMessageDecoder;
+import org.apache.dns.messages.DnsMessage;
+
+
+public class DnsMessageDecoderTest extends DnsTestCase
+{
+       private ByteBuffer requestByteBuffer;
+       
+       public void testParseQuery() throws Exception
+       {
+               requestByteBuffer = getByteBufferFromFile( "DNS-QUERY.pdu" );
+               
+               DnsMessageDecoder decoder = new DnsMessageDecoder();
+               DnsMessage dnsRequest = decoder.decode( requestByteBuffer );
+               
+               print( dnsRequest );
+       }
+       
+       public void testParseResponse() throws Exception
+       {
+               requestByteBuffer = getByteBufferFromFile( "DNS-RESPONSE.pdu" );
+               
+               DnsMessageDecoder decoder = new DnsMessageDecoder();
+               DnsMessage dnsRequest = decoder.decode( requestByteBuffer );
+               
+               print( dnsRequest );
+       }
+}
+

Added: 
incubator/directory/dns/trunk/core/src/test/org/apache/dns/DnsTestCase.java
Url: 
http://svn.apache.org/viewcvs/incubator/directory/dns/trunk/core/src/test/org/apache/dns/DnsTestCase.java?view=auto&rev=124588
==============================================================================
--- (empty file)
+++ incubator/directory/dns/trunk/core/src/test/org/apache/dns/DnsTestCase.java 
Fri Jan  7 15:16:33 2005
@@ -0,0 +1,66 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+
+package org.apache.dns;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+import junit.framework.TestCase;
+
+import org.apache.dns.messages.DnsMessage;
+
+
+public class DnsTestCase extends TestCase
+{
+       private static final int MINIMUM_DNS_DATAGRAM_SIZE = 576;
+       
+       protected void print( DnsMessage message )
+       {
+               System.out.println( message.getTransactionId() );
+               System.out.println( message.getMessageType() );
+               System.out.println( message.getOpCode() );
+               System.out.println( message.isAuthoritativeAnswer() );
+               System.out.println( message.isTruncated() );
+               System.out.println( message.isRecursionDesired() );
+               System.out.println( message.isRecursionAvailable() );
+               System.out.println( message.getResponseCode() );
+               System.out.println( message.getQuestionCount() );
+               System.out.println( message.getAnswerCount() );
+               System.out.println( message.getAuthorityCount() );
+               System.out.println( message.getAdditionalCount() );
+       }
+       
+    protected ByteBuffer getByteBufferFromFile( String file ) throws 
IOException
+       {
+        InputStream is = getClass().getResourceAsStream( file );
+    
+        byte[] bytes = new byte[ MINIMUM_DNS_DATAGRAM_SIZE ];
+    
+        int offset = 0;
+        int numRead = 0;
+        while (offset < bytes.length && (numRead=is.read(bytes, offset, 
bytes.length-offset)) >= 0)
+        {
+            offset += numRead;
+        }
+        
+        is.close();
+        return ByteBuffer.wrap( bytes );
+    }
+}
+

Reply via email to