File: test_crypto.rb

package info (click to toggle)
sup-mail 0.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,168 kB
  • sloc: ruby: 12,583; sh: 115; makefile: 9
file content (119 lines) | stat: -rw-r--r-- 4,430 bytes parent folder | download | duplicates (2)
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
# tests for sup's crypto libs
#
# Copyright Clint Byrum <clint@ubuntu.com> 2011. All Rights Reserved.
# Copyright Sup Developers                 2013.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

require 'test_helper'
require 'sup'
require 'stringio'
require 'tmpdir'

module Redwood

class TestCryptoManager < Minitest::Test

    def setup
        @from_email = 'sup-test-1@foo.bar'
        @from_email_ecc = 'sup-fake-ecc@fake.fake'
        @to_email   = 'sup-test-2@foo.bar'
        # Use test gnupg setup
        @orig_gnupghome = ENV['GNUPGHOME']
        ENV['GNUPGHOME'] = File.join(File.dirname(__FILE__), 'gnupg_test_home')

        @path = Dir.mktmpdir
        Redwood::HookManager.init File.join(@path, 'hooks')

        am = {:default=> {name: "test", email: @from_email, alternates: [@from_email_ecc]}}
        Redwood::AccountManager.init am

        Redwood::CryptoManager.init

        if not CryptoManager.have_crypto?
          warn "No crypto set up, crypto will not be tested. Reason: #{CryptoManager.not_working_reason}"
        end
    end

    def teardown
      CryptoManager.deinstantiate!
      AccountManager.deinstantiate!
      HookManager.deinstantiate!
      FileUtils.rm_r @path

      ENV['GNUPGHOME'] = @orig_gnupghome
    end

    def test_sign
        if CryptoManager.have_crypto? then
            signed = CryptoManager.sign @from_email,@to_email,"ABCDEFG"
            assert_instance_of RMail::Message, signed
            assert_equal "ABCDEFG", signed.body[0]
            assert signed.body[1].body.length > 0 , "signature length must be > 0"
            assert (signed.body[1].body.include? "-----BEGIN PGP SIGNATURE-----") , "Expecting PGP armored data"
        end
    end

    def test_encrypt
        if CryptoManager.have_crypto? then
            encrypted = CryptoManager.encrypt @from_email, [@to_email], "ABCDEFG"
            assert_instance_of RMail::Message, encrypted
            assert (encrypted.body[1].body.include? "-----BEGIN PGP MESSAGE-----") , "Expecting PGP armored data"
        end
    end

    def test_sign_and_encrypt
        if CryptoManager.have_crypto? then
            encrypted = CryptoManager.sign_and_encrypt @from_email, [@to_email], "ABCDEFG"
            assert_instance_of RMail::Message, encrypted
            assert (encrypted.body[1].body.include? "-----BEGIN PGP MESSAGE-----") , "Expecting PGP armored data"
        end
    end

    def test_decrypt
        if CryptoManager.have_crypto? then
            encrypted = CryptoManager.encrypt @from_email, [@to_email], "ABCDEFG"
            assert_instance_of RMail::Message, encrypted
            assert_instance_of String, (encrypted.body[1].body)
            decrypted = CryptoManager.decrypt encrypted.body[1], true
            assert_instance_of Array, decrypted
            assert_instance_of Chunk::CryptoNotice, decrypted[0]
            assert_instance_of Chunk::CryptoNotice, decrypted[1]
            assert_instance_of RMail::Message, decrypted[2]
            assert_equal "ABCDEFG" , decrypted[2].body
        end
    end

    def test_verify
        if CryptoManager.have_crypto?
            signed = CryptoManager.sign @from_email, @to_email, "ABCDEFG"
            assert_instance_of RMail::Message, signed
            assert_instance_of String, (signed.body[1].body)
            CryptoManager.verify signed.body[0], signed.body[1], true
        end
    end

    def test_verify_unknown_keytype
        if CryptoManager.have_crypto?
            signed = CryptoManager.sign @from_email_ecc, @to_email, "ABCDEFG"
            assert_instance_of RMail::Message, signed
            assert_instance_of String, (signed.body[1].body)
            CryptoManager.verify signed.body[0], signed.body[1], true
        end
    end
end

end