Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 503 lines (463 sloc) 19.82 kb
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
#!/opt/bin/ruby -w

#------------------------------------------------------------------------
# Unit tests for gurgitate-mail
#------------------------------------------------------------------------

require 'test/unit'
require 'test/unit/ui/console/testrunner'

class TC_Header < Test::Unit::TestCase

    def setup
        require './gurgitate-mail'
    end

    # def teardown
    # end

    def test_simple_header
        h=Gurgitate::Header.new("From: fromheader@example.com")
        assert_equal(h.name,"From", "Simple header name is From")
        assert_equal(h.contents,"fromheader@example.com",
            "Contents is fromheader@example.com")
        assert_equal(h.value,"fromheader@example.com",
            "Contents is fromheader@example.com")
    end

    # This is an illegal header that turns up in spam sometimes.
    # Crashing when you get spam is bad.
    def test_malcapitalized_header
        h=Gurgitate::Header.new("FROM: fromheader@example.com")
        assert_equal(h.name,"From", "Badly-capitalized header is From")
        assert_equal(h.contents,"fromheader@example.com",
            "Badly-capitalized header")
        assert_equal(h.value,"fromheader@example.com",
            "Badly-capitalized header")
    end

    # I got a message with "X-Qmail-Scanner-1.19" once. I hate whoever did
    # that.
    def test_dot_in_header
        h=Gurgitate::Header.new("From.Header: fromheader@example.com")
        assert_equal(h.name, "From.header",
                     "header with dot in it is From.header")
        assert_equal(h.contents, "fromheader@example.com",
                     "header with dot in it")
        assert_equal(h.value, "fromheader@example.com",
                     "header with dot in it")
    end

    # This is another particularly horrible spamware-generated not-a-header.
    def test_header_that_starts_with_hyphen
        h=Gurgitate::Header.new("-From: -fromheader@example.com")
        assert_equal(h.name, "-From",
                     "header with leading hyphen is -From")
        assert_equal(h.contents, "-fromheader@example.com",
                     "header with leading hyphen")
        assert_equal(h.value, "-fromheader@example.com",
                     "header with leading hyphen")
    end

    # This is another illegal header that turns up in spam sometimes.
    # Crashing when you get spam is bad.
    def test_nonalphabetic_initial_char_header
        h=Gurgitate::Header.new("2From: fromheader@example.com")
        assert_equal(h.name,"2from",
                     "Header that starts with illegal char is 2From")
        assert_equal(h.contents, "fromheader@example.com",
                     "Header that starts with illegal char")
        assert_equal(h.value, "fromheader@example.com",
                     "Header that starts with illegal char")
    end

    def test_bad_headers
        assert_raises(Gurgitate::IllegalHeader,"Empty name") {
            h=Gurgitate::Header.new(": Hi")
        }
# assert_raises(Gurgitate::IllegalHeader,"Empty header contents") {
# h=Gurgitate::Header.new("From: ")
# }
        assert_raises(Gurgitate::IllegalHeader,"Bad header syntax") {
            h=Gurgitate::Header.new("This is completely wrong")
        }
    end
    def test_extending_header
        h=Gurgitate::Header.new("From: fromheader@example.com")
        h << " (Dave Brown)"
        assert_equal(h.name,"From","Extended header is From")
        assert_equal(h.contents,"fromheader@example.com\n (Dave Brown)",
            "Extended header contains all data")
        assert_equal(h.contents,h.value,"Contents same as value")
    end

    def test_empty_header_with_extension
        h=Gurgitate::Header.new("From:")
        h << " fromheader@example.com"
        assert_equal("From",h.name,"Empty extended header is From")
        assert_equal("\n fromheader@example.com", h.contents,
                     "Empty extended header contains all data")
        assert_equal(h.contents, h.value, "Contents same as value")
    end

    def test_changing_header
        h=Gurgitate::Header.new("From: fromheader@example.com")
        h.contents="anotherfromheader@example.com"
        assert_equal(h.contents,"anotherfromheader@example.com",
            "header contents contains new data")
    end

    def test_tabseparated_header
        h=Gurgitate::Header.new("From:\tfromheader@example.com")
        assert_equal(h.name,"From","Tabseparated header is From (bug#154)")
        assert_equal(h.contents,"fromheader@example.com",
            "Tabseparated header's contents are correct (bug#154)")
        assert_equal(h.contents,h.value,"Contents same as value (bug#154)")
    end

    def test_conversion_to_String
        h=Gurgitate::Header.new("From: fromheader@example.com")
        assert_equal(h.to_s,"From: fromheader@example.com", "Conversion to string returns input")
    end

    def test_regex_match
        h=Gurgitate::Header.new("From: fromheader@example.com")
        assert_equal(0,h.matches(/fromheader/),"Matches regex that would match input")
        assert_equal(nil,h.matches(/notininput/),"Does not match regex that would not match input")
    end
