mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 14:15:06 +03:00
db entity fix
db entity fix added sql. NullTime struct
This commit is contained in:
parent
80ea41600f
commit
e9e79ac3f4
11 changed files with 295 additions and 126 deletions
234
src/core/db.go
234
src/core/db.go
|
@ -13,51 +13,70 @@ import (
|
|||
type PeerInfoDB struct {
|
||||
PeerInfo
|
||||
Id int
|
||||
CoordsBytes []byte
|
||||
KeyBytes []byte
|
||||
RootBytes []byte
|
||||
PeerErr sql.NullString
|
||||
Coords Blob
|
||||
Key PublicKeyContainer
|
||||
Root PublicKeyContainer
|
||||
Error ErrorContainer
|
||||
LastErrorTime sql.NullTime
|
||||
}
|
||||
|
||||
type SelfInfoDB struct {
|
||||
SelfInfo
|
||||
Id int
|
||||
KeyBytes []byte
|
||||
Key PublicKeyContainer
|
||||
}
|
||||
|
||||
type TreeEntryInfoDB struct {
|
||||
TreeEntryInfo
|
||||
Id int
|
||||
KeyBytes []byte
|
||||
ParentBytes []byte
|
||||
Key PublicKeyContainer
|
||||
Parent PublicKeyContainer
|
||||
}
|
||||
|
||||
type PathEntryInfoDB struct {
|
||||
PathEntryInfo
|
||||
Id int
|
||||
KeyBytes []byte
|
||||
PathBytes []byte
|
||||
Key PublicKeyContainer
|
||||
Path Blob
|
||||
}
|
||||
|
||||
type SessionInfoDB struct {
|
||||
SessionInfo
|
||||
Id int
|
||||
KeyBytes []byte
|
||||
Key PublicKeyContainer
|
||||
}
|
||||
|
||||
func NewPeerInfoDB(peerInfo PeerInfo) (_ *PeerInfoDB, err error) {
|
||||
peer := &PeerInfoDB{
|
||||
PeerInfo: peerInfo,
|
||||
}
|
||||
peer.PeerErr = MarshalError(peer.LastError)
|
||||
peer.CoordsBytes = ConvertToByteSlise(peer.Coords)
|
||||
peer.KeyBytes, err = MarshalPKIXPublicKey(&peer.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
peer.Error = ErrorContainer{
|
||||
_err: peerInfo.LastError,
|
||||
}
|
||||
peer.RootBytes, err = MarshalPKIXPublicKey(&peer.Root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
peer.Error.ParseError(peerInfo.LastError)
|
||||
|
||||
peer.Coords = Blob{
|
||||
_uint: peerInfo.Coords,
|
||||
}
|
||||
peer.Coords.ParseUint64Sliсe(peer.Coords._uint)
|
||||
|
||||
peer.Key = PublicKeyContainer{
|
||||
_publicKey: peerInfo.Key,
|
||||
}
|
||||
publicKey := peer.Key.GetPKIXPublicKey()
|
||||
peer.Key.MarshalPKIXPublicKey(&publicKey)
|
||||
|
||||
peer.Root = PublicKeyContainer{
|
||||
_publicKey: peerInfo.Root,
|
||||
}
|
||||
publicKey = peer.Root.GetPKIXPublicKey()
|
||||
peer.Root.MarshalPKIXPublicKey(&publicKey)
|
||||
peer.LastErrorTime = sql.NullTime{}
|
||||
if !peerInfo.LastErrorTime.IsZero() {
|
||||
peer.LastErrorTime.Time = peerInfo.LastErrorTime
|
||||
peer.LastErrorTime.Valid = true
|
||||
} else {
|
||||
peer.LastErrorTime.Valid = false
|
||||
}
|
||||
return peer, nil
|
||||
}
|
||||
|
@ -66,7 +85,11 @@ func NewSelfInfoDB(selfinfo SelfInfo) (_ *SelfInfoDB, err error) {
|
|||
model := &SelfInfoDB{
|
||||
SelfInfo: selfinfo,
|
||||
}
|
||||
model.KeyBytes, err = MarshalPKIXPublicKey(&model.Key)
|
||||
model.Key = PublicKeyContainer{
|
||||
_publicKey: selfinfo.Key,
|
||||
}
|
||||
publicKey := model.Key.GetPKIXPublicKey()
|
||||
model.Key.MarshalPKIXPublicKey(&publicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -77,14 +100,16 @@ func NewTreeEntryInfoDB(treeEntyInfo TreeEntryInfo) (_ *TreeEntryInfoDB, err err
|
|||
model := &TreeEntryInfoDB{
|
||||
TreeEntryInfo: treeEntyInfo,
|
||||
}
|
||||
model.KeyBytes, err = MarshalPKIXPublicKey(&model.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
model.Key = PublicKeyContainer{
|
||||
_publicKey: treeEntyInfo.Key,
|
||||
}
|
||||
model.ParentBytes, err = MarshalPKIXPublicKey(&model.Parent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
publicKey := model.Key.GetPKIXPublicKey()
|
||||
model.Key.MarshalPKIXPublicKey(&publicKey)
|
||||
model.Parent = PublicKeyContainer{
|
||||
_publicKey: treeEntyInfo.Parent,
|
||||
}
|
||||
publicKey = model.Parent.GetPKIXPublicKey()
|
||||
model.Parent.MarshalPKIXPublicKey(&publicKey)
|
||||
return model, nil
|
||||
}
|
||||
|
||||
|
@ -92,12 +117,16 @@ func NewPathEntryInfoDB(PathEntryInfo PathEntryInfo) (_ *PathEntryInfoDB, err er
|
|||
model := &PathEntryInfoDB{
|
||||
PathEntryInfo: PathEntryInfo,
|
||||
}
|
||||
model.KeyBytes, err = MarshalPKIXPublicKey(&model.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
model.Key = PublicKeyContainer{
|
||||
_publicKey: PathEntryInfo.Key,
|
||||
}
|
||||
model.PathBytes = ConvertToByteSlise(model.Path)
|
||||
publicKey := model.Key.GetPKIXPublicKey()
|
||||
model.Key.MarshalPKIXPublicKey(&publicKey)
|
||||
|
||||
model.Path = Blob{
|
||||
_uint: PathEntryInfo.Path,
|
||||
}
|
||||
model.Path.ParseUint64Sliсe(model.Path._uint)
|
||||
return model, nil
|
||||
}
|
||||
|
||||
|
@ -105,25 +134,146 @@ func NewSessionInfoDB(SessionInfo SessionInfo) (_ *SessionInfoDB, err error) {
|
|||
model := &SessionInfoDB{
|
||||
SessionInfo: SessionInfo,
|
||||
}
|
||||
model.KeyBytes, err = MarshalPKIXPublicKey(&model.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
model.Key = PublicKeyContainer{
|
||||
_publicKey: SessionInfo.Key,
|
||||
}
|
||||
publicKey := model.Key.GetPKIXPublicKey()
|
||||
model.Key.MarshalPKIXPublicKey(&publicKey)
|
||||
return model, nil
|
||||
}
|
||||
|
||||
func ConvertToByteSlise(uintSlise []uint64) []byte {
|
||||
var ByteSlise []byte
|
||||
if uintSlise != nil {
|
||||
ByteSlise = make([]byte, len(uintSlise)*8)
|
||||
for i, coord := range uintSlise {
|
||||
binary.LittleEndian.PutUint64(ByteSlise[i*8:], coord)
|
||||
}
|
||||
}
|
||||
return ByteSlise
|
||||
type Blob struct {
|
||||
_byte []byte
|
||||
_uint []uint64
|
||||
}
|
||||
|
||||
func ConvertToUintSlise(ByteSlise []byte) (_ []uint64, err error) {
|
||||
type PublicKeyContainer struct {
|
||||
_byte []byte
|
||||
_publicKey ed25519.PublicKey
|
||||
}
|
||||
|
||||
type ErrorContainer struct {
|
||||
_ErrStr sql.NullString
|
||||
_err error
|
||||
}
|
||||
|
||||
func (blob *PublicKeyContainer) GetPKIXPublicKeyBytes() []byte {
|
||||
if blob._publicKey == nil {
|
||||
return nil
|
||||
}
|
||||
result, err := MarshalPKIXPublicKey(&blob._publicKey)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (blob *PublicKeyContainer) GetPKIXPublicKey() ed25519.PublicKey {
|
||||
if blob._publicKey == nil {
|
||||
return nil
|
||||
}
|
||||
return blob._publicKey
|
||||
}
|
||||
|
||||
func (blob *PublicKeyContainer) ParsePKIXPublicKey(derBytes *[]byte) {
|
||||
result, err := ParsePKIXPublicKey(derBytes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
blob._byte = *derBytes
|
||||
blob._publicKey = result
|
||||
}
|
||||
|
||||
func (blob *PublicKeyContainer) MarshalPKIXPublicKey(PublicKey *ed25519.PublicKey) {
|
||||
result, err := MarshalPKIXPublicKey(PublicKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
blob._publicKey = *PublicKey
|
||||
blob._byte = result
|
||||
}
|
||||
|
||||
func (blob *Blob) ConvertToUintSliсe() []uint64 {
|
||||
if blob._byte == nil {
|
||||
return nil
|
||||
}
|
||||
result, err := ConvertToUintSliсe(blob._byte)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (blob *Blob) ConvertToByteSliсe() []byte {
|
||||
if blob._uint == nil {
|
||||
return nil
|
||||
}
|
||||
result := ConvertToByteSliсe(blob._uint)
|
||||
return result
|
||||
}
|
||||
|
||||
func (blob *Blob) ParseByteSliсe(slice []byte) {
|
||||
result, err := ConvertToUintSliсe(slice)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
blob._byte = slice
|
||||
blob._uint = result
|
||||
}
|
||||
|
||||
func (blob *Blob) ParseUint64Sliсe(slice []uint64) {
|
||||
blob._uint = slice
|
||||
blob._byte = ConvertToByteSliсe(slice)
|
||||
}
|
||||
|
||||
func (ErrorContainer *ErrorContainer) ParseError(err error) {
|
||||
if err != nil {
|
||||
ErrorContainer._ErrStr = sql.NullString{
|
||||
String: err.Error(),
|
||||
Valid: true}
|
||||
ErrorContainer._err = err
|
||||
}
|
||||
}
|
||||
|
||||
func (ErrorContainer *ErrorContainer) ParseMessage(message string) {
|
||||
if message != "" {
|
||||
ErrorContainer._ErrStr = sql.NullString{
|
||||
String: message,
|
||||
Valid: true}
|
||||
ErrorContainer._err = errors.New(message)
|
||||
}
|
||||
}
|
||||
|
||||
func (ErrorContainer *ErrorContainer) GetError() error {
|
||||
if ErrorContainer._err != nil {
|
||||
return ErrorContainer._err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ErrorContainer *ErrorContainer) GetErrorMessage() string {
|
||||
if ErrorContainer._ErrStr.Valid {
|
||||
return ErrorContainer._ErrStr.String
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (ErrorContainer *ErrorContainer) GetErrorSqlError() sql.NullString {
|
||||
return ErrorContainer._ErrStr
|
||||
}
|
||||
|
||||
func ConvertToByteSliсe(uintSlice []uint64) []byte {
|
||||
var ByteSlice []byte
|
||||
if uintSlice != nil {
|
||||
ByteSlice = make([]byte, len(uintSlice)*8)
|
||||
for i, coord := range uintSlice {
|
||||
binary.LittleEndian.PutUint64(ByteSlice[i*8:], coord)
|
||||
}
|
||||
}
|
||||
return ByteSlice
|
||||
}
|
||||
|
||||
func ConvertToUintSliсe(ByteSlise []byte) (_ []uint64, err error) {
|
||||
if len(ByteSlise)%8 != 0 {
|
||||
return nil, fmt.Errorf("length of byte slice must be a multiple of 8")
|
||||
}
|
||||
|
|
|
@ -43,8 +43,8 @@ func (cfg *PathEntryInfoDBConfig) Add(model *core.PathEntryInfoDB) (_ sql.Result
|
|||
query := "INSERT INTO path_entry_info (Key, Path, Sequence) VALUES (?, ?, ?)"
|
||||
result, err := cfg.DbConfig.DB.Exec(
|
||||
query,
|
||||
model.KeyBytes,
|
||||
model.PathBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Path.ConvertToByteSliсe(),
|
||||
model.Sequence)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -74,7 +74,7 @@ func (cfg *PathEntryInfoDBConfig) Update(model *core.PathEntryInfoDB) (err error
|
|||
Path = ?
|
||||
WHERE
|
||||
Id = ?`,
|
||||
model.Sequence, model.KeyBytes, model.PathBytes, model.Id)
|
||||
model.Sequence, model.Key.GetPKIXPublicKeyBytes(), model.Path.ConvertToByteSliсe(), model.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -89,11 +89,15 @@ func (cfg *PathEntryInfoDBConfig) Get(model *core.PathEntryInfoDB) (_ *sql.Rows,
|
|||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var _key []byte
|
||||
var _path []byte
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&model.Sequence, &model.KeyBytes, &model.PathBytes)
|
||||
err = rows.Scan(&model.Sequence, &_key, &_path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
model.Key.ParsePKIXPublicKey(&_key)
|
||||
model.Path.ParseByteSliсe(_path)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
|
|
@ -57,11 +57,11 @@ func (cfg *PeerInfoDBConfig) Add(model *core.PeerInfoDB) (_ sql.Result, err erro
|
|||
model.URI,
|
||||
model.Up,
|
||||
model.Inbound,
|
||||
model.PeerErr,
|
||||
model.Error.GetErrorMessage(),
|
||||
model.LastErrorTime,
|
||||
model.KeyBytes,
|
||||
model.RootBytes,
|
||||
model.CoordsBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Root.GetPKIXPublicKeyBytes(),
|
||||
model.Coords.ConvertToByteSliсe(),
|
||||
model.Port,
|
||||
model.Priority,
|
||||
model.RXBytes,
|
||||
|
@ -101,30 +101,22 @@ func (cfg *PeerInfoDBConfig) Get(model *core.PeerInfoDB) (_ *sql.Rows, err error
|
|||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var _key []byte
|
||||
var _root []byte
|
||||
var _err string
|
||||
var _coords []byte
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&model.Up, &model.Inbound, &model.PeerErr, &model.LastErrorTime, &model.CoordsBytes,
|
||||
err = rows.Scan(&model.Up, &model.Inbound, &_err, &model.LastErrorTime, &_coords,
|
||||
&model.Port, &model.Priority, &model.RXBytes, &model.TXBytes, &model.Uptime, &model.Latency,
|
||||
&model.URI, &model.KeyBytes, &model.RootBytes)
|
||||
&model.URI, &_key, &_root)
|
||||
if err != nil {
|
||||
return rows, err
|
||||
}
|
||||
}
|
||||
|
||||
model.Coords, err = core.ConvertToUintSlise(model.CoordsBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
publickey, err := core.ParsePKIXPublicKey(&model.KeyBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
model.Key = publickey
|
||||
publicRoot, err := core.ParsePKIXPublicKey(&model.RootBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
model.Root = publicRoot
|
||||
model.LastError = core.ParseError(model.PeerErr)
|
||||
model.Coords.ParseByteSliсe(_coords)
|
||||
model.Key.ParsePKIXPublicKey(&_key)
|
||||
model.Root.ParsePKIXPublicKey(&_root)
|
||||
model.Error.ParseMessage(_err)
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
|
@ -147,8 +139,8 @@ func (cfg *PeerInfoDBConfig) Update(model *core.PeerInfoDB) (err error) {
|
|||
root = ?
|
||||
WHERE
|
||||
Id = ?`,
|
||||
model.Up, model.Inbound, model.PeerErr, model.LastErrorTime, model.CoordsBytes, model.Port, model.Priority,
|
||||
model.RXBytes, model.TXBytes, model.Uptime, model.Latency, model.URI, model.KeyBytes, model.RootBytes, model.Id)
|
||||
model.Up, model.Inbound, model.Error.GetErrorSqlError(), model.LastErrorTime, model.Coords.ConvertToByteSliсe(), model.Port, model.Priority,
|
||||
model.RXBytes, model.TXBytes, model.Uptime, model.Latency, model.URI, model.Key.GetPKIXPublicKeyBytes(), model.Root.GetPKIXPublicKeyBytes(), model.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ func New() (*SelfInfoDBConfig, error) {
|
|||
func (cfg *SelfInfoDBConfig) Add(model *core.SelfInfoDB) (_ sql.Result, err error) {
|
||||
query := "INSERT OR REPLACE INTO self_info (Key, RoutingEntries) VALUES (?, ?)"
|
||||
result, err := cfg.DbConfig.DB.Exec(query,
|
||||
model.KeyBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.RoutingEntries)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -61,7 +61,7 @@ func (cfg *SelfInfoDBConfig) Update(model *core.SelfInfoDB) (err error) {
|
|||
Key = ?
|
||||
WHERE
|
||||
Id = ?`,
|
||||
model.RoutingEntries, model.KeyBytes, model.Id)
|
||||
model.RoutingEntries, model.Key.GetPKIXPublicKeyBytes(), model.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -84,11 +84,13 @@ func (cfg *SelfInfoDBConfig) Get(model *core.SelfInfoDB) (_ *sql.Rows, err error
|
|||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var _key []byte
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&model.RoutingEntries, &model.KeyBytes)
|
||||
err = rows.Scan(&model.RoutingEntries, &_key)
|
||||
if err != nil {
|
||||
return rows, err
|
||||
}
|
||||
model.Key.ParsePKIXPublicKey(&_key)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ func New() (*SessionInfoDBConfig, error) {
|
|||
func (cfg *SessionInfoDBConfig) Add(model *core.SessionInfoDB) (_ sql.Result, err error) {
|
||||
query := "INSERT INTO session_info (Key, RXBytes, TXBytes, Duration) VALUES (?, ?, ?, ?)"
|
||||
result, err := cfg.DbConfig.DB.Exec(query,
|
||||
model.KeyBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.RXBytes,
|
||||
model.TXBytes,
|
||||
model.Uptime,
|
||||
|
@ -77,7 +77,7 @@ func (cfg *SessionInfoDBConfig) Update(model *core.SessionInfoDB) (err error) {
|
|||
Key = ?
|
||||
WHERE
|
||||
Id = ?`,
|
||||
model.RXBytes, model.TXBytes, model.Uptime, model.KeyBytes, model.Id)
|
||||
model.RXBytes, model.TXBytes, model.Uptime, model.Key.GetPKIXPublicKeyBytes(), model.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -92,11 +92,13 @@ func (cfg *SessionInfoDBConfig) Get(model *core.SessionInfoDB) (_ *sql.Rows, err
|
|||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var _key []byte
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&model.RXBytes, &model.TXBytes, &model.Uptime, &model.KeyBytes)
|
||||
err = rows.Scan(&model.RXBytes, &model.TXBytes, &model.Uptime, &_key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
model.Key.ParsePKIXPublicKey(&_key)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
|
|
@ -42,8 +42,8 @@ func New() (*TreeEntryInfoDBConfig, error) {
|
|||
func (cfg *TreeEntryInfoDBConfig) Add(model *core.TreeEntryInfoDB) (_ sql.Result, err error) {
|
||||
query := "INSERT INTO tree_entry_info (Key, Parent, Sequence) VALUES (?, ?, ?)"
|
||||
result, err := cfg.DbConfig.DB.Exec(query,
|
||||
model.KeyBytes,
|
||||
model.ParentBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Parent.GetPKIXPublicKeyBytes(),
|
||||
model.Sequence,
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -74,7 +74,7 @@ func (cfg *TreeEntryInfoDBConfig) Update(model *core.TreeEntryInfoDB) (err error
|
|||
Parent = ?
|
||||
WHERE
|
||||
Id = ?`,
|
||||
model.Sequence, model.KeyBytes, model.ParentBytes, model.Id)
|
||||
model.Sequence, model.Key.GetPKIXPublicKeyBytes(), model.Parent.GetPKIXPublicKeyBytes(), model.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -89,11 +89,15 @@ func (cfg *TreeEntryInfoDBConfig) Get(model *core.TreeEntryInfoDB) (_ *sql.Rows,
|
|||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var _key []byte
|
||||
var _path []byte
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&model.Sequence, &model.KeyBytes, &model.ParentBytes)
|
||||
err = rows.Scan(&model.Sequence, &_key, &_path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
model.Key.ParsePKIXPublicKey(&_key)
|
||||
model.Parent.ParsePKIXPublicKey(&_path)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package db_test
|
|||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
@ -21,12 +22,11 @@ func TestPeerGetCoords(t *testing.T) {
|
|||
peerinfo := core.PeerInfo{
|
||||
Coords: []uint64{1, 2, 3, 4},
|
||||
}
|
||||
peer := core.PeerInfoDB{
|
||||
PeerInfo: peerinfo,
|
||||
}
|
||||
peer, err := core.NewPeerInfoDB(peerinfo)
|
||||
require.NoError(t, err)
|
||||
|
||||
target := []byte{1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0}
|
||||
coordinates := core.ConvertToByteSlise(peer.Coords)
|
||||
coordinates := peer.Coords.ConvertToByteSliсe()
|
||||
if !reflect.DeepEqual(target, coordinates) {
|
||||
t.Error(fmt.Errorf("Not equal"))
|
||||
}
|
||||
|
@ -34,15 +34,12 @@ func TestPeerGetCoords(t *testing.T) {
|
|||
|
||||
func TestPeerSetCoords(t *testing.T) {
|
||||
peerinfo := core.PeerInfo{}
|
||||
peer := core.PeerInfoDB{
|
||||
PeerInfo: peerinfo,
|
||||
}
|
||||
var err error
|
||||
peer.CoordsBytes = []byte{4, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}
|
||||
peer.Coords, err = core.ConvertToUintSlise(peer.CoordsBytes)
|
||||
peer, err := core.NewPeerInfoDB(peerinfo)
|
||||
require.NoError(t, err)
|
||||
peer.Coords.ParseByteSliсe([]byte{4, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0})
|
||||
require.NoError(t, err)
|
||||
coords := []uint64{4, 3, 2, 1}
|
||||
if !reflect.DeepEqual(coords, peer.Coords) {
|
||||
if !reflect.DeepEqual(coords, peer.Coords.ConvertToUintSliсe()) {
|
||||
t.Error(fmt.Errorf("Not equal"))
|
||||
}
|
||||
}
|
||||
|
@ -83,11 +80,11 @@ func TestAddPeer(t *testing.T) {
|
|||
peer.URI,
|
||||
peer.Up,
|
||||
peer.Inbound,
|
||||
peer.PeerErr,
|
||||
peer.Error.GetErrorMessage(),
|
||||
peer.LastErrorTime,
|
||||
peer.KeyBytes,
|
||||
peer.RootBytes,
|
||||
peer.CoordsBytes,
|
||||
peer.Key.GetPKIXPublicKeyBytes(),
|
||||
peer.Root.GetPKIXPublicKeyBytes(),
|
||||
peer.Coords.ConvertToByteSliсe(),
|
||||
peer.Port,
|
||||
peer.Priority,
|
||||
peer.RXBytes,
|
||||
|
@ -182,9 +179,9 @@ func TestGetPeer(t *testing.T) {
|
|||
|
||||
rows := sqlmock.NewRows([]string{"up", "inbound", "last_error", "last_error_time", "coords",
|
||||
"port", "priority", "Rxbytes", "Txbytes", "uptime", "latency", "uri", "key", "root"}).
|
||||
AddRow(peer.Up, peer.Inbound, peer.PeerErr, peer.LastErrorTime, peer.CoordsBytes,
|
||||
AddRow(peer.Up, peer.Inbound, peer.Error.GetErrorMessage(), peer.LastErrorTime, peer.Coords.ConvertToByteSliсe(),
|
||||
peer.Port, peer.Priority, peer.RXBytes, peer.TXBytes, peer.Uptime, peer.Latency,
|
||||
peer.URI, peer.KeyBytes, peer.RootBytes)
|
||||
peer.URI, peer.Key.GetPKIXPublicKeyBytes(), peer.Root.GetPKIXPublicKeyBytes())
|
||||
|
||||
mock.ExpectQuery("SELECT (.+) FROM peer_infos WHERE Id = \\?").
|
||||
WithArgs(peer.Id).
|
||||
|
@ -247,8 +244,8 @@ func TestUpdatePeer(t *testing.T) {
|
|||
WHERE
|
||||
Id = \?`).
|
||||
WithArgs(
|
||||
peer.Up, peer.Inbound, peer.PeerErr, peer.LastErrorTime, peer.CoordsBytes, peer.Port, peer.Priority,
|
||||
peer.RXBytes, peer.TXBytes, peer.Uptime, peer.Latency, peer.URI, peer.KeyBytes, peer.RootBytes, peer.Id).
|
||||
peer.Up, peer.Inbound, peer.Error.GetErrorMessage(), peer.LastErrorTime, peer.Coords.ConvertToByteSliсe(), peer.Port, peer.Priority,
|
||||
peer.RXBytes, peer.TXBytes, peer.Uptime, peer.Latency, peer.URI, peer.Key.GetPKIXPublicKeyBytes(), peer.Root.GetPKIXPublicKeyBytes(), peer.Id).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
err = cfg.Update(peer)
|
||||
|
@ -297,7 +294,6 @@ func TestMain(t *testing.T) {
|
|||
}
|
||||
peer, err := core.NewPeerInfoDB(peerinfo)
|
||||
require.NoError(t, err)
|
||||
|
||||
root2PubKey, _, err := ed25519.GenerateKey(rand.Reader)
|
||||
require.NoError(t, err)
|
||||
peerinfo2 := core.PeerInfo{
|
||||
|
@ -318,7 +314,6 @@ func TestMain(t *testing.T) {
|
|||
}
|
||||
peer2, err := core.NewPeerInfoDB(peerinfo2)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = peerdb.Add(peer)
|
||||
require.NoError(t, err)
|
||||
_, err = peerdb.Add(peer2)
|
||||
|
@ -340,20 +335,40 @@ func TestMain(t *testing.T) {
|
|||
|
||||
require.Condition(t, condition, "Expected count to be 1", count)
|
||||
|
||||
peer2.Latency = 10
|
||||
peer2.URI = "test"
|
||||
peer2.Up = false
|
||||
peer2.Inbound = false
|
||||
peer2.Error.ParseMessage("Test")
|
||||
peer2.LastErrorTime = sql.NullTime{}
|
||||
peer2.Key.MarshalPKIXPublicKey(&root2PubKey)
|
||||
peer2.Root.MarshalPKIXPublicKey(&pubkey)
|
||||
peer2.Coords.ParseUint64Sliсe([]uint64{1, 1, 1, 1})
|
||||
peer2.Port = 80
|
||||
peer2.Priority = 2
|
||||
peer2.RXBytes = 1024
|
||||
peer2.TXBytes = 1024
|
||||
peer2.Port = 80
|
||||
peer2.Uptime = 1000
|
||||
peer2.Latency = 10
|
||||
err = peerdb.Update(peer2)
|
||||
require.NoError(t, err)
|
||||
_, err = peerdb.Get(peer2)
|
||||
require.NoError(t, err)
|
||||
|
||||
condition = func() bool {
|
||||
return peer2.Latency == 10 &&
|
||||
return peer2.URI == "test" &&
|
||||
peer2.Up == false &&
|
||||
peer2.Inbound == false &&
|
||||
peer2.Error.GetErrorMessage() == "Test" &&
|
||||
peer2.LastErrorTime.Time.IsZero() &&
|
||||
peer2.Key.GetPKIXPublicKey().Equal(root2PubKey) &&
|
||||
peer2.Root.GetPKIXPublicKey().Equal(pubkey) &&
|
||||
reflect.DeepEqual(peer2.Coords.ConvertToUintSliсe(), []uint64{1, 1, 1, 1}) &&
|
||||
peer2.Port == 80 &&
|
||||
peer2.Priority == 2 &&
|
||||
peer2.RXBytes == 2048 &&
|
||||
peer2.TXBytes == 3072 &&
|
||||
peer2.Port == 80 && peer2.URI == "new.test" && peer2.Key.Equal(pubkey)
|
||||
peer2.Uptime == 4600 &&
|
||||
peer2.Latency == 10
|
||||
}
|
||||
|
||||
require.Condition(t, condition, "Inner exception")
|
||||
|
|
|
@ -36,7 +36,7 @@ func TestSelectPathEntryInfo(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"Sequence", "Key", "Path"}).
|
||||
AddRow(100, model.KeyBytes, model.PathBytes)
|
||||
AddRow(100, model.Key.GetPKIXPublicKeyBytes(), model.Path.ConvertToByteSliсe())
|
||||
|
||||
mock.ExpectQuery("SELECT (.+) FROM path_entry_info WHERE Id = \\?").
|
||||
WithArgs(model.Id).
|
||||
|
@ -72,8 +72,8 @@ func TestInsertPathEntryInfo(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
mock.ExpectExec("INSERT INTO path_entry_info").
|
||||
WithArgs(
|
||||
model.KeyBytes,
|
||||
model.PathBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Path.ConvertToByteSliсe(),
|
||||
model.Sequence,
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
@ -145,8 +145,8 @@ func TestUpdatePathEntryInfo(t *testing.T) {
|
|||
Id = \?`).
|
||||
WithArgs(
|
||||
model.Sequence,
|
||||
model.KeyBytes,
|
||||
model.PathBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Path.ConvertToByteSliсe(),
|
||||
model.Id,
|
||||
).WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ func TestSelectSelfInfo(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"RoutingEntries", "Key"}).
|
||||
AddRow(100, model.KeyBytes)
|
||||
AddRow(100, model.Key.GetPKIXPublicKeyBytes())
|
||||
|
||||
mock.ExpectQuery("SELECT (.+) FROM self_info WHERE Id = \\?").
|
||||
WithArgs(model.Id).
|
||||
|
@ -68,7 +68,7 @@ func TestInsertSelfInfo(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
mock.ExpectExec("INSERT OR REPLACE INTO self_info").
|
||||
WithArgs(
|
||||
model.KeyBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.RoutingEntries,
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
@ -136,7 +136,7 @@ func TestUpdateSelfInfo(t *testing.T) {
|
|||
Id = \?`).
|
||||
WithArgs(
|
||||
model.RoutingEntries,
|
||||
model.KeyBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Id,
|
||||
).WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ func TestSelectSessionInfo(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"RXBytes", "TXBytes", "Duration", "Key"}).
|
||||
AddRow(100, 200, 100, model.KeyBytes)
|
||||
AddRow(100, 200, 100, model.Key.GetPKIXPublicKeyBytes())
|
||||
|
||||
mock.ExpectQuery("SELECT (.+) FROM session_info WHERE Id = \\?").
|
||||
WithArgs(model.Id).
|
||||
|
@ -75,7 +75,7 @@ func TestInsertSessionInfo(t *testing.T) {
|
|||
|
||||
mock.ExpectExec("INSERT INTO session_info").
|
||||
WithArgs(
|
||||
model.KeyBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.RXBytes,
|
||||
model.TXBytes,
|
||||
model.Uptime,
|
||||
|
@ -154,7 +154,7 @@ func TestUpdateSessionInfo(t *testing.T) {
|
|||
model.RXBytes,
|
||||
model.TXBytes,
|
||||
model.Uptime,
|
||||
model.KeyBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Id,
|
||||
).WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ func TestSelectTreeEntryInfo(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
rows := sqlmock.NewRows([]string{"Sequence", "Key", "Parent"}).
|
||||
AddRow(100, model.KeyBytes, model.ParentBytes)
|
||||
AddRow(100, model.Key.GetPKIXPublicKeyBytes(), model.Parent.GetPKIXPublicKeyBytes())
|
||||
|
||||
mock.ExpectQuery("SELECT (.+) FROM tree_entry_info WHERE Id = \\?").
|
||||
WithArgs(model.Id).
|
||||
|
@ -73,8 +73,8 @@ func TestInsertTreeEntryInfo(t *testing.T) {
|
|||
|
||||
mock.ExpectExec("INSERT INTO tree_entry_info").
|
||||
WithArgs(
|
||||
model.KeyBytes,
|
||||
model.ParentBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Parent.GetPKIXPublicKeyBytes(),
|
||||
model.Sequence,
|
||||
).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
@ -146,8 +146,8 @@ func TestUpdateTreeEntryInfo(t *testing.T) {
|
|||
Id = \?`).
|
||||
WithArgs(
|
||||
model.Sequence,
|
||||
model.KeyBytes,
|
||||
model.ParentBytes,
|
||||
model.Key.GetPKIXPublicKeyBytes(),
|
||||
model.Parent.GetPKIXPublicKeyBytes(),
|
||||
model.Id,
|
||||
).WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue