| 1 |
package proton |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/ProtonMail/gluon/rfc822" |
| 7 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
func TestSendDraftReq_AddMIMEPackage(t *testing.T) { |
| 12 |
key, err := crypto.GenerateKey("name", "email", "rsa", 2048) |
| 13 |
require.NoError(t, err) |
| 14 |
|
| 15 |
kr, err := crypto.NewKeyRing(key) |
| 16 |
require.NoError(t, err) |
| 17 |
|
| 18 |
tests := []struct { |
| 19 |
name string |
| 20 |
mimeBody string |
| 21 |
prefs map[string]SendPreferences |
| 22 |
wantErr bool |
| 23 |
}{ |
| 24 |
{ |
| 25 |
name: "Clear MIME with detached signature", |
| 26 |
mimeBody: "this is a mime body", |
| 27 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 28 |
Encrypt: false, |
| 29 |
SignatureType: DetachedSignature, |
| 30 |
EncryptionScheme: ClearMIMEScheme, |
| 31 |
MIMEType: rfc822.MultipartMixed, |
| 32 |
}}, |
| 33 |
wantErr: false, |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "Clear MIME with no signature (error)", |
| 37 |
mimeBody: "this is a mime body", |
| 38 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 39 |
Encrypt: false, |
| 40 |
SignatureType: NoSignature, |
| 41 |
EncryptionScheme: ClearMIMEScheme, |
| 42 |
MIMEType: rfc822.MultipartMixed, |
| 43 |
}}, |
| 44 |
wantErr: true, |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "Clear MIME with plain text (error)", |
| 48 |
mimeBody: "this is a mime body", |
| 49 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 50 |
Encrypt: false, |
| 51 |
SignatureType: DetachedSignature, |
| 52 |
EncryptionScheme: ClearMIMEScheme, |
| 53 |
MIMEType: rfc822.TextPlain, |
| 54 |
}}, |
| 55 |
wantErr: true, |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "Clear MIME with rich text (error)", |
| 59 |
mimeBody: "this is a mime body", |
| 60 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 61 |
Encrypt: false, |
| 62 |
SignatureType: DetachedSignature, |
| 63 |
EncryptionScheme: ClearMIMEScheme, |
| 64 |
MIMEType: rfc822.TextHTML, |
| 65 |
}}, |
| 66 |
wantErr: true, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "PGP MIME with detached signature", |
| 70 |
mimeBody: "this is a mime body", |
| 71 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 72 |
Encrypt: true, |
| 73 |
PubKey: kr, |
| 74 |
SignatureType: DetachedSignature, |
| 75 |
EncryptionScheme: PGPMIMEScheme, |
| 76 |
MIMEType: rfc822.MultipartMixed, |
| 77 |
}}, |
| 78 |
wantErr: false, |
| 79 |
}, |
| 80 |
{ |
| 81 |
name: "PGP MIME with plain text (error)", |
| 82 |
mimeBody: "this is a mime body", |
| 83 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 84 |
Encrypt: true, |
| 85 |
PubKey: kr, |
| 86 |
SignatureType: DetachedSignature, |
| 87 |
EncryptionScheme: PGPMIMEScheme, |
| 88 |
MIMEType: rfc822.TextPlain, |
| 89 |
}}, |
| 90 |
wantErr: true, |
| 91 |
}, |
| 92 |
{ |
| 93 |
name: "PGP MIME with rich text (error)", |
| 94 |
mimeBody: "this is a mime body", |
| 95 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 96 |
Encrypt: true, |
| 97 |
PubKey: kr, |
| 98 |
SignatureType: DetachedSignature, |
| 99 |
EncryptionScheme: PGPMIMEScheme, |
| 100 |
MIMEType: rfc822.TextHTML, |
| 101 |
}}, |
| 102 |
wantErr: true, |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: "PGP MIME with missing public key (error)", |
| 106 |
mimeBody: "this is a mime body", |
| 107 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 108 |
Encrypt: true, |
| 109 |
SignatureType: DetachedSignature, |
| 110 |
EncryptionScheme: PGPMIMEScheme, |
| 111 |
MIMEType: rfc822.MultipartMixed, |
| 112 |
}}, |
| 113 |
wantErr: true, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "PGP MIME with no signature (error)", |
| 117 |
mimeBody: "this is a mime body", |
| 118 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 119 |
Encrypt: true, |
| 120 |
PubKey: kr, |
| 121 |
SignatureType: NoSignature, |
| 122 |
EncryptionScheme: PGPMIMEScheme, |
| 123 |
MIMEType: rfc822.MultipartMixed, |
| 124 |
}}, |
| 125 |
wantErr: true, |
| 126 |
}, |
| 127 |
} |
| 128 |
|
| 129 |
for _, tt := range tests { |
| 130 |
t.Run(tt.name, func(t *testing.T) { |
| 131 |
var req SendDraftReq |
| 132 |
|
| 133 |
if err := req.AddMIMEPackage(kr, tt.mimeBody, tt.prefs); (err != nil) != tt.wantErr { |
| 134 |
t.Errorf("SendDraftReq.AddMIMEPackage() error = %v, wantErr %v", err, tt.wantErr) |
| 135 |
} |
| 136 |
}) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func TestSendDraftReq_AddPackage(t *testing.T) { |
| 141 |
key, err := crypto.GenerateKey("name", "email", "rsa", 2048) |
| 142 |
require.NoError(t, err) |
| 143 |
|
| 144 |
kr, err := crypto.NewKeyRing(key) |
| 145 |
require.NoError(t, err) |
| 146 |
|
| 147 |
tests := []struct { |
| 148 |
name string |
| 149 |
body string |
| 150 |
mimeType rfc822.MIMEType |
| 151 |
prefs map[string]SendPreferences |
| 152 |
attKeys map[string]*crypto.SessionKey |
| 153 |
wantErr bool |
| 154 |
}{ |
| 155 |
{ |
| 156 |
name: "internal plain text with detached signature", |
| 157 |
body: "this is a text/plain body", |
| 158 |
mimeType: rfc822.TextPlain, |
| 159 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 160 |
Encrypt: true, |
| 161 |
PubKey: kr, |
| 162 |
SignatureType: DetachedSignature, |
| 163 |
EncryptionScheme: InternalScheme, |
| 164 |
MIMEType: rfc822.TextPlain, |
| 165 |
}}, |
| 166 |
wantErr: false, |
| 167 |
}, |
| 168 |
{ |
| 169 |
name: "internal rich text with detached signature", |
| 170 |
body: "this is a text/html body", |
| 171 |
mimeType: rfc822.TextHTML, |
| 172 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 173 |
Encrypt: true, |
| 174 |
PubKey: kr, |
| 175 |
SignatureType: DetachedSignature, |
| 176 |
EncryptionScheme: InternalScheme, |
| 177 |
MIMEType: rfc822.TextHTML, |
| 178 |
}}, |
| 179 |
wantErr: false, |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "internal rich text with bad package content type (error)", |
| 183 |
body: "this is a text/html body", |
| 184 |
mimeType: "bad content type", |
| 185 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 186 |
Encrypt: true, |
| 187 |
PubKey: kr, |
| 188 |
SignatureType: DetachedSignature, |
| 189 |
EncryptionScheme: InternalScheme, |
| 190 |
MIMEType: rfc822.TextHTML, |
| 191 |
}}, |
| 192 |
wantErr: true, |
| 193 |
}, |
| 194 |
{ |
| 195 |
name: "internal rich text with bad recipient content type (error)", |
| 196 |
body: "this is a text/html body", |
| 197 |
mimeType: rfc822.TextHTML, |
| 198 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 199 |
Encrypt: true, |
| 200 |
PubKey: kr, |
| 201 |
SignatureType: DetachedSignature, |
| 202 |
EncryptionScheme: InternalScheme, |
| 203 |
MIMEType: "bad content type", |
| 204 |
}}, |
| 205 |
wantErr: true, |
| 206 |
}, |
| 207 |
{ |
| 208 |
name: "internal with multipart (error)", |
| 209 |
body: "this is a text/html body", |
| 210 |
mimeType: rfc822.MultipartMixed, |
| 211 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 212 |
Encrypt: true, |
| 213 |
PubKey: kr, |
| 214 |
SignatureType: DetachedSignature, |
| 215 |
EncryptionScheme: InternalScheme, |
| 216 |
MIMEType: rfc822.MultipartMixed, |
| 217 |
}}, |
| 218 |
wantErr: true, |
| 219 |
}, |
| 220 |
{ |
| 221 |
name: "internal without encryption (error)", |
| 222 |
body: "this is a text/html body", |
| 223 |
mimeType: rfc822.TextHTML, |
| 224 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 225 |
Encrypt: false, |
| 226 |
PubKey: kr, |
| 227 |
SignatureType: DetachedSignature, |
| 228 |
EncryptionScheme: InternalScheme, |
| 229 |
MIMEType: rfc822.TextHTML, |
| 230 |
}}, |
| 231 |
wantErr: true, |
| 232 |
}, |
| 233 |
{ |
| 234 |
name: "internal without pubkey (error)", |
| 235 |
body: "this is a text/html body", |
| 236 |
mimeType: rfc822.TextHTML, |
| 237 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 238 |
Encrypt: true, |
| 239 |
SignatureType: DetachedSignature, |
| 240 |
EncryptionScheme: InternalScheme, |
| 241 |
MIMEType: rfc822.TextHTML, |
| 242 |
}}, |
| 243 |
wantErr: true, |
| 244 |
}, |
| 245 |
{ |
| 246 |
name: "internal without signature (error)", |
| 247 |
body: "this is a text/html body", |
| 248 |
mimeType: rfc822.TextHTML, |
| 249 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 250 |
Encrypt: true, |
| 251 |
PubKey: kr, |
| 252 |
SignatureType: NoSignature, |
| 253 |
EncryptionScheme: InternalScheme, |
| 254 |
MIMEType: rfc822.TextHTML, |
| 255 |
}}, |
| 256 |
wantErr: true, |
| 257 |
}, |
| 258 |
{ |
| 259 |
name: "clear rich text without signature", |
| 260 |
body: "this is a text/html body", |
| 261 |
mimeType: rfc822.TextHTML, |
| 262 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 263 |
Encrypt: false, |
| 264 |
SignatureType: NoSignature, |
| 265 |
EncryptionScheme: ClearScheme, |
| 266 |
MIMEType: rfc822.TextHTML, |
| 267 |
}}, |
| 268 |
wantErr: false, |
| 269 |
}, |
| 270 |
{ |
| 271 |
name: "clear plain text without signature", |
| 272 |
body: "this is a text/plain body", |
| 273 |
mimeType: rfc822.TextPlain, |
| 274 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 275 |
Encrypt: false, |
| 276 |
SignatureType: NoSignature, |
| 277 |
EncryptionScheme: ClearScheme, |
| 278 |
MIMEType: rfc822.TextPlain, |
| 279 |
}}, |
| 280 |
wantErr: false, |
| 281 |
}, |
| 282 |
{ |
| 283 |
name: "clear plain text with signature", |
| 284 |
body: "this is a text/plain body", |
| 285 |
mimeType: rfc822.TextPlain, |
| 286 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 287 |
Encrypt: false, |
| 288 |
SignatureType: DetachedSignature, |
| 289 |
EncryptionScheme: ClearScheme, |
| 290 |
MIMEType: rfc822.TextPlain, |
| 291 |
}}, |
| 292 |
wantErr: false, |
| 293 |
}, |
| 294 |
{ |
| 295 |
name: "clear plain text with bad scheme (error)", |
| 296 |
body: "this is a text/plain body", |
| 297 |
mimeType: rfc822.TextPlain, |
| 298 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 299 |
Encrypt: false, |
| 300 |
SignatureType: DetachedSignature, |
| 301 |
EncryptionScheme: PGPInlineScheme, |
| 302 |
MIMEType: rfc822.TextPlain, |
| 303 |
}}, |
| 304 |
wantErr: true, |
| 305 |
}, |
| 306 |
{ |
| 307 |
name: "clear rich text with signature (error)", |
| 308 |
body: "this is a text/html body", |
| 309 |
mimeType: rfc822.TextHTML, |
| 310 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 311 |
Encrypt: false, |
| 312 |
SignatureType: DetachedSignature, |
| 313 |
EncryptionScheme: ClearScheme, |
| 314 |
MIMEType: rfc822.TextHTML, |
| 315 |
}}, |
| 316 |
wantErr: true, |
| 317 |
}, |
| 318 |
{ |
| 319 |
name: "encrypted plain text with signature", |
| 320 |
body: "this is a text/plain body", |
| 321 |
mimeType: rfc822.TextPlain, |
| 322 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 323 |
Encrypt: true, |
| 324 |
PubKey: kr, |
| 325 |
SignatureType: DetachedSignature, |
| 326 |
EncryptionScheme: PGPInlineScheme, |
| 327 |
MIMEType: rfc822.TextPlain, |
| 328 |
}}, |
| 329 |
wantErr: false, |
| 330 |
}, |
| 331 |
{ |
| 332 |
name: "encrypted html text with signature (error)", |
| 333 |
body: "this is a text/html body", |
| 334 |
mimeType: rfc822.TextHTML, |
| 335 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 336 |
Encrypt: true, |
| 337 |
PubKey: kr, |
| 338 |
SignatureType: DetachedSignature, |
| 339 |
EncryptionScheme: PGPInlineScheme, |
| 340 |
MIMEType: rfc822.TextHTML, |
| 341 |
}}, |
| 342 |
wantErr: true, |
| 343 |
}, |
| 344 |
{ |
| 345 |
name: "encrypted mixed text with signature (error)", |
| 346 |
body: "this is a multipart/mixed body", |
| 347 |
mimeType: rfc822.MultipartMixed, |
| 348 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 349 |
Encrypt: true, |
| 350 |
PubKey: kr, |
| 351 |
SignatureType: DetachedSignature, |
| 352 |
EncryptionScheme: PGPInlineScheme, |
| 353 |
MIMEType: rfc822.MultipartMixed, |
| 354 |
}}, |
| 355 |
wantErr: true, |
| 356 |
}, |
| 357 |
{ |
| 358 |
name: "encrypted for outside (error)", |
| 359 |
body: "this is a text/plain body", |
| 360 |
mimeType: rfc822.TextPlain, |
| 361 |
prefs: map[string]SendPreferences{"[email protected]": { |
| 362 |
Encrypt: true, |
| 363 |
PubKey: kr, |
| 364 |
SignatureType: DetachedSignature, |
| 365 |
EncryptionScheme: EncryptedOutsideScheme, |
| 366 |
MIMEType: rfc822.TextPlain, |
| 367 |
}}, |
| 368 |
wantErr: true, |
| 369 |
}, |
| 370 |
} |
| 371 |
|
| 372 |
for _, tt := range tests { |
| 373 |
t.Run(tt.name, func(t *testing.T) { |
| 374 |
var req SendDraftReq |
| 375 |
|
| 376 |
if err := req.AddTextPackage(kr, tt.body, tt.mimeType, tt.prefs, tt.attKeys); (err != nil) != tt.wantErr { |
| 377 |
t.Errorf("SendDraftReq.AddPackage() error = %v, wantErr %v", err, tt.wantErr) |
| 378 |
} |
| 379 |
}) |
| 380 |
} |
| 381 |
} |