end

class TC_Headers < Test::Unit::TestCase
    def setup
        require './gurgitate-mail'
    end

    def test_single_header
        h=Gurgitate::Headers.new(<<'EOF'
From fromline@example.com Sat Sep 27 12:20:25 PDT 2003
From: fromheader@example.com
EOF
        )
        assert_equal("fromline@example.com",h.from)
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
    end

    def test_fromline_simple_username
        h=Gurgitate::Headers.new(<<'EOF'
From fromline Sat Sep 27 12:20:25 PDT 2003
From: fromheader@example.com
EOF
        )
        assert_equal("fromline",h.from)
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
    end

    def test_fromline_no_username
        h=Gurgitate::Headers.new(<<'EOF'
From Sat Sep 27 12:20:25 PDT 2003
From: fromheader@example.com
EOF
        )
        assert_equal("",h.from)
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
    end

    def test_missing_toline
        h=Gurgitate::Headers.new(<<'EOF'
From: fromheader@example.com
EOF
        )
        assert_equal('fromheader@example.com',h.from)
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
    end

    def test_multiple_headers
        h=Gurgitate::Headers.new(<<'EOF'
From fromline@example.com Sat Sep 27 12:20:25 PDT 2003
From: fromheader@example.com
To: toheader@example.com
Subject: Subject line
EOF
        )
        assert_equal(h.from,"fromline@example.com")
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
        assert_equal(1,h["To"].length)
        assert_equal("To",h["To"][0].name)
        assert_equal("toheader@example.com",h["To"][0].contents)
        assert_equal(1,h["Subject"].length)
        assert_equal("Subject",h["Subject"][0].name)
        assert_equal("Subject line",h["Subject"][0].contents)
    end

    def test_missing_fromline
        h=Gurgitate::Headers.new(<<'EOF'
From: fromheader@example.com
To: toheader@example.com
Subject: Subject line
EOF
        )
        assert_equal('fromheader@example.com',h.from)
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
        assert_equal(1,h["To"].length)
        assert_equal("To",h["To"][0].name)
        assert_equal("toheader@example.com",h["To"][0].contents)
        assert_equal(1,h["Subject"].length)
        assert_equal("Subject",h["Subject"][0].name)
        assert_equal("Subject line",h["Subject"][0].contents)
    end

    def test_multiline_headers
        h=Gurgitate::Headers.new(<<'EOF'
From fromline@example.com Sat Oct 25 12:58:31 PDT 2003
From: fromheader@example.com
To: toheader@example.com,
nexttoheader@example.com
Subject: Subject line
EOF
        )
        assert_equal(h.from,"fromline@example.com")
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
        assert_equal(1,h["To"].length)
        assert_equal("To",h["To"][0].name)
        assert_equal("toheader@example.com,\n nexttoheader@example.com",h["To"][0].contents)
        assert_equal(1,h["Subject"].length)
        assert_equal("Subject",h["Subject"][0].name)
        assert_equal("Subject line",h["Subject"][0].contents)
    end

    def test_multiline_headers_with_extra_colons
        h=Gurgitate::Headers.new(<<'EOF'
From fromline@example.com Sat Oct 25 12:58:31 PDT 2003
From: fromheader@example.com
To: toheader@example.com,
nexttoheader@example.com (The test: header)
Subject: Subject line
EOF
        )
        assert_equal(h.from,"fromline@example.com")
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
        assert_equal(1,h["To"].length)
        assert_equal("To",h["To"][0].name)
        assert_equal("toheader@example.com,\n nexttoheader@example.com (The test: header)",h["To"][0].contents)
        assert_equal(1,h["Subject"].length)
        assert_equal("Subject",h["Subject"][0].name)
        assert_equal("Subject line",h["Subject"][0].contents)
    end

    def test_multiline_headers_with_various_levels_of_indentation
        h=Gurgitate::Headers.new(<<'EOF'
From fromline@example.com Sat Oct 25 12:58:31 PDT 2003
From: fromheader@example.com
To: toheader@example.com,
nexttoheader@example.com,
thirdtoheader@example.com,
fourthtoheader@example.com
Subject: Subject line
EOF
        )
        assert_equal(h.from,"fromline@example.com")
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("fromheader@example.com",h["From"][0].contents)
        assert_equal(1,h["To"].length)
        assert_equal("To",h["To"][0].name)
        assert_equal("toheader@example.com,\n nexttoheader@example.com,\n thirdtoheader@example.com,\n fourthtoheader@example.com",h["To"][0].contents)
        assert_equal(1,h["Subject"].length)
        assert_equal("Subject",h["Subject"][0].name)
        assert_equal("Subject line",h["Subject"][0].contents)
    end

    def test_a_header_that_actually_crashed_gurgitate
        h=Gurgitate::Headers.new(<<'EOF'
Return-path: <nifty-bounces@neurotica.com>
Received: from pd8mr3no.prod.shaw.ca
(pd8mr3no-qfe2.prod.shaw.ca [10.0.144.160]) by l-daemon
(iPlanet Messaging Server 5.2 HotFix 1.16 (built May 14 2003))
with ESMTP id <0HO6002FDGPREL@l-daemon> for dagbrown@shaw.ca; Tue,
11 Nov 2003 00:56:15 -0700 (MST)
Received: from pd7mi4no.prod.shaw.ca ([10.0.149.117])
by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.18 (built Jul 28 2003))
with ESMTP id <0HO60055LGPR40@l-daemon> for dagbrown@shaw.ca
(ORCPT dagbrown@shaw.ca); Tue, 11 Nov 2003 00:56:15 -0700 (MST)
Received: from venom.easydns.com (smtp.easyDNS.com [216.220.40.247])
by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.18 (built Jul 28 2003))
with ESMTP id <0HO60079HGPR79@l-daemon> for dagbrown@shaw.ca; Tue,
11 Nov 2003 00:56:15 -0700 (MST)
Received: from ohno.mrbill.net (ohno.mrbill.net [207.200.6.75])
by venom.easydns.com (Postfix) with ESMTP id D6493722BB for
<dagbrown@lart.ca>; Tue, 11 Nov 2003 02:53:50 -0500 (EST)
Received: by ohno.mrbill.net (Postfix) id ED0AD53380; Tue,
11 Nov 2003 01:56:13 -0600 (CST)
Received: from mail.neurotica.com (neurotica.com [207.100.203.161])
by ohno.mrbill.net (Postfix) with ESMTP id 5CD465337F for
<dagbrown@dagbrown.com>; Tue, 11 Nov 2003 01:56:13 -0600 (CST)
Received: from mail.neurotica.com (localhost [127.0.0.1])
by mail.neurotica.com (Postfix) with ESMTP id CDAA2364C; Tue,
11 Nov 2003 02:56:03 -0500 (EST)
Received: from smtpzilla5.xs4all.nl (smtpzilla5.xs4all.nl [194.109.127.141])
by mail.neurotica.com (Postfix) with ESMTP id B6A22361E for
<nifty@neurotica.com>; Tue, 11 Nov 2003 02:56:00 -0500 (EST)
Received: from xs1.xs4all.nl (xs1.xs4all.nl [194.109.21.2])
by smtpzilla5.xs4all.nl (8.12.9/8.12.9) with ESMTP id hAB7u5ZZ042116 for
<nifty@neurotica.com>; Tue, 11 Nov 2003 08:56:05 +0100 (CET)
Received: from xs1.xs4all.nl (wstan@localhost.xs4all.nl [127.0.0.1])
by xs1.xs4all.nl (8.12.10/8.12.9) with ESMTP id hAB7u5xE048677 for
<nifty@neurotica.com>; Tue,
11 Nov 2003 08:56:05 +0100 (CET envelope-from wstan@xs4all.nl)
Received: (from wstan@localhost) by xs1.xs4all.nl (8.12.10/8.12.9/Submit)
id hAB7u4sZ048676 for nifty@neurotica.com; Tue,
11 Nov 2003 08:56:04 +0100 (CET envelope-from wstan)
Date: Tue, 11 Nov 2003 08:56:04 +0100
From: William Staniewicz <wstan@xs4all.nl>
Subject: Re: [nifty] Ping...
In-reply-to: <9636B78C-140B-11D8-9EE6-003065D0C184@nimitzbrood.com>
Sender: nifty-bounces@neurotica.com
To: Nifty <nifty@neurotica.com>
Cc:
Errors-to: nifty-bounces@neurotica.com
Reply-to: Nifty <nifty@neurotica.com>
Message-id: <20031111075604.GE79497@xs4all.nl>
MIME-version: 1.0
Content-type: text/plain; charset=us-ascii
Content-disposition: inline
Precedence: list
X-BeenThere: nifty@mail.neurotica.com
Delivered-to: dagbrown@mrbill.net
Delivered-to: nifty@neurotica.com
User-Agent: Mutt/1.4.1i
X-Original-To: nifty@neurotica.com
References: <9636B78C-140B-11D8-9EE6-003065D0C184@nimitzbrood.com>
X-Mailman-Version: 2.1.2
List-Post: <mailto:nifty@mail.neurotica.com>
List-Subscribe: <http://mail.neurotica.com:8080/mailman/listinfo/nifty>,
<mailto:nifty-request@mail.neurotica.com?subject=subscribe>
List-Unsubscribe: <http://mail.neurotica.com:8080/mailman/listinfo/nifty>,
<mailto:nifty-request@mail.neurotica.com?subject=unsubscribe>
List-Help: <mailto:nifty-request@mail.neurotica.com?subject=help>
List-Id: Nifty <nifty.mail.neurotica.com>
Original-recipient: rfc822;dagbrown@shaw.ca
EOF
        )
        assert_equal(h.from,"nifty-bounces@neurotica.com")
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("William Staniewicz <wstan@xs4all.nl>",h["From"][0].contents)
        assert_equal(1,h["To"].length)
        assert_equal("To",h["To"][0].name)
        assert_equal('Nifty <nifty@neurotica.com>',h["To"][0].contents)

        assert_equal(1,h["Subject"].length)
        assert_equal("Subject",h["Subject"][0].name)
        assert_equal("Re: [nifty] Ping...",h["Subject"][0].contents)
    end

    def another_crashy_test
        h=Gurgitate::Headers.new(<<'EOF'
From HEYITBLEWUP Fri Nov 21 14:41:08 PST 2003
Received: from unknown (harley.radius [192.168.0.123]) by yoda.radius with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2653.13)
id LYN7YZKG; Wed, 9 Jul 2003 14:36:40 -0700
Subject: IAP password
EOF
        )
        assert_equal(h.from,"HEYITBLEWUP")
        assert_equal(1,h["From"].length)
        assert_equal("From",h["From"][0].name)
        assert_equal("IAP password",h["Subject"][0].name)
    end

    def test_fromline_no_hostname # illegal from line
        m=<<'EOF'
