start fixing up the tests on the new struct implementations

This commit is contained in:
idk
2021-04-24 22:48:48 -04:00
parent ee7d8a0d63
commit 77f1c6dd0a
2 changed files with 67 additions and 52 deletions

View File

@ -79,11 +79,7 @@ func (certificate Certificate) Cert() []byte {
// and an error if the certificate is shorter than the minimum certificate size.
//
func (certificate Certificate) Type() (cert_type int, err error) {
_, err = certificate.Type()
if err != nil {
return
}
return
return certificate.CertType, nil
}
//
@ -92,7 +88,16 @@ func (certificate Certificate) Type() (cert_type int, err error) {
// match the provided data.
//
func (certificate Certificate) Length() (length int, err error) {
_, err = certificate.Type()
length = len(certificate.Cert())
if length < CERT_MIN_SIZE {
log.WithFields(log.Fields{
"at": "(Certificate) Length",
"certificate_bytes_length": length,
"reason": "data shorter than expected",
}).Warn("certificate format warning")
err = errors.New("certificate parsing warning: certificate data shorter than expected")
return
}
if err != nil {
return
}
@ -125,44 +130,51 @@ func (certificate Certificate) Data() (data []byte, err error) {
// Read a Certificate from a slice of bytes, returning any extra data on the end of the slice
// and any errors if a valid Certificate could not be read.
//
func ReadCertificate(data []byte) (Certificate, []byte, error) {
var remainder []byte
var err error
var certificate Certificate //= Certificate(data)
certificate.CertType = Integer([]byte{data[0]})
func ReadCertificate(data []byte) (certificate Certificate, remainder []byte, err error) {
certificate.CertType = Integer(data[0:1])
cert_len := len(data)
_, err = certificate.Type()
if err != nil {
return certificate, nil, err
}
length := Integer(data[1:CERT_MIN_SIZE])
inferred_len := length + CERT_MIN_SIZE
if inferred_len > cert_len {
if cert_len < CERT_MIN_SIZE {
log.WithFields(log.Fields{
"at": "(Certificate) Length",
"certificate_bytes_length": cert_len,
"certificate_length_field": length,
"expected_bytes_length": inferred_len,
"reason": "data shorter than specified",
"reason": "data shorter than expected",
}).Warn("certificate format warning")
err = errors.New("certificate parsing warning: certificate data is shorter than specified by length")
} else if cert_len > inferred_len {
log.WithFields(log.Fields{
"at": "(Certificate) Length",
"certificate_bytes_length": cert_len,
"certificate_length_field": length,
"expected_bytes_length": inferred_len,
"reason": "data longer than expected",
}).Warn("certificate format warning")
err = errors.New("certificate parsing warning: certificate contains data beyond length")
err = errors.New("certificate parsing warning: certificate data shorter than expected")
return
} else {
_, err = certificate.Type()
if err != nil {
return
}
length := Integer(data[1:CERT_MIN_SIZE])
inferred_len := length + CERT_MIN_SIZE
if inferred_len > cert_len {
log.WithFields(log.Fields{
"at": "(Certificate) Length",
"certificate_bytes_length": cert_len,
"certificate_length_field": length,
"expected_bytes_length": inferred_len,
"reason": "data shorter than specified",
}).Warn("certificate format warning")
err = errors.New("certificate parsing warning: certificate data is shorter than specified by length")
} else if cert_len > inferred_len {
log.WithFields(log.Fields{
"at": "(Certificate) Length",
"certificate_bytes_length": cert_len,
"certificate_length_field": length,
"expected_bytes_length": inferred_len,
"reason": "data longer than expected",
}).Warn("certificate format warning")
err = errors.New("certificate parsing warning: certificate contains data beyond length")
}
certificate.CertLen = length
certificate.CertBytes = data[CERT_MIN_SIZE:]
length, err = certificate.Length()
if err != nil && err.Error() == "certificate parsing warning: certificate contains data beyond length" {
certificate.CertBytes = data[:length+CERT_MIN_SIZE]
remainder = data[length+CERT_MIN_SIZE:]
err = nil
}
}
certificate.CertLen = length
certificate.CertBytes = data[CERT_MIN_SIZE:]
length, err = certificate.Length()
if err != nil && err.Error() == "certificate parsing warning: certificate contains data beyond length" {
certificate.CertBytes = data[:length+CERT_MIN_SIZE]
remainder = data[length+CERT_MIN_SIZE:]
err = nil
}
return certificate, remainder, err
return
}

View File

@ -9,7 +9,10 @@ func TestCertificateTypeIsFirstByte(t *testing.T) {
assert := assert.New(t)
bytes := []byte{0x03, 0x00, 0x00}
certificate := Certificate(bytes)
certificate, _, err := ReadCertificate(bytes)
if err != nil {
t.Log(err)
}
cert_type, err := certificate.Type()
assert.Equal(cert_type, 3, "certificate.Type() should be the first bytes in a certificate")
@ -20,7 +23,7 @@ func TestCertificateLengthCorrect(t *testing.T) {
assert := assert.New(t)
bytes := []byte{0x03, 0x00, 0x02, 0xff, 0xff}
certificate := Certificate(bytes)
certificate, _, err := ReadCertificate(bytes)
cert_len, err := certificate.Length()
assert.Equal(cert_len, 2, "certificate.Length() should return integer from second two bytes")
@ -31,7 +34,7 @@ func TestCertificateLengthErrWhenTooShort(t *testing.T) {
assert := assert.New(t)
bytes := []byte{0x03, 0x01}
certificate := Certificate(bytes)
certificate, _, err := ReadCertificate(bytes)
cert_len, err := certificate.Length()
assert.Equal(cert_len, 0, "certificate.Length() did not return zero length for missing length data")
@ -44,7 +47,7 @@ func TestCertificateLengthErrWhenDataTooShort(t *testing.T) {
assert := assert.New(t)
bytes := []byte{0x03, 0x00, 0x02, 0xff}
certificate := Certificate(bytes)
certificate, _, err := ReadCertificate(bytes)
cert_len, err := certificate.Length()
assert.Equal(cert_len, 2, "certificate.Length() did not return indicated length when data was actually missing")
@ -57,7 +60,7 @@ func TestCertificateDataWhenCorrectSize(t *testing.T) {
assert := assert.New(t)
bytes := []byte{0x03, 0x00, 0x01, 0xaa}
certificate := Certificate(bytes)
certificate, _, err := ReadCertificate(bytes)
cert_data, err := certificate.Data()
assert.Nil(err, "certificate.Data() returned error with valid data")
@ -70,8 +73,8 @@ 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()
certificate, _, err := ReadCertificate(bytes)
cert_data := certificate.Cert() //, err := certificate.Data()
if assert.NotNil(err) {
assert.Equal("certificate parsing warning: certificate contains data beyond length", err.Error(), "correct error message should be returned")
@ -87,7 +90,7 @@ func TestCertificateDataWhenTooShort(t *testing.T) {
assert := assert.New(t)
bytes := []byte{0x03, 0x00, 0x02, 0xff}
certificate := Certificate(bytes)
certificate, _, err := ReadCertificate(bytes)
cert_data, err := certificate.Data()
if assert.NotNil(err) {
@ -104,7 +107,7 @@ func TestReadCertificateWithCorrectData(t *testing.T) {
bytes := []byte{0x00, 0x00, 0x02, 0xff, 0xff}
cert, remainder, err := ReadCertificate(bytes)
assert.Equal(len(cert), 5, "ReadCertificate() did not return correct amount of data for valid certificate")
assert.Equal(len(cert.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")
}
@ -115,7 +118,7 @@ func TestReadCertificateWithDataTooShort(t *testing.T) {
bytes := []byte{0x00, 0x00, 0x02, 0xff}
cert, remainder, err := ReadCertificate(bytes)
assert.Equal(len(cert), 4, "ReadCertificate() did not return correct amount of data for certificate with missing data")
assert.Equal(len(cert.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")
@ -128,7 +131,7 @@ func TestReadCertificateWithRemainder(t *testing.T) {
bytes := []byte{0x00, 0x00, 0x02, 0xff, 0xff, 0x01}
cert, remainder, err := ReadCertificate(bytes)
assert.Equal(len(cert), 5, "ReadCertificate() did not return correct amount of data for certificate with extra data")
assert.Equal(len(cert.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)
@ -140,7 +143,7 @@ func TestReadCertificateWithInvalidLength(t *testing.T) {
bytes := []byte{0x00, 0x00}
cert, remainder, err := ReadCertificate(bytes)
assert.Equal(len(cert), 2, "ReadCertificate() should populate the certificate with the provided data even when invalid")
assert.Equal(len(cert.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")