<commit_before>/*
 *   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.ldap.common.codec.abandon;

import java.nio.ByteBuffer;
import java.util.List;

import org.apache.asn1.codec.DecoderException;
import org.apache.asn1.codec.EncoderException;
import org.apache.asn1.ber.Asn1Decoder;
import org.apache.asn1.ber.IAsn1Container;
import org.apache.ldap.common.codec.Control;
import org.apache.ldap.common.codec.LdapDecoder;
import org.apache.ldap.common.codec.LdapMessage;
import org.apache.ldap.common.codec.LdapMessageContainer;
import org.apache.ldap.common.codec.abandon.AbandonRequest;
import org.apache.ldap.common.util.StringTools;

import junit.framework.TestCase;

/**
 * Test an AbandonRequest
 * 
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 */
public class AbandonRequestTest extends TestCase {
    /**
     * Test the decoding of a AbandonRequest with no controls
     */
    public void testDecodeAbandonRequestNoControls()
    {
        Asn1Decoder ldapDecoder = new LdapDecoder();

        ByteBuffer  stream      = ByteBuffer.allocate( 0x25 );
        stream.put(
            new byte[]
            {
                0x30, 0x23, 		  // LDAPMessage ::=SEQUENCE {
  				  0x02, 0x01, 0x03,   //        messageID MessageID
				  0x50, 0x01, 0x02,	  //        CHOICE { ..., abandonRequest AbandonRequest,...
									  // AbandonRequest ::= [APPLICATION 16] MessageID
                  (byte)0xA0, 0x1B,   // A control 
                    0x30, 0x19, 
                      0x04, 0x17, 
                        0x32, 0x2E, 0x31, 0x36, 0x2E, 0x38, 0x34, 0x30, 
                        0x2E, 0x31, 0x2E, 0x31, 0x31, 0x33, 0x37, 0x33, 
                        0x30, 0x2E, 0x33, 0x2E, 0x34, 0x2E, 0x32
            } );

        String decodedPdu = StringTools.dumpBytes( stream.array() );
        stream.flip();

        // Allocate a LdapMessageContainer Container
        IAsn1Container ldapMessageContainer = new LdapMessageContainer();

        // Decode the PDU
        try
        {
            ldapDecoder.decode( stream, ldapMessageContainer );
        }
        catch ( DecoderException de )
        {
            de.printStackTrace();
            fail( de.getMessage() );
        }
    	
        // Check that everything is OK
        LdapMessage message = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage();
        AbandonRequest abandonRequest = message.getAbandonRequest();

        assertEquals( 3, message.getMessageId() );
        assertEquals( 2, abandonRequest.getAbandonedMessageId() );
        
        // Check the length
        assertEquals( 0x25, message.computeLength());
        
        // Check the Control
        List controls = message.getControls();
        
        assertEquals( 1, controls.size() );
        
        Control control = message.getControls( 0 );
        assertEquals( "2.16.840.1.113730.3.4.2", control.getControlType() );
        assertEquals( "", StringTools.dumpBytes( (byte[])control.getControlValue() ) );

        // Check the encoding
        try
        {
            ByteBuffer bb = message.encode( null );
            
            String encodedPdu = StringTools.dumpBytes( bb.array() ); 
            
            assertEquals(encodedPdu, decodedPdu );
        }
        catch ( EncoderException ee )
        {
            ee.printStackTrace();
            fail( ee.getMessage() );
        }
    }

