From 1304e044aa87a65145bf8b6d4bc141586556c0ed Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Tue, 11 Nov 2014 14:31:49 -0500 Subject: [PATCH] 2853: Add tests for reference set and some edge cases. --- sdk/go/keepclient/root_sorter_test.go | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sdk/go/keepclient/root_sorter_test.go diff --git a/sdk/go/keepclient/root_sorter_test.go b/sdk/go/keepclient/root_sorter_test.go new file mode 100644 index 0000000000..0d45e908ed --- /dev/null +++ b/sdk/go/keepclient/root_sorter_test.go @@ -0,0 +1,61 @@ +package keepclient + +import ( + "crypto/md5" + "fmt" + . "gopkg.in/check.v1" + "strconv" + "strings" +) + +type RootSorterSuite struct{} +var _ = Suite(&RootSorterSuite{}) + +func FakeSvcRoot(i uint64) (string) { + return fmt.Sprintf("https://%x.svc/", i) +} + +func FakeSvcUuid(i uint64) (string) { + return fmt.Sprintf("zzzzz-bi6l4-%015x", i) +} + +func FakeServiceRoots(n uint64) (map[string]string) { + sr := map[string]string{} + for i := uint64(0); i < n; i ++ { + sr[FakeSvcUuid(i)] = FakeSvcRoot(i) + } + return sr +} + +func Md5String(data string) (string) { + return fmt.Sprintf("%032x", md5.Sum([]byte(data))) +} + +func (*RootSorterSuite) EmptyRoots(c *C) { + rs := NewRootSorter(map[string]string{}, Md5String("foo")) + c.Check(rs.GetSortedRoots(), Equals, []string{}) +} + +func (*RootSorterSuite) JustOneRoot(c *C) { + rs := NewRootSorter(FakeServiceRoots(1), Md5String("foo")) + c.Check(rs.GetSortedRoots(), Equals, []string{FakeSvcRoot(0)}) +} + +func (*RootSorterSuite) ReferenceSet(c *C) { + fakeroots := FakeServiceRoots(16) + expected_orders := []string{ + "3eab2d5fc9681074", + "097dba52e648f1c3", + "c5b4e023f8a7d691", + "9d81c02e76a3bf54", + } + for h, expected_order := range expected_orders { + hash := Md5String(fmt.Sprintf("%064x", h)) + roots := NewRootSorter(fakeroots, hash).GetSortedRoots() + for i, svc_id_s := range strings.Split(expected_order, "") { + svc_id, err := strconv.ParseUint(svc_id_s, 16, 64) + c.Assert(err, Equals, nil) + c.Check(roots[i], Equals, FakeSvcRoot(svc_id)) + } + } +} -- 2.39.5