Skip to content

Commit

Permalink
Fixing unit tests and some source formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fredli74 committed Dec 8, 2016
1 parent 0bcf3b4 commit a7c2829
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
4 changes: 2 additions & 2 deletions core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
type HashboxBlock struct {
BlockID Byte128 // = md5( LinkLength Links DataLength Data )
Links []Byte128 // Array of BlockIDs
DataType uint8 // 1 byte data type
DataType uint8 // 1 byte data type

Data bytearray.ByteArray

Expand Down Expand Up @@ -81,7 +81,7 @@ func (b *HashboxBlock) Unserialize(r io.Reader) (size int) {
size += b.Links[i].Unserialize(r)
}
size += ReadUint8(r, &b.DataType)
size += ReadUint32(r, &n) // Data length
size += ReadUint32(r, &n) // Data length
size += CopyNOrPanic(&b.Data, r, int(n))

b.Compressed = true
Expand Down
2 changes: 1 addition & 1 deletion core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewClient(conn net.Conn, account string, accesskey Byte128) *Client {
go client.ioHandler()

{ // Say hello
r := client.dispatchAndWait(MsgTypeGreeting, &MsgClientGreeting{ Version: ProtocolVersion }).(*MsgServerGreeting)
r := client.dispatchAndWait(MsgTypeGreeting, &MsgClientGreeting{Version: ProtocolVersion}).(*MsgServerGreeting)
client.SessionNonce = r.SessionNonce
client.GenerateSessionKey(client.AccessKey)
}
Expand Down
18 changes: 10 additions & 8 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ func DeepHmac(depth int, data []byte, key Byte128) Byte128 {
return hash
}

type Uint32 uint32
type Uint32 uint32

func (m Uint32) Serialize(w io.Writer) (size int) {
return WriteUint32(w, uint32(m))
}
func (m *Uint32) Unserialize(r io.Reader) (size int) {
func (m *Uint32) Unserialize(r io.Reader) (size int) {
var l uint32
size += ReadUint32(r, &l)
*m = Uint32(l)
Expand Down Expand Up @@ -144,8 +145,8 @@ func (a *DatasetArray) Unserialize(r io.Reader) (size int) {
*a = A
return
}
func (a DatasetArray) Len() int { return len(a) }
func (a DatasetArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a DatasetArray) Len() int { return len(a) }
func (a DatasetArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a DatasetArray) Less(i, j int) bool { return string(a[i].Name) < string(a[j].Name) }

// DatasetState stores a specific state (snapshot) of a Dataset.
Expand Down Expand Up @@ -173,13 +174,14 @@ func (m *DatasetState) Unserialize(r io.Reader) (size int) {
}

const ( // 1 byte, max 8 flags
StateFlagInvalid = 1 << 7 // Dataset State is invalid
StateFlagInvalid = 1 << 7 // Dataset State is invalid
)

type DatasetStateEntry struct {
StateFlags uint8 // 1 byte Dataset state flags
State DatasetState
StateFlags uint8 // 1 byte Dataset state flags
State DatasetState
}

func (e DatasetStateEntry) Serialize(w io.Writer) (size int) {
size += WriteUint8(w, e.StateFlags)
size += e.State.Serialize(w)
Expand Down
2 changes: 1 addition & 1 deletion core/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *Session) GenerateSessionKey(AccessKey Byte128) {
//

const (
ProtocolVersion Uint32 = 1
ProtocolVersion Uint32 = 1

MsgTypeOldGreeting uint32 = 0x686F6C61 // = "hola"
MsgTypeGreeting uint32 = 0x68616C6F // = "halo"
Expand Down
14 changes: 8 additions & 6 deletions core/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func randomUint32() Uint32 {
return Uint32( rand.Uint32())
return Uint32(rand.Uint32())
}
func randomByte128() (b Byte128) {
for i := 0; i < 16; i++ {
Expand Down Expand Up @@ -208,12 +208,14 @@ func TestMessageSerialization(t *testing.T) {

if ok, dump := protocolPipeCompare(MsgTypeListDataset&MsgTypeServerMask, &MsgServerListDataset{
States: DatasetStateArray{
DatasetState{
DatasetStateEntry{
StateFlags: uint8(rand.Uint32()),
StateID: randomByte128(),
BlockID: randomByte128(),
Size: rand.Int63(),
UniqueSize: rand.Int63(),
State: DatasetState{
StateID: randomByte128(),
BlockID: randomByte128(),
Size: rand.Int63(),
UniqueSize: rand.Int63(),
},
},
},
ListH: randomByte128()}); !ok {
Expand Down
10 changes: 5 additions & 5 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ReadBytes(r io.Reader, data []byte) int {
panic(err)
} else {
return n
}
}
}
func ReadUint8(r io.Reader, data *uint8) int {
var b [1]byte
Expand Down Expand Up @@ -61,15 +61,15 @@ func WriteUint8(w io.Writer, data uint8) int {
return WriteBytes(w, b[:])
}
func WriteUint16(w io.Writer, data uint16) int {
var b [2]byte = [2]byte{byte(data >> 8),byte(data)}
var b [2]byte = [2]byte{byte(data >> 8), byte(data)}
return WriteBytes(w, b[:])
}
func WriteUint32(w io.Writer, data uint32) int {
var b [4]byte = [4]byte{byte(data >> 24),byte(data >> 16),byte(data >> 8),byte(data)}
var b [4]byte = [4]byte{byte(data >> 24), byte(data >> 16), byte(data >> 8), byte(data)}
return WriteBytes(w, b[:])
}
func WriteInt64(w io.Writer, data int64) int {
var b [8]byte = [8]byte{byte(data >> 56),byte(data >> 48),byte(data >> 40),byte(data>>32),byte(data >> 24),byte(data >> 16),byte(data >> 8),byte(data)}
var b [8]byte = [8]byte{byte(data >> 56), byte(data >> 48), byte(data >> 40), byte(data >> 32), byte(data >> 24), byte(data >> 16), byte(data >> 8), byte(data)}
return WriteBytes(w, b[:])
}

Expand Down Expand Up @@ -102,7 +102,7 @@ var logMarks []string = []string{"!", "*", ".", "(", "?"}

func Log(level int, format string, a ...interface{}) {
if level <= LogLevel {
fmt.Printf("%s %s " + format + "\n", append([]interface{}{time.Now().UTC().Format(LOGTIMEFORMAT), logMarks[level]}, a...)...)
fmt.Printf("%s %s "+format+"\n", append([]interface{}{time.Now().UTC().Format(LOGTIMEFORMAT), logMarks[level]}, a...)...)
}
}

Expand Down
2 changes: 1 addition & 1 deletion hashback/hashback.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ func main() {
fmt.Printf("Backup %s %x (%s) completed\n", cmd.Args[2], session.State.StateID[:], date.Format(time.RFC3339))
} else {
list := session.Client.ListDataset(cmd.Args[2])
for i := len(list.States)-1; i >= 0; i-- {
for i := len(list.States) - 1; i >= 0; i-- {
if list.States[i].StateFlags&core.StateFlagInvalid != core.StateFlagInvalid {
latestBackup = binary.BigEndian.Uint64(list.States[i].State.StateID[:])
break
Expand Down
4 changes: 2 additions & 2 deletions hashback/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,13 @@ func (session *BackupSession) Store(datasetName string, path ...string) {
// Load up last backup into the reference engine
if !session.FullBackup {
list := session.Client.ListDataset(datasetName)
found := len(list.States)-1
found := len(list.States) - 1
for ; found >= 0; found-- {
if list.States[found].StateFlags&core.StateFlagInvalid != core.StateFlagInvalid {
break
}
}
if (found >= 0) {
if found >= 0 {
session.reference.start(&list.States[found].State.BlockID)
} else {
session.reference.start(nil)
Expand Down
4 changes: 2 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ func TestClientServerDataset(t *testing.T) {
keepstate := core.DatasetState{StateID: stateID, BlockID: blockID, Size: 5, UniqueSize: 42}

client.AddDatasetState("testset", keepstate)
array = append(array, keepstate)
array = append(array, core.DatasetStateEntry{State: keepstate})
copy(stateID[:], []byte("testD"))
keepstate = core.DatasetState{StateID: stateID, BlockID: blockID, Size: 125, UniqueSize: 33342}
client.AddDatasetState("testset", keepstate)
array = append(array, keepstate)
array = append(array, core.DatasetStateEntry{State: keepstate})

copy(stateID[:], []byte("testA"))
client.AddDatasetState("testset", core.DatasetState{StateID: stateID, BlockID: blockID, Size: 5, UniqueSize: 42})
Expand Down

0 comments on commit a7c2829

Please sign in to comment.