Skip to content

Commit

Permalink
Added fix to not to write on the old p2p.save file but recreate a new…
Browse files Browse the repository at this point in the history
… instead and added tests for this fix
  • Loading branch information
azretkenzhaliev committed Apr 19, 2018
1 parent 997b314 commit c0fe151
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
17 changes: 16 additions & 1 deletion instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,26 @@ func (p *InstanceList) decodeInstances(data []byte) ([]RunArgs, error) {
// Calls encodeInstances() and saves results into specified file
// Return number of bytes written and error if any
func (p *InstanceList) saveInstances(filename string) (int, error) {
file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0700)
file, err := os.Open(filename)
if err == nil {
file.Close()
os.Remove(filename)
}
file, err = os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0700)
if err != nil {
return 0, err
}
defer file.Close()
stat, _ := file.Stat()
if stat.Size() > 0 {
auxiliary := make([]byte, 100000)
len, err := file.Read(auxiliary)
if err != nil {
return 0, err
}
auxiliary = bytes.Trim(auxiliary, "\x00")
return 0, fmt.Errorf("SaveFile was not empty: %+v %+v", len, bytes.NewBuffer(auxiliary).String())
}
data := p.encodeInstances()
s, err := file.Write(data)
if err != nil {
Expand Down
43 changes: 41 additions & 2 deletions instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"runtime"
"testing"
"os"
)

func TestOperate(t *testing.T) {
Expand Down Expand Up @@ -299,8 +300,46 @@ func TestSaveInstances(t *testing.T) {
instanceList.update("instance", P2Pinstance)
_, err := instanceList.saveInstances("/")
if err == nil {
t.Errorf("Failed to load instances (1): must have returned non-nil but returned nil")
t.Errorf("Failed to save instances (1): must have returned non-nil but returned nil")
}
_, err = instanceList.saveInstances("save-4.save")
if err != nil {
t.Errorf("Failed to save instances (2): %v", err)
}
file, err := os.Open("save-4.save")
if err != nil {
t.Errorf("Failed to save instances (3): %v", err)
}
auxiliary := make([]byte, 100000)
file.Read(auxiliary)
file.Close()
auxiliary = bytes.Trim(auxiliary, "\x00")
t.Log(bytes.NewBuffer(auxiliary).String())
P2PinstanceSecond := new(P2PInstance)
P2PinstanceSecond.Args.IP = "10.10.10.2"
P2PinstanceSecond.Args.Mac = "Mac"
P2PinstanceSecond.Args.Dev = "Dev"
P2PinstanceSecond.Args.Hash = "Hash"
P2PinstanceSecond.Args.Dht = "Dht"
P2PinstanceSecond.Args.Keyfile = "Keyfile"
P2PinstanceSecond.Args.Key = "Key"
P2PinstanceSecond.Args.TTL = "TTL"
P2PinstanceSecond.Args.Fwd = false
P2PinstanceSecond.Args.Port = 0
instanceList.update("instanceSecond", P2PinstanceSecond)
_, err = instanceList.saveInstances("save-4.save")
if err != nil {
t.Errorf("Failed to save instances (4): %v", err)
}
file, err = os.Open("save-4.save")
if err != nil {
t.Errorf("Failed to save instances (5): %v", err)
}
auxiliary = make([]byte, 100000)
file.Read(auxiliary)
file.Close()
auxiliary = bytes.Trim(auxiliary, "\x00")
t.Log(bytes.NewBuffer(auxiliary).String())
}

func TestLoadInstances(t *testing.T) {
Expand Down Expand Up @@ -331,7 +370,7 @@ func TestInitialize(t *testing.T) {
daemon := new(Daemon)
daemon.Initialize("saveFile")
if daemon.SaveFile != "saveFile" {
t.Errorf("Failed to load initialize (1): daemon couldn't initialize")
t.Errorf("Failed to initialize (1): daemon couldn't initialize")
}
}

Expand Down

0 comments on commit c0fe151

Please sign in to comment.