mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-18 18:44:30 -04:00
Merge pull request #28 from satk0/add-lease-tests
Add lease tests and make names more clear
This commit is contained in:
@@ -9,7 +9,7 @@ Package lease implements the I2P lease common data structure
|
||||
```go
|
||||
const (
|
||||
LEASE_SIZE = 44
|
||||
LEASE_HASH_SIZE = 32
|
||||
LEASE_TUNNEL_GW_SIZE = 32
|
||||
LEASE_TUNNEL_ID_SIZE = 4
|
||||
)
|
||||
```
|
||||
|
@@ -6,7 +6,7 @@ import . "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
// Sizes in bytes of various components of a Lease
|
||||
const (
|
||||
LEASE_SIZE = 44
|
||||
LEASE_HASH_SIZE = 32
|
||||
LEASE_TUNNEL_GW_SIZE = 32
|
||||
LEASE_TUNNEL_ID_SIZE = 4
|
||||
)
|
||||
|
||||
@@ -51,13 +51,13 @@ type Lease [LEASE_SIZE]byte
|
||||
|
||||
// TunnelGateway returns the tunnel gateway as a Hash.
|
||||
func (lease Lease) TunnelGateway() (hash Hash) {
|
||||
copy(hash[:], lease[:LEASE_HASH_SIZE])
|
||||
copy(hash[:], lease[:LEASE_TUNNEL_GW_SIZE])
|
||||
return
|
||||
}
|
||||
|
||||
// TunnelID returns the tunnel id as a uint23.
|
||||
func (lease Lease) TunnelID() uint32 {
|
||||
i := Integer(lease[LEASE_HASH_SIZE : LEASE_HASH_SIZE+LEASE_TUNNEL_ID_SIZE])
|
||||
i := Integer(lease[LEASE_TUNNEL_GW_SIZE : LEASE_TUNNEL_GW_SIZE+LEASE_TUNNEL_ID_SIZE])
|
||||
return uint32(
|
||||
i.Int(),
|
||||
)
|
||||
@@ -65,7 +65,7 @@ func (lease Lease) TunnelID() uint32 {
|
||||
|
||||
// Date returns the date as an I2P Date.
|
||||
func (lease Lease) Date() (date Date) {
|
||||
copy(date[:], lease[LEASE_HASH_SIZE+LEASE_TUNNEL_ID_SIZE:])
|
||||
copy(date[:], lease[LEASE_TUNNEL_GW_SIZE+LEASE_TUNNEL_ID_SIZE:])
|
||||
return
|
||||
}
|
||||
|
||||
|
51
lib/common/lease/lease_test.go
Normal file
51
lib/common/lease/lease_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package lease
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
. "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
)
|
||||
|
||||
func TestTunnelGateway(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
expectedTunnelGatewayBytes := []byte("example_32_bytes_hash_to_test_00")
|
||||
|
||||
var lease_bytes []byte
|
||||
lease_bytes = append(lease_bytes, expectedTunnelGatewayBytes...)
|
||||
lease_bytes = append(lease_bytes, make([]byte, LEASE_SIZE - LEASE_TUNNEL_GW_SIZE)...)
|
||||
lease := Lease(lease_bytes)
|
||||
|
||||
tunnelGateway := lease.TunnelGateway()
|
||||
assert.ElementsMatch(tunnelGateway.Bytes(), expectedTunnelGatewayBytes)
|
||||
}
|
||||
|
||||
func TestTunnelID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
expectedTunnelIDBytes := []byte{0x21, 0x37, 0x31, 0x33}
|
||||
|
||||
var lease_bytes []byte
|
||||
lease_bytes = append(lease_bytes, make([]byte, LEASE_TUNNEL_GW_SIZE)...)
|
||||
lease_bytes = append(lease_bytes, expectedTunnelIDBytes...)
|
||||
lease_bytes = append(lease_bytes, make([]byte, LEASE_SIZE - LEASE_TUNNEL_ID_SIZE - LEASE_TUNNEL_GW_SIZE)...)
|
||||
lease := Lease(lease_bytes)
|
||||
|
||||
tunnelID := lease.TunnelID()
|
||||
assert.Equal(tunnelID, uint32(Integer(expectedTunnelIDBytes).Int()))
|
||||
}
|
||||
|
||||
func TestDate(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
expectedDateBytes := []byte{0x21, 0x37, 0x31, 0x33, 0x16, 0x93, 0x13, 0x28}
|
||||
|
||||
var lease_bytes []byte
|
||||
lease_bytes = append(lease_bytes, make([]byte, LEASE_TUNNEL_GW_SIZE + LEASE_TUNNEL_ID_SIZE)...)
|
||||
lease_bytes = append(lease_bytes, expectedDateBytes...)
|
||||
lease := Lease(lease_bytes)
|
||||
|
||||
date := lease.Date()
|
||||
assert.ElementsMatch(date.Bytes(), expectedDateBytes)
|
||||
}
|
Reference in New Issue
Block a user