Move RandomID() to backend package

This commit is contained in:
Alexander Neumann 2016-08-07 12:36:24 +02:00
parent fd6c854a21
commit 3c3a180417
2 changed files with 26 additions and 20 deletions

View File

@ -0,0 +1,17 @@
package backend
import (
"crypto/rand"
"io"
)
// RandomID retuns a randomly generated ID. This is mainly used for testing.
// When reading from rand fails, the function panics.
func RandomID() ID {
id := ID{}
_, err := io.ReadFull(rand.Reader, id[:])
if err != nil {
panic(err)
}
return id
}

View File

@ -2,8 +2,6 @@ package repository_test
import ( import (
"bytes" "bytes"
"crypto/rand"
"io"
"testing" "testing"
"restic/backend" "restic/backend"
@ -12,15 +10,6 @@ import (
. "restic/test" . "restic/test"
) )
func randomID() backend.ID {
id := backend.ID{}
_, err := io.ReadFull(rand.Reader, id[:])
if err != nil {
panic(err)
}
return id
}
func TestIndexSerialize(t *testing.T) { func TestIndexSerialize(t *testing.T) {
type testEntry struct { type testEntry struct {
id backend.ID id backend.ID
@ -34,11 +23,11 @@ func TestIndexSerialize(t *testing.T) {
// create 50 packs with 20 blobs each // create 50 packs with 20 blobs each
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
packID := randomID() packID := backend.RandomID()
pos := uint(0) pos := uint(0)
for j := 0; j < 20; j++ { for j := 0; j < 20; j++ {
id := randomID() id := backend.RandomID()
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(repository.PackedBlob{ idx.Store(repository.PackedBlob{
Type: pack.Data, Type: pack.Data,
@ -104,11 +93,11 @@ func TestIndexSerialize(t *testing.T) {
// add more blobs to idx // add more blobs to idx
newtests := []testEntry{} newtests := []testEntry{}
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
packID := randomID() packID := backend.RandomID()
pos := uint(0) pos := uint(0)
for j := 0; j < 10; j++ { for j := 0; j < 10; j++ {
id := randomID() id := backend.RandomID()
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(repository.PackedBlob{ idx.Store(repository.PackedBlob{
Type: pack.Data, Type: pack.Data,
@ -138,7 +127,7 @@ func TestIndexSerialize(t *testing.T) {
Assert(t, idx.Final(), Assert(t, idx.Final(),
"index not final after encoding") "index not final after encoding")
id := randomID() id := backend.RandomID()
OK(t, idx.SetID(id)) OK(t, idx.SetID(id))
id2, err := idx.ID() id2, err := idx.ID()
Assert(t, id2.Equal(id), Assert(t, id2.Equal(id),
@ -175,11 +164,11 @@ func TestIndexSize(t *testing.T) {
packs := 200 packs := 200
blobs := 100 blobs := 100
for i := 0; i < packs; i++ { for i := 0; i < packs; i++ {
packID := randomID() packID := backend.RandomID()
pos := uint(0) pos := uint(0)
for j := 0; j < blobs; j++ { for j := 0; j < blobs; j++ {
id := randomID() id := backend.RandomID()
length := uint(i*100 + j) length := uint(i*100 + j)
idx.Store(repository.PackedBlob{ idx.Store(repository.PackedBlob{
Type: pack.Data, Type: pack.Data,
@ -359,10 +348,10 @@ func TestIndexPacks(t *testing.T) {
packs := backend.NewIDSet() packs := backend.NewIDSet()
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
packID := randomID() packID := backend.RandomID()
idx.Store(repository.PackedBlob{ idx.Store(repository.PackedBlob{
Type: pack.Data, Type: pack.Data,
ID: randomID(), ID: backend.RandomID(),
PackID: packID, PackID: packID,
Offset: 0, Offset: 0,
Length: 23, Length: 23,