github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/repository/testing.go (about)

     1  package repository
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/restic/restic/internal/backend/local"
     9  	"github.com/restic/restic/internal/backend/mem"
    10  	"github.com/restic/restic/internal/crypto"
    11  	"github.com/restic/restic/internal/restic"
    12  	"github.com/restic/restic/internal/test"
    13  
    14  	"github.com/restic/chunker"
    15  )
    16  
    17  // testKDFParams are the parameters for the KDF to be used during testing.
    18  var testKDFParams = crypto.Params{
    19  	N: 128,
    20  	R: 1,
    21  	P: 1,
    22  }
    23  
    24  type logger interface {
    25  	Logf(format string, args ...interface{})
    26  }
    27  
    28  // TestUseLowSecurityKDFParameters configures low-security KDF parameters for testing.
    29  func TestUseLowSecurityKDFParameters(t logger) {
    30  	t.Logf("using low-security KDF parameters for test")
    31  	Params = &testKDFParams
    32  }
    33  
    34  // TestBackend returns a fully configured in-memory backend.
    35  func TestBackend(t testing.TB) (be restic.Backend, cleanup func()) {
    36  	return mem.New(), func() {}
    37  }
    38  
    39  const testChunkerPol = chunker.Pol(0x3DA3358B4DC173)
    40  
    41  // TestRepositoryWithBackend returns a repository initialized with a test
    42  // password. If be is nil, an in-memory backend is used. A constant polynomial
    43  // is used for the chunker and low-security test parameters.
    44  func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r restic.Repository, cleanup func()) {
    45  	TestUseLowSecurityKDFParameters(t)
    46  
    47  	var beCleanup func()
    48  	if be == nil {
    49  		be, beCleanup = TestBackend(t)
    50  	}
    51  
    52  	repo := New(be)
    53  
    54  	cfg := restic.TestCreateConfig(t, testChunkerPol)
    55  	err := repo.init(context.TODO(), test.TestPassword, cfg)
    56  	if err != nil {
    57  		t.Fatalf("TestRepository(): initialize repo failed: %v", err)
    58  	}
    59  
    60  	return repo, func() {
    61  		if beCleanup != nil {
    62  			beCleanup()
    63  		}
    64  	}
    65  }
    66  
    67  // TestRepository returns a repository initialized with a test password on an
    68  // in-memory backend. When the environment variable RESTIC_TEST_REPO is set to
    69  // a non-existing directory, a local backend is created there and this is used
    70  // instead. The directory is not removed, but left there for inspection.
    71  func TestRepository(t testing.TB) (r restic.Repository, cleanup func()) {
    72  	dir := os.Getenv("RESTIC_TEST_REPO")
    73  	if dir != "" {
    74  		_, err := os.Stat(dir)
    75  		if err != nil {
    76  			be, err := local.Create(local.Config{Path: dir})
    77  			if err != nil {
    78  				t.Fatalf("error creating local backend at %v: %v", dir, err)
    79  			}
    80  			return TestRepositoryWithBackend(t, be)
    81  		}
    82  
    83  		if err == nil {
    84  			t.Logf("directory at %v already exists, using mem backend", dir)
    85  		}
    86  	}
    87  
    88  	return TestRepositoryWithBackend(t, nil)
    89  }
    90  
    91  // TestOpenLocal opens a local repository.
    92  func TestOpenLocal(t testing.TB, dir string) (r restic.Repository) {
    93  	be, err := local.Open(local.Config{Path: dir})
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	repo := New(be)
    99  	err = repo.SearchKey(context.TODO(), test.TestPassword, 10)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	return repo
   105  }