1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
require 'test_helper'
class GpgTest < MailGpgTestCase
def check_headers(mail = @mail, encrypted = @encrypted)
assert_equal mail.to, encrypted.to
assert_equal mail.cc, encrypted.cc
assert_equal mail.bcc, encrypted.bcc
assert_equal mail.subject, encrypted.subject
end
def check_mime_structure(mail = @mail, encrypted = @encrypted)
assert_equal 2, encrypted.parts.size
v_part, enc_part = encrypted.parts
assert_match /Version: 1/, v_part.to_s
assert_match /application\/pgp-encrypted(?:; charset=UTF-8)?/, v_part.content_type
assert_equal 'application/octet-stream; name=encrypted.asc',
enc_part.content_type
end
def check_attachment_name(mail = @mail, encrypted = @encrypted)
v_part, enc_part = encrypted.parts
assert_equal 'application/octet-stream; name=custom_filename.asc', enc_part.content_type
assert_equal 'inline; filename=custom_filename.asc', enc_part.content_disposition
end
def check_content(mail = @mail, encrypted = @encrypted)
assert enc = encrypted.parts.last
assert clear = GPGME::Crypto.new.decrypt(enc.to_s, password: 'abc').to_s
assert_match /encrypt me/, clear
assert_equal mail.to_s, clear
end
def check_signature(mail = @mail, signed = @signed)
assert signed.signed?
assert signature = signed.parts.detect{|p| p.content_type =~ /signature\.asc/}.body.to_s
assert signed_part = signed.parts.detect{|p| p.content_type !~ /signature\.asc/}
assert_equal mail.parts.size, signed_part.parts.size
GPGME::Crypto.new.verify(signature, signed_text: signed_part.encoded) do |sig|
assert sig.valid?
end
assert Mail::Gpg.signature_valid?(signed)
assert verified = signed.verify
assert verified.verify_result.present?
assert verified.verify_result.signatures.any?
assert verified.signatures.any?
assert verified.signature_valid?
end
def check_mime_structure_signed(mail = @mail, signed = @signed)
assert_match /multipart\/signed/, signed.content_type
assert_equal 2, signed.parts.size
orig_part, sign_part = signed.parts
assert_equal 'application/pgp-signature; name=signature.asc', sign_part.content_type
assert_equal mail.parts.size, orig_part.parts.size
assert_nil orig_part.to
assert_nil orig_part.from
assert_nil orig_part.subject
end
def check_headers_signed(mail = @mail, signed = @signed)
assert_equal mail.to, signed.to
if mail.cc
assert_equal mail.cc, signed.cc
end
if mail.bcc
assert_equal mail.bcc, signed.bcc
end
assert_equal mail.subject, signed.subject
assert_equal mail.return_path, signed.return_path
end
context "gpg installation" do
should "have keys for jane and joe" do
assert joe = GPGME::Key.find(:public, 'joe@foo.bar')
assert_equal 1, joe.size
joe = joe.first
assert jane = GPGME::Key.find(:public, 'jane@foo.bar')
assert_equal 1, jane.size
jane = jane.first
assert id = jane.fingerprint
assert jane = GPGME::Key.find(:public, id).first
assert_equal id, jane.fingerprint
end
end
context "gpg signed" do
setup do
@mail = Mail.new do
to 'joe@foo.bar'
from '<Jane Doe> jane@foo.bar'
subject 'test test'
body 'sign me!'
content_type 'text/plain; charset=UTF-8'
end
end
context 'simple mail' do
setup do
@signed = Mail::Gpg.sign(@mail, password: 'abc')
end
should 'preserve from name' do
assert_equal '<Jane Doe> jane@foo.bar', @signed.header['from'].value
end
should 'have same recipients and subject' do
check_headers_signed
end
should 'have proper gpgmime structure' do
check_mime_structure_signed
end
should 'have correct signature' do
check_signature
end
end
context 'mail with custom headers' do
setup do
@mail.header['X-Custom-Header'] = 'custom value'
@mail.header['Return-Path'] = 'bounces@example.com'
@mail.header['References'] = 'some-message-id'
@signed = Mail::Gpg.sign(@mail, password: 'abc')
end
should 'have same recipients and subject' do
check_headers_signed
end
should 'have proper gpgmime structure' do
check_mime_structure_signed
end
should 'have correct signature' do
check_signature
end
should 'preserve customer header values' do
assert_equal 'custom value', @signed.header['X-Custom-Header'].to_s
assert_equal 'bounces@example.com', @signed.return_path
assert_equal 'some-message-id', @signed.header['References'].value
end
end
context 'mail with multiple recipients' do
setup do
@mail.bcc 'jane@foo.bar'
@signed = Mail::Gpg.sign(@mail, password: 'abc')
end
should 'have same recipients and subject' do
check_headers_signed
end
should 'have proper gpgmime structure' do
check_mime_structure_signed
end
should 'have correct signature' do
check_signature
end
end
context 'multipart alternative mail' do
setup do
@mail = Mail.new do
to 'joe@foo.bar'
from 'jane@foo.bar'
subject 'test test'
text_part do
body 'sign me!'
end
html_part do
body '<h1>H1</h1>'
end
end
@signed = Mail::Gpg.sign(@mail, password: 'abc')
end
should 'have same recipients and subject' do
check_headers_signed
end
should 'have proper gpgmime structure' do
check_mime_structure_signed
end
should 'have correct signature' do
check_signature
end
should 'have multiple parts in original content' do
assert original_part = @signed.parts.first
assert @mail.multipart?
assert_match /alternative/, @mail.content_type
assert_match /alternative/, original_part.content_type
assert_equal original_part.parts.size, @mail.parts.size
assert_match /sign me!/, original_part.parts.first.body.to_s
assert_match /H1/, original_part.parts.last.body.to_s
end
end
end
context "gpg encrypted" do
setup do
@mail = Mail.new do
to 'jane@foo.bar'
from 'joe@foo.bar'
subject 'test test'
body 'encrypt me!'
end
end
context 'simple mail' do
setup do
@encrypted = Mail::Gpg.encrypt(@mail)
end
should 'have same recipients and subject' do
check_headers
end
should 'have proper gpgmime structure' do
check_mime_structure
end
should 'have correctly encrypted content' do
check_content
end
should 'decrypt' do
assert mail = Mail::Gpg.decrypt(@encrypted, { :password => 'abc' })
assert mail == @mail
end
end
context 'simple mail (custom filename)' do
setup do
@encrypted = Mail::Gpg.encrypt(@mail, {filename: 'custom_filename.asc'})
end
should 'have same custom attachment filename' do
check_attachment_name
end
end
context 'simple mail (signed)' do
setup do
@encrypted = Mail::Gpg.encrypt(@mail, { :sign => true, :password => 'abc' })
end
should 'have same recipients and subject' do
check_headers
end
should 'have proper gpgmime structure' do
check_mime_structure
end
should 'have correctly encrypted content' do
check_content
end
should 'decrypt and verify' do
assert mail = Mail::Gpg.decrypt(@encrypted, { :verify => true, :password => 'abc' })
assert mail == @mail
assert mail.verify_result
assert sig = mail.signatures.first
assert sig.to_s =~ /Joe/
assert sig.valid?
end
end
context 'mail with custom header' do
setup do
@mail.header['X-Custom-Header'] = 'custom value'
@mail.header['Return-Path'] = 'bounces@example.com'
@encrypted = Mail::Gpg.encrypt(@mail)
@encrypted.header['X-Another-Header'] = 'another value'
end
should 'have same recipients and subject' do
check_headers
end
should 'have proper gpgmime structure' do
check_mime_structure
end
should 'have correctly encrypted content' do
check_content
end
should 'preserve customer header values' do
assert_equal 'custom value', @encrypted.header['X-Custom-Header'].to_s
assert_equal 'bounces@example.com', @encrypted.return_path
end
context 'when decrypted' do
setup do
@decrypted_mail = Mail::Gpg.decrypt(@encrypted, { :password => 'abc' })
end
should 'have same subject and body as the original' do
assert_equal @mail.subject, @decrypted_mail.subject
assert_equal @mail.body.to_s, @decrypted_mail.body.to_s
end
should 'preserve custom header from encrypted inner mail' do
assert_equal 'custom value', @decrypted_mail.header['X-Custom-Header'].to_s
end
should 'preserve custom header from outer mail' do
assert_equal 'another value', @decrypted_mail.header['X-Another-Header'].to_s
end
end
end
context 'mail with multiple recipients' do
setup do
@mail.bcc 'joe@foo.bar'
@encrypted = Mail::Gpg.encrypt(@mail)
end
should 'have same recipients and subject' do
check_headers
end
should 'have proper gpgmime structure' do
check_mime_structure
end
should 'have correctly encrypted content' do
check_content
end
should "encrypt for all recipients" do
assert encrypted_body = @encrypted.parts.last.to_s
end
should 'decrypt' do
assert mail = Mail::Gpg.decrypt(@encrypted, { :password => 'abc' })
assert mail == @mail
end
end
context 'multipart mail' do
setup do
@mail.add_file 'Rakefile'
@encrypted = Mail::Gpg.encrypt(@mail, sign: true, password: 'abc')
end
should 'have same recipients and subject' do
check_headers
end
should 'have proper gpgmime structure' do
check_mime_structure
end
should 'have correctly encrypted content' do
check_content
end
should 'have multiple parts in encrypted content' do
assert encrypted_body = @encrypted.parts.last.to_s
assert clear = GPGME::Crypto.new.decrypt(encrypted_body.to_s, password: 'abc').to_s
assert m = Mail::Message.new(clear.to_s)
assert m.multipart?
assert_equal 2, m.parts.size
assert_match /encrypt me/, m.parts.first.body.to_s
assert_match /Rakefile/, m.parts.last.content_disposition
end
should 'decrypt and verify' do
assert mail = Mail::Gpg.decrypt(@encrypted, { :verify => true, :password => 'abc' })
assert mail == @mail
assert mail.parts[1] == @mail.parts[1]
assert mail.verify_result
assert signatures = mail.signatures
assert_equal 1, signatures.size
assert sig = signatures[0]
assert sig.to_s =~ /Joe/
assert sig.valid?
end
end
end
end
|