added cert tests and experimental function NewCertificateDeux

This commit is contained in:
Haris Khan
2024-12-04 00:55:53 -05:00
parent 76c3e5f293
commit ca1180c57f
3 changed files with 312 additions and 4 deletions

View File

@@ -1,5 +1,4 @@
test-cert-all: test-cert-type test-cert-length test-cert-data test-cert-read test-cert-length-correct test-cert-length-too-short test-cert-length-data-short test-cert-data-correct test-cert-data-too-long test-cert-data-too-short test-cert-read-correct test-cert-read-short test-cert-read-remainder test-cert-read-invalid
test-cert-all: test-cert-type test-cert-length test-cert-data test-cert-read test-cert-length-correct test-cert-length-too-short test-cert-length-data-short test-cert-data-correct test-cert-data-too-long test-cert-data-too-short test-cert-read-correct test-cert-read-short test-cert-read-remainder test-cert-read-invalid test-cert-new-null-type test-cert-new-null-payload test-cert-new-key-type test-cert-new-invalid-type test-cert-new-payload-too-long test-cert-bytes-serialization test-cert-fields-after-creation test-cert-zero-length-payload test-cert-new-deux test-cert-invalid-payload-length test-cert-excess-bytes test-cert-serialization test-cert-serialization-excess test-cert-serialization-empty test-cert-serialization-max
test-cert-type:
$(GO) test -v ./lib/common/certificate -run TestCertificateTypeIsFirstByte
@@ -43,7 +42,51 @@ test-cert-read-remainder:
test-cert-read-invalid:
$(GO) test -v ./lib/common/certificate -run TestReadCertificateWithInvalidLength
# Declare all targets as PHONY
test-cert-new-null-type:
$(GO) test -v ./lib/common/certificate -run TestNewCertificateNullType
test-cert-new-null-payload:
$(GO) test -v ./lib/common/certificate -run TestNewCertificateNullTypeWithPayload
test-cert-new-key-type:
$(GO) test -v ./lib/common/certificate -run TestNewCertificateKeyType
test-cert-new-invalid-type:
$(GO) test -v ./lib/common/certificate -run TestNewCertificateInvalidType
test-cert-new-payload-too-long:
$(GO) test -v ./lib/common/certificate -run TestNewCertificatePayloadTooLong
test-cert-bytes-serialization:
$(GO) test -v ./lib/common/certificate -run TestCertificateBytesSerialization
test-cert-fields-after-creation:
$(GO) test -v ./lib/common/certificate -run TestCertificateFieldsAfterCreation
test-cert-zero-length-payload:
$(GO) test -v ./lib/common/certificate -run TestCertificateWithZeroLengthPayload
test-cert-new-deux:
$(GO) test -v ./lib/common/certificate -run TestNewCertificateDeuxFunction
test-cert-invalid-payload-length:
$(GO) test -v ./lib/common/certificate -run TestNewCertificateWithInvalidPayloadLength
test-cert-excess-bytes:
$(GO) test -v ./lib/common/certificate -run TestCertificateExcessBytes
test-cert-serialization:
$(GO) test -v ./lib/common/certificate -run TestCertificateSerializationDeserialization
test-cert-serialization-excess:
$(GO) test -v ./lib/common/certificate -run TestCertificateSerializationDeserializationWithExcessBytes
test-cert-serialization-empty:
$(GO) test -v ./lib/common/certificate -run TestCertificateSerializationDeserializationEmptyPayload
test-cert-serialization-max:
$(GO) test -v ./lib/common/certificate -run TestCertificateSerializationDeserializationMaxPayload
.PHONY: test-cert-all \
test-cert-type \
test-cert-length \
@@ -58,4 +101,19 @@ test-cert-read-invalid:
test-cert-read-correct \
test-cert-read-short \
test-cert-read-remainder \
test-cert-read-invalid
test-cert-read-invalid \
test-cert-new-null-type \
test-cert-new-null-payload \
test-cert-new-key-type \
test-cert-new-invalid-type \
test-cert-new-payload-too-long \
test-cert-bytes-serialization \
test-cert-fields-after-creation \
test-cert-zero-length-payload \
test-cert-new-deux \
test-cert-invalid-payload-length \
test-cert-excess-bytes \
test-cert-serialization \
test-cert-serialization-excess \
test-cert-serialization-empty \
test-cert-serialization-max

View File

