package org.apache.commons.net.ssh.util;

import static org.junit.Assert.*;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;

import org.apache.commons.net.ssh.util.Buffer.BufferException;
import org.apache.commons.net.ssh.util.Constants.Message;
import org.junit.Before;
import org.junit.Test;

/**
 * Sample unit test for testing {@link Buffer} functionality
 * 
 * @author rorywinston
 * 
 */
public class BufferTest
{
    private Buffer posBuf;
    private Buffer buf;
    
    @Before()
    public void setUp() throws UnsupportedEncodingException, GeneralSecurityException
    {
        // for position test
        byte[] data = "Hello".getBytes("UTF-8");
        posBuf = new Buffer(data);
    }
    
    @Test
    public void testCommand()
    {
        // message identifier
        assertEquals(buf.putCommand(Message.IGNORE).getCommand(), Message.IGNORE);
    }
    
    @Test
    public void testDataTypes()
    {
        // bool
        assertEquals(buf.putBoolean(true).getBoolean(), true);
        
        // byte
        assertEquals(buf.putByte((byte) 10).getByte(), (byte) 10);
        
        // byte array
        assertArrayEquals(buf.putBytes("some string".getBytes()).getBytes(), "some string".getBytes());
        
        // mpint
        BigInteger bi = new BigInteger("1111111111111111111111111111111");
        assertEquals(buf.putMPInt(bi).getMPInt(), bi);
        
        // string
        assertEquals(buf.putString("some string").getString(), "some string");
        
        // uint32
        assertEquals(buf.putUInt(0xffffffffL).getUInt(), 0xffffffffL);
    }
    
    @Test
    public void testPassword()
    {
        char[] pass = "lolcatz".toCharArray();
        // test if put correctly as a string
        assertEquals(new Buffer().putPassword(pass).getString(), "lolcatz");
        // test that char[] was blanked out
        assertArrayEquals(pass, "       ".toCharArray());
    }
    
    @Test
    public void testPosition() throws UnsupportedEncodingException
    {
        assertEquals(5, posBuf.wpos());
        assertEquals(0, posBuf.rpos());
        assertEquals(5, posBuf.available());
        // read some bytes
        byte b = posBuf.getByte();
        assertEquals(b, (byte) 'H');
        assertEquals(1, posBuf.rpos());
        assertEquals(4, posBuf.available());
    }
    
    @Test
    public void testPublickey()
    {
        // TODO stub
    }
    
    @Test
    public void testSignature()
    {
        // TODO stub
    }
    
    @Test(expected = BufferException.class)
    public void testUnderflow()
    {
        // exhaust the buffer
        for (int i = 0; i < 5; ++i)
            posBuf.getByte();
        // underflow
        posBuf.getByte();
    }
    
}

@@ -534,17 +534,22 @@
  Buffer 
-b
+handyB
 uf;%0A    
@@ -768,16 +768,49 @@
 (data);%0A
+        handyBuf = new Buffer();%0A
     %7D%0A  
@@ -905,25 +905,30 @@
 ssertEquals(
-b
+handyB
 uf.putComman
@@ -1068,25 +1068,30 @@
 ssertEquals(
-b
+handyB
 uf.putBoolea
@@ -1161,25 +1161,30 @@
 ssertEquals(
-b
+handyB
 uf.putByte((
@@ -1269,25 +1269,30 @@
 ArrayEquals(
-b
+handyB
 uf.putBytes(
@@ -1474,25 +1474,30 @@
 ssertEquals(
-b
+handyB
 uf.putMPInt(
@@ -1561,25 +1561,30 @@
 ssertEquals(
-b
+handyB
 uf.putString
@@ -1680,16 +1680,20 @@
 als(
-b
+handyB
 uf.put
-U
 Int(
@@ -1708,17 +1708,16 @@
 ffL).get
-U
 Int(), 0

fix the BufferTest..