<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.directory.shared.ldap.codec.search.controls;


import java.nio.ByteBuffer;
import java.util.Arrays;

import org.apache.directory.shared.asn1.ber.Asn1Decoder;
import org.apache.directory.shared.asn1.codec.DecoderException;
import org.apache.directory.shared.ldap.util.StringTools;
import org.junit.Test;

import junit.framework.Assert;
import junit.framework.TestCase;


/**
 * Test the PagedSearchControlTest codec
 * 
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 */
public class PagedSearchControlTest extends TestCase
{
    /**
     * Test encoding of a PagedSearchControl.
     */
    @Test
    public void testEncodePagedSearchControl() throws Exception
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x0b );
        bb.put( new byte[]
            { 
              0x30, 0x09,           // realSearchControlValue ::= SEQUENCE {
                0x02, 0x01, 0x20,   // size INTEGER,
                0x04, 0x04, 't', 'e', 's', 't'   // cookie OCTET STRING,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
        }
        catch ( DecoderException de )
        {
            de.printStackTrace();
            Assert.fail( de.getMessage() );
        }

        PagedSearchControlCodec pagedSearch = container.getPagedSearchControl();
        assertEquals( 32, pagedSearch.getSize() );
        assertTrue( Arrays.equals( StringTools.getBytesUtf8( "test" ), 
            pagedSearch.getCookie() ) );
            
        String expected = StringTools.dumpBytes( bb.array() );
        bb.flip();

        PagedSearchControlCodec ctrl = new PagedSearchControlCodec();
        ctrl.setSize( 32 );
        ctrl.setCookie( StringTools.getBytesUtf8( "test" ) );
        bb = ctrl.encode( null );
        String decoded = StringTools.dumpBytes( bb.array() );
        assertEquals( expected, decoded );
    }
    
    
    /**
     * Test the decoding of a PagedSearchControl with no cookie
     */
    @Test
    public void testDecodePagedSearchRequestNoCookie()
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x05 );
        bb.put( new byte[]
            { 
            0x30, 0x03,         // realSearchControlValue ::= SEQUENCE {
              0x02, 0x01, 0x20  // size INTEGER,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {
            assertTrue( true );
        }
    }
    
    
    /**
     * Test the decoding of a PagedSearchControl with no size
     */
    @Test
    public void testDecodePagedSearchRequestNoSize()
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x08 );
        bb.put( new byte[]
            { 
            0x30, 0x06,         // realSearchControlValue ::= SEQUENCE {
              0x04, 0x04, 't', 'e', 's', 't'  // cookie OCTET STRING,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {
            assertTrue( true );
        }
    }
    
    
    /**
     * Test the decoding of a PagedSearchControl with no size  and no cookie
     */
    @Test
    public void testDecodePagedSearchRequestNoSizeNoCookie()
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x02 );
        bb.put( new byte[]
            { 
            0x30, 0x00,         // realSearchControlValue ::= SEQUENCE 
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {
            assertTrue( true );
        }
    }
    
    
    /**
     * Test encoding of a PagedSearchControl with a negative size
     */
    @Test
    public void testEncodePagedSearchControlNegativeSize() throws Exception
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x0b );
        bb.put( new byte[]
            { 
              0x30, 0x09,           // realSearchControlValue ::= SEQUENCE {
                0x02, 0x01, (byte)0xFF,   // size INTEGER,
                0x04, 0x04, 't', 'e', 's', 't'   // cookie OCTET STRING,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {
            assertTrue( true );
        }
    }
    
    
    /**
     * Test encoding of a PagedSearchControl with a empty size
     */
    @Test
    public void testEncodePagedSearchControlEmptySize() throws Exception
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x0a );
        bb.put( new byte[]
            { 
              0x30, 0x08,           // realSearchControlValue ::= SEQUENCE {
                0x02, 0x00,         // size INTEGER,
                0x04, 0x04, 't', 'e', 's', 't'   // cookie OCTET STRING,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {<commit_msg>Fixed a failing test in shared, due to some modification in the pagedSearch control : as we are now accepting negative values for the page sizeLimit, the test should check that.<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.directory.shared.ldap.codec.search.controls;


import java.nio.ByteBuffer;
import java.util.Arrays;

import org.apache.directory.shared.asn1.ber.Asn1Decoder;
import org.apache.directory.shared.asn1.codec.DecoderException;
import org.apache.directory.shared.ldap.util.StringTools;
import org.junit.Test;

import junit.framework.Assert;
import junit.framework.TestCase;


/**
 * Test the PagedSearchControlTest codec
 * 
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 */
public class PagedSearchControlTest extends TestCase
{
    /**
     * Test encoding of a PagedSearchControl.
     */
    @Test
    public void testEncodePagedSearchControl() throws Exception
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x0b );
        bb.put( new byte[]
            { 
              0x30, 0x09,           // realSearchControlValue ::= SEQUENCE {
                0x02, 0x01, 0x20,   // size INTEGER,
                0x04, 0x04, 't', 'e', 's', 't'   // cookie OCTET STRING,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
        }
        catch ( DecoderException de )
        {
            de.printStackTrace();
            Assert.fail( de.getMessage() );
        }

        PagedSearchControlCodec pagedSearch = container.getPagedSearchControl();
        assertEquals( 32, pagedSearch.getSize() );
        assertTrue( Arrays.equals( StringTools.getBytesUtf8( "test" ), 
            pagedSearch.getCookie() ) );
            
        String expected = StringTools.dumpBytes( bb.array() );
        bb.flip();

        PagedSearchControlCodec ctrl = new PagedSearchControlCodec();
        ctrl.setSize( 32 );
        ctrl.setCookie( StringTools.getBytesUtf8( "test" ) );
        bb = ctrl.encode( null );
        String decoded = StringTools.dumpBytes( bb.array() );
        assertEquals( expected, decoded );
    }
    
    
    /**
     * Test the decoding of a PagedSearchControl with no cookie
     */
    @Test
    public void testDecodePagedSearchRequestNoCookie()
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x05 );
        bb.put( new byte[]
            { 
            0x30, 0x03,         // realSearchControlValue ::= SEQUENCE {
              0x02, 0x01, 0x20  // size INTEGER,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {
            assertTrue( true );
        }
    }
    
    
    /**
     * Test the decoding of a PagedSearchControl with no size
     */
    @Test
    public void testDecodePagedSearchRequestNoSize()
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x08 );
        bb.put( new byte[]
            { 
            0x30, 0x06,         // realSearchControlValue ::= SEQUENCE {
              0x04, 0x04, 't', 'e', 's', 't'  // cookie OCTET STRING,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {
            assertTrue( true );
        }
    }
    
    
    /**
     * Test the decoding of a PagedSearchControl with no size  and no cookie
     */
    @Test
    public void testDecodePagedSearchRequestNoSizeNoCookie()
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x02 );
        bb.put( new byte[]
            { 
            0x30, 0x00,         // realSearchControlValue ::= SEQUENCE 
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {
            assertTrue( true );
        }
    }
    
    
    /**
     * Test encoding of a PagedSearchControl with a negative size
     */
    @Test
    public void testEncodePagedSearchControlNegativeSize() throws Exception
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x0b );
        bb.put( new byte[]
            { 
              0x30, 0x09,           // realSearchControlValue ::= SEQUENCE {
                0x02, 0x01, (byte)0xFF,   // size INTEGER,
                0x04, 0x04, 't', 'e', 's', 't'   // cookie OCTET STRING,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
        }
        catch ( DecoderException de )
        {
            de.printStackTrace();
            Assert.fail( de.getMessage() );
        }

        PagedSearchControlCodec pagedSearch = container.getPagedSearchControl();
        assertEquals( Integer.MAX_VALUE, pagedSearch.getSize() );
        assertTrue( Arrays.equals( StringTools.getBytesUtf8( "test" ), 
            pagedSearch.getCookie() ) );
            
        String expected = StringTools.dumpBytes( bb.array() );
        bb.flip();

        PagedSearchControlCodec ctrl = new PagedSearchControlCodec();
        ctrl.setSize( -1 );
        ctrl.setCookie( StringTools.getBytesUtf8( "test" ) );
        bb = ctrl.encode( null );
        String decoded = StringTools.dumpBytes( bb.array() );
        assertEquals( expected, decoded );
    }
    
    
    /**
     * Test encoding of a PagedSearchControl with a empty size
     */
    @Test
    public void testEncodePagedSearchControlEmptySize() throws Exception
    {
        Asn1Decoder decoder = new PagedSearchControlDecoder();
        ByteBuffer bb = ByteBuffer.allocate( 0x0a );
        bb.put( new byte[]
            { 
              0x30, 0x08,           // realSearchControlValue ::= SEQUENCE {
                0x02, 0x00,         // size INTEGER,
                0x04, 0x04, 't', 'e', 's', 't'   // cookie OCTET STRING,
            } );
        bb.flip();

        PagedSearchControlContainer container = new PagedSearchControlContainer();
        
        try
        {
            decoder.decode( bb, container );
            fail();
        }
        catch ( DecoderException de )
        {
{'fraction_non_alphanumeric': 0.09402412280701754, 'fraction_numerical': 0.01733826754385965, 'mean_word_length': 1.8817140600315956, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6992918', 'n_tokens_mistral': 4256, 'n_tokens_neox': 3957, 'n_words': 1056}