github.com/grailbio/base@v0.0.11/security/keycrypt/file/file_test.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package file
     6  
     7  import (
     8  	"io/ioutil"
     9  	"path/filepath"
    10  	"reflect"
    11  	"testing"
    12  
    13  	"github.com/grailbio/testutil"
    14  )
    15  
    16  func TestFile(t *testing.T) {
    17  	dir, cleanup := testutil.TempDir(t, "", "file")
    18  	defer cleanup()
    19  	c := &crypt{dir}
    20  	s := c.Lookup("foo")
    21  	b, err := s.Get()
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if got, want := len(b), 0; got != want {
    26  		t.Fatalf("got %v, want %v", got, want)
    27  	}
    28  	_, files := testutil.ListRecursively(t, dir)
    29  	if got, want := len(files), 0; got != want {
    30  		t.Fatalf("got %v, want %v", got, want)
    31  	}
    32  	hello := []byte("hello world")
    33  	if err := s.Put(hello); err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	b, err = s.Get()
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	if got, want := b, hello; !reflect.DeepEqual(got, want) {
    41  		t.Fatalf("got %v, want %v", got, want)
    42  	}
    43  	_, files = testutil.ListRecursively(t, dir)
    44  	if got, want := files, []string{filepath.Join(dir, "foo")}; !reflect.DeepEqual(got, want) {
    45  		t.Fatalf("got %v, want %v", got, want)
    46  	}
    47  	b, err = ioutil.ReadFile(filepath.Join(dir, "foo"))
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if got, want := b, hello; !reflect.DeepEqual(got, want) {
    52  		t.Fatalf("got %v, want %v", got, want)
    53  	}
    54  }