From fb959e17493768805045eda86e19a4bebf210bef Mon Sep 17 00:00:00 2001 From: alexmullins Date: Thu, 5 Nov 2015 23:13:02 -0600 Subject: [PATCH] move all password tests to crypto_test.go --- crypto_test.go | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 crypto_test.go diff --git a/crypto_test.go b/crypto_test.go new file mode 100644 index 0000000..b6706a5 --- /dev/null +++ b/crypto_test.go @@ -0,0 +1,102 @@ +package zip + +import ( + "bytes" + "io" + "path/filepath" + "testing" +) + +func TestPasswordSimple(t *testing.T) { + file := "hello-aes.zip" + var buf bytes.Buffer + r, err := OpenReader(filepath.Join("testdata", file)) + if err != nil { + t.Errorf("Expected %s to open: %v.", file, err) + } + defer r.Close() + if len(r.File) != 1 { + t.Errorf("Expected %s to contain one file.", file) + } + f := r.File[0] + if f.FileInfo().Name() != "hello.txt" { + t.Errorf("Expected %s to have a file named hello.txt", file) + } + if f.Method != 0 { + t.Errorf("Expected %s to have its Method set to 0.", file) + } + f.SetPassword([]byte("golang")) + rc, err := f.Open() + if err != nil { + t.Errorf("Expected to open the readcloser: %v.", err) + } + _, err = io.Copy(&buf, rc) + if err != nil { + t.Errorf("Expected to copy bytes: %v.", err) + } + if !bytes.Contains(buf.Bytes(), []byte("Hello World\r\n")) { + t.Errorf("Expected contents were not found.") + } +} + +func TestPasswordHelloWorldAes(t *testing.T) { + file := "world-aes.zip" + expecting := "helloworld" + r, err := OpenReader(filepath.Join("testdata", file)) + if err != nil { + t.Errorf("Expected %s to open: %v", file, err) + } + defer r.Close() + if len(r.File) != 2 { + t.Errorf("Expected %s to contain two files.", file) + } + var b bytes.Buffer + for _, f := range r.File { + if !f.IsEncrypted() { + t.Errorf("Expected %s to be encrypted.", f.FileInfo().Name) + } + f.SetPassword([]byte("golang")) + rc, err := f.Open() + if err != nil { + t.Errorf("Expected to open readcloser: %v", err) + } + defer rc.Close() + if _, err := io.Copy(&b, rc); err != nil { + t.Errorf("Expected to copy bytes to buffer: %v", err) + } + } + if !bytes.Equal([]byte(expecting), b.Bytes()) { + t.Errorf("Expected ending content to be %s instead of %s", expecting, b.Bytes()) + } +} + +func TestPasswordMacbethAct1(t *testing.T) { + file := "macbeth-act1.zip" + expecting := "Exeunt" + var b bytes.Buffer + r, err := OpenReader(filepath.Join("testdata", file)) + if err != nil { + t.Errorf("Expected %s to open: %v", file, err) + } + defer r.Close() + for _, f := range r.File { + if !f.IsEncrypted() { + t.Errorf("Expected %s to be encrypted.", f.Name) + } + f.SetPassword([]byte("golang")) + rc, err := f.Open() + if err != nil { + t.Errorf("Expected to open readcloser: %v", err) + } + defer rc.Close() + if _, err := io.Copy(&b, rc); err != nil { + t.Errorf("Expected to copy bytes to buffer: %v", err) + } + } + if !bytes.Contains(b.Bytes(), []byte(expecting)) { + t.Errorf("Expected to find %s in the buffer %v", expecting, b.Bytes()) + } +} + +// Test for AE-1 vs AE-2 +// Test for tampered data payload, use messWith