Skip to content
Permalink
Browse files

enabled longer factomd sizes

  • Loading branch information...
Emyrk committed Feb 8, 2017
1 parent 947a073 commit 6cbc87cce555b34833e9977fa8536d47eb8c4801
@@ -55,7 +55,7 @@ function execWalletd() {
}
});*/
console.log("Running as Windows")
walletd = spawn(path.join(__dirname, PATH_TO_BIN + 'enterprise-wallet.exe'),['-txDB=LDB'])
walletd = spawn(path.join(__dirname, PATH_TO_BIN + 'enterprise-wallet.exe'),[])
} else {
/*walletd = exec(path.join(__dirname, PATH_TO_BIN + 'enterprise-wallet -port=' + PORT_TO_SERVE), function callback(error, stdout, stderr){
console.log(stdout)
@@ -127,9 +127,11 @@ func (s *SettingsStruct) MarshalBinary() ([]byte, error) {

buf.Write(b)

var n [MAX_FACTOMDLOCATION_SIZE]byte
copy(n[:MAX_FACTOMDLOCATION_SIZE], s.FactomdLocation)
buf.Write(n[:MAX_FACTOMDLOCATION_SIZE])
data, err := MarshalStringToBytes(s.FactomdLocation, MAX_FACTOMDLOCATION_SIZE)
if err != nil {
return nil, err
}
buf.Write(data)

return buf.Next(buf.Len()), nil
}
@@ -184,13 +186,24 @@ func (s *SettingsStruct) UnmarshalBinaryData(data []byte) (newData []byte, err e
}
newData = newData[5:]

// Need to add a fix to unmarshal data of older databases.
if len(newData) == 0 { // Old database type
switch {
case len(newData) == 0: // v1 : No settings
s.FactomdLocation = "localhost:8088" // Will be overwritten if changed anyhow
} else {
nameData := bytes.Trim(newData[:MAX_FACTOMDLOCATION_SIZE], "\x00")
case len(newData) == 30 && bytes.Compare(newData[28:30], []byte{0x00, 0x00}) == 0: // v2 : Settings at length 30
//end := MAX_FACTOMDLOCATION_SIZE
nameData := bytes.Trim(newData[:30], "\x00")
s.FactomdLocation = fmt.Sprintf("%s", nameData)
newData = newData[MAX_FACTOMDLOCATION_SIZE:]
if s.FactomdLocation == "" {
s.FactomdLocation = "localhost:8088"
}
newData = newData[30:]
default: // current
var loc string
loc, newData, err = UnmarshalStringFromBytesData(newData, MAX_FACTOMDLOCATION_SIZE)
if err != nil {
return data, err
}
s.FactomdLocation = loc
}

return
@@ -7,6 +7,7 @@ import (
"testing"

. "github.com/FactomProject/enterprise-wallet"
"github.com/FactomProject/factomd/common/primitives/random"
)

var _ = fmt.Sprintf("")
@@ -94,6 +95,61 @@ func MarshalSettingAndGetNewUnmarshaled(a *SettingsStruct) (*SettingsStruct, err
return n, nil
}

func TestVersionChange(t *testing.T) {
dataStr := []string{
"66616c736566616c736566616c736566616c7365000000000000000000000000000000000000000000000000000000000000",
"66616c7365747275650066616c736566616c7365000000000000000000000000000000000000000000000000000000000000",
"66616c7365747275650066616c73657472756500000000000000000000000000000000000000000000000000000000000000",
"66616c7365747275650074727565007472756500000000000000000000000000000000000000000000000000000000000000",
"66616c7365747275650074727565007472756500416e6f746865722074797065206f6620737472696e670000000000000000",
"66616c7365747275650074727565007472756500",
"66616c736566616c736566616c736566616c736548656c6c6f2100"}

for _, s := range dataStr {
se := new(SettingsStruct)
data, err := hex.DecodeString(s)
if err != nil {
t.Error(err)
}

data, err = se.UnmarshalBinaryData(data)
if err != nil {
t.Error(err)
}

if len(data) != 0 {
t.Errorf("Should be length 0, found %d", len(data))
}
}

for i := 0; i < 1000; i++ {
se := new(SettingsStruct)
str := random.RandomString()
max := random.RandIntBetween(0, MAX_FACTOMDLOCATION_SIZE)
if len(str) > max {
str = str[:max]
}

se.FactomdLocation = str

data, err := se.MarshalBinary()
if err != nil {
t.Error(err)
}

sa := new(SettingsStruct)
data, err = sa.UnmarshalBinaryData(data)
if !sa.IsSameAs(se) {
t.Error("Should be same")
}

if len(data) != 0 {
t.Errorf("Data length should be 0, found %d", len(data))
}
}

}

// Cannot really test to verify the data, will just test if they don't fail
func TestHandlers(t *testing.T) {
MasterSettings = new(SettingsStruct)
60 util.go
@@ -0,0 +1,60 @@
package main

import (
"fmt"
)

func MarshalStringToBytes(str string, maxlength int) ([]byte, error) {
if len(str) > maxlength {
return nil, fmt.Errorf("Length of string is too long, found length is %d, max length is %d",
len(str), maxlength)
}

data := []byte(str)
for i := 0; i < len(data); i++ {
if data[i] == 0x00 {
// Naughty, Naughty, Naughty
data[i] = 0x01
}
}
data = append(data, 0x00)

return data, nil
}

func UnmarshalStringFromBytes(data []byte, maxlength int) (resp string, err error) {
resp, _, err = UnmarshalStringFromBytesData(data, maxlength)
return
}

func UnmarshalStringFromBytesData(data []byte, maxlength int) (resp string, newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("A panic has occurred while unmarshaling: %s", r)
return
}
}()

newData = data
end := -1
if len(data)-1 < maxlength {
maxlength = len(data) - 1
}

for i := 0; i <= maxlength; i++ {
if newData[i] == 0x00 {
// found null terminator
end = i
break
}
}

if end == -1 {
err = fmt.Errorf("Could not find a 0x00 byte before max length + 1")
return
}

resp = string(newData[:end])
newData = newData[end+1:]
return
}
@@ -0,0 +1,59 @@
package main_test

import (
"math/rand"
"testing"
"time"

. "github.com/FactomProject/enterprise-wallet"
"github.com/FactomProject/factomd/common/primitives/random"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func TestMarshal(t *testing.T) {
for i := 0; i < 1000; i++ {
str := random.RandomString()
max := random.RandIntBetween(0, 100)
if max < len(str) {
str = str[:max]
}

data, err := MarshalStringToBytes(str, max)
if err != nil {
t.Error(err)
}

resp, data, err := UnmarshalStringFromBytesData(data, max)
if err != nil {
t.Error(err)
}

if resp != str {
t.Error("Unmarshal Fail")
}

if len(data) != 0 {
t.Error("Unmarshal Return Data")
}
}

str := "123456"

data, err := MarshalStringToBytes(str, 2)
if err == nil {
t.Error("Should error")
}

data, err = MarshalStringToBytes(str, 10)
if err != nil {
t.Error(err)
}

_, _, err = UnmarshalStringFromBytesData(data, 2)
if err == nil {
t.Error("should error")
}
}

Large diffs are not rendered by default.

0 comments on commit 6cbc87c

Please sign in to comment.
You can’t perform that action at this time.