From fc9b661ce6a87aad0e7ed8c6469f7ec5ad33d041 Mon Sep 17 00:00:00 2001 From: Hayden Parker Date: Mon, 28 Mar 2016 22:16:52 -0700 Subject: [PATCH] improving tests with testify, unexporting sorting types --- lib/common/certificate_test.go | 164 +++++++++++++---------------- lib/common/date_test.go | 8 +- lib/common/integer_test.go | 30 +++--- lib/common/key_certificate_test.go | 110 +++++++++---------- lib/common/mapping.go | 20 ++-- 5 files changed, 161 insertions(+), 171 deletions(-) diff --git a/lib/common/certificate_test.go b/lib/common/certificate_test.go index b22827e..e5592e4 100644 --- a/lib/common/certificate_test.go +++ b/lib/common/certificate_test.go @@ -1,166 +1,148 @@ package common import ( + "github.com/stretchr/testify/assert" "testing" ) func TestCertificateTypeIsFirstByte(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x03, 0x00, 0x00} certificate := Certificate(bytes) cert_type, err := certificate.Type() - if cert_type != 3 { - t.Fatal("certificate.Type() is not first byte") - } - if err != nil { - t.Fatal("certificate.Type returned error on valid data:", err) - } + + assert.Equal(cert_type, 3, "certificate.Type() should be the first bytes in a certificate") + assert.Nil(err) } func TestCertificateLengthCorrect(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x03, 0x00, 0x02, 0xff, 0xff} certificate := Certificate(bytes) cert_len, err := certificate.Length() - if cert_len != 2 { - t.Fatal("certificate.Length() is not correct:", cert_len) - } - if err != nil { - t.Fatal("certificate.Length() returned err", err) - } + + assert.Equal(cert_len, 2, "certificate.Length() should return integer from second two bytes") + assert.Nil(err) } func TestCertificateLengthErrWhenTooShort(t *testing.T) { - bytes := []byte{0x03, 0x00} + assert := assert.New(t) + + bytes := []byte{0x03, 0x01} certificate := Certificate(bytes) cert_len, err := certificate.Length() - if cert_len != 0 { - t.Fatal("certificate.Length() is not correct:", cert_len) - } - if err == nil || err.Error() != "error parsing certificate length: certificate is too short" { - t.Fatal("certificate.Length() did not return correct err:", err) + + 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") } } func TestCertificateLengthErrWhenDataTooShort(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x03, 0x00, 0x02, 0xff} certificate := Certificate(bytes) cert_len, err := certificate.Length() - if cert_len != 2 { - t.Fatal("certificate.Length() is not correct:", cert_len) - } - if err == nil || err.Error() != "certificate parsing warning: certificate data is shorter than specified by length" { - t.Fatal("certificate.Length() did not return correct err:", err) + + 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") } } func TestCertificateDataWhenCorrectSize(t *testing.T) { - bytes := []byte{0x03, 0x00, 0x02, 0xff, 0xff} + assert := assert.New(t) + + bytes := []byte{0x03, 0x00, 0x01, 0xaa} certificate := Certificate(bytes) cert_data, err := certificate.Data() - if err != nil { - t.Fatal("certificate.Data() returned error", err) - } + + assert.Nil(err, "certificate.Data() returned error with valid data") cert_len := len(cert_data) - if cert_len != 2 { - t.Fatal("certificate.Data() did not return correct length:", cert_len) - } - if cert_data[0] != 0xff || cert_data[1] != 0xff { - t.Fatal("certificate.Data() returned incorrect data") - } + 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") } func TestCertificateDataWhenTooLong(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x03, 0x00, 0x02, 0xff, 0xff, 0xaa, 0xaa} certificate := Certificate(bytes) cert_data, err := certificate.Data() - if err == nil || err.Error() != "certificate parsing warning: certificate contains data beyond length" { - t.Fatal("certificate.Data() returned wrong error:", err) + + if assert.NotNil(err) { + assert.Equal("certificate parsing warning: certificate contains data beyond length", err.Error(), "correct error message should be returned") } cert_len := len(cert_data) - if cert_len != 2 { - t.Fatal("certificate.Data() did not return correct length:", cert_len) - } + assert.Equal(cert_len, 2, "certificate.Length() did not return indicated length when data was too long") if cert_data[0] != 0xff || cert_data[1] != 0xff { - t.Fatal("certificate.Data() returned incorrect data") + t.Fatal("certificate.Data() returned incorrect data when data was too long") } } func TestCertificateDataWhenTooShort(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x03, 0x00, 0x02, 0xff} certificate := Certificate(bytes) cert_data, err := certificate.Data() - if err == nil || err.Error() != "certificate parsing warning: certificate data is shorter than specified by length" { - t.Fatal("certificate.Data() did not return correct error:", err) + + 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") } cert_len := len(cert_data) - if cert_len != 1 { - t.Fatal("certificate.Data() did not return correct length when too short:", cert_len) - } - if cert_data[0] != 0xff { - t.Fatal("certificate.Data() returned incorrect data") - } - + 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") } func TestReadCertificateWithCorrectData(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x00, 0x00, 0x02, 0xff, 0xff} cert, remainder, err := ReadCertificate(bytes) - cert_len := len(cert) - if cert_len != 5 { - t.Fatal("ReadCertificate() did not return correct certificate length:", cert_len) - } - if len(remainder) != 0 { - t.Fatal("ReadCertificate() returned a remainder incorrectly:", len(remainder)) - } - if err != nil { - t.Fatal("ReadCertificate returned error:", err) - } + + 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") } func TestReadCertificateWithDataTooShort(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x00, 0x00, 0x02, 0xff} cert, remainder, err := ReadCertificate(bytes) - cert_len := len(cert) - if cert_len != 4 { - t.Fatal("ReadCertificate() did not return correct certificate length:", cert_len) - } - if len(remainder) != 0 { - t.Fatal("ReadCertificate() returned a remainder incorrectly when data too short:", len(remainder)) - } - if err == nil || err.Error() != "certificate parsing warning: certificate data is shorter than specified by length" { - t.Fatal("ReadCertificate returned incorrect error:", err) + + 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") } } func TestReadCertificateWithRemainder(t *testing.T) { - bytes := []byte{0x00, 0x00, 0x02, 0xff, 0xff, 0x00} + assert := assert.New(t) + + bytes := []byte{0x00, 0x00, 0x02, 0xff, 0xff, 0x01} cert, remainder, err := ReadCertificate(bytes) - cert_len := len(cert) - if cert_len != 5 { - t.Fatal("ReadCertificate() did not return correct certificate length:", cert_len) - } - if len(remainder) != 1 { - t.Fatal("ReadCertificate() returned a remainder incorrectly:", len(remainder)) - } - if remainder[0] != 0x00 { - t.Fatal("ReadCertificate() did not return correct remainder value") - } - if err != nil { - t.Fatal("ReadCertificate returned error:", err) - } + + 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) } func TestReadCertificateWithInvalidLength(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x00, 0x00} cert, remainder, err := ReadCertificate(bytes) - cert_len := len(cert) - if cert_len != 2 { - t.Fatal("ReadCertificate() did not populate certificate even though data invalid", cert_len) - } - remainder_len := len(remainder) - if remainder_len != 0 { - t.Fatal("ReadCertificate() did not return 0 length remainder with invalid length:", remainder_len) - } - if err == nil || err.Error() != "error parsing certificate length: certificate is too short" { - t.Fatal("ReadCertificate() returned an incorrect error with invalid length:", err) + + 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") } } diff --git a/lib/common/date_test.go b/lib/common/date_test.go index 7e0a8e0..bb13349 100644 --- a/lib/common/date_test.go +++ b/lib/common/date_test.go @@ -1,13 +1,15 @@ package common import ( + "github.com/stretchr/testify/assert" "testing" ) func TestTimeFromMiliseconds(t *testing.T) { + assert := assert.New(t) + next_day := Date{0x00, 0x00, 0x00, 0x00, 0x05, 0x26, 0x5c, 0x00} go_time := next_day.Time() - if go_time.Unix() != 86400 { - t.Fatal("Date.Time() did not parse time in milliseconds") - } + + assert.Equal(go_time.Unix(), int64(86400), "Date.Time() did not parse time in milliseconds") } diff --git a/lib/common/integer_test.go b/lib/common/integer_test.go index 2bd825a..da265f2 100644 --- a/lib/common/integer_test.go +++ b/lib/common/integer_test.go @@ -1,27 +1,31 @@ package common import ( + "github.com/stretchr/testify/assert" "testing" ) func TestIntegerBigEndian(t *testing.T) { + assert := assert.New(t) + bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} - i := Integer(bytes) - if i != 1 { - t.Fatal("Integer() not big endian") - } + integer := Integer(bytes) + + assert.Equal(integer, 1, "Integer() did not parse bytes big endian") } -func TestWorksWith1Byte(t *testing.T) { - i := Integer([]byte{0x01}) - if i != 1 { - t.Fatal("Integer() does not work with 1 byte") - } +func TestWorksWithOneByte(t *testing.T) { + assert := assert.New(t) + + integer := Integer([]byte{0x01}) + + assert.Equal(integer, 1, "Integer() did not correctly parse single byte slice") } func TestIsZeroWithNoData(t *testing.T) { - i := Integer([]byte{}) - if i != 0 { - t.Fatal("Integer() does not work with 0 bytes") - } + assert := assert.New(t) + + integer := Integer([]byte{}) + + assert.Equal(integer, 0, "Integer() did not correctly parse zero length byte slice") } diff --git a/lib/common/key_certificate_test.go b/lib/common/key_certificate_test.go index cb051ee..e2599b5 100644 --- a/lib/common/key_certificate_test.go +++ b/lib/common/key_certificate_test.go @@ -1,125 +1,127 @@ package common import ( + "github.com/stretchr/testify/assert" "testing" ) func TestSingingPublicKeyTypeReturnsCorrectInteger(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x03, 0x00, 0x00}) pk_type, err := key_cert.SigningPublicKeyType() - if err != nil { - t.Fatal("err reading SigningPublicKey type on valid data:", err) - } - if pk_type != KEYCERT_SIGN_P521 { - t.Fatal("SigningPublicKeyType did not return correct type:", pk_type) - } + + assert.Nil(err, "SigningPublicKeyType() returned error with valid data") + assert.Equal(pk_type, KEYCERT_SIGN_P521, "SigningPublicKeyType() did not return correct typec") } func TestSingingPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x01, 0x00}) _, err := key_cert.SigningPublicKeyType() - if err == nil || err.Error() != "error parsing key certificate: not enough data" { - t.Fatal("incorrect error reported by SigningPublicKeyType:", err) + + if assert.NotNil(err) { + assert.Equal("error parsing key certificate: not enough data", err.Error(), "correct error message should be returned") } } func TestPublicKeyTypeReturnsCorrectInteger(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03}) pk_type, err := key_cert.PublicKeyType() - if err != nil { - t.Fatal("err reading PublicKey type on valid data:", err) - } - if pk_type != KEYCERT_SIGN_P521 { - t.Fatal("PublicKeyType did not return correct type:", pk_type) - } + + assert.Nil(err, "PublicKey() returned error with valid data") + assert.Equal(pk_type, KEYCERT_SIGN_P521, "PublicKeyType() did not return correct typec") } func TestPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x02, 0x00, 0x00}) _, err := key_cert.PublicKeyType() - if err == nil || err.Error() != "error parsing key certificate: not enough data" { - t.Fatal("incorrect error reported by PublicKeyType:", err) + + if assert.NotNil(err) { + assert.Equal("error parsing key certificate: not enough data", err.Error(), "correct error message should be returned") } } func TestConstructPublicKeyReportsWhenDataTooSmall(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}) data := make([]byte, 255) _, err := key_cert.ConstructPublicKey(data) - if err == nil || err.Error() != "error constructing public key: not enough data" { - t.Fatal("ConstructPubliKey reported incorrect error with missing data:", err) + + if assert.NotNil(err) { + assert.Equal("error constructing public key: not enough data", err.Error(), "correct error message should be returned") } } func TestConstructPublicKeyReturnsCorrectDataWithElg(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}) data := make([]byte, 256) pk, err := key_cert.ConstructPublicKey(data) - if err != nil { - t.Fatal("ConstructPublicKey returned error with valid data:", err) - } - if pk.Len() != 256 { - t.Fatal("ConstructPublicKey did not return public key with correct length") - } + + assert.Nil(err, "ConstructPublicKey() returned error with valid data") + assert.Equal(pk.Len(), 256, "ConstructPublicKey() did not return public key with correct length") } func TestConstructSigningPublicKeyReportsWhenDataTooSmall(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}) data := make([]byte, 127) _, err := key_cert.ConstructSigningPublicKey(data) - if err == nil || err.Error() != "error constructing signing public key: not enough data" { - t.Fatal("ConstructSigngingPubliKey reported incorrect error with missing data:", err) + + if assert.NotNil(err) { + assert.Equal("error constructing signing public key: not enough data", err.Error(), "correct error message should be returned") } } func TestConstructSigningPublicKeyWithDSASHA1(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}) data := make([]byte, 128) spk, err := key_cert.ConstructSigningPublicKey(data) - if err != nil { - t.Fatal("ConstructSigningPublicKey with DSA SHA1 returned err on valid data:", err) - } - spk_len := spk.Len() - if spk_len != KEYCERT_SIGN_DSA_SHA1_SIZE { - t.Fatal("ConstructSigningPublicKeyWithDSASHA1 returned incorrect SigningPublicKey length:", spk_len) - } + + assert.Nil(err, "ConstructSigningPublicKey() with DSA SHA1 returned error with valid data") + assert.Equal(spk.Len(), KEYCERT_SIGN_DSA_SHA1_SIZE, "ConstructSigningPublicKey() with DSA SHA1 returned incorrect SigningPublicKey length") } func TestConstructSigningPublicKeyWithP256(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x01}) data := make([]byte, 128) spk, err := key_cert.ConstructSigningPublicKey(data) - if err != nil { - t.Fatal("ConstructSigningPublicKey with P256 returned err on valid data:", err) - } - spk_len := spk.Len() - if spk_len != KEYCERT_SIGN_P256_SIZE { - t.Fatal("ConstructSigningPublicKey with P256 returned incorrect SigningPublicKey length:", spk_len) - } + + assert.Nil(err, "ConstructSigningPublicKey() with P256 returned err on valid data") + assert.Equal(spk.Len(), KEYCERT_SIGN_P256_SIZE, "ConstructSigningPublicKey() with P256 returned incorrect SigningPublicKey length") } func TestConstructSigningPublicKeyWithP384(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x02, 0x00, 0x02}) data := make([]byte, 128) spk, err := key_cert.ConstructSigningPublicKey(data) - if err != nil { - t.Fatal("ConstructSigningPublicKey with P384 returned err on valid data:", err) - } - spk_len := spk.Len() - if spk_len != KEYCERT_SIGN_P384_SIZE { - t.Fatal("ConstructSigningPublicKey with P384 returned incorrect SigningPublicKey length:", spk_len) - } + + assert.Nil(err, "ConstructSigningPublicKey() with P384 returned err on valid data") + assert.Equal(spk.Len(), KEYCERT_SIGN_P384_SIZE, "ConstructSigningPublicKey() with P384 returned incorrect SigningPublicKey length") } func TestConstructSigningPublicKeyWithP521(t *testing.T) { + assert := assert.New(t) + key_cert := KeyCertificate([]byte{0x05, 0x00, 0x08, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00}) data := make([]byte, 128) spk, err := key_cert.ConstructSigningPublicKey(data) - if err != nil { - t.Fatal("ConstructSigningPublicKey with P521 returned err on valid data:", err) - } - spk_len := spk.Len() - if spk_len != KEYCERT_SIGN_P521_SIZE { - t.Fatal("ConstructSigningPublicKey with P521 returned incorrect SigningPublicKey length:", spk_len) - } + + assert.Nil(err, "ConstructSigningPublicKey() with P521 returned err on valid data") + assert.Equal(spk.Len(), KEYCERT_SIGN_P521_SIZE, "ConstructSigningPublicKey() with P521 returned incorrect SigningPublicKey length") } diff --git a/lib/common/mapping.go b/lib/common/mapping.go index 6816f03..9651858 100644 --- a/lib/common/mapping.go +++ b/lib/common/mapping.go @@ -177,21 +177,21 @@ func GoMapToMapping(gomap map[string]string) (mapping Mapping, err error) { return } -type ByValue MappingValues +type byValue MappingValues -func (set ByValue) Len() int { return len(set) } -func (set ByValue) Swap(i, j int) { set[i], set[j] = set[j], set[i] } -func (set ByValue) Less(i, j int) bool { +func (set byValue) Len() int { return len(set) } +func (set byValue) Swap(i, j int) { set[i], set[j] = set[j], set[i] } +func (set byValue) Less(i, j int) bool { data1, _ := set[i][1].Data() data2, _ := set[j][1].Data() return data1 < data2 } -type ByKey MappingValues +type byKey MappingValues -func (set ByKey) Len() int { return len(set) } -func (set ByKey) Swap(i, j int) { set[i], set[j] = set[j], set[i] } -func (set ByKey) Less(i, j int) bool { +func (set byKey) Len() int { return len(set) } +func (set byKey) Swap(i, j int) { set[i], set[j] = set[j], set[i] } +func (set byKey) Less(i, j int) bool { data1, _ := set[i][0].Data() data2, _ := set[j][0].Data() return data1 < data2 @@ -203,8 +203,8 @@ func (set ByKey) Less(i, j int) bool { // than by keys to ensure a consistent order. // func mappingOrder(values MappingValues) { - sort.Stable(ByValue(values)) - sort.Stable(ByKey(values)) + sort.Stable(byValue(values)) + sort.Stable(byKey(values)) } //