Debian Codegraph @

cmd/mime-extractor/mime-extractor_test.go

raw
1 package main
2
3 import (
4 "bytes"
5 "fmt"
6 "io/ioutil"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11 )
12
13 func TestNewDefaultExtractor(t *testing.T) {
14 if newDefaultExtractor() == nil {
15 t.Fatal("Extractor instance should not be nil")
16 }
17 }
18
19 func TestExtractEmptyFilename(t *testing.T) {
20 b := &bytes.Buffer{}
21 testExtractor := &extractor{
22 errOut: b,
23 stdOut: ioutil.Discard,
24 }
25 exitCode := testExtractor.extract("", "")
26 if exitCode != 1 {
27 t.Fatal("Exit code should be 1, failed")
28 }
29 if !strings.Contains(b.String(), "Missing filename argument") {
30 t.Fatal("Should not succeed with an empty filename, failed")
31 }
32 }
33
34 func TestExtractEmptyOutputDirectory(t *testing.T) {
35 b := &bytes.Buffer{}
36 workingDirectoryFn := func() (string, error) {
37 return "", nil
38 }
39 testExtractor := &extractor{
40 errOut: b,
41 stdOut: ioutil.Discard,
42 wd: workingDirectoryFn,
43 }
44 exitCode := testExtractor.extract("some.file", "")
45 if exitCode != 2 {
46 t.Fatal("Exit code should be 2, failed")
47 }
48 if !strings.Contains(b.String(), "Mkdir failed.") {
49 t.Fatal("Should not succeed with an empty output directory, failed")
50 }
51 }
52
53 func TestExtractFailedToOpenFile(t *testing.T) {
54 b := &bytes.Buffer{}
55 testExtractor := &extractor{
56 errOut: b,
57 stdOut: ioutil.Discard,
58 wd: os.Getwd,
59 }
60 exitCode := testExtractor.extract("some.file", "")
61 if exitCode != 1 {
62 t.Fatal("Exit code should be 1, failed")
63 }
64 if !strings.Contains(b.String(), "Failed to open file:") {
65 t.Fatal("File should not exist, failed")
66 }
67 }
68
69 func TestExtractFailedToParse(t *testing.T) {
70 s := &bytes.Buffer{}
71 testExtractor := &extractor{
72 errOut: s,
73 stdOut: ioutil.Discard,
74 wd: os.Getwd,
75 }
76 exitCode := testExtractor.extract(filepath.Join("..", "..", "testdata", "mail", "erroneous.raw"), "")
77 if exitCode != 1 {
78 t.Fatal("Exit code should be 1, failed")
79 }
80 if !strings.Contains(s.String(), "During enmime.ReadEnvelope") {
81 t.Fatal("Should have failed to write the attachment, failed")
82 }
83 }
84
85 func TestExtractAttachmentWriteFail(t *testing.T) {
86 s := &bytes.Buffer{}
87 fw := func(filename string, data []byte, perm os.FileMode) error {
88 return fmt.Errorf("AttachmentWriteFail")
89 }
90 testExtractor := &extractor{
91 errOut: ioutil.Discard,
92 stdOut: s,
93 wd: os.Getwd,
94 fileWrite: fw,
95 }
96 exitCode := testExtractor.extract(filepath.Join("..", "..", "testdata", "mail", "attachment.raw"), "")
97 if exitCode != 0 {
98 t.Fatal("Exit code should be 0, failed")
99 }
100 if !strings.HasSuffix(s.String(), "AttachmentWriteFail\n") {
101 t.Fatal("Should have failed to write the attachment, failed")
102 }
103 }
104
105 func TestExtractSuccess(t *testing.T) {
106 b := &bytes.Buffer{}
107 attachmentCount := 0
108 fw := func(filename string, data []byte, perm os.FileMode) error {
109 attachmentCount++
110 return nil
111 }
112 testExtractor := &extractor{
113 errOut: b,
114 stdOut: ioutil.Discard,
115 wd: os.Getwd,
116 fileWrite: fw,
117 }
118 exitCode := testExtractor.extract(filepath.Join("..", "..", "testdata", "mail", "attachment.raw"), "")
119 if exitCode != 0 {
120 t.Fatal("Exit code should be 0, failed")
121 }
122 if attachmentCount < 1 {
123 t.Fatal("Should be one attachment, failed")
124 }
125 if !strings.Contains(b.String(), "Done!") {
126 t.Fatal("Should have succeeded, failed")
127 }
128 }