2016-02-05 02:23:11 -08:00
package common
2016-02-14 23:10:37 -08:00
import (
2016-03-28 22:16:52 -07:00
"github.com/stretchr/testify/assert"
2016-02-14 23:10:37 -08:00
"testing"
)
2016-02-05 02:23:11 -08:00
func TestCertificateTypeIsFirstByte ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
2016-02-05 02:23:11 -08:00
bytes := [ ] byte { 0x03 , 0x00 , 0x00 }
certificate := Certificate ( bytes )
2016-02-13 21:00:29 -08:00
cert_type , err := certificate . Type ( )
2016-03-28 22:16:52 -07:00
assert . Equal ( cert_type , 3 , "certificate.Type() should be the first bytes in a certificate" )
assert . Nil ( err )
2016-02-05 02:23:11 -08:00
}
func TestCertificateLengthCorrect ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
2016-02-05 02:23:11 -08:00
bytes := [ ] byte { 0x03 , 0x00 , 0x02 , 0xff , 0xff }
certificate := Certificate ( bytes )
cert_len , err := certificate . Length ( )
2016-03-28 22:16:52 -07:00
assert . Equal ( cert_len , 2 , "certificate.Length() should return integer from second two bytes" )
assert . Nil ( err )
2016-02-05 02:23:11 -08:00
}
func TestCertificateLengthErrWhenTooShort ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
bytes := [ ] byte { 0x03 , 0x01 }
2016-02-05 02:23:11 -08:00
certificate := Certificate ( bytes )
cert_len , err := certificate . Length ( )
2016-03-28 22:16:52 -07:00
assert . Equal ( cert_len , 0 , "certificate.Length() did not return zero length for missing length data" )
if assert . NotNil ( err ) {
assert . Equal ( "error parsing certificate length: certificate is too short" , err . Error ( ) , "correct error message should be returned" )
2016-02-05 02:23:11 -08:00
}
}
func TestCertificateLengthErrWhenDataTooShort ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
2016-02-05 02:23:11 -08:00
bytes := [ ] byte { 0x03 , 0x00 , 0x02 , 0xff }
certificate := Certificate ( bytes )
cert_len , err := certificate . Length ( )
2016-03-28 22:16:52 -07:00
assert . Equal ( cert_len , 2 , "certificate.Length() did not return indicated length when data was actually missing" )
if assert . NotNil ( err ) {
assert . Equal ( "certificate parsing warning: certificate data is shorter than specified by length" , err . Error ( ) , "correct error message should be returned" )
2016-02-05 02:23:11 -08:00
}
}
func TestCertificateDataWhenCorrectSize ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
bytes := [ ] byte { 0x03 , 0x00 , 0x01 , 0xaa }
2016-02-05 02:23:11 -08:00
certificate := Certificate ( bytes )
cert_data , err := certificate . Data ( )
2016-03-28 22:16:52 -07:00
assert . Nil ( err , "certificate.Data() returned error with valid data" )
2016-02-05 02:23:11 -08:00
cert_len := len ( cert_data )
2016-03-28 22:16:52 -07:00
assert . Equal ( cert_len , 1 , "certificate.Length() did not return indicated length when data was valid" )
assert . Equal ( 170 , int ( cert_data [ 0 ] ) , "certificate.Data() returned incorrect data" )
2016-02-05 02:23:11 -08:00
}
func TestCertificateDataWhenTooLong ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
2016-02-05 02:23:11 -08:00
bytes := [ ] byte { 0x03 , 0x00 , 0x02 , 0xff , 0xff , 0xaa , 0xaa }
certificate := Certificate ( bytes )
cert_data , err := certificate . Data ( )
2016-03-28 22:16:52 -07:00
if assert . NotNil ( err ) {
assert . Equal ( "certificate parsing warning: certificate contains data beyond length" , err . Error ( ) , "correct error message should be returned" )
2016-02-05 02:23:11 -08:00
}
cert_len := len ( cert_data )
2016-03-28 22:16:52 -07:00
assert . Equal ( cert_len , 2 , "certificate.Length() did not return indicated length when data was too long" )
2016-02-05 02:23:11 -08:00
if cert_data [ 0 ] != 0xff || cert_data [ 1 ] != 0xff {
2016-03-28 22:16:52 -07:00
t . Fatal ( "certificate.Data() returned incorrect data when data was too long" )
2016-02-05 02:23:11 -08:00
}
}
func TestCertificateDataWhenTooShort ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
2016-02-05 02:23:11 -08:00
bytes := [ ] byte { 0x03 , 0x00 , 0x02 , 0xff }
certificate := Certificate ( bytes )
cert_data , err := certificate . Data ( )
2016-03-28 22:16:52 -07:00
if assert . NotNil ( err ) {
assert . Equal ( "certificate parsing warning: certificate data is shorter than specified by length" , err . Error ( ) , "correct error message should be returned" )
2016-02-05 02:23:11 -08:00
}
cert_len := len ( cert_data )
2016-03-28 22:16:52 -07:00
assert . Equal ( cert_len , 1 , "certificate.Data() did not return correct amount of data when data too short" )
assert . Equal ( 255 , int ( cert_data [ 0 ] ) , "certificate.Data() did not return correct data values when data was too short" )
2016-02-05 02:23:11 -08:00
}
func TestReadCertificateWithCorrectData ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
2016-02-05 02:23:11 -08:00
bytes := [ ] byte { 0x00 , 0x00 , 0x02 , 0xff , 0xff }
cert , remainder , err := ReadCertificate ( bytes )
2016-03-28 22:16:52 -07:00
assert . Equal ( len ( cert ) , 5 , "ReadCertificate() did not return correct amount of data for valid certificate" )
assert . Equal ( len ( remainder ) , 0 , "ReadCertificate() did not return a zero length remainder on a valid certificate" )
assert . Nil ( err , "ReadCertificate() should not return an error with valid data" )
2016-02-05 02:23:11 -08:00
}
func TestReadCertificateWithDataTooShort ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
2016-02-05 02:23:11 -08:00
bytes := [ ] byte { 0x00 , 0x00 , 0x02 , 0xff }
cert , remainder , err := ReadCertificate ( bytes )
2016-03-28 22:16:52 -07:00
assert . Equal ( len ( cert ) , 4 , "ReadCertificate() did not return correct amount of data for certificate with missing data" )
assert . Equal ( len ( remainder ) , 0 , "ReadCertificate() did not return a zero length remainder on certificate with missing data" )
if assert . NotNil ( err ) {
assert . Equal ( "certificate parsing warning: certificate data is shorter than specified by length" , err . Error ( ) , "correct error message should be returned" )
2016-02-05 02:23:11 -08:00
}
}
func TestReadCertificateWithRemainder ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
bytes := [ ] byte { 0x00 , 0x00 , 0x02 , 0xff , 0xff , 0x01 }
2016-02-05 02:23:11 -08:00
cert , remainder , err := ReadCertificate ( bytes )
2016-03-28 22:16:52 -07:00
assert . Equal ( len ( cert ) , 5 , "ReadCertificate() did not return correct amount of data for certificate with extra data" )
assert . Equal ( len ( remainder ) , 1 , "ReadCertificate() returned incorrect length remainder on certificate with extra data" )
assert . Equal ( 1 , int ( remainder [ 0 ] ) , "ReadCertificate() did not return correct remainder value" )
assert . Nil ( err )
2016-02-05 02:23:11 -08:00
}
func TestReadCertificateWithInvalidLength ( t * testing . T ) {
2016-03-28 22:16:52 -07:00
assert := assert . New ( t )
2016-02-05 02:23:11 -08:00
bytes := [ ] byte { 0x00 , 0x00 }
cert , remainder , err := ReadCertificate ( bytes )
2016-03-28 22:16:52 -07:00
assert . Equal ( len ( cert ) , 2 , "ReadCertificate() should populate the certificate with the provided data even when invalid" )
assert . Equal ( len ( remainder ) , 0 , "ReadCertificate() returned non-zero length remainder on invalid certificate" )
if assert . NotNil ( err ) {
assert . Equal ( "error parsing certificate length: certificate is too short" , err . Error ( ) , "correct error message should be returned" )
2016-02-05 02:23:11 -08:00
}
}