Move to tests

Some util functions
This commit is contained in:
Oskar Thoren 2019-04-18 11:26:56 +08:00
parent 84e18c6afd
commit 3c2933f081
No known key found for this signature in database
GPG Key ID: 33AAFB33580C538E
2 changed files with 99 additions and 3 deletions

View File

@ -50,6 +50,18 @@ func (t TestContent) Equals(other merkletree.Content) (bool, error) {
return t.x == other.(TestContent).x, nil
}
func toHex(merkletree []byte) string {
return hex.EncodeToString(merkletree)
}
func fromHex(merkletree string) []byte {
res, err := hex.DecodeString(merkletree)
if err != nil {
log.Fatal(err)
}
return res
}
// Get merkle root from trusted location
// TODO: Implement location/method logic and groupID dispatching, hardcoding for now
@ -61,7 +73,10 @@ func (t TestContent) Equals(other merkletree.Content) (bool, error) {
// list = append(list, TestContent{x: "Hola"})
//
// t, err := merkletree.NewTree(list)
func getRoot(location string, groupID string) string {
// TODO: see how this can be used: mt.GetMerklePath
func getTrustedRoot(location string, groupID string) string {
return "5f30cc80133b9394156e24b233f0c4be32b24e44bb3381f02c7ba52619d0febc"
}
@ -76,7 +91,7 @@ func pull(location string, mr string, haves []string) []string {
if location == "byzantine" {
diff = append(diff, "xxx")
} else {
diff = append(diff, "Hola")
diff = append(diff, "4")
}
return diff
}
@ -122,7 +137,7 @@ func main() {
//log.Println(t)
// 1. Get trusted root
newMR := getRoot("", "")
newMR := getTrustedRoot("", "")
log.Println("Trusted root:", newMR)
// 2. Sync difference
@ -159,4 +174,9 @@ func main() {
log.Println("Untrusted root [Good case]:", untrustedRoot2)
log.Println("Good data [Good case]?", untrustedRoot2 == newMR)
// Some more cases, let's tease them out
// 1) No data, only trusted root, good feedback (enhancement: partial list of other childs)
// 2) Some data
}

View File

@ -0,0 +1,76 @@
package main
import "testing"
import "github.com/cbergoon/merkletree"
func TestBasicGood(t *testing.T) {
// Pre-compute expected tree
var list []merkletree.Content
list = append(list, TestContent{x: "1"})
list = append(list, TestContent{x: "2"})
list = append(list, TestContent{x: "3"})
list = append(list, TestContent{x: "4"})
mt, _ := merkletree.NewTree(list)
trustedRoot := toHex(mt.MerkleRoot())
// Local node setup, partial
// Assume has access to trustedRoot
var contents []merkletree.Content
contents = append(contents, TestContent{x: "1"})
contents = append(contents, TestContent{x: "2"})
contents = append(contents, TestContent{x: "3"})
// Byzantine case, currently sending nothing
// TODO: Since haves is empty it should return full contents?
// XXX: Doesn't make sense to have tree hardcoded in other code
var haves []string
untrustedPayloads := pull("good", trustedRoot, haves)
content := TestContent{x: untrustedPayloads[0]}
contents = append(contents, content)
// XXX: is there no way to append to tree?
untrusted := mt
_ = untrusted.RebuildTreeWith(contents)
untrustedRoot := toHex(untrusted.MerkleRoot())
expect := (untrustedRoot == trustedRoot)
if !expect {
t.Errorf("Good basic: Untrusted root %s and trusted root %s should match", untrustedRoot, trustedRoot)
}
}
func TestBasicByzantine(t *testing.T) {
// Pre-compute expected tree
var list []merkletree.Content
list = append(list, TestContent{x: "1"})
list = append(list, TestContent{x: "2"})
list = append(list, TestContent{x: "3"})
list = append(list, TestContent{x: "4"})
mt, _ := merkletree.NewTree(list)
trustedRoot := toHex(mt.MerkleRoot())
// Local node setup, partial
// Assume has access to trustedRoot
var contents []merkletree.Content
contents = append(contents, TestContent{x: "1"})
contents = append(contents, TestContent{x: "2"})
contents = append(contents, TestContent{x: "3"})
// Byzantine case, currently sending nothing
// TODO: Since haves is empty it should return full contents?
// XXX: Doesn't make sense to have tree hardcoded in other code
var haves []string
untrustedPayloads := pull("byzantine", trustedRoot, haves)
content := TestContent{x: untrustedPayloads[0]}
contents = append(contents, content)
// XXX: is there no way to append to tree?
untrusted := mt
_ = untrusted.RebuildTreeWith(contents)
untrustedRoot := toHex(untrusted.MerkleRoot())
expect := (untrustedRoot != trustedRoot)
if !expect {
t.Errorf("Byzantine basic: Untrusted root %s and trusted root %s shouldn't match", untrustedRoot, trustedRoot)
}
}