From d2026364c785b86c491605c70fc4b638878c7460 Mon Sep 17 00:00:00 2001 From: vishalnayak Date: Fri, 23 Dec 2016 17:09:44 -0500 Subject: [PATCH] physical/file: added test for base64 encoding the storage file names --- physical/file.go | 4 +- physical/file_test.go | 122 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/physical/file.go b/physical/file.go index 262d8b49f..f18fffe2b 100644 --- a/physical/file.go +++ b/physical/file.go @@ -167,8 +167,8 @@ func (b *FileBackend) Put(entry *Entry) error { // base64 URL encode the file name to make all the characters compatible by // the host OS (specially Windows). However, the basePath can contain // disallowed characters. Encoding all the directory names and the file - // name is an over kill, and encoding all at once will flatten the file - // system, which *may* not be desire. + // name is an over kill, and encoding the fullPath will flatten the + // storage, which *may* not be desired. fullPath = filepath.Join(basePath, "_"+base64.URLEncoding.EncodeToString([]byte(fileName))) // JSON encode the entry and write it diff --git a/physical/file_test.go b/physical/file_test.go index 50be4011c..9810f4beb 100644 --- a/physical/file_test.go +++ b/physical/file_test.go @@ -1,14 +1,136 @@ package physical import ( + "encoding/json" "io/ioutil" "os" + "path/filepath" + "reflect" "testing" "github.com/hashicorp/vault/helper/logformat" log "github.com/mgutz/logxi/v1" ) +func TestFileBackend_Base64URLEncoding(t *testing.T) { + backendPath, err := ioutil.TempDir("", "vault") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(backendPath) + + logger := logformat.NewVaultLogger(log.LevelTrace) + + b, err := NewBackend("file", logger, map[string]string{ + "path": backendPath, + }) + if err != nil { + t.Fatalf("err: %s", err) + } + + // List the entries. Length should be zero. + keys, err := b.List("") + if err != nil { + t.Fatalf("err: %v", err) + } + if len(keys) != 0 { + t.Fatalf("bad: len(keys): expected: 0, actual: %d", len(keys)) + } + + // Create a storage entry without base64 encoding the file name + rawFullPath := filepath.Join(backendPath, "_foo") + e := &Entry{Key: "foo", Value: []byte("test")} + f, err := os.OpenFile( + rawFullPath, + os.O_CREATE|os.O_TRUNC|os.O_WRONLY, + 0600) + if err != nil { + t.Fatal(err) + } + json.NewEncoder(f).Encode(e) + f.Close() + + // Get should work + out, err := b.Get("foo") + if err != nil { + t.Fatalf("err: %v", err) + } + if !reflect.DeepEqual(out, e) { + t.Fatalf("bad: %v expected: %v", out, e) + } + + // List the entries. There should be one entry. + keys, err = b.List("") + if err != nil { + t.Fatalf("err: %v", err) + } + if len(keys) != 1 { + t.Fatalf("bad: len(keys): expected: 1, actual: %d", len(keys)) + } + + err = b.Put(e) + if err != nil { + t.Fatalf("err: %v", err) + } + + // List the entries again. There should still be one entry. + keys, err = b.List("") + if err != nil { + t.Fatalf("err: %v", err) + } + if len(keys) != 1 { + t.Fatalf("bad: len(keys): expected: 1, actual: %d", len(keys)) + } + + // Get should work + out, err = b.Get("foo") + if err != nil { + t.Fatalf("err: %v", err) + } + if !reflect.DeepEqual(out, e) { + t.Fatalf("bad: %v expected: %v", out, e) + } + + err = b.Delete("foo") + if err != nil { + t.Fatalf("err: %v", err) + } + + out, err = b.Get("foo") + if err != nil { + t.Fatalf("err: %v", err) + } + if out != nil { + t.Fatalf("bad: entry: expected: nil, actual: %#v", e) + } + + keys, err = b.List("") + if err != nil { + t.Fatalf("err: %v", err) + } + if len(keys) != 0 { + t.Fatalf("bad: len(keys): expected: 0, actual: %d", len(keys)) + } + + f, err = os.OpenFile( + rawFullPath, + os.O_CREATE|os.O_TRUNC|os.O_WRONLY, + 0600) + if err != nil { + t.Fatal(err) + } + json.NewEncoder(f).Encode(e) + f.Close() + + keys, err = b.List("") + if err != nil { + t.Fatalf("err: %v", err) + } + if len(keys) != 1 { + t.Fatalf("bad: len(keys): expected: 1, actual: %d", len(keys)) + } +} + func TestFileBackend(t *testing.T) { dir, err := ioutil.TempDir("", "vault") if err != nil {