func (t *DirsTest) UnknownEntryType() {
	// Set up a listing with an unsupported entry type.
	t.listing = []*fs.FileInfo{
		&fs.FileInfo{
			Type: fs.TypeCharDevice,
			Name: "foo",
			Scores: []blob.Score{
				blob.ComputeScore([]byte("0")),
			},
		},
	}

	var err error
	t.contents, err = repr.MarshalDir(t.listing)
	AssertEq(nil, err)

	t.score = blob.ComputeScore(t.contents)
	t.allScores = append(t.allScores, t.score)
	t.node = makeNode(true, t.score)

	// Load
	ExpectCall(t.blobStore, "Load")(Any(), Any()).
		WillOnce(Return(t.contents, nil))

	// Call
	_, err = t.call(t.node)

	ExpectThat(err, Error(HasSubstr("entry type")))
	ExpectThat(err, Error(HasSubstr(fmt.Sprintf("%d", fs.TypeCharDevice))))
	ExpectThat(t.getRecords(), ElementsAre())
}
func (t *DirsTest) SymlinkWithScores() {
	// Set up a listing with a symlink that unexpectedly has associated scores.
	t.listing = []*fs.FileInfo{
		&fs.FileInfo{
			Type: fs.TypeSymlink,
			Name: "foo",
			Scores: []blob.Score{
				blob.ComputeScore([]byte("0")),
			},
		},
	}

	var err error
	t.contents, err = repr.MarshalDir(t.listing)
	AssertEq(nil, err)

	t.score = blob.ComputeScore(t.contents)
	t.allScores = append(t.allScores, t.score)
	t.node = makeNode(true, t.score)

	// Load
	ExpectCall(t.blobStore, "Load")(Any(), Any()).
		WillOnce(Return(t.contents, nil))

	// Call
	_, err = t.call(t.node)

	ExpectThat(err, Error(HasSubstr(t.score.Hex())))
	ExpectThat(err, Error(HasSubstr("symlink")))
	ExpectThat(err, Error(HasSubstr("scores")))
	ExpectThat(t.getRecords(), ElementsAre())
}
Exemple #3
0
func (t *VisitorTest) PresentInScoreMap_NonEmpty() {
	var err error

	// Node setup
	t.node.RelPath = "foo"
	t.node.Info.Type = fs.TypeFile
	t.node.Info.MTime = t.clock.Now().Add(-100 * time.Hour)

	// Add an entry to the score map.
	key := makeScoreMapKey(&t.node, &t.clock)
	AssertNe(nil, key)

	scores := []blob.Score{
		blob.ComputeScore([]byte("taco")),
		blob.ComputeScore([]byte("burrito")),
	}
	t.scoreMap.Set(*key, scores)

	// Call
	err = t.call()
	AssertEq(nil, err)

	AssertNe(nil, t.node.Info.Scores)
	ExpectThat(t.node.Info.Scores, DeepEquals(scores))
}
Exemple #4
0
func (t *RoundtripTest) PreservesScores() {
	// Input
	in := []*fs.FileInfo{
		makeLegalEntry(),
		makeLegalEntry(),
	}

	score00 := blob.ComputeScore([]byte("taco"))
	score01 := blob.ComputeScore([]byte("burrito"))
	score10 := blob.ComputeScore([]byte("enchilada"))

	in[0].Scores = []blob.Score{score00, score01}
	in[1].Scores = []blob.Score{score10}

	// Marshal
	d, err := repr.MarshalDir(in)
	AssertEq(nil, err)
	AssertNe(nil, d)

	// Unmarshal
	out, err := repr.UnmarshalDir(d)
	AssertEq(nil, err)
	AssertNe(nil, out)

	// Output
	AssertThat(out, ElementsAre(Any(), Any()))

	AssertThat(out[0].Scores, ElementsAre(Any(), Any()))
	ExpectEq(score00, out[0].Scores[0])
	ExpectEq(score01, out[0].Scores[1])

	AssertThat(out[1].Scores, ElementsAre(Any()))
	ExpectEq(score10, out[1].Scores[0])
}
Exemple #5
0
func (t *ScoreMapTest) DecodingOverwritesContents() {
	key0 := state.ScoreMapKey{Path: "taco"}
	key1 := state.ScoreMapKey{Path: "burrito"}
	key2 := state.ScoreMapKey{Path: "enchilada"}

	// Source contents
	scores0 := []blob.Score{blob.ComputeScore([]byte("foo"))}
	scores1 := []blob.Score{blob.ComputeScore([]byte("bar"))}

	t.m.Set(key0, scores0)
	t.m.Set(key1, scores1)

	// Encode
	buf := new(bytes.Buffer)
	encoder := gob.NewEncoder(buf)
	AssertEq(nil, encoder.Encode(&t.m))

	// Destination
	decoded := state.NewScoreMap()
	decoded.Set(key0, scores1)
	decoded.Set(key2, scores0)

	// Decode
	decoder := gob.NewDecoder(buf)
	AssertEq(nil, decoder.Decode(&decoded))

	ExpectThat(decoded.Get(key0), DeepEquals(scores0))
	ExpectThat(decoded.Get(key1), DeepEquals(scores1))
	ExpectEq(nil, decoded.Get(key2))
}
Exemple #6
0
func (t *ScoreMapTest) SomeElements() {
	// Set taco
	tacoKey := state.ScoreMapKey{
		Path: "taco",
		Size: 17,
	}

	tacoScores := []blob.Score{
		blob.ComputeScore([]byte("foo")),
		blob.ComputeScore([]byte("bar")),
	}

	t.m.Set(tacoKey, tacoScores)

	// Set burrito
	burritoKey := tacoKey
	burritoKey.Path = "burrito"

	burritoScores := []blob.Score{
		blob.ComputeScore([]byte("baz")),
	}

	t.m.Set(burritoKey, burritoScores)

	// Look up
	ExpectThat(t.m.Get(tacoKey), DeepEquals(tacoScores))
	ExpectThat(t.m.Get(burritoKey), DeepEquals(burritoScores))
	ExpectEq(nil, t.m.Get(state.ScoreMapKey{}))
}
func (t *FilesTest) SetUp(ti *TestInfo) {
	t.contents = []byte("foobarbaz")

	// Set up canned nodes.
	t.knownNode = Node{
		Dir:   false,
		Score: blob.ComputeScore(t.contents),
	}

	t.unknownNode = Node{
		Dir:   false,
		Score: blob.ComputeScore(append(t.contents, 'a')),
	}

	t.allScores = append(t.allScores, t.knownNode.Score)

	t.superTest.setUp(ti)
}
Exemple #8
0
func (t *EncryptingStore_LoadTest) WrappedReturnsError() {
	// Wrapped
	ExpectCall(t.wrapped, "Load")(Any(), Any()).
		WillOnce(oglemock.Return(nil, errors.New("taco")))

	// Call
	_, err := t.store.Load(t.ctx, blob.ComputeScore([]byte{}))

	ExpectThat(err, Error(Equals("taco")))
}
Exemple #9
0
func (t *EncryptingStore_LoadTest) CallsWrapped() {
	score := blob.ComputeScore([]byte("taco"))

	// Wrapped
	ExpectCall(t.wrapped, "Load")(Any(), DeepEquals(score)).
		WillOnce(oglemock.Return(nil, errors.New("")))

	// Call
	t.store.Load(t.ctx, score)
}
Exemple #10
0
func (t *ScoreMapTest) AddTwice() {
	key := state.ScoreMapKey{Path: "taco"}

	// First
	scores0 := []blob.Score{
		blob.ComputeScore([]byte("foo")),
	}

	t.m.Set(key, scores0)

	// Second
	scores1 := []blob.Score{
		blob.ComputeScore([]byte("bar")),
	}

	t.m.Set(key, scores1)

	// Look up
	ExpectThat(t.m.Get(key), DeepEquals(scores1))
}
Exemple #11
0
func (t *VisitorTest) File_LastChunkIsPartial() {
	var err error

	// Node setup
	t.node.RelPath = "foo"
	p := path.Join(t.dir, t.node.RelPath)
	t.node.Info.Type = fs.TypeFile

	chunk0 := bytes.Repeat([]byte{0}, t.chunkSize)
	chunk1 := bytes.Repeat([]byte{1}, t.chunkSize-1)

	var contents []byte
	contents = append(contents, chunk0...)
	contents = append(contents, chunk1...)

	err = ioutil.WriteFile(p, contents, 0700)
	AssertEq(nil, err)

	// Blob store (chunk 0)
	expected0, err := repr.MarshalFile(chunk0)
	AssertEq(nil, err)

	score0 := blob.ComputeScore(expected0)
	ExpectCall(t.blobStore, "Store")(Any(), blobEquals(expected0)).
		WillOnce(Return(score0, nil))

	// Blob store (chunk 1)
	expected1, err := repr.MarshalFile(chunk1)
	AssertEq(nil, err)

	score1 := blob.ComputeScore(expected1)
	ExpectCall(t.blobStore, "Store")(Any(), blobEquals(expected1)).
		WillOnce(Return(score1, nil))

	// Call
	err = t.call()
	AssertEq(nil, err)

	ExpectThat(t.node.Info.Scores, ElementsAre(score0, score1))
}
func (t *DependencyResolverTest) SomeChildren() {
	var err error

	// Set up a listing.
	listing := []*fs.FileInfo{
		&fs.FileInfo{
			Type:        fs.TypeFile,
			Name:        "foo",
			Permissions: 0754,
			MTime:       time.Now().Round(time.Millisecond),
		},
		&fs.FileInfo{
			Type:   fs.TypeDirectory,
			Name:   "bar",
			Scores: []blob.Score{blob.ComputeScore([]byte(""))},
			MTime:  time.Now().Round(time.Millisecond),
		},
	}

	serialized, err := repr.MarshalDir(listing)
	AssertEq(nil, err)

	score, err := t.store(serialized)
	AssertEq(nil, err)

	// Set up the node.
	n := &node{
		RelPath: "taco/burrito",
		Info: fs.FileInfo{
			Type:   fs.TypeDirectory,
			Scores: []blob.Score{score},
		},
	}

	// Call
	deps, err := t.call(n)

	AssertEq(nil, err)
	AssertEq(2, len(deps))
	AssertThat(n.Children, DeepEquals(deps))
	var child *node

	child = n.Children[0]
	ExpectEq("taco/burrito/foo", child.RelPath)
	ExpectThat(child.Info, DeepEquals(*listing[0]))

	child = n.Children[1]
	ExpectEq("taco/burrito/bar", child.RelPath)
	ExpectThat(child.Info, DeepEquals(*listing[1]))
}
Exemple #13
0
func (t *EncryptingStore_LoadTest) CallsCrypter() {
	// Wrapped
	ciphertext := []byte{0xde, 0xad}

	ExpectCall(t.wrapped, "Load")(Any(), Any()).
		WillOnce(oglemock.Return(ciphertext, nil))

	// Crypter
	ExpectCall(t.crypter, "Decrypt")(DeepEquals(ciphertext)).
		WillOnce(oglemock.Return(nil, errors.New("")))

	// Call
	t.store.Load(t.ctx, blob.ComputeScore([]byte{}))
}
Exemple #14
0
func (t *EncryptingStore_LoadTest) CrypterReturnsError() {
	// Wrapped
	ExpectCall(t.wrapped, "Load")(Any(), Any()).
		WillOnce(oglemock.Return([]byte{}, nil))

	// Crypter
	ExpectCall(t.crypter, "Decrypt")(Any()).
		WillOnce(oglemock.Return(nil, errors.New("taco")))

	// Call
	_, err := t.store.Load(t.ctx, blob.ComputeScore([]byte{}))

	ExpectThat(err, Error(HasSubstr("Decrypt")))
	ExpectThat(err, Error(HasSubstr("taco")))
}
Exemple #15
0
func (t *ScoreMapTest) GobRoundTrip() {
	// Contents
	key0 := state.ScoreMapKey{Path: "taco"}
	scores0 := []blob.Score{blob.ComputeScore([]byte("foo"))}
	t.m.Set(key0, scores0)

	key1 := state.ScoreMapKey{Path: "burrito"}
	scores1 := []blob.Score{blob.ComputeScore([]byte("bar"))}
	t.m.Set(key1, scores1)

	// Encode
	buf := new(bytes.Buffer)
	encoder := gob.NewEncoder(buf)
	AssertEq(nil, encoder.Encode(&t.m))

	// Decode
	decoder := gob.NewDecoder(buf)
	var decoded state.ScoreMap
	AssertEq(nil, decoder.Decode(&decoded))

	ExpectThat(decoded.Get(key0), DeepEquals(scores0))
	ExpectThat(decoded.Get(key1), DeepEquals(scores1))
	ExpectEq(nil, decoded.Get(state.ScoreMapKey{}))
}
func (t *DependencyResolverTest) BlobMissing() {
	s := blob.ComputeScore([]byte(""))
	n := &node{
		Info: fs.FileInfo{
			Type:   fs.TypeDirectory,
			Scores: []blob.Score{s},
		},
	}

	// Call
	_, err := t.call(n)

	ExpectThat(err, Error(HasSubstr("not found")))
	ExpectThat(err, Error(HasSubstr(s.Hex())))
}
Exemple #17
0
func (t *EncryptingStore_StoreTest) WrappedSucceeds() {
	// Crypter
	ExpectCall(t.crypter, "Encrypt")(Any(), Any()).
		WillOnce(oglemock.Return([]byte{}, nil))

	// Wrapped
	expected := blob.ComputeScore([]byte("taco"))

	ExpectCall(t.wrapped, "Store")(Any(), Any()).
		WillOnce(oglemock.Return(expected, nil))

	// Call
	score, err := t.store.Store(t.ctx, &blob.StoreRequest{})
	AssertEq(nil, err)

	ExpectThat(score, DeepEquals(expected))
}
Exemple #18
0
func (t *EncryptingStore_LoadTest) CrypterSucceeds() {
	// Wrapped
	ExpectCall(t.wrapped, "Load")(Any(), Any()).
		WillOnce(oglemock.Return([]byte{}, nil))

	// Crypter
	expected := []byte{0xde, 0xad}

	ExpectCall(t.crypter, "Decrypt")(Any()).
		WillOnce(oglemock.Return(expected, nil))

	// Call
	blob, err := t.store.Load(t.ctx, blob.ComputeScore([]byte{}))
	AssertEq(nil, err)

	ExpectThat(blob, DeepEquals(expected))
}
Exemple #19
0
func (t *VisitorTest) Directory() {
	var err error

	// Children
	child0 := &fsNode{
		Info: fs.FileInfo{
			Name:  "taco",
			MTime: time.Date(2012, time.August, 15, 12, 56, 00, 0, time.Local),
		},
	}

	child1 := &fsNode{
		Info: fs.FileInfo{
			Name:  "burrito",
			MTime: time.Date(2015, 4, 5, 2, 15, 0, 0, time.Local),
		},
	}

	// Node setup
	t.node.RelPath = ""
	t.node.Info = fs.FileInfo{
		Type: fs.TypeDirectory,
	}

	t.node.Children = []*fsNode{child0, child1}

	// Snoop on the call to the blob store.
	var savedReq *blob.StoreRequest
	expectedScore := blob.ComputeScore([]byte("taco"))

	ExpectCall(t.blobStore, "Store")(Any(), Any()).
		WillOnce(DoAll(SaveArg(1, &savedReq), Return(expectedScore, nil)))

	// Call
	err = t.call()
	AssertEq(nil, err)
	AssertThat(t.node.Info.Scores, ElementsAre(expectedScore))

	// Parse the blob.
	entries, err := repr.UnmarshalDir(savedReq.Blob)
	AssertEq(nil, err)
	AssertEq(2, len(entries))

	ExpectThat(*entries[0], DeepEquals(child0.Info))
	ExpectThat(*entries[1], DeepEquals(child1.Info))
}
func (t *DirsTest) BlobIsJunk() {
	// Set up junk contents.
	t.contents = append(t.contents, 'a')
	t.score = blob.ComputeScore(t.contents)
	t.allScores = append(t.allScores, t.score)
	t.node = makeNode(true, t.score)

	// Load
	ExpectCall(t.blobStore, "Load")(Any(), Any()).
		WillOnce(Return(t.contents, nil))

	// Call
	_, err := t.call(t.node)

	ExpectThat(err, Error(HasSubstr(t.score.Hex())))
	ExpectThat(err, Error(HasSubstr("UnmarshalDir")))
	ExpectThat(t.getRecords(), ElementsAre())
}
func (t *DirsTest) SetUp(ti *TestInfo) {
	// Set up canned data for a valid listing.
	t.listing = []*fs.FileInfo{
		&fs.FileInfo{
			Type: fs.TypeFile,
			Name: "foo",
			Scores: []blob.Score{
				blob.ComputeScore([]byte("0")),
				blob.ComputeScore([]byte("1")),
			},
		},

		&fs.FileInfo{
			Type: fs.TypeDirectory,
			Name: "bar",
			Scores: []blob.Score{
				blob.ComputeScore([]byte("2")),
			},
		},

		&fs.FileInfo{
			Type:   fs.TypeSymlink,
			Name:   "baz",
			Target: "asdf",
		},
	}

	var err error
	t.contents, err = repr.MarshalDir(t.listing)
	AssertEq(nil, err)

	t.score = blob.ComputeScore(t.contents)
	t.node = makeNode(true, t.score)
	t.allScores = append(t.allScores, t.score)

	// Set up canned nodes.
	t.knownNode = Node{
		Dir:   true,
		Score: blob.ComputeScore([]byte("knownNode")),
	}

	t.unknownNode = Node{
		Dir:   true,
		Score: blob.ComputeScore([]byte("unknownNode")),
	}

	t.allScores = append(t.allScores, t.knownNode.Score)

	// Call through.
	t.superTest.setUp(ti)
}