From HEYITBLEWUP Sat Mar 27 16:02:12 PST 2004
Received: from ohno.mrbill.net (ohno.mrbill.net [207.200.6.75])
by lart.ca (Postfix) with ESMTP id A485F104CA9
for <dagbrown@lart.ca>; Sat, 27 Mar 2004 15:58:06 -0800 (PST)
Received: by ohno.mrbill.net (Postfix)
id 0D3423A289; Sat, 27 Mar 2004 17:58:42 -0600 (CST)
Delivered-To: dagbrown@mrbill.net
Received: from 66-168-59-126.jvl.wi.charter.com (66-168-59-126.jvl.wi.charter.com [66.168.59.126])
by ohno.mrbill.net (Postfix) with SMTP id 948BD3A288
for <dagbrown@dagbrown.com>; Sat, 27 Mar 2004 17:58:41 -0600 (CST)
X-Message-Info: HOCBSQX
Message-Id: <20040327235841.948BD3A288@ohno.mrbill.net>
Date: Sat, 27 Mar 2004 17:58:41 -0600 (CST)
From: ""@
To: undisclosed-recipients: ;
EOF
        h=Gurgitate::Headers.new(m)
        assert_equal(%{""@},h["From"][0].contents)
        assert_equal(1,h["From"].length)
    end

    def test_editing_header
        m = <<'EOF'
