starting keys and cert tests

This commit is contained in:
Hayden Parker
2016-06-16 22:45:38 -07:00
parent fc9974c33f
commit 57f19b74bc
6 changed files with 58 additions and 12 deletions

View File

@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2015 bounce-chat Copyright (c) 2015 Hayden Parker
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -1,4 +1,15 @@
# go-i2p # go-i2p
An I2P router and library implemented in Go
work in progress check back later A pure go implementation of the I2P router
## Status
go-i2p is in early development. The common data structures are starting to get
## Contributing
## License
This project is licensed under the MIT license, see LICENSE for more information.

View File

@ -2,7 +2,7 @@ package common
/* /*
I2P Certificate I2P Certificate
https://geti2p.net/en/docs/spec/common-structures#type_Certificate https://geti2p.net/spec/common-structures#type-certificate
Accurate for version 0.9.24 Accurate for version 0.9.24
+----+----+----+----+----+-// +----+----+----+----+----+-//

View File

@ -2,7 +2,7 @@ package common
/* /*
I2P KeysAndCert I2P KeysAndCert
https://geti2p.net/en/docs/spec/common-structures#struct_KeysAndCert https://geti2p.net/spec/common-structures#keysandcert
Accurate for version 0.9.24 Accurate for version 0.9.24
+----+----+----+----+----+----+----+----+ +----+----+----+----+----+----+----+----+
@ -55,6 +55,7 @@ const (
KEYS_AND_CERT_PUBKEY_SIZE = 256 KEYS_AND_CERT_PUBKEY_SIZE = 256
KEYS_AND_CERT_SPK_SIZE = 128 KEYS_AND_CERT_SPK_SIZE = 128
KEYS_AND_CERT_MIN_SIZE = 387 KEYS_AND_CERT_MIN_SIZE = 387
KEYS_AND_CERT_DATA_SIZE = 384
) )
type KeysAndCert []byte type KeysAndCert []byte
@ -155,7 +156,7 @@ func (keys_and_cert KeysAndCert) Certificate() (cert Certificate, err error) {
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size") err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
return return
} }
cert, _, err = ReadCertificate(keys_and_cert[KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE:]) cert, _, err = ReadCertificate(keys_and_cert[KEYS_AND_CERT_DATA_SIZE:])
return return
} }
@ -174,14 +175,14 @@ func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte,
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size") err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
return return
} }
copy(data[:KEYS_AND_CERT_MIN_SIZE], keys_and_cert) keys_and_cert = KeysAndCert(data[:KEYS_AND_CERT_MIN_SIZE])
cert, _ := keys_and_cert.Certificate() cert, _ := keys_and_cert.Certificate()
n, err := cert.Length() cert_len, _ := cert.Length()
if err != nil { if cert_len == 0 {
remainder = data[KEYS_AND_CERT_MIN_SIZE:] remainder = data[KEYS_AND_CERT_MIN_SIZE:]
return return
} }
keys_and_cert = append(keys_and_cert, data[KEYS_AND_CERT_MIN_SIZE:n+3]...) keys_and_cert = append(keys_and_cert, data[KEYS_AND_CERT_MIN_SIZE:KEYS_AND_CERT_MIN_SIZE+cert_len]...)
remainder = data[KEYS_AND_CERT_MIN_SIZE+n+3:] remainder = data[KEYS_AND_CERT_MIN_SIZE+cert_len:]
return return
} }

View File

@ -1,7 +1,7 @@
package common package common
import ( import (
//"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
) )
@ -48,9 +48,37 @@ func TestReadKeysAndCertWithMissingCertData(t *testing.T) {
} }
func TestReadKeysAndCertWithValidDataWithCertificate(t *testing.T) { func TestReadKeysAndCertWithValidDataWithCertificate(t *testing.T) {
assert := assert.New(t)
cert_data := make([]byte, 128+256)
cert_data = append(cert_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}...)
keys_and_cert, remainder, err := ReadKeysAndCert(cert_data)
assert.Equal(0, len(remainder))
assert.Nil(err)
_, err = keys_and_cert.PublicKey()
assert.Nil(err, "keys_and_cert.PublicKey() returned error with valid data containing certificate")
_, err = keys_and_cert.SigningPublicKey()
assert.Nil(err, "keys_and_cert.SigningPublicKey() returned error with valid data containing certificate")
_, err = keys_and_cert.Certificate()
assert.Nil(err, "keys_and_cert.Certificate() returned error with valid data containing certificate")
} }
func TestReadKeysAndCertWithValidDataWithoutCertificate(t *testing.T) { func TestReadKeysAndCertWithValidDataWithoutCertificate(t *testing.T) {
assert := assert.New(t)
cert_data := make([]byte, 128+256)
cert_data = append(cert_data, []byte{0x00, 0x00, 0x00}...)
keys_and_cert, remainder, err := ReadKeysAndCert(cert_data)
assert.Equal(0, len(remainder))
assert.Nil(err)
_, err = keys_and_cert.PublicKey()
assert.Nil(err, "keys_and_cert.PublicKey() returned error with valid data not containing certificate")
_, err = keys_and_cert.SigningPublicKey()
assert.Nil(err, "keys_and_cert.SigningPublicKey() returned error with valid data not containing certificate")
_, err = keys_and_cert.Certificate()
assert.Nil(err, "keys_and_cert.Certificate() returned error with valid data not containing certificate")
} }
func TestReadKeysAndCertWithValidDataWithCertificateAndRemainder(t *testing.T) { func TestReadKeysAndCertWithValidDataWithCertificateAndRemainder(t *testing.T) {

View File

@ -1 +1,7 @@
package common package common
import (
//"github.com/stretchr/testify/assert"
//"testing"
)