Example #1
0
func makeNodeID(rng *xr.PRNG) (*xi.NodeID, error) {
	var buffer []byte
	// quasi-random choice, whether to use an SHA1 or SHA3 nodeID
	if rng.NextBoolean() {
		buffer = make([]byte, xu.SHA1_BIN_LEN)
	} else {
		buffer = make([]byte, xu.SHA3_BIN_LEN)
	}
	rng.NextBytes(buffer)
	return xi.NewNodeID(buffer)
}
Example #2
0
func (s *XLSuite) makeANodeID(c *C, rng *xr.PRNG) (id *NodeID) {
	var length int
	if rng.NextBoolean() {
		length = xu.SHA1_BIN_LEN
	} else {
		length = xu.SHA2_BIN_LEN
	}
	data := make([]byte, length)
	rng.NextBytes(data)
	id, err := New(data)
	c.Assert(err, IsNil)
	return id
}
Example #3
0
// 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)
}