From fromline@example.com Sat Oct 25 12:58:31 PDT 2003
From: fromline@example.com
To: toline@example.com
Subject: Subject line
EOF
        h=Gurgitate::Headers.new(m)
        h["From"]="anotherfromline@example.com"
        assert_equal("anotherfromline@example.com",h["From"][0].contents,
            "From line correctly changed")
        assert_match(/^From: anotherfromline@example.com$/,h.to_s,
            "From line correctly turns up in finished product")
    end

    def test_editing_from
        m = <<'EOF'
From fromline@example.com Sat Oct 25 12:58:31 PDT 2003
From: fromline@example.com
To: toline@example.com
Subject: Subject line
EOF
        h=Gurgitate::Headers.new(m)
        t=Time.new.to_s
        h.from="anotherfromline@example.com"
        assert_equal("anotherfromline@example.com",h.from,
            "Envelope from correctly changed")
        assert_match(/^From anotherfromline@example.com #{t}/,
            h.to_mbox, "Envelope from changed in finished product")
    end

    def test_match_multiple_headers
        m = <<'EOF'
From fromline@example.com Sat Oct 25 12:58:31 PDT 2003
From: fromline@example.com
To: toline@example.com
Subject: Subject line
EOF
        h=Gurgitate::Headers.new(m)
        assert_equal(true,h["From","To"] =~ /fromline@example.com/,
            "headers contains fromline")
        assert_equal(true,h["From","To"] =~ /toline@example.com/,
            "headers contains toline")
        assert_equal(false,h["From","To"] =~ /nonexistent@example.com/,
            "headers do not contain nonexistent value")
        assert_equal(false,h["Rabbit"] =~ /nonexistent/,
            "Asking for a nonexistent header")
    end

    def test_broken_spam
        m=<<'EOF'
