| 1 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package dirhash |
| 6 |
|
| 7 |
import ( |
| 8 |
"archive/zip" |
| 9 |
"crypto/sha256" |
| 10 |
"encoding/base64" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"strings" |
| 16 |
"testing" |
| 17 |
) |
| 18 |
|
| 19 |
func h(s string) string { |
| 20 |
return fmt.Sprintf("%x", sha256.Sum256([]byte(s))) |
| 21 |
} |
| 22 |
|
| 23 |
func htop(k string, s string) string { |
| 24 |
sum := sha256.Sum256([]byte(s)) |
| 25 |
return k + ":" + base64.StdEncoding.EncodeToString(sum[:]) |
| 26 |
} |
| 27 |
|
| 28 |
func TestHash1(t *testing.T) { |
| 29 |
files := []string{"xyz", "abc"} |
| 30 |
open := func(name string) (io.ReadCloser, error) { |
| 31 |
return io.NopCloser(strings.NewReader("data for " + name)), nil |
| 32 |
} |
| 33 |
want := htop("h1", fmt.Sprintf("%s %s\n%s %s\n", h("data for abc"), "abc", h("data for xyz"), "xyz")) |
| 34 |
out, err := Hash1(files, open) |
| 35 |
if err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
if out != want { |
| 39 |
t.Errorf("Hash1(...) = %s, want %s", out, want) |
| 40 |
} |
| 41 |
|
| 42 |
_, err = Hash1([]string{"xyz", "a\nbc"}, open) |
| 43 |
if err == nil { |
| 44 |
t.Error("Hash1: expected error on newline in filenames") |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestHashDir(t *testing.T) { |
| 49 |
dir := t.TempDir() |
| 50 |
if err := os.WriteFile(filepath.Join(dir, "xyz"), []byte("data for xyz"), 0666); err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
if err := os.WriteFile(filepath.Join(dir, "abc"), []byte("data for abc"), 0666); err != nil { |
| 54 |
t.Fatal(err) |
| 55 |
} |
| 56 |
want := htop("h1", fmt.Sprintf("%s %s\n%s %s\n", h("data for abc"), "prefix/abc", h("data for xyz"), "prefix/xyz")) |
| 57 |
out, err := HashDir(dir, "prefix", Hash1) |
| 58 |
if err != nil { |
| 59 |
t.Fatalf("HashDir: %v", err) |
| 60 |
} |
| 61 |
if out != want { |
| 62 |
t.Errorf("HashDir(...) = %s, want %s", out, want) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestHashZip(t *testing.T) { |
| 67 |
f, err := os.CreateTemp(t.TempDir(), "dirhash-test-") |
| 68 |
if err != nil { |
| 69 |
t.Fatal(err) |
| 70 |
} |
| 71 |
defer f.Close() |
| 72 |
|
| 73 |
z := zip.NewWriter(f) |
| 74 |
w, err := z.Create("prefix/xyz") |
| 75 |
if err != nil { |
| 76 |
t.Fatal(err) |
| 77 |
} |
| 78 |
w.Write([]byte("data for xyz")) |
| 79 |
w, err = z.Create("prefix/abc") |
| 80 |
if err != nil { |
| 81 |
t.Fatal(err) |
| 82 |
} |
| 83 |
w.Write([]byte("data for abc")) |
| 84 |
if err := z.Close(); err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
if err := f.Close(); err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
|
| 91 |
want := htop("h1", fmt.Sprintf("%s %s\n%s %s\n", h("data for abc"), "prefix/abc", h("data for xyz"), "prefix/xyz")) |
| 92 |
out, err := HashZip(f.Name(), Hash1) |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("HashDir: %v", err) |
| 95 |
} |
| 96 |
if out != want { |
| 97 |
t.Errorf("HashDir(...) = %s, want %s", out, want) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestDirFiles(t *testing.T) { |
| 102 |
t.Run("valid directory with files", func(t *testing.T) { |
| 103 |
dir := t.TempDir() |
| 104 |
if err := os.WriteFile(filepath.Join(dir, "xyz"), []byte("data for xyz"), 0666); err != nil { |
| 105 |
t.Fatal(err) |
| 106 |
} |
| 107 |
if err := os.WriteFile(filepath.Join(dir, "abc"), []byte("data for abc"), 0666); err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
if err := os.Mkdir(filepath.Join(dir, "subdir"), 0777); err != nil { |
| 111 |
t.Fatal(err) |
| 112 |
} |
| 113 |
if err := os.WriteFile(filepath.Join(dir, "subdir", "xyz"), []byte("data for subdir xyz"), 0666); err != nil { |
| 114 |
t.Fatal(err) |
| 115 |
} |
| 116 |
prefix := "foo/[email protected]" |
| 117 |
out, err := DirFiles(dir, prefix) |
| 118 |
if err != nil { |
| 119 |
t.Fatalf("DirFiles: %v", err) |
| 120 |
} |
| 121 |
for _, file := range out { |
| 122 |
if !strings.HasPrefix(file, prefix) { |
| 123 |
t.Errorf("Dir file = %s, want prefix %s", file, prefix) |
| 124 |
} |
| 125 |
} |
| 126 |
}) |
| 127 |
|
| 128 |
t.Run("invalid directory", func(t *testing.T) { |
| 129 |
path := filepath.Join(t.TempDir(), "not-a-directory.txt") |
| 130 |
if err := os.WriteFile(path, []byte("This is a file."), 0644); err != nil { |
| 131 |
t.Fatal(err) |
| 132 |
} |
| 133 |
defer os.RemoveAll(path) |
| 134 |
|
| 135 |
out, err := DirFiles(path, "") |
| 136 |
if err == nil { |
| 137 |
t.Errorf("DirFiles(...) = %v, expected an error", err) |
| 138 |
} |
| 139 |
if len(out) > 0 { |
| 140 |
t.Errorf("DirFiles(...) = unexpected files %s", out) |
| 141 |
} |
| 142 |
}) |
| 143 |
} |