    /**
     * Test the decoding of a AbandonRequest with controls
     */
    public void testDecodeAbandonRequestWithControls()
    {
        Asn1Decoder ldapDecoder = new LdapDecoder();

        ByteBuffer  stream      = ByteBuffer.allocate( 0x64 );
        stream.put(
            new byte[]
            {
                0x30, 0x62,          // LDAPMessage ::=SEQUENCE {
                  0x02, 0x01, 0x03,  //        messageID MessageID
                  0x50, 0x01, 0x02,  //        CHOICE { ..., abandonRequest AbandonRequest,...
                  (byte)0xA0, 0x5A,  //    controls       [0] Controls OPTIONAL }
                    0x30, 0x1A,      // Control ::= SEQUENCE {
                                     //    controlType             LDAPOID, 
                      0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '1',
                      0x01, 0x01, (byte)0xFF,   //    criticality             BOOLEAN DEFAULT FALSE,
                                     //    controlValue            OCTET STRING OPTIONAL }
                      0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f',
                    0x30, 0x17,      // Control ::= SEQUENCE {
                                     //    controlType             LDAPOID, 
                      0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2',
                                     //    controlValue            OCTET STRING OPTIONAL }
                      0x04, 0x06, 'g', 'h', 'i', 'j', 'k', 'l',
                    0x30, 0x12,      // Control ::= SEQUENCE {
                                     //    controlType             LDAPOID, 
                      0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3',
                      0x01, 0x01, (byte)0xFF,   //    criticality             BOOLEAN DEFAULT FALSE}
                    0x30, 0x0F,      // Control ::= SEQUENCE {<commit_msg>Deleted a duplicated test<commit_after>/*
 *   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.ldap.common.codec.abandon;

import java.nio.ByteBuffer;
import java.util.List;

import org.apache.asn1.codec.DecoderException;
import org.apache.asn1.codec.EncoderException;
import org.apache.asn1.ber.Asn1Decoder;
import org.apache.asn1.ber.IAsn1Container;
import org.apache.ldap.common.codec.Control;
import org.apache.ldap.common.codec.LdapDecoder;
import org.apache.ldap.common.codec.LdapMessage;
import org.apache.ldap.common.codec.LdapMessageContainer;
import org.apache.ldap.common.codec.abandon.AbandonRequest;
import org.apache.ldap.common.util.StringTools;

import junit.framework.TestCase;

/**
 * Test an AbandonRequest
 * 
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 */
public class AbandonRequestTest extends TestCase {
    /**
     * Test the decoding of a AbandonRequest with controls
     */
    public void testDecodeAbandonRequestWithControls()
    {
        Asn1Decoder ldapDecoder = new LdapDecoder();

        ByteBuffer  stream      = ByteBuffer.allocate( 0x64 );
        stream.put(
            new byte[]
            {
                0x30, 0x62,          // LDAPMessage ::=SEQUENCE {
                  0x02, 0x01, 0x03,  //        messageID MessageID
                  0x50, 0x01, 0x02,  //        CHOICE { ..., abandonRequest AbandonRequest,...
                  (byte)0xA0, 0x5A,  //    controls       [0] Controls OPTIONAL }
                    0x30, 0x1A,      // Control ::= SEQUENCE {
                                     //    controlType             LDAPOID, 
                      0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '1',
                      0x01, 0x01, (byte)0xFF,   //    criticality             BOOLEAN DEFAULT FALSE,
                                     //    controlValue            OCTET STRING OPTIONAL }
                      0x04, 0x06, 'a', 'b', 'c', 'd', 'e', 'f',
                    0x30, 0x17,      // Control ::= SEQUENCE {
                                     //    controlType             LDAPOID, 
                      0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2',
                                     //    controlValue            OCTET STRING OPTIONAL }
                      0x04, 0x06, 'g', 'h', 'i', 'j', 'k', 'l',
                    0x30, 0x12,      // Control ::= SEQUENCE {
                                     //    controlType             LDAPOID, 
                      0x04, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '3',
                      0x01, 0x01, (byte)0xFF,   //    criticality             BOOLEAN DEFAULT FALSE}
                    0x30, 0x0F,      // Control ::= SEQUENCE {
{'fraction_non_alphanumeric': 0.12283345892991711, 'fraction_numerical': 0.04241576057702659, 'mean_word_length': 1.6603665521191295, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5110209', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2735, 'n_words': 678}