From d60c23560bd0464b2f8862f43711781eb45eac49 Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Fri, 25 Dec 2015 20:41:18 -0600 Subject: Add tests for readInt --- .../trilead/ssh2/crypto/SimpleDERReaderTest.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java b/sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java index 3eaec20..cabadce 100644 --- a/sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java +++ b/sshlib/src/test/java/com/trilead/ssh2/crypto/SimpleDERReaderTest.java @@ -3,6 +3,7 @@ package com.trilead.ssh2.crypto; import org.junit.Test; import java.io.IOException; +import java.math.BigInteger; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; @@ -64,4 +65,51 @@ public class SimpleDERReaderTest { SimpleDERReader reader = new SimpleDERReader(vector); assertEquals(9, reader.readLength()); } + + @Test + public void readInt_MaxInt() throws Exception { + byte[] vector = new byte[] { + (byte) 0x02, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + }; + SimpleDERReader reader = new SimpleDERReader(vector); + assertEquals(BigInteger.valueOf(0xFFFFFFFF), reader.readInt()); + } + + @Test + public void readInt_NotReallyInteger() throws Exception { + byte[] vector = new byte[] { + (byte) 0x01, (byte) 0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + }; + SimpleDERReader reader = new SimpleDERReader(vector); + try { + reader.readInt(); + } catch (IOException expected) { + assertThat(expected.getMessage(), containsString("Expected DER Integer")); + } + } + + @Test + public void readInt_InvalidLength() throws Exception { + byte[] vector = new byte[] { + (byte) 0x02, (byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + }; + SimpleDERReader reader = new SimpleDERReader(vector); + try { + reader.readInt(); + } catch (IOException expected) { + assertThat(expected.getMessage(), containsString("Illegal len")); + } + } + + @Test + public void readInt_ShortArray() throws Exception { + byte[] vector = new byte[] { + (byte) 0x02, (byte) 0x02, (byte) 0xFF + }; + SimpleDERReader reader = new SimpleDERReader(vector); + try { + reader.readInt(); + } catch (IOException expected) { + } + } } \ No newline at end of file -- cgit v1.2.3