@@ -220,6 +220,34 @@ func NewCertificate() *Certificate {
}
}
func NewCertificateDeux(certType int, payload []byte) (*Certificate, error) {
if certType < 0 || certType > 255 {
return nil, fmt.Errorf("invalid certificate type: %d", certType)
}
certTypeByte := byte(certType)
if len(payload) > 65535 {
return nil, fmt.Errorf("payload too long: %d bytes", len(payload))
}
_len, err := NewIntegerFromInt(len(payload), 2)
if err != nil {
panic(err)
}
cert := &Certificate{
kind: Integer([]byte{certTypeByte}),
len: *_len,
payload: payload,
}
log.WithFields(logrus.Fields{
"type": certType,
"length": len(payload),
}).Debug("Successfully created new certificate")
return cert, nil
}
// NewCertificateWithType creates a new Certificate with specified type and payload
func NewCertificateWithType(certType uint8, payload []byte) (*Certificate, error) {
// Validate certificate type

View File

@@ -1,6 +1,7 @@
package certificate
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
@@ -144,3 +145,224 @@ func TestReadCertificateWithInvalidLength(t *testing.T) {
assert.Equal("error parsing certificate: certificate is too short", err.Error(), "correct error message should be returned")
}
}
func TestNewCertificateNullType(t *testing.T) {
assert := assert.New(t)
// Create a NULL certificate with no payload
cert, err := NewCertificateWithType(CERT_NULL, []byte{})
assert.Nil(err, "Expected no error when creating NULL certificate with empty payload")
assert.Equal(CERT_NULL, cert.Type(), "Certificate type should be CERT_NULL")
assert.Equal(0, cert.Length(), "Certificate length should be 0 for NULL certificate")
assert.Equal(0, len(cert.Data()), "Certificate data should be empty for NULL certificate")
}
func TestNewCertificateNullTypeWithPayload(t *testing.T) {
assert := assert.New(t)
// Attempt to create a NULL certificate with a payload (should fail)
_, err := NewCertificateWithType(CERT_NULL, []byte{0x00})
assert.NotNil(err, "Expected error when creating NULL certificate with payload")
assert.Equal("NULL certificates must have empty payload", err.Error(), "Correct error message should be returned")
}
func TestNewCertificateKeyType(t *testing.T) {
assert := assert.New(t)
payload := []byte{0x00, 0x01, 0x02, 0x03, 0x04}
cert, err := NewCertificateWithType(CERT_KEY, payload)
assert.Nil(err, "Expected no error when creating KEY certificate with valid payload")
assert.Equal(CERT_KEY, cert.Type(), "Certificate type should be CERT_KEY")
assert.Equal(len(payload), cert.Length(), "Certificate length should match payload length")
assert.Equal(payload, cert.Data(), "Certificate data should match payload")
}
func TestNewCertificateInvalidType(t *testing.T) {
assert := assert.New(t)
invalidCertType := uint8(6) // Invalid type (valid types are 0-5)
_, err := NewCertificateWithType(invalidCertType, []byte{})
assert.NotNil(err, "Expected error when creating certificate with invalid type")
assert.Equal("invalid certificate type: 6", err.Error(), "Correct error message should be returned")
}
func TestNewCertificatePayloadTooLong(t *testing.T) {
assert := assert.New(t)
// Create a payload that exceeds the maximum allowed length (65535 bytes)
payload := make([]byte, 65536) // 65536 bytes
_, err := NewCertificateWithType(CERT_KEY, payload)
assert.NotNil(err, "Expected error when creating certificate with payload too long")
assert.Equal("certificate payload too long: maximum length is 65535 bytes", err.Error(), "Correct error message should be returned")
}
func TestCertificateBytesSerialization(t *testing.T) {
assert := assert.New(t)
payload := []byte{0xAA, 0xBB, 0xCC}
certType := CERT_SIGNED
cert, err := NewCertificateWithType(uint8(certType), payload)
assert.Nil(err, "Expected no error when creating SIGNED certificate")
expectedBytes := []byte{
byte(certType), // Certificate type
0x00, byte(len(payload)), // Certificate length (2 bytes)
0xAA, 0xBB, 0xCC, // Payload
}
actualBytes := cert.Bytes()
assert.Equal(expectedBytes, actualBytes, "Certificate bytes should match expected serialization")
}
func TestCertificateFieldsAfterCreation(t *testing.T) {
assert := assert.New(t)
payload := []byte{0xDE, 0xAD, 0xBE, 0xEF}
certType := CERT_MULTIPLE
cert, err := NewCertificateWithType(uint8(certType), payload)
assert.Nil(err, "Expected no error when creating MULTIPLE certificate")
assert.Equal(certType, cert.Type(), "Certificate type should match")
assert.Equal(len(payload), cert.Length(), "Certificate length should match payload length")
assert.Equal(payload, cert.Data(), "Certificate data should match payload")
}
func TestCertificateWithZeroLengthPayload(t *testing.T) {
assert := assert.New(t)
certType := CERT_HASHCASH
cert, err := NewCertificateWithType(uint8(certType), []byte{})
assert.Nil(err, "Expected no error when creating certificate with zero-length payload")
assert.Equal(certType, cert.Type(), "Certificate type should match")
assert.Equal(0, cert.Length(), "Certificate length should be 0 for zero-length payload")
assert.Equal(0, len(cert.Data()), "Certificate data should be empty")
}
func TestNewCertificateDeuxFunction(t *testing.T) {
assert := assert.New(t)
payload := []byte{0x11, 0x22}
certType := CERT_HIDDEN
cert, err := NewCertificateDeux(certType, payload)
assert.Nil(err, "Expected no error when creating certificate with NewCertificateDeux")
assert.Equal(certType, cert.Type(), "Certificate type should match")
assert.Equal(len(payload), cert.Length(), "Certificate length should match payload length")
assert.Equal(payload, cert.Data(), "Certificate data should match payload")
}
func TestNewCertificateWithInvalidPayloadLength(t *testing.T) {
assert := assert.New(t)
payload := make([]byte, 70000) // Exceeds 65535 bytes
_, err := NewCertificateDeux(CERT_KEY, payload)
assert.NotNil(err, "Expected error when creating certificate with payload exceeding maximum length")
assert.Equal("payload too long: 70000 bytes", err.Error(), "Correct error message should be returned")
}
func TestCertificateExcessBytes(t *testing.T) {
assert := assert.New(t)
payload := []byte{0x01, 0x02}
extraBytes := []byte{0x03, 0x04}
certData := append(payload, extraBytes...)
certBytes := append([]byte{byte(CERT_SIGNED)}, []byte{0x00, byte(len(payload))}...)
certBytes = append(certBytes, certData...)
cert, err := readCertificate(certBytes)
assert.Nil(err, "Expected no error when reading certificate with excess bytes")
excess := cert.ExcessBytes()
assert.Equal(extraBytes, excess, "ExcessBytes should return the extra bytes in the payload")
assert.Equal(payload, cert.Data(), "Data() should return the valid payload excluding excess bytes")
}
func TestCertificateSerializationDeserialization(t *testing.T) {
assert := assert.New(t)
payload := []byte{0xAA, 0xBB, 0xCC}
certType := CERT_SIGNED
originalCert, err := NewCertificateWithType(uint8(certType), payload)
assert.Nil(err, "Expected no error when creating SIGNED certificate")
serializedBytes := originalCert.Bytes()
assert.NotNil(serializedBytes, "Serialized bytes should not be nil")
deserializedCert, err := readCertificate(serializedBytes)
assert.Nil(err, "Expected no error when deserializing certificate")
assert.Equal(originalCert.Type(), deserializedCert.Type(), "Certificate types should match")
assert.Equal(originalCert.Length(), deserializedCert.Length(), "Certificate lengths should match")
assert.Equal(originalCert.Data(), deserializedCert.Data(), "Certificate payloads should match")
}
func TestCertificateSerializationDeserializationWithExcessBytes(t *testing.T) {
assert := assert.New(t)
payload := []byte{0x01, 0x02}
certType := CERT_MULTIPLE
originalCert, err := NewCertificateWithType(uint8(certType), payload)
assert.Nil(err, "Expected no error when creating MULTIPLE certificate")
serializedBytes := originalCert.Bytes()
excessBytes := []byte{0x03, 0x04}
serializedBytesWithExcess := append(serializedBytes, excessBytes...)
deserializedCert, err := readCertificate(serializedBytesWithExcess)
assert.Nil(err, "Expected no error when deserializing certificate with excess bytes")
assert.Equal(originalCert.Type(), deserializedCert.Type(), "Certificate types should match")
assert.Equal(originalCert.Length(), deserializedCert.Length(), "Certificate lengths should match")
assert.Equal(originalCert.Data(), deserializedCert.Data(), "Certificate payloads should match")
excess := deserializedCert.ExcessBytes()
assert.Equal(excessBytes, excess, "ExcessBytes should return the extra bytes appended to the serialized data")
}
func TestCertificateSerializationDeserializationEmptyPayload(t *testing.T) {
assert := assert.New(t)
payload := []byte{}
certType := CERT_NULL
originalCert, err := NewCertificateWithType(uint8(certType), payload)
assert.Nil(err, "Expected no error when creating NULL certificate")
serializedBytes := originalCert.Bytes()
deserializedCert, err := readCertificate(serializedBytes)
assert.Nil(err, "Expected no error when deserializing NULL certificate")
assert.Equal(originalCert.Type(), deserializedCert.Type(), "Certificate types should match")
assert.Equal(originalCert.Length(), deserializedCert.Length(), "Certificate lengths should match")
assert.Equal(originalCert.Data(), deserializedCert.Data(), "Certificate payloads should match")
}
func TestCertificateSerializationDeserializationMaxPayload(t *testing.T) {
assert := assert.New(t)
payload := make([]byte, 65535)
for i := range payload {
payload[i] = byte(i % 256)
}
certType := CERT_KEY
originalCert, err := NewCertificateWithType(uint8(certType), payload)
assert.Nil(err, "Expected no error when creating KEY certificate with maximum payload")
serializedBytes := originalCert.Bytes()
assert.Equal(1+2+65535, len(serializedBytes), "Serialized bytes length should be correct for maximum payload")
deserializedCert, err := readCertificate(serializedBytes)
assert.Nil(err, "Expected no error when deserializing certificate with maximum payload")
assert.Equal(originalCert.Type(), deserializedCert.Type(), "Certificate types should match")
assert.Equal(originalCert.Length(), deserializedCert.Length(), "Certificate lengths should match")
assert.True(bytes.Equal(originalCert.Data(), deserializedCert.Data()), "Certificate payloads should match")
}