File: EnigmaMimeMessage.php

package info (click to toggle)
roundcube 1.6.11%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,868 kB
  • sloc: javascript: 195,588; php: 76,818; sql: 3,150; sh: 2,882; pascal: 1,079; makefile: 234; xml: 93; perl: 73; ansic: 48; python: 21
file content (201 lines) | stat: -rw-r--r-- 6,773 bytes parent folder | download
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
<?php

namespace Roundcube\Plugins\Tests;

use PHPUnit\Framework\TestCase;

class Tests_EnigmaMimeMessage extends TestCase
{
    static function setUpBeforeClass(): void
    {
        include_once INSTALL_PATH . 'plugins/enigma/lib/enigma_mime_message.php';
    }

    /**
     * Test isMultipart()
     */
    function test_is_multipart()
    {
        $mime     = new \Mail_mime();
        $message1 = new \enigma_mime_message($mime, \enigma_mime_message::PGP_SIGNED);

        $this->assertSame(false, $message1->isMultipart());

        $mime->setHTMLBody('<html></html>');
        $message = new \enigma_mime_message($mime, \enigma_mime_message::PGP_SIGNED);

        $this->assertSame(true, $message->isMultipart());

        $message = new \enigma_mime_message($message1, \enigma_mime_message::PGP_SIGNED);

        $this->assertSame(true, $message->isMultipart());
    }

    /**
     * Test getFromAddress()
     */
    function test_get_from_address()
    {
        $mime    = new \Mail_mime();
        $message = new \enigma_mime_message($mime, \enigma_mime_message::PGP_SIGNED);

        $this->assertSame(null, $message->getFromAddress());

        $mime->setFrom('test@domain.com');
        $message = new \enigma_mime_message($mime, \enigma_mime_message::PGP_SIGNED);

        $this->assertSame('test@domain.com', $message->getFromAddress());
    }

    /**
     * Test getRecipients()
     */
    function test_get_recipients()
    {
        $mime = new \Mail_mime();
        $mime->setFrom('test1@domain.com');
        $mime->addTo('<test2@domain.com>, undisclosed-recipients:');
        $mime->addCc('<test3@domain.com>');
        $mime->addBcc('test4@domain.com');

        $expected = ['test2@domain.com', 'test3@domain.com', 'test4@domain.com'];

        $message = new \enigma_mime_message($mime, \enigma_mime_message::PGP_SIGNED);

        $this->assertSame($expected, $message->getRecipients());
    }

    /**
     * Test getOrigBody()
     */
    function test_get_orig_body()
    {
        $mime = new \Mail_mime();
        $mime->setTXTBody('test body');
        $message = new \enigma_mime_message($mime, \enigma_mime_message::PGP_SIGNED);

        $expected = "Content-Transfer-Encoding: quoted-printable\r\n"
            . "Content-Type: text/plain; charset=ISO-8859-1\r\n"
            . "\r\n"
            . "test body\r\n";

        $this->assertSame($expected, $message->getOrigBody());
    }

    /**
     * Test get()
     */
    function test_get()
    {
        $mime = new \Mail_mime();
        $mime->setTXTBody('test body');

        $message = new \enigma_mime_message($mime, \enigma_mime_message::PGP_SIGNED);

        $expected = "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)\r\n"
            ."\r\n"
            . "--=_%x\r\n"
            . "Content-Transfer-Encoding: quoted-printable\r\n"
            . "Content-Type: text/plain; charset=ISO-8859-1\r\n"
            . "\r\n"
            . "test body\r\n"
            . "\r\n"
            . "--=_%x--\r\n";

        $signed_headers = "MIME-Version: 1.0\r\n"
            . "Content-Type: multipart/signed;\r\n"
            . " protocol=\"application/pgp-signature\";\r\n"
            . " boundary=\"=_%x\"\r\n";

        // Note: The str_replace() below is for phpunit <= 6.5

        $this->assertStringMatchesFormat(
            str_replace("\r\n", "\n", $expected),
            str_replace("\r\n", "\n", $message->get())
        );
        $this->assertStringMatchesFormat(
            str_replace("\r\n", "\n", $signed_headers),
            str_replace("\r\n", "\n", $message->txtHeaders())
        );

        $mime = new \Mail_mime();
        $mime->setTXTBody('test body');
        $message = new \enigma_mime_message($mime, \enigma_mime_message::PGP_SIGNED);
        $message->addPGPSignature('signature', 'algorithm');

        $signed = "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)\r\n"
            ."\r\n"
            . "--=_%x\r\n"
            . "Content-Transfer-Encoding: quoted-printable\r\n"
            . "Content-Type: text/plain; charset=ISO-8859-1\r\n"
            . "\r\n"
            . "test body\r\n"
            . "\r\n"
            . "--=_%x\r\n"
            . "Content-Type: application/pgp-signature;\r\n"
            . " name=signature.asc\r\n"
            . "Content-Disposition: attachment;\r\n"
            . " filename=signature.asc;\r\n"
            . " size=9\r\n"
            . "Content-Description: OpenPGP digital signature\r\n"
            . "\r\n"
            . "signature\r\n"
            . "--=_%x--\r\n";

        $signed_headers = "MIME-Version: 1.0\r\n"
            . "Content-Type: multipart/signed;\r\n"
            . " protocol=\"application/pgp-signature\";\r\n"
            . " boundary=\"=_%x\";\r\n"
            . " micalg=pgp-algorithm\r\n";

        // Note: The str_replace() below is for phpunit <= 6.5

        $this->assertStringMatchesFormat(
            str_replace("\r\n", "\n", $signed),
            str_replace("\r\n", "\n", $message->get())
        );
        $this->assertStringMatchesFormat(
            str_replace("\r\n", "\n", $signed_headers),
            str_replace("\r\n", "\n", $message->txtHeaders())
        );

        $mime = new \Mail_mime();
        $mime->setTXTBody('test body');
        $message = new \enigma_mime_message($mime, \enigma_mime_message::PGP_ENCRYPTED);
        $message->setPGPEncryptedBody('encrypted body');

        $encrypted = "This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156)\r\n"
            ."\r\n"
            . "--=_%x\r\n"
            . "Content-Type: application/pgp-encrypted\r\n"
            . "Content-Description: PGP/MIME version identification\r\n"
            . "\r\n"
            . "Version: 1\r\n"
            . "--=_%x\r\n"
            . "Content-Type: application/octet-stream;\r\n"
            . " name=encrypted.asc\r\n"
            . "Content-Disposition: inline;\r\n"
            . " filename=encrypted.asc;\r\n"
            . " size=14\r\n"
            . "Content-Description: PGP/MIME encrypted message\r\n"
            . "\r\n"
            . "encrypted body\r\n"
            . "--=_%x--\r\n";

        $encrypted_headers = "MIME-Version: 1.0\r\n"
            . "Content-Type: multipart/encrypted;\r\n"
            . " protocol=\"application/pgp-encrypted\";\r\n"
            . " boundary=\"=_%x\"\r\n";

        // Note: The str_replace() below is for phpunit <= 6.5

        $this->assertStringMatchesFormat(
            str_replace("\r\n", "\n", $encrypted),
            str_replace("\r\n", "\n", $message->get())
        );
        $this->assertStringMatchesFormat(
            str_replace("\r\n", "\n", $encrypted_headers),
            str_replace("\r\n", "\n", $message->txtHeaders())
        );
    }
}