func (s *XLSuite) doTestMDParser(c *C, rng *xr.PRNG, whichSHA int) { var tHash []byte switch whichSHA { case xu.USING_SHA1: tHash = make([]byte, xu.SHA1_BIN_LEN) case xu.USING_SHA2: tHash = make([]byte, xu.SHA2_BIN_LEN) case xu.USING_SHA3: tHash = make([]byte, xu.SHA3_BIN_LEN) // DEFAULT = ERROR } rng.NextBytes(tHash) // not really a hash, of course sHash := hex.EncodeToString(tHash) // string form of tHash withoutSlash := rng.NextFileName(8) dirName := withoutSlash + "/" length := rng.Intn(4) var rSpaces string for i := 0; i < length; i++ { rSpaces += " " // on the right } // TEST FIRST LINE PARSER ----------------------------- line := sHash + " " + dirName + rSpaces treeHash2, dirName2, err := ParseMerkleDocFirstLine(line) c.Assert(err, IsNil) c.Assert(bytes.Equal(treeHash2, tHash), Equals, true) // we retain the terminating slash in MerkleDoc first lines c.Assert(dirName2, Equals, dirName) }
// Return an initialized and tested host, with a NodeID, ckPriv, // and skPriv. OpenAcc() is not called and so any acceptors are not open. func (s *XLSuite) makeHost(c *C, rng *xr.PRNG) *Node { // XXX names may not be unique name := rng.NextFileName(6) for { first := string(name[0]) if !strings.Contains(first, "0123456789") && !strings.Contains(name, "-") { break } name = rng.NextFileName(6) } id, err := makeNodeID(rng) c.Assert(err, Equals, nil) c.Assert(id, Not(IsNil)) lfs := "tmp/" + hex.EncodeToString(id.Value()) n, err := NewNew(name, id, lfs) c.Assert(err, IsNil) c.Assert(n, Not(IsNil)) c.Assert(name, Equals, n.GetName()) actualID := n.GetNodeID() c.Assert(true, Equals, id.Equal(actualID)) s.doKeyTests(c, n, rng) c.Assert(0, Equals, (*n).SizePeers()) c.Assert(0, Equals, (*n).SizeOverlays()) c.Assert(0, Equals, n.SizeConnections()) c.Assert(lfs, Equals, n.GetLFS()) return n }
func doTestSimpleConstructor(c *C, rng *xr.PRNG, usingSHA1 bool) { var sha hash.Hash if usingSHA1 { sha = sha1.New() } else { sha = sha256.New() } name := rng.NextFileName(8) n := rng.SomeBytes(8) sha.Write(n) hash0 := sha.Sum(nil) leaf0, err := NewNLHLeaf(name, hash0) c.Assert(err, IsNil) c.Assert(name, Equals, leaf0.Name()) c.Assert(hash0, Equals, leaf0.BinHash()) name2 := name for name2 == name { name2 = rng.NextFileName(8) } n = rng.SomeBytes(8) sha.Write(n) hash1 := sha.Sum(nil) leaf1, err := NewNLHLeaf(name2, hash1) c.Assert(err, IsNil) c.Assert(name2, Equals, leaf1.Name()) c.Assert(hash1, Equals, leaf1.BinHash()) c.Assert(leaf0, Equals, leaf0) c.Assert(leaf1, Equals, leaf1) c.Assert(leaf0.Equal(leaf1), Equals, false) }
func (s *XLSuite) makeHostAndKeys(c *C, rng *xr.PRNG) ( n *xn.Node, ckPriv, skPriv *rsa.PrivateKey) { // XXX names may not be unique name := rng.NextFileName(6) for { first := string(name[0]) if !strings.Contains(first, "0123456789") && !strings.Contains(name, "-") { break } name = rng.NextFileName(6) } id := s.makeANodeID(c, rng) lfs := "tmp/" + hex.EncodeToString(id.Value()) ckPriv = s.makeAnRSAKey(c) skPriv = s.makeAnRSAKey(c) n, err2 := xn.New(name, id, lfs, ckPriv, skPriv, nil, nil, nil) c.Assert(err2, IsNil) c.Assert(n, Not(IsNil)) c.Assert(name, Equals, n.GetName()) actualID := n.GetNodeID() c.Assert(true, Equals, id.Equal(actualID)) // s.doKeyTests(c, n, rng) c.Assert(0, Equals, (*n).SizePeers()) c.Assert(0, Equals, (*n).SizeOverlays()) c.Assert(0, Equals, n.SizeConnections()) c.Assert(lfs, Equals, n.GetLFS()) return n, ckPriv, skPriv }
// Make a RegCluster for test purposes. Cluster member names are guaranteed // to be unique but the name of the cluster itself may not be. // // THIS IS THE REGISTRY'S VIEW OF A CLUSTER func (s *XLSuite) makeARegCluster(c *C, rng *xr.PRNG, epCount, size uint32) ( rc *RegCluster) { var err error c.Assert(MIN_CLUSTER_SIZE <= size && size <= MAX_CLUSTER_SIZE, Equals, true) attrs := uint64(rng.Int63()) name := rng.NextFileName(8) // no guarantee of uniqueness id := s.makeANodeID(c, rng) rc, err = NewRegCluster(name, id, attrs, size, epCount) c.Assert(err, IsNil) for count := uint32(0); count < size; count++ { cm := s.makeAClientInfo(c, rng, epCount) for { if _, ok := rc.MembersByName[cm.GetName()]; ok { // name is in use, so try again cm = s.makeAClientInfo(c, rng, epCount) } else { err = rc.AddMember(cm) c.Assert(err, IsNil) break } } } return }
func doTestSimpleTreeConstructor(c *C, rng *xr.PRNG, usingSHA1 bool) { name := rng.NextFileName(8) tree := NewNLHTree(name, usingSHA1) c.Assert(tree.name, Equals, name) c.Assert(tree.usingSHA1, Equals, usingSHA1) c.Assert(len(tree.nodes), Equals, 0) }
func (s *XLSuite) noDotsOrDashes(rng *xr.PRNG) string { var length int = 3 + rng.Intn(16) var name = rng.NextFileName(length) for len(name) < 3 || strings.ContainsAny(name, ".-") || strings.ContainsAny(name[0:1], "0123456789") { name = rng.NextFileName(length) } return name }
func doTestConstructor(c *C, rng *xr.PRNG, usingSHA1 bool) { name := rng.NextFileName(8) b := NewNLHBase(name, usingSHA1) c.Assert(b.Name(), Equals, name) c.Assert(b.UsingSHA1(), Equals, usingSHA1) root := b.Root() ct := b.CurTree() c.Assert(root.Name(), Equals, ct.Name()) }
func (s *XLSuite) doTestLoadEntries(c *C, rng *xr.PRNG, whichSHA int) { K := 16 + rng.Intn(16) // create a unique name for a scratch file pathToFile := filepath.Join("tmp", rng.NextFileName(16)) found, err := xf.PathExists(pathToFile) c.Assert(err, IsNil) for found { pathToFile = filepath.Join("tmp", rng.NextFileName(16)) found, err = xf.PathExists(pathToFile) c.Assert(err, IsNil) } f, err := os.OpenFile(pathToFile, os.O_CREATE|os.O_WRONLY, 0600) c.Assert(err, IsNil) // create K entries, saving them in a slice while writing them // to disk var entries []*LogEntry for i := 0; i < K; i++ { t, key, nodeID, src, path := s.makeEntryData(c, rng, whichSHA) entry, err := NewLogEntry(t, key, nodeID, src, path) c.Assert(err, IsNil) strEntry := entry.String() entries = append(entries, entry) var count int count, err = f.WriteString(strEntry + "\n") c.Assert(err, IsNil) c.Assert(count, Equals, len(strEntry)+1) } f.Close() c.Assert(len(entries), Equals, K) // use UpaxServer.LoadEntries to load the stuff in the file. m, err := xi.NewNewIDMap() c.Assert(err, IsNil) count, err := loadEntries(pathToFile, m, whichSHA) c.Assert(err, IsNil) c.Assert(count, Equals, K) // K entries loaded. for i := 0; i < K; i++ { var entry, eInMap *LogEntry var whatever interface{} entry = entries[i] key := entry.key whatever, err = m.Find(key) c.Assert(err, IsNil) c.Assert(whatever, NotNil) eInMap = whatever.(*LogEntry) // DEBUG // XXX NEED LogEntry.Equal() // END c.Assert(bytes.Equal(key, eInMap.key), Equals, true) } }
func (s *XLSuite) getTwoUniqueDirectoryNames(c *C, rng *xr.PRNG) ( string, string) { dirName1 := rng.NextFileName(MAX_NAME_LEN) dirName2 := rng.NextFileName(MAX_NAME_LEN) for dirName2 == dirName1 { dirName2 = rng.NextFileName(MAX_NAME_LEN) } return dirName1, dirName2 }
func (s *XLSuite) getAName(rng *xr.PRNG) (name string) { name = string(rng.NextFileName(8)) for { first := string(name[0]) if !strings.ContainsAny(name, "-_.") && !strings.ContainsAny(first, "0123456789") { break } name = string(rng.NextFileName(8)) } return }
func getTwoUniqueDirectoryNames(c *C, rng *xr.PRNG) (dirName1, dirName2 string) { dirName1 = rng.NextFileName(8) dirName2 = dirName1 for dirName2 == dirName1 { dirName2 = rng.NextFileName(8) } c.Assert(len(dirName1) > 0, Equals, true) c.Assert(len(dirName2) > 0, Equals, true) c.Assert(dirName1 != dirName2, Equals, true) return }
func makeTwoTestDirectories(c *C, rng *xr.PRNG, depth, width int) ( dirName1, dirPath1, dirName2, dirPath2 string) { dirName1 = rng.NextFileName(8) dirPath1 = makeOneNamedTestDirectory(c, rng, dirName1, depth, width) dirName2 = dirName1 for dirName2 == dirName1 { dirName2 = rng.NextFileName(8) } dirPath2 = makeOneNamedTestDirectory(c, rng, dirName2, depth, width) return }
func (s *XLSuite) makeContext(c *C, rng *xr.PRNG, size int) ( k, v []string, context *gc.Context) { var err error context = gc.NewNewContext() k = s.makeSymbolSet(c, rng, size) v = make([]string, size) for i := 0; i < size; i++ { value := rng.NextFileName(8) v[i] = value err = context.Bind(k[i], value) c.Assert(err, IsNil) } return k, v, context }
func (s *XLSuite) makeAMemberInfo(c *C, rng *xr.PRNG) *xcl.MemberInfo { attrs := uint64(rng.Int63()) peer, err := xn.NewPeer( rng.NextFileName(8), s.makeANodeID(c, rng), &s.makeAnRSAKey(c).PublicKey, &s.makeAnRSAKey(c).PublicKey, nil, // overlays nil) // XXX CONNECTORS c.Assert(err, IsNil) return &xcl.MemberInfo{ Attrs: attrs, Peer: peer, } } // GEEP
// PARSER TESTS ===================================================== func (s *XLSuite) doTestParser(c *C, rng *xr.PRNG, whichSHA int) { var tHash []byte switch whichSHA { case xu.USING_SHA1: tHash = make([]byte, xu.SHA1_BIN_LEN) case xu.USING_SHA2: tHash = make([]byte, xu.SHA2_BIN_LEN) case xu.USING_SHA3: tHash = make([]byte, xu.SHA3_BIN_LEN) // XXX DEFAULT = ERROR } rng.NextBytes(tHash) // not really a hash, of course sHash := hex.EncodeToString(tHash) // string form of tHash dirName := rng.NextFileName(8) + "/" nameWithoutSlash := dirName[0 : len(dirName)-1] indent := rng.Intn(4) var lSpaces, rSpaces string for i := 0; i < indent; i++ { lSpaces += " " // on the left rSpaces += " " // on the right } // TEST FIRST LINE PARSER ----------------------------- line := lSpaces + sHash + " " + dirName + rSpaces indent2, treeHash2, dirName2, err := ParseFirstLine(line, " ") c.Assert(err, IsNil) c.Assert(indent2, Equals, indent) c.Assert(bytes.Equal(treeHash2, tHash), Equals, true) c.Assert(dirName2, Equals, nameWithoutSlash) // TEST OTHER LINE PARSER ----------------------------- yesIsDir := rng.NextBoolean() if yesIsDir { line = lSpaces + sHash + " " + dirName + rSpaces } else { line = lSpaces + sHash + " " + nameWithoutSlash + rSpaces } nodeDepth, nodeHash, nodeName, isDir, err := ParseOtherLine(line, " ") c.Assert(err, IsNil) c.Assert(nodeDepth, Equals, indent) c.Assert(bytes.Equal(nodeHash, tHash), Equals, true) c.Assert(nodeName, Equals, nameWithoutSlash) c.Assert(isDir, Equals, yesIsDir) }
func (s *XLSuite) makeSymbolSet(c *C, rng *xr.PRNG, size int) (ss []string) { // make size unique names ss = make([]string, size) nameCache := make(map[string]string) for i := 0; i < size; i++ { name := rng.NextFileName(8) _, ok := nameCache[name] for ok { name = rng.NextFileName(8) _, ok = nameCache[name] } nameCache[name] = "" ss[i] = name } return // FOO }
func (s *XLSuite) makeAMemberInfo(c *C, rng *xr.PRNG) *xcl.MemberInfo { attrs := uint64(rng.Int63()) bn, err := xn.NewBaseNode( rng.NextFileName(8), s.makeANodeID(c, rng), &s.makeAnRSAKey(c).PublicKey, &s.makeAnRSAKey(c).PublicKey, nil) // overlays c.Assert(err, IsNil) asPeer := &xn.Peer{ BaseNode: *bn, } return &xcl.MemberInfo{ Attrs: attrs, Peer: asPeer, } }
func (s *XLSuite) makeAClientInfo(c *C, rng *xr.PRNG, epCount uint32) *ClientInfo { attrs := uint64(rng.Int63()) var myEnds []string for i := uint32(0); i < epCount; i++ { myEnds = append(myEnds, "127.0.0.1:0") } bn, err := xn.NewBaseNode( rng.NextFileName(8), s.makeANodeID(c, rng), &s.makeAnRSAKey(c).PublicKey, &s.makeAnRSAKey(c).PublicKey, nil) // overlays c.Assert(err, IsNil) return &ClientInfo{ Attrs: attrs, MyEnds: myEnds, BaseNode: *bn, } }
func setUpMB3(c *C, rng *xr.PRNG) ( filter *MappedBloomSHA, m, k uint, keys [][]byte, backingFile string) { m = 20 k = 8 keys = make([][]byte, 100) for i := 0; i < 100; i++ { keys[i] = make([]byte, 20) } backingFile = "tmp/" + rng.NextFileName(8) // make sure the file does not already exist found, err := xf.PathExists(backingFile) c.Assert(err, IsNil) for found { backingFile = "tmp/" + rng.NextFileName(8) found, err = xf.PathExists(backingFile) c.Assert(err, IsNil) } return }
// XXX should be a utility routine func (s *XLSuite) scratchFileName(c *C, rng *xr.PRNG, dirName string) (fileName string) { length := len(dirName) c.Assert(length > 0, Equals, true) if strings.HasSuffix(dirName, "/") { dirName = dirName[:length-1] } err := os.MkdirAll(dirName, 0755) c.Assert(err, IsNil) fileName = fmt.Sprintf("%s/%s", dirName, rng.NextFileName(8)) for { _, err = os.Stat(fileName) if os.IsNotExist(err) { break } c.Assert(err, IsNil) fileName = fmt.Sprintf("%s/%s", dirName, rng.NextFileName(8)) } return }
// Create a single randomly chosen MiscItem. If sOK it may be an S. In any // case it may be either a Comment or a PI. func (s *XLSuite) createMiscItem(sOK bool, rng *xr.PRNG) *MiscItem { var body []rune var t MiscType if sOK { t = MiscType(rng.Intn(int(MISC_S) + 1)) } else { t = MiscType(rng.Intn(int(MISC_S))) } switch t { case MISC_COMMENT: // The comment must not end with a dash for { body = []rune(rng.NextFileName(16)) // a quasi-random string, len < 16 text := string(body) if !strings.HasSuffix(text, "-") { break } } case MISC_PI: body = []rune(rng.NextFileName(16)) // a quasi-random string, len < 16 case MISC_S: var runes []rune count := 1 + rng.Intn(3) // 1 to 3 inclusive for i := 0; i < count; i++ { kind := rng.Intn(4) // 0 to 3 inclusive switch kind { case 0: runes = append(runes, '\t') case 1: runes = append(runes, '\n') case 2: runes = append(runes, '\r') case 3: runes = append(runes, ' ') } } body = runes } return &MiscItem{_type: t, body: body} }
func (s *XLSuite) makeEntryData(c *C, rng *xr.PRNG, whichSHA int) ( t int64, key, nodeID []byte, src, path string) { t = rng.Int63() // timestamp var length int switch whichSHA { case xu.USING_SHA1: length = xu.SHA1_BIN_LEN case xu.USING_SHA2: length = xu.SHA2_BIN_LEN case xu.USING_SHA3: length = xu.SHA3_BIN_LEN // XXX DEFAULT = ERROR } key = make([]byte, length) rng.NextBytes(key) nodeID = make([]byte, length) rng.NextBytes(nodeID) src = rng.NextFileName(32) // 32 is max len path = rng.NextFileName(32) for strings.Contains(path, ".") { // that crude fix path = rng.NextFileName(32) } return }
// Create 4 leaf nodes with random but unique names. Insert // them into a tree, verifying that the resulting sort is correct. func doTestInsert4Leafs(c *C, rng *xr.PRNG, usingSHA1 bool) { var sha hash.Hash if usingSHA1 { sha = sha1.New() } else { sha = sha256.New() } _ = sha // XXX NOT USED name := rng.NextFileName(8) tree := NewNLHTree(name, usingSHA1) leafNames := make(map[string]bool) aL := makeLeaf(c, rng, leafNames, usingSHA1) bL := makeLeaf(c, rng, leafNames, usingSHA1) cL := makeLeaf(c, rng, leafNames, usingSHA1) dL := makeLeaf(c, rng, leafNames, usingSHA1) c.Assert(len(tree.nodes), Equals, 0) tree.Insert(aL) c.Assert(len(tree.nodes), Equals, 1) tree.Insert(bL) c.Assert(len(tree.nodes), Equals, 2) tree.Insert(cL) c.Assert(len(tree.nodes), Equals, 3) tree.Insert(dL) c.Assert(len(tree.nodes), Equals, 4) // we expect the nodes to be sorted for i := 0; i < 3; i++ { c.Assert(tree.nodes[i].Name() < tree.nodes[i+1].Name(), Equals, true) } matches, err := tree.List("*") c.Assert(err, IsNil) for i := 0; i < len(tree.Nodes()); i++ { q := tree.Nodes()[i] c.Assert(matches[i], Equals, " "+q.Name()) } c.Assert(tree.Equal(tree), Equals, true) }
func (s *XLSuite) makeACluster(c *C, rng *xr.PRNG, epCount, size uint32) ( rc *reg.RegCluster) { var err error c.Assert(1 < size && size <= 64, Equals, true) attrs := uint64(rng.Int63()) name := rng.NextFileName(8) // no guarantee of uniqueness id := s.makeANodeID(c, rng) rc, err = reg.NewRegCluster(name, id, attrs, size, epCount) c.Assert(err, IsNil) for count := uint32(0); count < size; count++ { cm := s.makeAMemberInfo(c, rng) for { if _, ok := rc.MembersByName[cm.Peer.GetName()]; ok { // name is in use, so try again cm = s.makeAMemberInfo(c, rng) } else { // copy the connector list as strings myEnds := make([]string, 0, 0) for endCount := 0; endCount < int(size); endCount++ { thisEnd := cm.Peer.GetConnector(endCount).String() myEnds = append(myEnds, thisEnd) } asClient := ®.ClientInfo{ Attrs: cm.Attrs, MyEnds: myEnds, BaseNode: cm.Peer.BaseNode, } err = rc.AddMember(asClient) break } } } return }
func makeLeaf(c *C, rng *xr.PRNG, namesSoFar map[string]bool, usingSHA1 bool) ( leaf *NLHLeaf) { var err error var name string for { name = rng.NextFileName(8) if !namesSoFar[name] { namesSoFar[name] = true break } } n := rng.SomeBytes(8) // 8 quasi-random bytes var sha hash.Hash if usingSHA1 { sha = sha1.New() } else { sha = sha256.New() } sha.Write(n) leaf, err = NewNLHLeaf(name, sha.Sum(nil)) c.Assert(err, IsNil) return }
func (s *XLSuite) createAttrValPair(rng *xr.PRNG) *AttrValPair { var attr, val string attr = rng.NextFileName(8) // so 1 to 7 characters long for u.IsDigit(rune(attr[0])) { attr = rng.NextFileName(8) } val = rng.NextFileName(8) // XXX len of zero should be OK return &AttrValPair{Attr: attr, Val: val} }
func (s *XLSuite) makeTwoTestDirectories(c *C, rng *xr.PRNG, depth, width int) (string, string, string, string) { dirName1 := rng.NextFileName(MAX_NAME_LEN) dirPath1 := s.makeOneNamedTestDirectory(c, rng, dirName1, depth, width) dirName2 := rng.NextFileName(MAX_NAME_LEN) for dirName2 == dirName1 { dirName2 = rng.NextFileName(MAX_NAME_LEN) } dirPath2 := s.makeOneNamedTestDirectory(c, rng, dirName2, depth, width) return dirName1, dirPath1, dirName2, dirPath2 }
func (s *XLSuite) doTestSimpleConstructor(c *C, rng *xr.PRNG, whichSHA int) { fileName := rng.NextFileName(8) leaf1, err := NewMerkleLeaf(fileName, nil, whichSHA) c.Assert(err, IsNil) c.Assert(leaf1.Name(), Equals, fileName) c.Assert(len(leaf1.GetHash()), Equals, 0) c.Assert(leaf1.WhichSHA(), Equals, whichSHA) fileName2 := rng.NextFileName(8) for fileName2 == fileName { fileName2 = rng.NextFileName(8) } leaf2, err := NewMerkleLeaf(fileName2, nil, whichSHA) c.Assert(err, IsNil) c.Assert(leaf2.Name(), Equals, fileName2) c.Assert(leaf1.Equal(leaf1), Equals, true) c.Assert(leaf1.Equal(leaf2), Equals, false) }
// Return either spaces, or a dollar sign, or a random 'word', or a // newline, or nothing. func (s *XLSuite) moreBits(c *C, rng *xr.PRNG) (txt string) { start := rng.Intn(7) switch start { case 0: txt += " " case 1: txt += "$" case 2: txt += "\n" case 3: txt += rng.NextFileName(8) case 4: txt += rng.NextFileName(8) case 5: txt += rng.NextFileName(8) case 6: // nothing } return }