| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/jhillyerd/enmime" |
| 9 |
) |
| 10 |
|
| 11 |
func TestHelperComparePartsEqual(t *testing.T) { |
| 12 |
testCases := []struct { |
| 13 |
name string |
| 14 |
part *enmime.Part |
| 15 |
}{ |
| 16 |
{"nil", nil}, |
| 17 |
{"empty", &enmime.Part{}}, |
| 18 |
{"Parent", &enmime.Part{Parent: &enmime.Part{}}}, |
| 19 |
{"FirstChild", &enmime.Part{FirstChild: &enmime.Part{}}}, |
| 20 |
{"NextSibling", &enmime.Part{NextSibling: &enmime.Part{}}}, |
| 21 |
{"ContentType", &enmime.Part{ContentType: "such/wow"}}, |
| 22 |
{"Disposition", &enmime.Part{Disposition: "irritable"}}, |
| 23 |
{"FileName", &enmime.Part{FileName: "readme.txt"}}, |
| 24 |
{"Charset", &enmime.Part{Charset: "utf-7.999"}}, |
| 25 |
{"PartID", &enmime.Part{PartID: "0.1"}}, |
| 26 |
} |
| 27 |
for _, tc := range testCases { |
| 28 |
t.Run(tc.name, func(t *testing.T) { |
| 29 |
mockt := &testing.T{} |
| 30 |
if !ComparePart(mockt, tc.part, tc.part) { |
| 31 |
t.Errorf("Got false while comparing a Part %v to itself: %+v", tc.name, tc.part) |
| 32 |
} |
| 33 |
if mockt.Failed() { |
| 34 |
t.Errorf("Helper failed test for %q, should have been successful", tc.name) |
| 35 |
} |
| 36 |
}) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// TestHelperComparePartsInequal tests compareParts with differing Parts |
| 41 |
func TestHelperComparePartsInequal(t *testing.T) { |
| 42 |
testCases := []struct { |
| 43 |
name string |
| 44 |
a, b *enmime.Part |
| 45 |
}{ |
| 46 |
{ |
| 47 |
name: "nil", |
| 48 |
a: nil, |
| 49 |
b: &enmime.Part{}, |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "Parent", |
| 53 |
a: &enmime.Part{}, |
| 54 |
b: &enmime.Part{Parent: &enmime.Part{}}, |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "FirstChild", |
| 58 |
a: &enmime.Part{}, |
| 59 |
b: &enmime.Part{FirstChild: &enmime.Part{}}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "NextSibling", |
| 63 |
a: &enmime.Part{}, |
| 64 |
b: &enmime.Part{NextSibling: &enmime.Part{}}, |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "ContentType", |
| 68 |
a: &enmime.Part{ContentType: "text/plain"}, |
| 69 |
b: &enmime.Part{ContentType: "text/html"}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "Disposition", |
| 73 |
a: &enmime.Part{Disposition: "happy"}, |
| 74 |
b: &enmime.Part{Disposition: "sad"}, |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "FileName", |
| 78 |
a: &enmime.Part{FileName: "foo.gif"}, |
| 79 |
b: &enmime.Part{FileName: "bar.jpg"}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "Charset", |
| 83 |
a: &enmime.Part{Charset: "foo"}, |
| 84 |
b: &enmime.Part{Charset: "bar"}, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "PartID", |
| 88 |
a: &enmime.Part{PartID: "0"}, |
| 89 |
b: &enmime.Part{PartID: "1.1"}, |
| 90 |
}, |
| 91 |
} |
| 92 |
|
| 93 |
for _, tc := range testCases { |
| 94 |
t.Run(tc.name, func(t *testing.T) { |
| 95 |
mockt := &testing.T{} |
| 96 |
if ComparePart(mockt, tc.a, tc.b) { |
| 97 |
t.Errorf( |
| 98 |
"Got true while comparing inequal Parts (%v):\n"+ |
| 99 |
"Part A: %+v\nPart B: %+v", tc.name, tc.a, tc.b) |
| 100 |
} |
| 101 |
if tc.name != "" && !mockt.Failed() { |
| 102 |
t.Errorf("Mock test succeeded for %s, should have failed", tc.name) |
| 103 |
} |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// TestOpenTestDataPanic verifies that this function will panic as predicted |
| 109 |
func TestOpenTestDataPanic(t *testing.T) { |
| 110 |
defer func() { |
| 111 |
if r := recover(); r == nil { |
| 112 |
t.Error("OpenTestData did not panic") |
| 113 |
} |
| 114 |
}() |
| 115 |
_ = OpenTestData("invalidDir", "invalidFile") |
| 116 |
} |
| 117 |
|
| 118 |
// TestOpenTestData ensures that the returned io.Reader has the correct underlying type, that |
| 119 |
// the file descriptor referenced is a directory and that we have permission to read it |
| 120 |
func TestOpenTestData(t *testing.T) { |
| 121 |
// this will open a handle to the "testdata" directory |
| 122 |
r := OpenTestData("", "") |
| 123 |
if r == nil { |
| 124 |
t.Error("The returned io.Reader should not be nil") |
| 125 |
} |
| 126 |
|
| 127 |
osFilePtr, ok := r.(*os.File) |
| 128 |
if !ok { |
| 129 |
t.Errorf("Underlying type should be *os.File, but got %T instead", r) |
| 130 |
} |
| 131 |
|
| 132 |
info, err := osFilePtr.Stat() |
| 133 |
if err != nil { |
| 134 |
t.Error("We should have read permission for \"testdata\" directory") |
| 135 |
} |
| 136 |
|
| 137 |
if !info.IsDir() { |
| 138 |
t.Error("File descriptor labeled \"testdata\" should be a directory") |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// TestContentContainsString checks if the string contains a provided sub-string |
| 143 |
func TestContentContainsString(t *testing.T) { |
| 144 |
// Success |
| 145 |
ContentContainsString(t, []byte("someString"), "some") |
| 146 |
// Failure |
| 147 |
ContentContainsString(&testing.T{}, []byte("someString"), "nope") |
| 148 |
} |
| 149 |
|
| 150 |
// TestContentEqualsString checks if the strings are equal |
| 151 |
func TestContentEqualsString(t *testing.T) { |
| 152 |
// Success |
| 153 |
ContentEqualsString(t, []byte("someString"), "someString") |
| 154 |
// Failure |
| 155 |
ContentEqualsString(&testing.T{}, []byte("someString"), "nope") |
| 156 |
} |
| 157 |
|
| 158 |
// TestContentEqualsBytes checks if the slices of bytes are equal |
| 159 |
func TestContentEqualsBytes(t *testing.T) { |
| 160 |
// Success |
| 161 |
ContentEqualsBytes(t, []byte("someString"), []byte("someString")) |
| 162 |
// Failure |
| 163 |
ContentEqualsBytes(&testing.T{}, []byte("someString"), []byte("nope")) |
| 164 |
} |
| 165 |
|
| 166 |
// TestCompareEnvelope checks all publicly accessible members of an envelope for differences |
| 167 |
func TestCompareEnvelope(t *testing.T) { |
| 168 |
fileA, err := os.Open(filepath.Join("..", "..", "testdata", "mail", "attachment.raw")) |
| 169 |
if err != nil { |
| 170 |
t.Error(err) |
| 171 |
} |
| 172 |
envelopeA, err := enmime.ReadEnvelope(fileA) |
| 173 |
if err != nil { |
| 174 |
t.Error(err) |
| 175 |
} |
| 176 |
|
| 177 |
// Success |
| 178 |
success := CompareEnvelope(t, envelopeA, envelopeA) |
| 179 |
if !success { |
| 180 |
t.Error("Same file should have identical envelopes") |
| 181 |
} |
| 182 |
|
| 183 |
// Success on "want" and "got" nil |
| 184 |
success = CompareEnvelope(t, nil, nil) |
| 185 |
if !success { |
| 186 |
t.Error("Comparing nil to nil should result in true") |
| 187 |
} |
| 188 |
|
| 189 |
// Fail on "got" nil |
| 190 |
success = CompareEnvelope(&testing.T{}, nil, envelopeA) |
| 191 |
if success { |
| 192 |
t.Error("Got is nil, envelopeA should not be the same") |
| 193 |
} |
| 194 |
|
| 195 |
// Fail on "want" nil |
| 196 |
success = CompareEnvelope(&testing.T{}, envelopeA, nil) |
| 197 |
if success { |
| 198 |
t.Error("Want is nil, envelopeA should not be the same") |
| 199 |
} |
| 200 |
|
| 201 |
// Fail on root Part mismatch nil |
| 202 |
envelopeB := *envelopeA |
| 203 |
envelopeB.Root = nil |
| 204 |
success = CompareEnvelope(&testing.T{}, envelopeA, &envelopeB) |
| 205 |
if success { |
| 206 |
t.Error("Envelope Root parts should not be the same") |
| 207 |
} |
| 208 |
envelopeB.Root = envelopeA.Root |
| 209 |
|
| 210 |
// Fail on Text mismatch |
| 211 |
envelopeB.Text = "mismatch" |
| 212 |
success = CompareEnvelope(&testing.T{}, envelopeA, &envelopeB) |
| 213 |
if success { |
| 214 |
t.Error("Envelope Text parts should not be the same") |
| 215 |
} |
| 216 |
envelopeB.Text = envelopeA.Text |
| 217 |
|
| 218 |
// Fail on HTML mismatch |
| 219 |
envelopeB.HTML = "mismatch" |
| 220 |
success = CompareEnvelope(&testing.T{}, envelopeA, &envelopeB) |
| 221 |
if success { |
| 222 |
t.Error("Envelope HTML parts should not be the same") |
| 223 |
} |
| 224 |
envelopeB.HTML = envelopeA.HTML |
| 225 |
|
| 226 |
// Fail on Attachment count mismatch |
| 227 |
envelopeB.Attachments = append(envelopeB.Attachments, &enmime.Part{}) |
| 228 |
success = CompareEnvelope(&testing.T{}, envelopeA, &envelopeB) |
| 229 |
if success { |
| 230 |
t.Error("Envelope Attachment slices should not be the same") |
| 231 |
} |
| 232 |
envelopeB.Attachments = envelopeA.Attachments |
| 233 |
|
| 234 |
// Fail on Inlines count mismatch |
| 235 |
envelopeB.Inlines = append(envelopeB.Inlines, &enmime.Part{}) |
| 236 |
success = CompareEnvelope(&testing.T{}, envelopeA, &envelopeB) |
| 237 |
if success { |
| 238 |
t.Error("Envelope Inlines slices should not be the same") |
| 239 |
} |
| 240 |
envelopeB.Inlines = envelopeA.Inlines |
| 241 |
|
| 242 |
// Fail on OtherParts count mismatch |
| 243 |
envelopeB.OtherParts = append(envelopeB.OtherParts, &enmime.Part{}) |
| 244 |
success = CompareEnvelope(&testing.T{}, envelopeA, &envelopeB) |
| 245 |
if success { |
| 246 |
t.Error("Envelope OtherParts slices should not be the same") |
| 247 |
} |
| 248 |
envelopeB.OtherParts = envelopeA.OtherParts |
| 249 |
|
| 250 |
// Fail on Errors count mismatch |
| 251 |
envelopeB.Errors = append(envelopeB.Errors, &enmime.Error{}) |
| 252 |
success = CompareEnvelope(&testing.T{}, envelopeA, &envelopeB) |
| 253 |
if success { |
| 254 |
t.Error("Envelope Errors slices should not be the same") |
| 255 |
} |
| 256 |
envelopeB.Errors = envelopeA.Errors |
| 257 |
} |