Return-Path: kirstenparsonsry@yahoo.com
Delivery-Date: Fri May 21 19:42:02 PDT
Return-Path: kirstenparsonsry@yahoo.com
Delivery-Date: Fri May 21 17:39:51 2004
Return-Path: <kirstenparsonsry@yahoo.com>
X-Original-To: dagbrown@lart.ca
Delivered-To: dagbrown@lart.ca
Received: from anest.co.jp (c-24-1-221-189.client.comcast.net [24.1.221.189])
by lart.ca (Postfix) with ESMTP id 05B7F5704
for <dagbrown@lart.ca>; Fri, 21 May 2004 17:39:51 -0700 (PDT)
Message-ID: <NKELFLPJDPLDHJCMGFHDFEKLLNAA.kirstenparsonsry@yahoo.com>
From: "Kirsten Parsons" <kirstenparsonsry@yahoo.com>
To: dagbrown@lart.ca
Subject: Congrats!
Date: Fri, 21 May 2004 20:56:27 +0000
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: base64
EOF
        h=Gurgitate::Headers.new(m)

        assert_equal(Gurgitate::Header.new("To","dagbrown@lart.ca").contents,
                     h["To"][0].contents,"To header is as expected")

        assert_equal(false,h["To","Cc"] =~ /\blunar@lunar-linux.org\b/i,
            "There should be no Lunar Linux mailing list here")

        assert_equal(false,h["To"] =~ /\blunar@lunar-linux.org\b/i,
            "There should be no Lunar Linux mailing list in To line")
        assert_equal(false,h["Cc"] =~ /\blunar@lunar-linux.org\b/i,
            "There should be no Lunar Linux mailing list in Cc line")
    end
end

def runtests
    Test::Unit::UI::Console::TestRunner.run(TC_Header)
    Test::Unit::UI::Console::TestRunner.run(TC_Headers)
end

if __FILE__ == $0 then
    if(ARGV[0] == '-c')
        require 'coverage'
    end
    runtests
end
Something went wrong with that request. Please try again.