clear up a couple more tests before bed
This commit is contained in:
@ -8,8 +8,7 @@ import (
|
||||
func TestTimeFromMiliseconds(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// next_day := Date{0x00, 0x00, 0x00, 0x00, 0x05, 0x26, 0x5c, 0x00}
|
||||
next_day := Date{0x00, 0x5c, 0x26, 0x05, 0x00, 0x00, 0x00, 0x00}
|
||||
next_day := Date{0x00, 0x00, 0x00, 0x00, 0x05, 0x26, 0x5c, 0x00}
|
||||
go_time := next_day.Time()
|
||||
|
||||
assert.Equal(int64(86400), go_time.Unix(), "Date.Time() did not parse time in milliseconds")
|
||||
|
@ -8,8 +8,8 @@ Accurate for version 0.9.24
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
// log "github.com/sirupsen/logrus"
|
||||
// "errors"
|
||||
// log "github.com/sirupsen/logrus"
|
||||
// "errors"
|
||||
)
|
||||
|
||||
// Total byte length of an I2P integer
|
||||
@ -20,17 +20,19 @@ const (
|
||||
type Integer []byte
|
||||
|
||||
func (i Integer) longBytes() (value [INTEGER_SIZE]byte) {
|
||||
value = [INTEGER_SIZE]byte{0, 0, 0, 0, 0, 0, 0, 0}
|
||||
value = [INTEGER_SIZE]byte{0,0,0,0,0,0,0,0}
|
||||
pad := INTEGER_SIZE-len([]byte(i))
|
||||
for index, element := range []byte(i) {
|
||||
value[INTEGER_SIZE-1-index] = element
|
||||
value[pad+index] = element
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (i Integer) Value() int {
|
||||
r := i.longBytes()
|
||||
// log.Println("LONG BYTES", r)
|
||||
return int(binary.BigEndian.Uint64(r[:]))
|
||||
// return int(binary.BigEndian.Int64(r[:]))
|
||||
// return int(binary.BigEndian.Int64(r[:]))
|
||||
}
|
||||
|
||||
func (i Integer) Bytes() []byte {
|
||||
@ -47,9 +49,9 @@ func (i Integer) Bytes() []byte {
|
||||
//
|
||||
func NewInteger(number []byte) (value Integer, err error) {
|
||||
value = number //[INTEGER_SIZE]byte(number)
|
||||
// for index, element := range number {
|
||||
// value[INTEGER_SIZE-1-index] = element
|
||||
// }
|
||||
// for index, element := range number {
|
||||
// value[INTEGER_SIZE-1-index] = element
|
||||
// }
|
||||
/*length := len(number)
|
||||
if length < INTEGER_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
func TestIntegerBigEndian(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||
bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
|
||||
integer, err := NewInteger(bytes)
|
||||
assert.Nil(err)
|
||||
|
||||
|
@ -256,33 +256,39 @@ func ReadKeyCertificate(data []byte) (key_certificate KeyCertificate, err error)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cert_type, _, err := cert.Type()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
log.Println("KEYSANDCERT CERT TYPE=", cert_type, cert.CertBytes)
|
||||
key_certificate.Certificate = cert
|
||||
data = cert.CertBytes
|
||||
data_len := len(data)
|
||||
if data_len < 4 {
|
||||
if data_len < 2 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SigningPublicKeyType",
|
||||
"data_len": data_len,
|
||||
"required_len": 4,
|
||||
"required_len": 2,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing key certificate signing public key")
|
||||
err = errors.New("error parsing key certificate signing public key: not enough data")
|
||||
// key_certificate.SPKType, err = NewInteger(data[:])
|
||||
key_certificate.PKType, err = NewInteger(data[2:])
|
||||
key_certificate.PKType, err = NewInteger(data[:])
|
||||
return
|
||||
}
|
||||
key_certificate.PKType, err = NewInteger(data[2:4])
|
||||
// key_certificate.SPKType, err = NewInteger(data[2:4])
|
||||
if data_len < 6 {
|
||||
// key_certificate.SPKType, err = NewInteger(data[0:2])
|
||||
key_certificate.PKType, err = NewInteger(data[0:2])
|
||||
if data_len < 4 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) PublicKeyType",
|
||||
"data_len": data_len,
|
||||
"required_len": 6,
|
||||
"required_len": 4,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing key certificate public key")
|
||||
err = errors.New("error parsing key certificate public key: not enough data")
|
||||
return
|
||||
}
|
||||
key_certificate.SPKType, err = NewInteger(data[4:6])
|
||||
// key_certificate.PKType, err = NewInteger(data[4:6])
|
||||
// key_certificate.PKType, err = NewInteger(data[2:4])
|
||||
key_certificate.SPKType, err = NewInteger(data[2:4])
|
||||
return
|
||||
}
|
||||
|
@ -13,15 +13,15 @@ func TestSingingPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
||||
spk_type, err := key_cert.SigningPublicKeyType()
|
||||
|
||||
assert.Nil(err, "SigningPublicKeyType() returned error with valid data")
|
||||
assert.Equal(spk_type, KEYCERT_SIGN_P521, "SigningPublicKeyType() did not return correct type")
|
||||
assert.Equal(spk_type, KEYCERT_SIGN_DSA_SHA1, "SigningPublicKeyType() did not return correct type")
|
||||
}
|
||||
|
||||
func TestSingingPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
func TestPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert, err := ReadKeyCertificate([]byte{0x05, 0x00, 0x01, 0x00})
|
||||
// assert.NotNil(err, "ReadKeyCertificate() returned error with valid data")
|
||||
_, err = key_cert.SigningPublicKeyType()
|
||||
_, err = key_cert.PublicKeyType()
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error retrieving signing public key type: not enough data", err.Error(), "correct error message should be returned")
|
||||
@ -36,17 +36,17 @@ func TestPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
||||
pk_type, err := key_cert.PublicKeyType()
|
||||
|
||||
assert.Nil(err, "PublicKeyType() returned error with valid data")
|
||||
assert.Equal(pk_type, KEYCERT_CRYPTO_X25519, "PublicKeyType() did not return correct type")
|
||||
assert.Equal(pk_type, KEYCERT_CRYPTO_P521, "PublicKeyType() did not return correct type")
|
||||
}
|
||||
|
||||
func TestPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
func TestSigningPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert, err := ReadKeyCertificate([]byte{0x05, 0x00, 0x02, 0x00, 0x00})
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing key certificate public key: not enough data", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
_, err = key_cert.PublicKeyType()
|
||||
_, err = key_cert.SigningPublicKeyType()
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error retrieving signing public key type: not enough data", err.Error(), "correct error message should be returned")
|
||||
|
@ -151,44 +151,6 @@ func (keys_and_cert KeysAndCert) GetCertificate() (cert CertificateInterface, er
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Read a KeysAndCert from a slice of bytes, retuning it and the remaining data as well as any errors
|
||||
// encoutered parsing the KeysAndCert.
|
||||
//
|
||||
func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte, err error) {
|
||||
data_len := len(data)
|
||||
if data_len < KEYS_AND_CERT_MIN_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "ReadKeysAndCert",
|
||||
"data_len": data_len,
|
||||
"required_len": KEYS_AND_CERT_MIN_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing keys and cert")
|
||||
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
|
||||
return
|
||||
}
|
||||
cert, remainder, err := ReadCertificate(data[:KEYS_AND_CERT_MIN_SIZE])
|
||||
if err != nil {
|
||||
//return
|
||||
log.Error("ERROR READ CERTIFICATE", err)
|
||||
err = nil
|
||||
|
||||
}
|
||||
log.Println("READ CERTIFICATE")
|
||||
keys_and_cert.CertificateInterface = cert
|
||||
spk, pk, remainder, err := ReadKeys(data, cert)
|
||||
if err != nil {
|
||||
// return
|
||||
log.Error("ERROR READ KEYS", err)
|
||||
err = nil
|
||||
|
||||
}
|
||||
log.Println("READ KEYS")
|
||||
keys_and_cert.SigningPublicKey = spk
|
||||
keys_and_cert.PublicKey = pk
|
||||
return
|
||||
}
|
||||
|
||||
func ReadKeys(data []byte, cert CertificateInterface) (spk crypto.SigningPublicKey, pk crypto.PublicKey, remainder []byte, err error) {
|
||||
data_len := len(data)
|
||||
if data_len < KEYS_AND_CERT_MIN_SIZE {
|
||||
@ -265,3 +227,41 @@ func ReadKeys(data []byte, cert CertificateInterface) (spk crypto.SigningPublicK
|
||||
remainder = data[KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE:]
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Read a KeysAndCert from a slice of bytes, retuning it and the remaining data as well as any errors
|
||||
// encoutered parsing the KeysAndCert.
|
||||
//
|
||||
func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte, err error) {
|
||||
data_len := len(data)
|
||||
if data_len < KEYS_AND_CERT_MIN_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "ReadKeysAndCert",
|
||||
"data_len": data_len,
|
||||
"required_len": KEYS_AND_CERT_MIN_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing keys and cert")
|
||||
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
|
||||
return
|
||||
}
|
||||
cert, remainder, err := ReadCertificate(data[:KEYS_AND_CERT_MIN_SIZE])
|
||||
if err != nil {
|
||||
//return
|
||||
log.Error("ERROR READ CERTIFICATE", err)
|
||||
err = nil
|
||||
|
||||
}
|
||||
log.Println("READ CERTIFICATE")
|
||||
keys_and_cert.CertificateInterface = cert
|
||||
spk, pk, remainder, err := ReadKeys(data, cert)
|
||||
if err != nil {
|
||||
// return
|
||||
log.Error("ERROR READ KEYS", err)
|
||||
err = nil
|
||||
|
||||
}
|
||||
log.Println("READ KEYS")
|
||||
keys_and_cert.SigningPublicKey = spk
|
||||
keys_and_cert.PublicKey = pk
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user