summaryrefslogtreecommitdiff
path: root/test/net
diff options
context:
space:
mode:
Diffstat (limited to 'test/net')
-rw-r--r--test/net/fixtures/cacert.pem24
-rw-r--r--test/net/fixtures/dhparams.pem29
-rw-r--r--test/net/fixtures/server.crt82
-rw-r--r--test/net/fixtures/server.key28
-rw-r--r--test/net/ftp/test_buffered_socket.rb48
-rw-r--r--test/net/ftp/test_ftp.rb2471
-rw-r--r--test/net/ftp/test_mlsx_entry.rb98
-rw-r--r--test/net/http/test_buffered_io.rb18
-rw-r--r--test/net/http/test_http.rb1186
-rw-r--r--test/net/http/test_http_request.rb80
-rw-r--r--test/net/http/test_httpheader.rb122
-rw-r--r--test/net/http/test_httpresponse.rb437
-rw-r--r--test/net/http/test_httpresponses.rb25
-rw-r--r--test/net/http/test_https.rb217
-rw-r--r--test/net/http/test_https_proxy.rb40
-rw-r--r--test/net/http/utils.rb110
-rw-r--r--test/net/imap/Makefile15
-rw-r--r--test/net/imap/test_imap.rb769
-rw-r--r--test/net/imap/test_imap_response_parser.rb322
-rw-r--r--test/net/pop/test_pop.rb166
-rw-r--r--test/net/protocol/test_protocol.rb29
-rw-r--r--test/net/smtp/test_response.rb100
-rw-r--r--test/net/smtp/test_smtp.rb200
-rw-r--r--test/net/smtp/test_ssl_socket.rb97
24 files changed, 6687 insertions, 26 deletions
diff --git a/test/net/fixtures/cacert.pem b/test/net/fixtures/cacert.pem
new file mode 100644
index 0000000000..f623bd62ed
--- /dev/null
+++ b/test/net/fixtures/cacert.pem
@@ -0,0 +1,24 @@
+-----BEGIN CERTIFICATE-----
+MIID7TCCAtWgAwIBAgIJAIltvxrFAuSnMA0GCSqGSIb3DQEBCwUAMIGMMQswCQYD
+VQQGEwJKUDEQMA4GA1UECAwHU2hpbWFuZTEUMBIGA1UEBwwLTWF0ei1lIGNpdHkx
+FzAVBgNVBAoMDlJ1YnkgQ29yZSBUZWFtMRUwEwYDVQQDDAxSdWJ5IFRlc3QgQ0Ex
+JTAjBgkqhkiG9w0BCQEWFnNlY3VyaXR5QHJ1YnktbGFuZy5vcmcwHhcNMTkwMTAy
+MDI1ODI4WhcNMjQwMTAxMDI1ODI4WjCBjDELMAkGA1UEBhMCSlAxEDAOBgNVBAgM
+B1NoaW1hbmUxFDASBgNVBAcMC01hdHotZSBjaXR5MRcwFQYDVQQKDA5SdWJ5IENv
+cmUgVGVhbTEVMBMGA1UEAwwMUnVieSBUZXN0IENBMSUwIwYJKoZIhvcNAQkBFhZz
+ZWN1cml0eUBydWJ5LWxhbmcub3JnMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
+CgKCAQEAznlbjRVhz1NlutHVrhcGnK8W0qug2ujKXv1njSC4U6nJF6py7I9EeehV
+SaKePyv+I9z3K1LnfUHOtUbdwdKC77yN66A6q2aqzu5q09/NSykcZGOIF0GuItYI
+3nvW3IqBddff2ffsyR+9pBjfb5AIPP08WowF9q4s1eGULwZc4w2B8PFhtxYANd7d
+BvGLXFlcufv9tDtzyRi4t7eqxCRJkZQIZNZ6DHHIJrNxejOILfHLarI12yk8VK6L
+2LG4WgGqyeePiRyd1o1MbuiAFYqAwpXNUbRKg5NaZGwBHZk8UZ+uFKt1QMBURO5R
+WFy1c349jbWszTqFyL4Lnbg9HhAowQIDAQABo1AwTjAdBgNVHQ4EFgQU9tEiKdU9
+I9derQyc5nWPnc34nVMwHwYDVR0jBBgwFoAU9tEiKdU9I9derQyc5nWPnc34nVMw
+DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAxj7F/u3C3fgq24N7hGRA
+of7ClFQxGmo/IGT0AISzW3HiVYiFaikKhbO1NwD9aBpD8Zwe62sCqMh8jGV/b0+q
+aOORnWYNy2R6r9FkASAglmdF6xn3bhgGD5ls4pCvcG9FynGnGc24g6MrjFNrBYUS
+2iIZsg36i0IJswo/Dy6HLphCms2BMCD3DeWtfjePUiTmQHJo6HsQIKP/u4N4Fvee
+uMBInei2M4VU74fLXbmKl1F9AEX7JDP3BKSZG19Ch5pnUo4uXM1uNTGsi07P4Y0s
+K44+SKBC0bYEFbDK0eQWMrX3kIhkPxyIWhxdq9/NqPYjShuSEAhA6CSpmRg0pqc+
+mA==
+-----END CERTIFICATE-----
diff --git a/test/net/fixtures/dhparams.pem b/test/net/fixtures/dhparams.pem
new file mode 100644
index 0000000000..389285bf59
--- /dev/null
+++ b/test/net/fixtures/dhparams.pem
@@ -0,0 +1,29 @@
+ DH Parameters: (2048 bit)
+ prime:
+ 00:ec:4e:a4:06:b6:22:ca:f9:8a:00:cc:d0:ee:2f:
+ 16:bf:05:64:f5:8f:fe:7f:c4:bb:b0:24:cd:ef:5d:
+ 8a:90:ad:dc:a9:dd:63:84:90:d8:25:ba:d8:78:d5:
+ 77:91:42:0a:84:fc:56:1e:13:9b:1c:aa:43:d5:1f:
+ 38:52:92:fe:b3:66:f9:e7:e8:8c:77:a1:a6:2f:b3:
+ 98:98:d2:13:fc:57:1c:2a:14:dc:bd:e6:9b:54:19:
+ 99:4f:ce:81:64:a6:32:7f:8e:61:50:5f:45:3a:e5:
+ 0c:f7:13:f3:b8:ad:d5:77:ca:09:42:f7:d8:30:27:
+ 7b:2c:f0:b4:b5:a0:04:96:34:0b:47:81:1d:7f:c1:
+ 3a:62:86:8e:7d:f8:13:7f:9a:b1:8b:09:23:9e:55:
+ 59:41:cd:f0:86:09:c4:b7:d1:69:54:cb:d0:f5:e9:
+ 27:c9:e1:81:e4:a1:df:6b:20:1c:df:e8:54:02:f2:
+ 37:fc:2a:f7:d5:b3:6f:79:7e:70:22:78:79:18:3c:
+ 75:14:68:4a:05:9f:ac:d4:7f:9a:79:db:9d:0a:6e:
+ ec:0a:04:70:bf:c9:4a:59:81:a2:1f:33:9b:4a:66:
+ bc:03:ce:8a:1b:e3:03:ec:ba:39:26:ab:90:dc:39:
+ 41:a1:d8:f7:20:3c:8f:af:12:2f:f7:a9:6f:44:f1:
+ 6d:03
+ generator: 2 (0x2)
+-----BEGIN DH PARAMETERS-----
+MIIBCAKCAQEA7E6kBrYiyvmKAMzQ7i8WvwVk9Y/+f8S7sCTN712KkK3cqd1jhJDY
+JbrYeNV3kUIKhPxWHhObHKpD1R84UpL+s2b55+iMd6GmL7OYmNIT/FccKhTcveab
+VBmZT86BZKYyf45hUF9FOuUM9xPzuK3Vd8oJQvfYMCd7LPC0taAEljQLR4Edf8E6
+YoaOffgTf5qxiwkjnlVZQc3whgnEt9FpVMvQ9eknyeGB5KHfayAc3+hUAvI3/Cr3
+1bNveX5wInh5GDx1FGhKBZ+s1H+aedudCm7sCgRwv8lKWYGiHzObSma8A86KG+MD
+7Lo5JquQ3DlBodj3IDyPrxIv96lvRPFtAwIBAg==
+-----END DH PARAMETERS-----
diff --git a/test/net/fixtures/server.crt b/test/net/fixtures/server.crt
new file mode 100644
index 0000000000..5ca78a6d14
--- /dev/null
+++ b/test/net/fixtures/server.crt
@@ -0,0 +1,82 @@
+Certificate:
+ Data:
+ Version: 3 (0x2)
+ Serial Number: 2 (0x2)
+ Signature Algorithm: sha256WithRSAEncryption
+ Issuer: C=JP, ST=Shimane, L=Matz-e city, O=Ruby Core Team, CN=Ruby Test CA/emailAddress=security@ruby-lang.org
+ Validity
+ Not Before: Jan 2 03:27:13 2019 GMT
+ Not After : Jan 1 03:27:13 2024 GMT
+ Subject: C=JP, ST=Shimane, O=Ruby Core Team, OU=Ruby Test, CN=localhost
+ Subject Public Key Info:
+ Public Key Algorithm: rsaEncryption
+ Public-Key: (2048 bit)
+ Modulus:
+ 00:e8:da:9c:01:2e:2b:10:ec:49:cd:5e:07:13:07:
+ 9c:70:9e:c6:74:bc:13:c2:e1:6f:c6:82:fd:e3:48:
+ e0:2c:a5:68:c7:9e:42:de:60:54:65:e6:6a:14:57:
+ 7a:30:d0:cc:b5:b6:d9:c3:d2:df:c9:25:97:54:67:
+ cf:f6:be:5e:cb:8b:ee:03:c5:e1:e2:f9:e7:f7:d1:
+ 0c:47:f0:b8:da:33:5a:ad:41:ad:e7:b5:a2:7b:b7:
+ bf:30:da:60:f8:e3:54:a2:bc:3a:fd:1b:74:d9:dc:
+ 74:42:e9:29:be:df:ac:b4:4f:eb:32:f4:06:f1:e1:
+ 8c:4b:a8:8b:fb:29:e7:b1:bf:1d:01:ee:73:0f:f9:
+ 40:dc:d5:15:79:d9:c6:73:d0:c0:dd:cb:e4:da:19:
+ 47:80:c6:14:04:72:fd:9a:7c:8f:11:82:76:49:04:
+ 79:cc:f2:5c:31:22:95:13:3e:5d:40:a6:4d:e0:a3:
+ 02:26:7d:52:3b:bb:ed:65:a1:0f:ed:6b:b0:3c:d4:
+ de:61:15:5e:d3:dd:68:09:9f:4a:57:a5:c2:a9:6d:
+ 86:92:c5:f4:a4:d4:b7:13:3b:52:63:24:05:e2:cc:
+ e3:8a:3c:d4:35:34:2b:10:bb:58:72:e7:e1:8d:1d:
+ 74:8c:61:16:20:3d:d0:1c:4e:8f:6e:fd:fe:64:10:
+ 4f:41
+ Exponent: 65537 (0x10001)
+ X509v3 extensions:
+ X509v3 Basic Constraints:
+ CA:FALSE
+ Netscape Comment:
+ OpenSSL Generated Certificate
+ X509v3 Subject Key Identifier:
+ ED:28:C2:7E:AB:4B:C8:E8:FE:55:6D:66:95:31:1C:2D:60:F9:02:36
+ X509v3 Authority Key Identifier:
+ keyid:F6:D1:22:29:D5:3D:23:D7:5E:AD:0C:9C:E6:75:8F:9D:CD:F8:9D:53
+
+ Signature Algorithm: sha256WithRSAEncryption
+ 1d:b8:c5:8b:72:41:20:65:ad:27:6f:15:63:06:26:12:8d:9c:
+ ad:ca:f4:db:97:b4:90:cb:ff:35:94:bb:2a:a7:a1:ab:1e:35:
+ 2d:a5:3f:c9:24:b0:1a:58:89:75:3e:81:0a:2c:4f:98:f9:51:
+ fb:c0:a3:09:d0:0a:9b:e7:a2:b7:c3:60:40:c8:f4:6d:b2:6a:
+ 56:12:17:4c:00:24:31:df:9c:60:ae:b1:68:54:a9:e6:b5:4a:
+ 04:e6:92:05:86:d9:5a:dc:96:30:a5:58:de:14:99:0f:e5:15:
+ 89:3e:9b:eb:80:e3:bd:83:c3:ea:33:35:4b:3e:2f:d3:0d:64:
+ 93:67:7f:8d:f5:3f:0c:27:bc:37:5a:cc:d6:47:16:af:5a:62:
+ d2:da:51:f8:74:06:6b:24:ad:28:68:08:98:37:7d:ed:0e:ab:
+ 1e:82:61:05:d0:ba:75:a0:ab:21:b0:9a:fd:2b:54:86:1d:0d:
+ 1f:c2:d4:77:1f:72:26:5e:ad:8a:9f:09:36:6d:44:be:74:c2:
+ 5a:3e:ff:5c:9d:75:d6:38:7b:c5:39:f9:44:6e:a1:d1:8e:ff:
+ 63:db:c4:bb:c6:91:92:ca:5c:60:9b:1d:eb:0a:de:08:ee:bf:
+ da:76:03:65:62:29:8b:f8:7f:c7:86:73:1e:f6:1f:2d:89:69:
+ fd:be:bd:6e
+-----BEGIN CERTIFICATE-----
+MIID4zCCAsugAwIBAgIBAjANBgkqhkiG9w0BAQsFADCBjDELMAkGA1UEBhMCSlAx
+EDAOBgNVBAgMB1NoaW1hbmUxFDASBgNVBAcMC01hdHotZSBjaXR5MRcwFQYDVQQK
+DA5SdWJ5IENvcmUgVGVhbTEVMBMGA1UEAwwMUnVieSBUZXN0IENBMSUwIwYJKoZI
+hvcNAQkBFhZzZWN1cml0eUBydWJ5LWxhbmcub3JnMB4XDTE5MDEwMjAzMjcxM1oX
+DTI0MDEwMTAzMjcxM1owYDELMAkGA1UEBhMCSlAxEDAOBgNVBAgMB1NoaW1hbmUx
+FzAVBgNVBAoMDlJ1YnkgQ29yZSBUZWFtMRIwEAYDVQQLDAlSdWJ5IFRlc3QxEjAQ
+BgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
+AOjanAEuKxDsSc1eBxMHnHCexnS8E8Lhb8aC/eNI4CylaMeeQt5gVGXmahRXejDQ
+zLW22cPS38kll1Rnz/a+XsuL7gPF4eL55/fRDEfwuNozWq1Bree1onu3vzDaYPjj
+VKK8Ov0bdNncdELpKb7frLRP6zL0BvHhjEuoi/sp57G/HQHucw/5QNzVFXnZxnPQ
+wN3L5NoZR4DGFARy/Zp8jxGCdkkEeczyXDEilRM+XUCmTeCjAiZ9Uju77WWhD+1r
+sDzU3mEVXtPdaAmfSlelwqlthpLF9KTUtxM7UmMkBeLM44o81DU0KxC7WHLn4Y0d
+dIxhFiA90BxOj279/mQQT0ECAwEAAaN7MHkwCQYDVR0TBAIwADAsBglghkgBhvhC
+AQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYEFO0o
+wn6rS8jo/lVtZpUxHC1g+QI2MB8GA1UdIwQYMBaAFPbRIinVPSPXXq0MnOZ1j53N
++J1TMA0GCSqGSIb3DQEBCwUAA4IBAQAduMWLckEgZa0nbxVjBiYSjZytyvTbl7SQ
+y/81lLsqp6GrHjUtpT/JJLAaWIl1PoEKLE+Y+VH7wKMJ0Aqb56K3w2BAyPRtsmpW
+EhdMACQx35xgrrFoVKnmtUoE5pIFhtla3JYwpVjeFJkP5RWJPpvrgOO9g8PqMzVL
+Pi/TDWSTZ3+N9T8MJ7w3WszWRxavWmLS2lH4dAZrJK0oaAiYN33tDqsegmEF0Lp1
+oKshsJr9K1SGHQ0fwtR3H3ImXq2Knwk2bUS+dMJaPv9cnXXWOHvFOflEbqHRjv9j
+28S7xpGSylxgmx3rCt4I7r/adgNlYimL+H/HhnMe9h8tiWn9vr1u
+-----END CERTIFICATE-----
diff --git a/test/net/fixtures/server.key b/test/net/fixtures/server.key
new file mode 100644
index 0000000000..7f2380e71e
--- /dev/null
+++ b/test/net/fixtures/server.key
@@ -0,0 +1,28 @@
+-----BEGIN PRIVATE KEY-----
+MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDo2pwBLisQ7EnN
+XgcTB5xwnsZ0vBPC4W/Ggv3jSOAspWjHnkLeYFRl5moUV3ow0My1ttnD0t/JJZdU
+Z8/2vl7Li+4DxeHi+ef30QxH8LjaM1qtQa3ntaJ7t78w2mD441SivDr9G3TZ3HRC
+6Sm+36y0T+sy9Abx4YxLqIv7Keexvx0B7nMP+UDc1RV52cZz0MDdy+TaGUeAxhQE
+cv2afI8RgnZJBHnM8lwxIpUTPl1Apk3gowImfVI7u+1loQ/ta7A81N5hFV7T3WgJ
+n0pXpcKpbYaSxfSk1LcTO1JjJAXizOOKPNQ1NCsQu1hy5+GNHXSMYRYgPdAcTo9u
+/f5kEE9BAgMBAAECggEBAOHkwhc7DLh8IhTDNSW26oMu5OP2WU1jmiYAigDmf+OQ
+DBgrZj+JQBci8qINQxL8XLukSZn5hvQCLc7Kbyu1/wyEEUFDxSGGwwzclodr9kho
+LX2LDASPZrOSzD2+fPi2wTKmXKuS6Uc44OjQfZkYMNkz9r4Vkm8xGgOD3VipjIYX
+QXlhhdqkXZcNABsihCV52GKkDFSVm8jv95YJc5xhoYCy/3a4/qPdF0aT2R7oYUej
+hKrxVDskyooe8Zg/JTydZNV5GQEDmW01/K3r6XGT26oPi1AqMU1gtv/jkW56CRQQ
+1got8smnqM+AV7Slf9R6DauIPdQJ2S8wsr/o8ISBsOECgYEA9YrqEP2gAYSGFXRt
+liw0WI2Ant8BqXS6yvq1jLo/qWhLw/ph4Di73OQ2mpycVTpgfGr2wFPQR1XJ+0Fd
+U+Ir/C3Q7FK4VIGHK7B0zNvZr5tEjlFfeRezo2JMVw5YWeSagIFcSwK+KqCTH9qc
+pw/Eb8nB/4XNcpTZu7Fg0Wc+ooUCgYEA8sVaicn1Wxkpb45a4qfrA6wOr5xdJ4cC
+A5qs7vjX2OdPIQOmoQhdI7bCWFXZzF33wA4YCws6j5wRaySLIJqdms8Gl9QnODy1
+ZlA5gwKToBC/jqPmWAXSKb8EH7cHilaxU9OKnQ7CfwlGLHqjMtjrhR7KHlt3CVRs
+oRmvsjZVXI0CgYAmPedslAO6mMhFSSfULrhMXmV82OCqYrrA6EEkVNGbcdnzAOkD
+gfKIWabDd8bFY10po4Mguy0CHzNhBXIioWQWV5BlbhC1YKMLw+S9DzSdLAKGY9gJ
+xQ4+UQ3wtRQ/k+IYR413RUsW2oFvgZ3KSyNeAb9MK6uuv84VdG/OzVSs/QKBgQDn
+kap//l2EbObiWyaERunckdVcW0lcN+KK75J/TGwPoOwQsLvTpPe65kxRGGrtDsEQ
+uCDk/+v3KkZPLgdrrTAih9FhJ+PVN8tMcb+6IM4SA4fFFr/UPJEwct0LJ3oQ0grJ
+y+HPWFHb/Uurh7t99/4H98uR02sjQh1wOeEmm78mzQKBgQDm+LzGH0se6CXQ6cdZ
+g1JRZeXkDEsrW3hfAsW62xJQmXcWxBoblP9OamMY+A06rM5og3JbDk5Zm6JsOaA8
+wS2gw4ilp46jors4eQey8ux7kB9LzdBoDBBElnsbjLO8oBNZlVcYXg+6BOl/CUi7
+2whRF0FEjKA8ehrNhAq+VFfFNw==
+-----END PRIVATE KEY-----
diff --git a/test/net/ftp/test_buffered_socket.rb b/test/net/ftp/test_buffered_socket.rb
new file mode 100644
index 0000000000..875c53f4e0
--- /dev/null
+++ b/test/net/ftp/test_buffered_socket.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+require "net/ftp"
+require "test/unit"
+require "ostruct"
+require "stringio"
+
+class BufferedSocketTest < Test::Unit::TestCase
+ def test_gets_empty
+ sock = create_buffered_socket("")
+ assert_equal(nil, sock.gets)
+ end
+
+ def test_gets_one_line
+ sock = create_buffered_socket("foo\n")
+ assert_equal("foo\n", sock.gets)
+ end
+
+ def test_gets_one_line_without_term
+ sock = create_buffered_socket("foo")
+ assert_equal("foo", sock.gets)
+ end
+
+ def test_gets_two_lines
+ sock = create_buffered_socket("foo\nbar\n")
+ assert_equal("foo\n", sock.gets)
+ assert_equal("bar\n", sock.gets)
+ end
+
+ def test_gets_two_lines_without_term
+ sock = create_buffered_socket("foo\nbar")
+ assert_equal("foo\n", sock.gets)
+ assert_equal("bar", sock.gets)
+ end
+
+ def test_read_nil
+ sock = create_buffered_socket("foo\nbar")
+ assert_equal("foo\nbar", sock.read)
+ assert_equal("", sock.read)
+ end
+
+ private
+
+ def create_buffered_socket(s)
+ io = StringIO.new(s)
+ return Net::FTP::BufferedSocket.new(io)
+ end
+end
diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb
new file mode 100644
index 0000000000..8e0a688135
--- /dev/null
+++ b/test/net/ftp/test_ftp.rb
@@ -0,0 +1,2471 @@
+# frozen_string_literal: true
+
+require "net/ftp"
+require "test/unit"
+require "ostruct"
+require "stringio"
+require "tempfile"
+require "tmpdir"
+
+class FTPTest < Test::Unit::TestCase
+ SERVER_NAME = "localhost"
+ SERVER_ADDR =
+ begin
+ Addrinfo.getaddrinfo(SERVER_NAME, 0, nil, :STREAM)[0].ip_address
+ rescue SocketError
+ "127.0.0.1"
+ end
+ CA_FILE = File.expand_path("../fixtures/cacert.pem", __dir__)
+ SERVER_KEY = File.expand_path("../fixtures/server.key", __dir__)
+ SERVER_CERT = File.expand_path("../fixtures/server.crt", __dir__)
+
+ def setup
+ @thread = nil
+ @default_passive = Net::FTP.default_passive
+ Net::FTP.default_passive = false
+ end
+
+ def teardown
+ Net::FTP.default_passive = @default_passive
+ if @thread
+ @thread.join
+ end
+ end
+
+ def test_not_connected
+ ftp = Net::FTP.new
+ assert_raise(Net::FTPConnectionError) do
+ ftp.quit
+ end
+ end
+
+ def test_closed_when_not_connected
+ ftp = Net::FTP.new
+ assert_equal(true, ftp.closed?)
+ assert_nothing_raised(Net::FTPConnectionError) do
+ ftp.close
+ end
+ end
+
+ def test_connect_fail
+ server = create_ftp_server { |sock|
+ sock.print("421 Service not available, closing control connection.\r\n")
+ }
+ begin
+ ftp = Net::FTP.new
+ assert_raise(Net::FTPTempError){ ftp.connect(SERVER_ADDR, server.port) }
+ ensure
+ ftp.close if ftp
+ server.close
+ end
+ end
+
+ def test_parse227
+ ftp = Net::FTP.new
+ host, port = ftp.send(:parse227, "227 Entering Passive Mode (192,168,0,1,12,34)")
+ assert_equal("192.168.0.1", host)
+ assert_equal(3106, port)
+ assert_raise(Net::FTPReplyError) do
+ ftp.send(:parse227, "500 Syntax error")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse227, "227 Entering Passive Mode")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse227, "227 Entering Passive Mode (192,168,0,1,12,34,56)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse227, "227 Entering Passive Mode (192,168,0,1)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse227, "227 ) foo bar (")
+ end
+ end
+
+ def test_parse228
+ ftp = Net::FTP.new
+ host, port = ftp.send(:parse228, "228 Entering Long Passive Mode (4,4,192,168,0,1,2,12,34)")
+ assert_equal("192.168.0.1", host)
+ assert_equal(3106, port)
+ host, port = ftp.send(:parse228, "228 Entering Long Passive Mode (6,16,16,128,0,0,0,0,0,0,0,8,8,0,32,12,65,122,2,12,34)")
+ assert_equal("1080:0000:0000:0000:0008:0800:200c:417a", host)
+ assert_equal(3106, port)
+ assert_raise(Net::FTPReplyError) do
+ ftp.send(:parse228, "500 Syntax error")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse228, "228 Entering Passive Mode")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse228, "228 Entering Long Passive Mode (6,4,192,168,0,1,2,12,34)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse228, "228 Entering Long Passive Mode (4,4,192,168,0,1,3,12,34,56)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse228, "228 Entering Long Passive Mode (4,16,16,128,0,0,0,0,0,0,0,8,8,0,32,12,65,122,2,12,34)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse228, "228 Entering Long Passive Mode (6,16,16,128,0,0,0,0,0,0,0,8,8,0,32,12,65,122,3,12,34,56)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse228, "228 Entering Long Passive Mode (6,16,16,128,0,0,0,0,0,0,0,8,8,0,32,12,65,122,2,12,34,56)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse227, "227 ) foo bar (")
+ end
+ end
+
+ def test_parse229
+ ftp = Net::FTP.new
+ sock = OpenStruct.new
+ sock.remote_address = OpenStruct.new
+ sock.remote_address.ip_address = "1080:0000:0000:0000:0008:0800:200c:417a"
+ ftp.instance_variable_set(:@bare_sock, sock)
+ host, port = ftp.send(:parse229, "229 Entering Passive Mode (|||3106|)")
+ assert_equal("1080:0000:0000:0000:0008:0800:200c:417a", host)
+ assert_equal(3106, port)
+ host, port = ftp.send(:parse229, "229 Entering Passive Mode (!!!3106!)")
+ assert_equal("1080:0000:0000:0000:0008:0800:200c:417a", host)
+ assert_equal(3106, port)
+ host, port = ftp.send(:parse229, "229 Entering Passive Mode (~~~3106~)")
+ assert_equal("1080:0000:0000:0000:0008:0800:200c:417a", host)
+ assert_equal(3106, port)
+ assert_raise(Net::FTPReplyError) do
+ ftp.send(:parse229, "500 Syntax error")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse229, "229 Entering Passive Mode")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse229, "229 Entering Passive Mode (|!!3106!)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse229, "229 Entering Passive Mode ( 3106 )")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse229, "229 Entering Passive Mode (\x7f\x7f\x7f3106\x7f)")
+ end
+ assert_raise(Net::FTPProtoError) do
+ ftp.send(:parse229, "229 ) foo bar (")
+ end
+ end
+
+ def test_parse_pasv_port
+ ftp = Net::FTP.new
+ assert_equal(12, ftp.send(:parse_pasv_port, "12"))
+ assert_equal(3106, ftp.send(:parse_pasv_port, "12,34"))
+ assert_equal(795192, ftp.send(:parse_pasv_port, "12,34,56"))
+ assert_equal(203569230, ftp.send(:parse_pasv_port, "12,34,56,78"))
+ end
+
+ def test_login
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_login_fail1
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("502 Command not implemented.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_raise(Net::FTPPermError){ ftp.login }
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_login_fail2
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("530 Not logged in.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_raise(Net::FTPPermError){ ftp.login }
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_implicit_login
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("332 Need account for login.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new(SERVER_ADDR,
+ port: server.port,
+ username: "foo",
+ password: "bar",
+ account: "baz")
+ assert_equal("USER foo\r\n", commands.shift)
+ assert_equal("PASS bar\r\n", commands.shift)
+ assert_equal("ACCT baz\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_s_open
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ Net::FTP.open(SERVER_ADDR, port: server.port, username: "anonymous") do
+ end
+ assert_equal("USER anonymous\r\n", commands.shift)
+ assert_equal("PASS anonymous@\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ server.close
+ end
+ end
+
+ def test_s_new_timeout_options
+ ftp = Net::FTP.new
+ assert_equal(nil, ftp.open_timeout)
+ assert_equal(60, ftp.read_timeout)
+
+ ftp = Net::FTP.new(nil, open_timeout: 123, read_timeout: 234)
+ assert_equal(123, ftp.open_timeout)
+ assert_equal(234, ftp.read_timeout)
+ end
+
+ # TODO: How can we test open_timeout? sleep before accept cannot delay
+ # connections.
+ def _test_open_timeout_exceeded
+ commands = []
+ server = create_ftp_server(0.2) { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.open_timeout = 0.1
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_raise(Net::OpenTimeout) do
+ ftp.login
+ end
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_read_timeout_exceeded
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sleep(0.1)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sleep(0.3)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sleep(0.1)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.read_timeout = 0.2
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_raise(Net::ReadTimeout) do
+ ftp.login
+ end
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_read_timeout_not_exceeded
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sleep(0.1)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sleep(0.1)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sleep(0.1)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.read_timeout = 1.0
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close
+ assert_equal(1.0, ftp.read_timeout)
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_list_read_timeout_exceeded
+ commands = []
+ list_lines = [
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 foo.txt",
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 bar.txt",
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 baz.txt"
+ ]
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Here comes the directory listing.\r\n")
+ begin
+ conn = TCPSocket.new(host, port)
+ list_lines.each_with_index do |l, i|
+ if i == 1
+ sleep(0.5)
+ else
+ sleep(0.1)
+ end
+ conn.print(l, "\r\n")
+ end
+ rescue Errno::EPIPE
+ ensure
+ assert_nil($!)
+ conn.close
+ end
+ sock.print("226 Directory send OK.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.read_timeout = 0.2
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_raise(Net::ReadTimeout) do
+ ftp.list
+ end
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("LIST\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_list_read_timeout_not_exceeded
+ commands = []
+ list_lines = [
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 foo.txt",
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 bar.txt",
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 baz.txt"
+ ]
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Here comes the directory listing.\r\n")
+ conn = TCPSocket.new(host, port)
+ list_lines.each do |l|
+ sleep(0.1)
+ conn.print(l, "\r\n")
+ end
+ conn.close
+ sock.print("226 Directory send OK.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.read_timeout = 1.0
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(list_lines, ftp.list)
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("LIST\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_list_fail
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("553 Requested action not taken.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_raise(Net::FTPPermError){ ftp.list }
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("LIST\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_open_data_port_fail_no_leak
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ sock.print("421 Service not available, closing control connection.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_raise(Net::FTPTempError){ ftp.list }
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_retrbinary_read_timeout_exceeded
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ sleep(0.1)
+ conn.print(binary_data[0,1024])
+ sleep(1.0)
+ conn.print(binary_data[1024, 1024]) rescue nil # may raise EPIPE or something
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.read_timeout = 0.5
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = String.new
+ assert_raise(Net::ReadTimeout) do
+ ftp.retrbinary("RETR foo", 1024) do |s|
+ buf << s
+ end
+ end
+ assert_equal(1024, buf.bytesize)
+ assert_equal(binary_data[0, 1024], buf)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close unless ftp.closed?
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_retrbinary_read_timeout_not_exceeded
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ binary_data.scan(/.{1,1024}/nm) do |s|
+ sleep(0.2)
+ conn.print(s)
+ end
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.read_timeout = 1.0
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = String.new
+ ftp.retrbinary("RETR foo", 1024) do |s|
+ buf << s
+ end
+ assert_equal(binary_data.bytesize, buf.bytesize)
+ assert_equal(binary_data, buf)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_retrbinary_fail
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("550 Requested action not taken.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_raise(Net::FTPPermError){ ftp.retrbinary("RETR foo", 1024) }
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_getbinaryfile
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ binary_data.scan(/.{1,1024}/nm) do |s|
+ conn.print(s)
+ end
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = ftp.getbinaryfile("foo", nil)
+ assert_equal(binary_data, buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_getbinaryfile_empty
+ commands = []
+ binary_data = ""
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = ftp.getbinaryfile("foo", nil)
+ assert_equal(binary_data, buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_getbinaryfile_with_filename_and_block
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ binary_data.scan(/.{1,1024}/nm) do |s|
+ conn.print(s)
+ end
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ Tempfile.create("foo", external_encoding: "ASCII-8BIT") do |f|
+ f.binmode
+ buf = String.new
+ res = ftp.getbinaryfile("foo", f.path) { |s|
+ buf << s
+ }
+ assert_equal(nil, res)
+ assert_equal(binary_data, buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_equal(binary_data, f.read)
+ end
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_storbinary
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ stored_data = nil
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo\r\n")
+ conn = TCPSocket.new(host, port)
+ stored_data = conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.storbinary("STOR foo", StringIO.new(binary_data), 1024)
+ assert_equal(binary_data, stored_data)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("STOR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_storbinary_fail
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("452 Requested file action aborted.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_raise(Net::FTPTempError){ ftp.storbinary("STOR foo", StringIO.new(binary_data), 1024) }
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("STOR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_retrlines
+ commands = []
+ text_data = <<EOF.gsub(/\n/, "\r\n")
+foo
+bar
+baz
+EOF
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening TEXT mode data connection for foo (#{text_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ text_data.each_line do |l|
+ conn.print(l)
+ end
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = String.new
+ ftp.retrlines("RETR foo") do |line|
+ buf << line + "\r\n"
+ end
+ assert_equal(text_data.bytesize, buf.bytesize)
+ assert_equal(text_data, buf)
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_gettextfile
+ commands = []
+ text_data = <<EOF.gsub(/\n/, "\r\n")
+foo
+bar
+baz
+EOF
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening TEXT mode data connection for foo (#{text_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ text_data.each_line do |l|
+ conn.print(l)
+ end
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = ftp.gettextfile("foo", nil)
+ assert_equal(text_data.gsub(/\r\n/, "\n"), buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_gettextfile_with_filename_and_block
+ commands = []
+ text_data = <<EOF.gsub(/\n/, "\r\n")
+foo
+bar
+baz
+EOF
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening TEXT mode data connection for foo (#{text_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ text_data.each_line do |l|
+ conn.print(l)
+ end
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ Tempfile.create("foo", external_encoding: "ascii-8bit") do |f|
+ buf = String.new
+ res = ftp.gettextfile("foo", f.path) { |s|
+ buf << s << "\n"
+ }
+ assert_equal(nil, res)
+ assert_equal(text_data.gsub(/\r\n/, "\n"), buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_equal(buf, f.read)
+ end
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_getbinaryfile_in_list
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join
+ list_lines = [
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 foo.txt",
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 bar.txt",
+ "-rw-r--r-- 1 0 0 0 Mar 30 11:22 baz.bin"
+ ]
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Here comes the directory listing.\r\n")
+ conn = TCPSocket.new(host, port)
+ list_lines.each_with_index do |l, i|
+ conn.print(l, "\r\n")
+ end
+ conn.close
+ sock.print("226 Directory send OK.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ conn.print(binary_data)
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.list do |line|
+ file = line.slice(/(\S*\.bin)\z/)
+ if file
+ data = ftp.getbinaryfile(file, nil)
+ assert_equal(binary_data, data)
+ end
+ end
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("LIST\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR baz.bin\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_abort
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("225 No transfer to ABOR.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.abort
+ assert_equal("ABOR\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_status
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("211 End of status\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.status
+ assert_equal("STAT\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_status_path
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("213 End of status\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.status "/"
+ assert_equal("STAT /\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_pathnames
+ require 'pathname'
+
+ commands = []
+ server = create_ftp_server(0.2) { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("257 'foo' directory created.\r\n")
+ commands.push(sock.gets)
+ sock.print("250 CWD command successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("250 CWD command successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("250 RMD command successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("213 test.txt Fri, 11 Jan 2013 11:20:41 -0500.\r\n")
+ commands.push(sock.gets)
+ sock.print("213 test.txt 16.\r\n")
+ commands.push(sock.gets)
+ sock.print("350 File exists, ready for destination name\r\n")
+ commands.push(sock.gets)
+ sock.print("250 RNTO command successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("250 DELE command successful.\r\n")
+ }
+
+ begin
+ begin
+ dir = Pathname.new("foo")
+ file = Pathname.new("test.txt")
+ file2 = Pathname.new("test2.txt")
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ ftp.mkdir(dir)
+ ftp.chdir(dir)
+ ftp.chdir("..")
+ ftp.rmdir(dir)
+ ftp.mdtm(file)
+ ftp.size(file)
+ ftp.rename(file, file2)
+ ftp.delete(file)
+
+ # TODO: These commented tests below expose the error but don't test anything:
+ # TypeError: no implicit conversion of Pathname into String
+ # ftp.nlst(dir)
+ # ftp.putbinaryfile(Pathname.new("/etc/hosts"), file2)
+ # ftp.puttextfile(Pathname.new("/etc/hosts"), file2)
+ # ftp.gettextfile(Pathname.new("/etc/hosts"), file2)
+ # ftp.getbinaryfile(Pathname.new("/etc/hosts"), file2)
+ # ftp.list(dir, dir, dir)
+
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_match(/\ATYPE /, commands.shift)
+ assert_match(/\AMKD /, commands.shift)
+ assert_match(/\ACWD /, commands.shift)
+ assert_match(/\ACDUP/, commands.shift)
+ assert_match(/\ARMD /, commands.shift)
+ assert_match(/\AMDTM /, commands.shift)
+ assert_match(/\ASIZE /, commands.shift)
+ assert_match(/\ARNFR /, commands.shift)
+ assert_match(/\ARNTO /, commands.shift)
+ assert_match(/\ADELE /, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_getmultiline
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ sock.print("123- foo\r\n")
+ sock.print("bar\r\n")
+ sock.print(" 123 baz\r\n")
+ sock.print("123 quux\r\n")
+ sock.print("123 foo\r\n")
+ sock.print("foo\r\n")
+ sock.print("\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_equal("123- foo\nbar\n 123 baz\n123 quux\n",
+ ftp.send(:getmultiline))
+ assert_equal("123 foo\n", ftp.send(:getmultiline))
+ assert_equal("foo\n", ftp.send(:getmultiline))
+ assert_equal("\n", ftp.send(:getmultiline))
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_size
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("213 12345\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_equal(12345, ftp.size("foo.txt"))
+ assert_match("SIZE foo.txt\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_mdtm
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("213 20150910161739\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_equal("20150910161739", ftp.mdtm("foo.txt"))
+ assert_match("MDTM foo.txt\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_mtime
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("213 20150910161739\r\n")
+ commands.push(sock.gets)
+ sock.print("213 20150910161739\r\n")
+ commands.push(sock.gets)
+ sock.print("213 20150910161739.123456\r\n")
+ commands.push(sock.gets)
+ sock.print("213 20150910161739.123\r\n")
+ commands.push(sock.gets)
+ sock.print("213 20150910161739.123456789\r\n")
+ commands.push(sock.gets)
+ sock.print("213 2015091016173\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_equal(Time.utc(2015, 9, 10, 16, 17, 39), ftp.mtime("foo.txt"))
+ assert_equal(Time.local(2015, 9, 10, 16, 17, 39),
+ ftp.mtime("foo.txt", true))
+ assert_equal(Time.utc(2015, 9, 10, 16, 17, 39, 123456),
+ ftp.mtime("bar.txt"))
+ assert_equal(Time.utc(2015, 9, 10, 16, 17, 39, 123000),
+ ftp.mtime("bar.txt"))
+ assert_equal(Time.utc(2015, 9, 10, 16, 17, 39,
+ Rational(123456789, 1000)),
+ ftp.mtime("bar.txt"))
+ assert_raise(Net::FTPProtoError) do
+ ftp.mtime("quux.txt")
+ end
+ assert_match("MDTM foo.txt\r\n", commands.shift)
+ assert_match("MDTM foo.txt\r\n", commands.shift)
+ assert_match("MDTM bar.txt\r\n", commands.shift)
+ assert_match("MDTM bar.txt\r\n", commands.shift)
+ assert_match("MDTM bar.txt\r\n", commands.shift)
+ assert_match("MDTM quux.txt\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_system
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("215 UNIX Type: L8\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ assert_equal("UNIX Type: L8", ftp.system)
+ assert_match("SYST\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_mlst
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("250- Listing foo\r\n")
+ sock.print(" Type=file;Unique=FC00U1E554A;Size=1234567;Modify=20131220035929;Perm=r;Unix.mode=0644;Unix.owner=122;Unix.group=0;Unix.ctime=20131220120140;Unix.atime=20131220131139; /foo\r\n")
+ sock.print("250 End\r\n")
+ commands.push(sock.gets)
+ sock.print("250 Malformed response\r\n")
+ commands.push(sock.gets)
+ sock.print("250- Listing foo\r\n")
+ sock.print("\r\n")
+ sock.print("250 End\r\n")
+ commands.push(sock.gets)
+ sock.print("250- Listing foo\r\n")
+ sock.print(" abc /foo\r\n")
+ sock.print("250 End\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ entry = ftp.mlst("foo")
+ assert_equal("/foo", entry.pathname)
+ assert_equal("file", entry.facts["type"])
+ assert_equal("FC00U1E554A", entry.facts["unique"])
+ assert_equal(1234567, entry.facts["size"])
+ assert_equal("r", entry.facts["perm"])
+ assert_equal(0644, entry.facts["unix.mode"])
+ assert_equal(122, entry.facts["unix.owner"])
+ assert_equal(0, entry.facts["unix.group"])
+ modify = entry.facts["modify"]
+ assert_equal(2013, modify.year)
+ assert_equal(12, modify.month)
+ assert_equal(20, modify.day)
+ assert_equal(3, modify.hour)
+ assert_equal(59, modify.min)
+ assert_equal(29, modify.sec)
+ assert_equal(true, modify.utc?)
+ ctime = entry.facts["unix.ctime"]
+ assert_equal(12, ctime.hour)
+ assert_equal(1, ctime.min)
+ assert_equal(40, ctime.sec)
+ atime = entry.facts["unix.atime"]
+ assert_equal(13, atime.hour)
+ assert_equal(11, atime.min)
+ assert_equal(39, atime.sec)
+ assert_match("MLST foo\r\n", commands.shift)
+ assert_raise(Net::FTPProtoError) do
+ ftp.mlst("foo")
+ end
+ assert_match("MLST foo\r\n", commands.shift)
+ assert_raise(Net::FTPProtoError) do
+ ftp.mlst("foo")
+ end
+ assert_match("MLST foo\r\n", commands.shift)
+ entry = ftp.mlst("foo")
+ assert_equal("/foo", entry.pathname)
+ assert_match("MLST foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_mlsd
+ commands = []
+ entry_lines = [
+ "Type=file;Unique=FC00U1E554A;Size=1234567;Modify=20131220035929.123456;Perm=r; foo bar",
+ "Type=cdir;Unique=FC00U1E554B;Modify=20131220035929;Perm=flcdmpe; .",
+ "Type=pdir;Unique=FC00U1E554C;Modify=20131220035929;Perm=flcdmpe; ..",
+ ]
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Here comes the directory listing.\r\n")
+ begin
+ conn = TCPSocket.new(host, port)
+ entry_lines.each do |l|
+ conn.print(l, "\r\n")
+ end
+ rescue Errno::EPIPE
+ ensure
+ assert_nil($!)
+ conn.close
+ end
+ sock.print("226 Directory send OK.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ entries = ftp.mlsd("/")
+ assert_equal(3, entries.size)
+ assert_equal("foo bar", entries[0].pathname)
+ assert_equal(".", entries[1].pathname)
+ assert_equal("..", entries[2].pathname)
+ assert_equal("file", entries[0].facts["type"])
+ assert_equal("cdir", entries[1].facts["type"])
+ assert_equal("pdir", entries[2].facts["type"])
+ assert_equal("flcdmpe", entries[1].facts["perm"])
+ modify = entries[0].facts["modify"]
+ assert_equal(2013, modify.year)
+ assert_equal(12, modify.month)
+ assert_equal(20, modify.day)
+ assert_equal(3, modify.hour)
+ assert_equal(59, modify.min)
+ assert_equal(29, modify.sec)
+ assert_equal(123456, modify.usec)
+ assert_equal(true, modify.utc?)
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_match("MLSD /\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_parse257
+ ftp = Net::FTP.new
+ assert_equal('/foo/bar',
+ ftp.send(:parse257, '257 "/foo/bar" directory created'))
+ assert_equal('/foo/bar"baz',
+ ftp.send(:parse257, '257 "/foo/bar""baz" directory created'))
+ assert_equal('/foo/x"y"z',
+ ftp.send(:parse257, '257 "/foo/x""y""z" directory created'))
+ assert_equal('/foo/bar',
+ ftp.send(:parse257, '257 "/foo/bar" "comment"'))
+ assert_equal('',
+ ftp.send(:parse257, '257 "" directory created'))
+ assert_equal('',
+ ftp.send(:parse257, '257 directory created'))
+ assert_raise(Net::FTPReplyError) do
+ ftp.send(:parse257, "500 Syntax error")
+ end
+ end
+
+ def test_putline_reject_crlf
+ ftp = Net::FTP.new
+ assert_raise(ArgumentError) do
+ ftp.send(:putline, "\r")
+ end
+ assert_raise(ArgumentError) do
+ ftp.send(:putline, "\n")
+ end
+ end
+
+ if defined?(OpenSSL::SSL)
+ def test_tls_unknown_ca
+ assert_raise(OpenSSL::SSL::SSLError) do
+ tls_test do |port|
+ begin
+ Net::FTP.new(SERVER_NAME,
+ :port => port,
+ :ssl => true)
+ rescue SystemCallError
+ skip $!
+ end
+ end
+ end
+ end
+
+ def test_tls_with_ca_file
+ assert_nothing_raised do
+ tls_test do |port|
+ begin
+ Net::FTP.new(SERVER_NAME,
+ :port => port,
+ :ssl => { :ca_file => CA_FILE })
+ rescue SystemCallError
+ skip $!
+ end
+ end
+ end
+ end
+
+ def test_tls_verify_none
+ assert_nothing_raised do
+ tls_test do |port|
+ Net::FTP.new(SERVER_ADDR,
+ :port => port,
+ :ssl => { :verify_mode => OpenSSL::SSL::VERIFY_NONE })
+ end
+ end
+ end
+
+ def test_tls_post_connection_check
+ assert_raise(OpenSSL::SSL::SSLError) do
+ tls_test do |port|
+ # SERVER_ADDR is different from the hostname in the certificate,
+ # so the following code should raise a SSLError.
+ Net::FTP.new(SERVER_ADDR,
+ :port => port,
+ :ssl => { :ca_file => CA_FILE })
+ end
+ end
+ end
+
+ def test_active_private_data_connection
+ server = TCPServer.new(SERVER_ADDR, 0)
+ port = server.addr[1]
+ commands = []
+ session_reused_for_data_connection = nil
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ @thread = Thread.start do
+ sock = server.accept
+ begin
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("234 AUTH success.\r\n")
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+ sock.sync_close = true
+ begin
+ sock.accept
+ commands.push(sock.gets)
+ sock.print("200 PSBZ success.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 PROT success.\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ conn = OpenSSL::SSL::SSLSocket.new(conn, ctx)
+ conn.sync_close = true
+ conn.accept
+ session_reused_for_data_connection = conn.session_reused?
+ binary_data.scan(/.{1,1024}/nm) do |s|
+ conn.print(s)
+ end
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ rescue OpenSSL::SSL::SSLError
+ end
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ ftp = Net::FTP.new(SERVER_NAME,
+ port: port,
+ ssl: { ca_file: CA_FILE },
+ passive: false)
+ begin
+ assert_equal("AUTH TLS\r\n", commands.shift)
+ assert_equal("PBSZ 0\r\n", commands.shift)
+ assert_equal("PROT P\r\n", commands.shift)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = ftp.getbinaryfile("foo", nil)
+ assert_equal(binary_data, buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ # FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
+ # See https://github.com/openssl/openssl/pull/5967 for details.
+ if OpenSSL::OPENSSL_LIBRARY_VERSION !~ /OpenSSL 1.1.0h/
+ assert_equal(true, session_reused_for_data_connection)
+ end
+ ensure
+ ftp.close
+ end
+ end
+
+ def test_passive_private_data_connection
+ server = TCPServer.new(SERVER_ADDR, 0)
+ port = server.addr[1]
+ commands = []
+ session_reused_for_data_connection = nil
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ @thread = Thread.start do
+ sock = server.accept
+ begin
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("234 AUTH success.\r\n")
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+ sock.sync_close = true
+ begin
+ sock.accept
+ commands.push(sock.gets)
+ sock.print("200 PSBZ success.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 PROT success.\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ data_server = create_data_connection_server(sock)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = data_server.accept
+ conn = OpenSSL::SSL::SSLSocket.new(conn, ctx)
+ conn.sync_close = true
+ conn.accept
+ session_reused_for_data_connection = conn.session_reused?
+ binary_data.scan(/.{1,1024}/nm) do |s|
+ conn.print(s)
+ end
+ conn.close
+ data_server.close
+ sock.print("226 Transfer complete.\r\n")
+ rescue OpenSSL::SSL::SSLError
+ end
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ ftp = Net::FTP.new(SERVER_NAME,
+ port: port,
+ ssl: { ca_file: CA_FILE },
+ passive: true)
+ begin
+ assert_equal("AUTH TLS\r\n", commands.shift)
+ assert_equal("PBSZ 0\r\n", commands.shift)
+ assert_equal("PROT P\r\n", commands.shift)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = ftp.getbinaryfile("foo", nil)
+ assert_equal(binary_data, buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_match(/\A(PASV|EPSV)\r\n/, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ # FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
+ if OpenSSL::OPENSSL_LIBRARY_VERSION !~ /OpenSSL 1.1.0h/
+ assert_equal(true, session_reused_for_data_connection)
+ end
+ ensure
+ ftp.close
+ end
+ end
+
+ def test_active_clear_data_connection
+ server = TCPServer.new(SERVER_ADDR, 0)
+ port = server.addr[1]
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ @thread = Thread.start do
+ sock = server.accept
+ begin
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("234 AUTH success.\r\n")
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+ sock.sync_close = true
+ begin
+ sock.accept
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ binary_data.scan(/.{1,1024}/nm) do |s|
+ conn.print(s)
+ end
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ rescue OpenSSL::SSL::SSLError
+ end
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ ftp = Net::FTP.new(SERVER_NAME,
+ port: port,
+ ssl: { ca_file: CA_FILE },
+ private_data_connection: false,
+ passive: false)
+ begin
+ assert_equal("AUTH TLS\r\n", commands.shift)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = ftp.getbinaryfile("foo", nil)
+ assert_equal(binary_data, buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close
+ end
+ end
+
+ def test_passive_clear_data_connection
+ server = TCPServer.new(SERVER_ADDR, 0)
+ port = server.addr[1]
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ @thread = Thread.start do
+ sock = server.accept
+ begin
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("234 AUTH success.\r\n")
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+ sock.sync_close = true
+ begin
+ sock.accept
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ data_server = create_data_connection_server(sock)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
+ conn = data_server.accept
+ binary_data.scan(/.{1,1024}/nm) do |s|
+ conn.print(s)
+ end
+ conn.close
+ data_server.close
+ sock.print("226 Transfer complete.\r\n")
+ rescue OpenSSL::SSL::SSLError
+ end
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ ftp = Net::FTP.new(SERVER_NAME,
+ port: port,
+ ssl: { ca_file: CA_FILE },
+ private_data_connection: false,
+ passive: true)
+ begin
+ assert_equal("AUTH TLS\r\n", commands.shift)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ buf = ftp.getbinaryfile("foo", nil)
+ assert_equal(binary_data, buf)
+ assert_equal(Encoding::ASCII_8BIT, buf.encoding)
+ assert_match(/\A(PASV|EPSV)\r\n/, commands.shift)
+ assert_equal("RETR foo\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close
+ end
+ end
+
+ def test_tls_connect_timeout
+ server = TCPServer.new(SERVER_ADDR, 0)
+ port = server.addr[1]
+ commands = []
+ sock = nil
+ @thread = Thread.start do
+ sock = server.accept
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("234 AUTH success.\r\n")
+ end
+ begin
+ assert_raise(Net::OpenTimeout) do
+ Net::FTP.new(SERVER_NAME,
+ port: port,
+ ssl: { ca_file: CA_FILE },
+ ssl_handshake_timeout: 0.1)
+ end
+ @thread.join
+ ensure
+ sock.close if sock
+ server.close
+ end
+ end
+ end
+
+ def test_abort_tls
+ return unless defined?(OpenSSL)
+
+ commands = []
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("234 AUTH success.\r\n")
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+ sock.sync_close = true
+ sock.accept
+ commands.push(sock.gets)
+ sock.print("200 PSBZ success.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 PROT success.\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("225 No transfer to ABOR.\r\n")
+ }
+ begin
+ begin
+ ftp = Net::FTP.new(SERVER_NAME,
+ port: server.port,
+ ssl: { ca_file: CA_FILE })
+ assert_equal("AUTH TLS\r\n", commands.shift)
+ assert_equal("PBSZ 0\r\n", commands.shift)
+ assert_equal("PROT P\r\n", commands.shift)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.abort
+ assert_equal("ABOR\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ rescue RuntimeError, LoadError
+ # skip (require openssl)
+ ensure
+ ftp.close if ftp
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_getbinaryfile_command_injection
+ skip "| is not allowed in filename on Windows" if windows?
+ [false, true].each do |resume|
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ binary_data.scan(/.{1,1024}/nm) do |s|
+ conn.print(s)
+ end
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ chdir_to_tmpdir do
+ begin
+ ftp = Net::FTP.new
+ ftp.resume = resume
+ ftp.read_timeout = 0.2
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.getbinaryfile("|echo hello")
+ assert_equal(binary_data, File.binread("./|echo hello"))
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR |echo hello\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ end
+ ensure
+ server.close
+ end
+ end
+ end
+
+ def test_gettextfile_command_injection
+ skip "| is not allowed in filename on Windows" if windows?
+ commands = []
+ text_data = <<EOF.gsub(/\n/, "\r\n")
+foo
+bar
+baz
+EOF
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening TEXT mode data connection for |echo hello (#{text_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ text_data.each_line do |l|
+ conn.print(l)
+ end
+ conn.shutdown(Socket::SHUT_WR)
+ conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ chdir_to_tmpdir do
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.gettextfile("|echo hello")
+ assert_equal(text_data.gsub(/\r\n/, "\n"),
+ File.binread("./|echo hello"))
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("RETR |echo hello\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_putbinaryfile_command_injection
+ skip "| is not allowed in filename on Windows" if windows?
+ commands = []
+ binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
+ received_data = nil
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n")
+ conn = TCPSocket.new(host, port)
+ received_data = conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ }
+ begin
+ chdir_to_tmpdir do
+ File.binwrite("./|echo hello", binary_data)
+ begin
+ ftp = Net::FTP.new
+ ftp.read_timeout = 0.2
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.putbinaryfile("|echo hello")
+ assert_equal(binary_data, received_data)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("STOR |echo hello\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ end
+ ensure
+ server.close
+ end
+ end
+
+ def test_puttextfile_command_injection
+ skip "| is not allowed in filename on Windows" if windows?
+ commands = []
+ received_data = nil
+ server = create_ftp_server { |sock|
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("331 Please specify the password.\r\n")
+ commands.push(sock.gets)
+ sock.print("230 Login successful.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to ASCII mode.\r\n")
+ line = sock.gets
+ commands.push(line)
+ host, port = process_port_or_eprt(sock, line)
+ commands.push(sock.gets)
+ sock.print("150 Opening TEXT mode data connection for |echo hello\r\n")
+ conn = TCPSocket.new(host, port)
+ received_data = conn.read
+ conn.close
+ sock.print("226 Transfer complete.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 Switching to Binary mode.\r\n")
+ }
+ begin
+ chdir_to_tmpdir do
+ File.open("|echo hello", "w") do |f|
+ f.puts("foo")
+ f.puts("bar")
+ f.puts("baz")
+ end
+ begin
+ ftp = Net::FTP.new
+ ftp.connect(SERVER_ADDR, server.port)
+ ftp.login
+ assert_match(/\AUSER /, commands.shift)
+ assert_match(/\APASS /, commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ ftp.puttextfile("|echo hello")
+ assert_equal(<<EOF.gsub(/\n/, "\r\n"), received_data)
+foo
+bar
+baz
+EOF
+ assert_equal("TYPE A\r\n", commands.shift)
+ assert_match(/\A(PORT|EPRT) /, commands.shift)
+ assert_equal("STOR |echo hello\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_equal(nil, commands.shift)
+ ensure
+ ftp.close if ftp
+ end
+ end
+ ensure
+ server.close
+ end
+ end
+
+ private
+
+ def create_ftp_server(sleep_time = nil)
+ server = TCPServer.new(SERVER_ADDR, 0)
+ @thread = Thread.start do
+ if sleep_time
+ sleep(sleep_time)
+ end
+ sock = server.accept
+ begin
+ sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_OOBINLINE, 1)
+ yield(sock)
+ sock.shutdown(Socket::SHUT_WR)
+ sock.read unless sock.eof?
+ ensure
+ sock.close
+ end
+ end
+ def server.port
+ addr[1]
+ end
+ return server
+ end
+
+ def tls_test
+ server = TCPServer.new(SERVER_ADDR, 0)
+ port = server.addr[1]
+ commands = []
+ @thread = Thread.start do
+ sock = server.accept
+ begin
+ sock.print("220 (test_ftp).\r\n")
+ commands.push(sock.gets)
+ sock.print("234 AUTH success.\r\n")
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+ sock.sync_close = true
+ begin
+ sock.accept
+ commands.push(sock.gets)
+ sock.print("200 PSBZ success.\r\n")
+ commands.push(sock.gets)
+ sock.print("200 PROT success.\r\n")
+ rescue OpenSSL::SSL::SSLError, SystemCallError
+ end
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ ftp = yield(port)
+ ftp.close
+
+ assert_equal("AUTH TLS\r\n", commands.shift)
+ assert_equal("PBSZ 0\r\n", commands.shift)
+ assert_equal("PROT P\r\n", commands.shift)
+ end
+
+ def process_port_or_eprt(sock, line)
+ case line
+ when /\APORT (.*)/
+ port_args = $1.split(/,/)
+ host = port_args[0, 4].join(".")
+ port = port_args[4, 2].map(&:to_i).inject {|x, y| (x << 8) + y}
+ sock.print("200 PORT command successful.\r\n")
+ return host, port
+ when /\AEPRT \|2\|(.*?)\|(.*?)\|/
+ host = $1
+ port = $2.to_i
+ sock.print("200 EPRT command successful.\r\n")
+ return host, port
+ else
+ flunk "PORT or EPRT expected"
+ end
+ end
+
+ def create_data_connection_server(sock)
+ data_server = TCPServer.new(SERVER_ADDR, 0)
+ port = data_server.local_address.ip_port
+ if data_server.local_address.ipv4?
+ sock.printf("227 Entering Passive Mode (127,0,0,1,%s).\r\n",
+ port.divmod(256).join(","))
+ elsif data_server.local_address.ipv6?
+ sock.printf("229 Entering Extended Passive Mode (|||%d|)\r\n", port)
+ else
+ flunk "Invalid local address"
+ end
+ return data_server
+ end
+
+ def chdir_to_tmpdir
+ Dir.mktmpdir do |dir|
+ pwd = Dir.pwd
+ Dir.chdir(dir)
+ begin
+ yield
+ ensure
+ Dir.chdir(pwd)
+ end
+ end
+ end
+end
diff --git a/test/net/ftp/test_mlsx_entry.rb b/test/net/ftp/test_mlsx_entry.rb
new file mode 100644
index 0000000000..92fe411548
--- /dev/null
+++ b/test/net/ftp/test_mlsx_entry.rb
@@ -0,0 +1,98 @@
+# frozen_string_literal: true
+
+require "net/ftp"
+require "test/unit"
+require "ostruct"
+require "stringio"
+
+class MLSxEntryTest < Test::Unit::TestCase
+ def test_file?
+ assert_equal(true, Net::FTP::MLSxEntry.new({"type"=>"file"}, "foo").file?)
+ assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"dir"}, "foo").file?)
+ assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"cdir"}, "foo").file?)
+ assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"pdir"}, "foo").file?)
+ end
+
+ def test_directory?
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"type"=>"file"}, "foo").directory?)
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"type"=>"dir"}, "foo").directory?)
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"type"=>"cdir"}, "foo").directory?)
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"type"=>"pdir"}, "foo").directory?)
+ end
+
+ def test_appendable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"a"}, "foo").appendable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").appendable?)
+ end
+
+ def test_creatable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"c"}, "foo").creatable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").creatable?)
+ end
+
+ def test_deletable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"d"}, "foo").deletable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").deletable?)
+ end
+
+ def test_enterable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"e"}, "foo").enterable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").enterable?)
+ end
+
+ def test_renamable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"f"}, "foo").renamable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").renamable?)
+ end
+
+ def test_listable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"l"}, "foo").listable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").listable?)
+ end
+
+ def test_directory_makable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"m"}, "foo").
+ directory_makable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").
+ directory_makable?)
+ end
+
+ def test_purgeable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"p"}, "foo").purgeable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").purgeable?)
+ end
+
+ def test_readable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"r"}, "foo").readable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").readable?)
+ end
+
+ def test_writable?
+ assert_equal(true,
+ Net::FTP::MLSxEntry.new({"perm"=>"w"}, "foo").writable?)
+ assert_equal(false,
+ Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").writable?)
+ end
+end
diff --git a/test/net/http/test_buffered_io.rb b/test/net/http/test_buffered_io.rb
new file mode 100644
index 0000000000..8c299ead03
--- /dev/null
+++ b/test/net/http/test_buffered_io.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: false
+require 'test/unit'
+require 'net/http'
+require 'stringio'
+
+require_relative 'utils'
+
+module Net
+ class TestBufferedIO < Test::Unit::TestCase
+ def test_eof?
+ s = StringIO.new
+ assert s.eof?
+ bio = BufferedIO.new(s)
+ assert_equal s, bio.io
+ assert_equal s.eof?, bio.eof?
+ end
+ end
+end
diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb
new file mode 100644
index 0000000000..d3c2aecb9f
--- /dev/null
+++ b/test/net/http/test_http.rb
@@ -0,0 +1,1186 @@
+# coding: US-ASCII
+# frozen_string_literal: false
+require 'test/unit'
+require 'net/http'
+require 'stringio'
+require_relative 'utils'
+
+class TestNetHTTP < Test::Unit::TestCase
+
+ def test_class_Proxy
+ no_proxy_class = Net::HTTP.Proxy nil
+
+ assert_equal Net::HTTP, no_proxy_class
+
+ proxy_class = Net::HTTP.Proxy 'proxy.example', 8000, 'user', 'pass'
+
+ assert_not_equal Net::HTTP, proxy_class
+
+ assert_operator proxy_class, :<, Net::HTTP
+
+ assert_equal 'proxy.example', proxy_class.proxy_address
+ assert_equal 8000, proxy_class.proxy_port
+ assert_equal 'user', proxy_class.proxy_user
+ assert_equal 'pass', proxy_class.proxy_pass
+
+ http = proxy_class.new 'hostname.example'
+
+ assert_not_predicate http, :proxy_from_env?
+
+
+ proxy_class = Net::HTTP.Proxy 'proxy.example'
+ assert_equal 'proxy.example', proxy_class.proxy_address
+ assert_equal 80, proxy_class.proxy_port
+ end
+
+ def test_class_Proxy_from_ENV
+ clean_http_proxy_env do
+ ENV['http_proxy'] = 'http://proxy.example:8000'
+
+ # These are ignored on purpose. See Bug 4388 and Feature 6546
+ ENV['http_proxy_user'] = 'user'
+ ENV['http_proxy_pass'] = 'pass'
+
+ proxy_class = Net::HTTP.Proxy :ENV
+
+ assert_not_equal Net::HTTP, proxy_class
+
+ assert_operator proxy_class, :<, Net::HTTP
+
+ assert_nil proxy_class.proxy_address
+ assert_nil proxy_class.proxy_user
+ assert_nil proxy_class.proxy_pass
+
+ assert_not_equal 8000, proxy_class.proxy_port
+
+ http = proxy_class.new 'hostname.example'
+
+ assert http.proxy_from_env?
+ end
+ end
+
+ def test_addr_port
+ http = Net::HTTP.new 'hostname.example', nil, nil
+ addr_port = http.__send__ :addr_port
+ assert_equal 'hostname.example', addr_port
+
+ http.use_ssl = true
+ addr_port = http.__send__ :addr_port
+ assert_equal 'hostname.example:80', addr_port
+
+ http = Net::HTTP.new '203.0.113.1', nil, nil
+ addr_port = http.__send__ :addr_port
+ assert_equal '203.0.113.1', addr_port
+
+ http.use_ssl = true
+ addr_port = http.__send__ :addr_port
+ assert_equal '203.0.113.1:80', addr_port
+
+ http = Net::HTTP.new '2001:db8::1', nil, nil
+ addr_port = http.__send__ :addr_port
+ assert_equal '[2001:db8::1]', addr_port
+
+ http.use_ssl = true
+ addr_port = http.__send__ :addr_port
+ assert_equal '[2001:db8::1]:80', addr_port
+
+ end
+
+ def test_edit_path
+ http = Net::HTTP.new 'hostname.example', nil, nil
+
+ edited = http.send :edit_path, '/path'
+
+ assert_equal '/path', edited
+
+ http.use_ssl = true
+
+ edited = http.send :edit_path, '/path'
+
+ assert_equal '/path', edited
+ end
+
+ def test_edit_path_proxy
+ http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'
+
+ edited = http.send :edit_path, '/path'
+
+ assert_equal 'http://hostname.example/path', edited
+
+ http.use_ssl = true
+
+ edited = http.send :edit_path, '/path'
+
+ assert_equal '/path', edited
+ end
+
+ def test_proxy_address
+ clean_http_proxy_env do
+ http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'
+ assert_equal 'proxy.example', http.proxy_address
+
+ http = Net::HTTP.new 'hostname.example', nil
+ assert_equal nil, http.proxy_address
+ end
+ end
+
+ def test_proxy_address_no_proxy
+ clean_http_proxy_env do
+ http = Net::HTTP.new 'hostname.example', nil, 'proxy.example', nil, nil, nil, 'example'
+ assert_nil http.proxy_address
+
+ http = Net::HTTP.new '10.224.1.1', nil, 'proxy.example', nil, nil, nil, 'example,10.224.0.0/22'
+ assert_nil http.proxy_address
+ end
+ end
+
+ def test_proxy_from_env_ENV
+ clean_http_proxy_env do
+ ENV['http_proxy'] = 'http://proxy.example:8000'
+
+ assert_equal false, Net::HTTP.proxy_class?
+ http = Net::HTTP.new 'hostname.example'
+
+ assert_equal true, http.proxy_from_env?
+ end
+ end
+
+ def test_proxy_address_ENV
+ clean_http_proxy_env do
+ ENV['http_proxy'] = 'http://proxy.example:8000'
+
+ http = Net::HTTP.new 'hostname.example'
+
+ assert_equal 'proxy.example', http.proxy_address
+ end
+ end
+
+ def test_proxy_eh_no_proxy
+ clean_http_proxy_env do
+ assert_equal false, Net::HTTP.new('hostname.example', nil, nil).proxy?
+ end
+ end
+
+ def test_proxy_eh_ENV
+ clean_http_proxy_env do
+ ENV['http_proxy'] = 'http://proxy.example:8000'
+
+ http = Net::HTTP.new 'hostname.example'
+
+ assert_equal true, http.proxy?
+ end
+ end
+
+ def test_proxy_eh_ENV_with_user
+ clean_http_proxy_env do
+ ENV['http_proxy'] = 'http://foo:bar@proxy.example:8000'
+
+ http = Net::HTTP.new 'hostname.example'
+
+ assert_equal true, http.proxy?
+ if Net::HTTP::ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE
+ assert_equal 'foo', http.proxy_user
+ assert_equal 'bar', http.proxy_pass
+ else
+ assert_nil http.proxy_user
+ assert_nil http.proxy_pass
+ end
+ end
+ end
+
+ def test_proxy_eh_ENV_none_set
+ clean_http_proxy_env do
+ assert_equal false, Net::HTTP.new('hostname.example').proxy?
+ end
+ end
+
+ def test_proxy_eh_ENV_no_proxy
+ clean_http_proxy_env do
+ ENV['http_proxy'] = 'http://proxy.example:8000'
+ ENV['no_proxy'] = 'hostname.example'
+
+ assert_equal false, Net::HTTP.new('hostname.example').proxy?
+ end
+ end
+
+ def test_proxy_port
+ clean_http_proxy_env do
+ http = Net::HTTP.new 'example', nil, 'proxy.example'
+ assert_equal 'proxy.example', http.proxy_address
+ assert_equal 80, http.proxy_port
+ http = Net::HTTP.new 'example', nil, 'proxy.example', 8000
+ assert_equal 8000, http.proxy_port
+ http = Net::HTTP.new 'example', nil
+ assert_equal nil, http.proxy_port
+ end
+ end
+
+ def test_proxy_port_ENV
+ clean_http_proxy_env do
+ ENV['http_proxy'] = 'http://proxy.example:8000'
+
+ http = Net::HTTP.new 'hostname.example'
+
+ assert_equal 8000, http.proxy_port
+ end
+ end
+
+ def test_newobj
+ clean_http_proxy_env do
+ ENV['http_proxy'] = 'http://proxy.example:8000'
+
+ http = Net::HTTP.newobj 'hostname.example'
+
+ assert_equal false, http.proxy?
+ end
+ end
+
+ def clean_http_proxy_env
+ orig = {
+ 'http_proxy' => ENV['http_proxy'],
+ 'http_proxy_user' => ENV['http_proxy_user'],
+ 'http_proxy_pass' => ENV['http_proxy_pass'],
+ 'no_proxy' => ENV['no_proxy'],
+ }
+
+ orig.each_key do |key|
+ ENV.delete key
+ end
+
+ yield
+ ensure
+ orig.each do |key, value|
+ ENV[key] = value
+ end
+ end
+
+ def test_failure_message_includes_failed_domain_and_port
+ # hostname to be included in the error message
+ host = Struct.new(:to_s).new("<example>")
+ port = 2119
+ # hack to let TCPSocket.open fail
+ def host.to_str; raise SocketError, "open failure"; end
+ uri = Struct.new(:scheme, :hostname, :port).new("http", host, port)
+ assert_raise_with_message(SocketError, /#{host}:#{port}/) do
+ clean_http_proxy_env{ Net::HTTP.get(uri) }
+ end
+ end
+
+end
+
+module TestNetHTTP_version_1_1_methods
+
+ def test_s_start
+ begin
+ h = Net::HTTP.start(config('host'), config('port'))
+ ensure
+ h&.finish
+ end
+ assert_equal config('host'), h.address
+ assert_equal config('port'), h.port
+ assert_equal true, h.instance_variable_get(:@proxy_from_env)
+
+ begin
+ h = Net::HTTP.start(config('host'), config('port'), :ENV)
+ ensure
+ h&.finish
+ end
+ assert_equal config('host'), h.address
+ assert_equal config('port'), h.port
+ assert_equal true, h.instance_variable_get(:@proxy_from_env)
+
+ begin
+ h = Net::HTTP.start(config('host'), config('port'), nil)
+ ensure
+ h&.finish
+ end
+ assert_equal config('host'), h.address
+ assert_equal config('port'), h.port
+ assert_equal false, h.instance_variable_get(:@proxy_from_env)
+ end
+
+ def test_s_get
+ assert_equal $test_net_http_data,
+ Net::HTTP.get(config('host'), '/', config('port'))
+ end
+
+ def test_head
+ start {|http|
+ res = http.head('/')
+ assert_kind_of Net::HTTPResponse, res
+ assert_equal $test_net_http_data_type, res['Content-Type']
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_equal $test_net_http_data.size, res['Content-Length'].to_i
+ end
+ }
+ end
+
+ def test_get
+ start {|http|
+ _test_get__get http
+ _test_get__iter http
+ _test_get__chunked http
+ }
+ end
+
+ def _test_get__get(http)
+ res = http.get('/')
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ assert_equal $test_net_http_data_type, res['Content-Type']
+ assert_equal $test_net_http_data.size, res.body.size
+ assert_equal $test_net_http_data, res.body
+
+ assert_nothing_raised {
+ http.get('/', { 'User-Agent' => 'test' }.freeze)
+ }
+
+ assert res.decode_content, '[Bug #7924]' if Net::HTTP::HAVE_ZLIB
+ end
+
+ def _test_get__iter(http)
+ buf = ''
+ res = http.get('/') {|s| buf << s }
+ assert_kind_of Net::HTTPResponse, res
+ # assert_kind_of String, res.body
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ assert_equal $test_net_http_data_type, res['Content-Type']
+ assert_equal $test_net_http_data.size, buf.size
+ assert_equal $test_net_http_data, buf
+ # assert_equal $test_net_http_data.size, res.body.size
+ # assert_equal $test_net_http_data, res.body
+ end
+
+ def _test_get__chunked(http)
+ buf = ''
+ res = http.get('/') {|s| buf << s }
+ assert_kind_of Net::HTTPResponse, res
+ # assert_kind_of String, res.body
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ assert_equal $test_net_http_data_type, res['Content-Type']
+ assert_equal $test_net_http_data.size, buf.size
+ assert_equal $test_net_http_data, buf
+ # assert_equal $test_net_http_data.size, res.body.size
+ # assert_equal $test_net_http_data, res.body
+ end
+
+ def test_get__break
+ i = 0
+ start {|http|
+ http.get('/') do |str|
+ i += 1
+ break
+ end
+ }
+ assert_equal 1, i
+ @log_tester = nil # server may encount ECONNRESET
+ end
+
+ def test_get__implicit_start
+ res = new().get('/')
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ end
+ assert_equal $test_net_http_data_type, res['Content-Type']
+ assert_equal $test_net_http_data.size, res.body.size
+ assert_equal $test_net_http_data, res.body
+ end
+
+ def test_get__crlf
+ start {|http|
+ assert_raise(ArgumentError) do
+ http.get("\r")
+ end
+ assert_raise(ArgumentError) do
+ http.get("\n")
+ end
+ }
+ end
+
+ def test_get2
+ start {|http|
+ http.get2('/') {|res|
+ EnvUtil.suppress_warning do
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of Net::HTTPResponse, res.header
+ end
+
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ end
+ assert_equal $test_net_http_data_type, res['Content-Type']
+ assert_kind_of String, res.body
+ assert_kind_of String, res.entity
+ assert_equal $test_net_http_data.size, res.body.size
+ assert_equal $test_net_http_data, res.body
+ assert_equal $test_net_http_data, res.entity
+ }
+ }
+ end
+
+ def test_post
+ start {|http|
+ _test_post__base http
+ _test_post__file http
+ _test_post__no_data http
+ }
+ end
+
+ def _test_post__base(http)
+ uheader = {}
+ uheader['Accept'] = 'application/octet-stream'
+ uheader['Content-Type'] = 'application/x-www-form-urlencoded'
+ data = 'post data'
+ res = http.post('/', data, uheader)
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ assert_equal data, res.body
+ assert_equal data, res.entity
+ end
+
+ def _test_post__file(http)
+ data = 'post data'
+ f = StringIO.new
+ http.post('/', data, {'content-type' => 'application/x-www-form-urlencoded'}, f)
+ assert_equal data, f.string
+ end
+
+ def _test_post__no_data(http)
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ EnvUtil.suppress_warning do
+ data = nil
+ res = http.post('/', data)
+ assert_not_equal '411', res.code
+ end
+ end
+ end
+
+ def test_s_post
+ url = "http://#{config('host')}:#{config('port')}/?q=a"
+ res = Net::HTTP.post(
+ URI.parse(url),
+ "a=x")
+ assert_equal "application/x-www-form-urlencoded", res["Content-Type"]
+ assert_equal "a=x", res.body
+ assert_equal url, res["X-request-uri"]
+
+ res = Net::HTTP.post(
+ URI.parse(url),
+ "hello world",
+ "Content-Type" => "text/plain; charset=US-ASCII")
+ assert_equal "text/plain; charset=US-ASCII", res["Content-Type"]
+ assert_equal "hello world", res.body
+ end
+
+ def test_s_post_form
+ url = "http://#{config('host')}:#{config('port')}/"
+ res = Net::HTTP.post_form(
+ URI.parse(url),
+ "a" => "x")
+ assert_equal ["a=x"], res.body.split(/[;&]/).sort
+
+ res = Net::HTTP.post_form(
+ URI.parse(url),
+ "a" => "x",
+ "b" => "y")
+ assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort
+
+ res = Net::HTTP.post_form(
+ URI.parse(url),
+ "a" => ["x1", "x2"],
+ "b" => "y")
+ assert_equal url, res['X-request-uri']
+ assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort
+
+ res = Net::HTTP.post_form(
+ URI.parse(url + '?a=x'),
+ "b" => "y")
+ assert_equal url + '?a=x', res['X-request-uri']
+ assert_equal ["b=y"], res.body.split(/[;&]/).sort
+ end
+
+ def test_patch
+ start {|http|
+ _test_patch__base http
+ }
+ end
+
+ def _test_patch__base(http)
+ uheader = {}
+ uheader['Accept'] = 'application/octet-stream'
+ uheader['Content-Type'] = 'application/x-www-form-urlencoded'
+ data = 'patch data'
+ res = http.patch('/', data, uheader)
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ assert_equal data, res.body
+ assert_equal data, res.entity
+ end
+
+ def test_timeout_during_HTTP_session
+ bug4246 = "expected the HTTP session to have timed out but have not. c.f. [ruby-core:34203]"
+
+ th = nil
+ # listen for connections... but deliberately do not read
+ TCPServer.open('localhost', 0) {|server|
+ port = server.addr[1]
+
+ conn = Net::HTTP.new('localhost', port)
+ conn.read_timeout = 0.01
+ conn.open_timeout = 0.1
+
+ th = Thread.new do
+ assert_raise(Net::ReadTimeout) {
+ conn.get('/')
+ }
+ end
+ assert th.join(10), bug4246
+ }
+ ensure
+ th.kill
+ th.join
+ end
+end
+
+
+module TestNetHTTP_version_1_2_methods
+
+ def test_request
+ start {|http|
+ _test_request__GET http
+ _test_request__accept_encoding http
+ _test_request__file http
+ # _test_request__range http # WEBrick does not support Range: header.
+ _test_request__HEAD http
+ _test_request__POST http
+ _test_request__stream_body http
+ _test_request__uri http
+ _test_request__uri_host http
+ }
+ end
+
+ def _test_request__GET(http)
+ req = Net::HTTP::Get.new('/')
+ http.request(req) {|res|
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ assert_equal $test_net_http_data.size, res.body.size
+ assert_equal $test_net_http_data, res.body
+
+ assert res.decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB
+ }
+ end
+
+ def _test_request__accept_encoding(http)
+ req = Net::HTTP::Get.new('/', 'accept-encoding' => 'deflate')
+ http.request(req) {|res|
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ assert_equal $test_net_http_data.size, res.body.size
+ assert_equal $test_net_http_data, res.body
+
+ assert_not_predicate res, :decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB
+ }
+ end
+
+ def _test_request__file(http)
+ req = Net::HTTP::Get.new('/')
+ http.request(req) {|res|
+ assert_kind_of Net::HTTPResponse, res
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ f = StringIO.new("".force_encoding("ASCII-8BIT"))
+ res.read_body f
+ assert_equal $test_net_http_data.bytesize, f.string.bytesize
+ assert_equal $test_net_http_data.encoding, f.string.encoding
+ assert_equal $test_net_http_data, f.string
+ }
+ end
+
+ def _test_request__range(http)
+ req = Net::HTTP::Get.new('/')
+ req['range'] = 'bytes=0-5'
+ assert_equal $test_net_http_data[0,6], http.request(req).body
+ end
+
+ def _test_request__HEAD(http)
+ req = Net::HTTP::Head.new('/')
+ http.request(req) {|res|
+ assert_kind_of Net::HTTPResponse, res
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ assert_nil res.body
+ }
+ end
+
+ def _test_request__POST(http)
+ data = 'post data'
+ req = Net::HTTP::Post.new('/')
+ req['Accept'] = $test_net_http_data_type
+ req['Content-Type'] = 'application/x-www-form-urlencoded'
+ http.request(req, data) {|res|
+ assert_kind_of Net::HTTPResponse, res
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_equal data.size, res['content-length'].to_i
+ end
+ assert_kind_of String, res.body
+ assert_equal data, res.body
+ }
+ end
+
+ def _test_request__stream_body(http)
+ req = Net::HTTP::Post.new('/')
+ data = $test_net_http_data
+ req.content_length = data.size
+ req['Content-Type'] = 'application/x-www-form-urlencoded'
+ req.body_stream = StringIO.new(data)
+ res = http.request(req)
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ assert_equal data.size, res.body.size
+ assert_equal data, res.body
+ end
+
+ def _test_request__path(http)
+ uri = URI 'https://hostname.example/'
+ req = Net::HTTP::Get.new('/')
+
+ res = http.request(req)
+
+ assert_kind_of URI::Generic, req.uri
+
+ assert_not_equal uri, req.uri
+
+ assert_equal uri, res.uri
+
+ assert_not_same uri, req.uri
+ assert_not_same req.uri, res.uri
+ end
+
+ def _test_request__uri(http)
+ uri = URI 'https://hostname.example/'
+ req = Net::HTTP::Get.new(uri)
+
+ res = http.request(req)
+
+ assert_kind_of URI::Generic, req.uri
+
+ assert_not_equal uri, req.uri
+
+ assert_equal req.uri, res.uri
+
+ assert_not_same uri, req.uri
+ assert_not_same req.uri, res.uri
+ end
+
+ def _test_request__uri_host(http)
+ uri = URI 'http://other.example/'
+
+ req = Net::HTTP::Get.new(uri)
+ req['host'] = 'hostname.example'
+
+ res = http.request(req)
+
+ assert_kind_of URI::Generic, req.uri
+
+ assert_equal URI("http://hostname.example:#{http.port}"), res.uri
+ end
+
+ def test_send_request
+ start {|http|
+ _test_send_request__GET http
+ _test_send_request__HEAD http
+ _test_send_request__POST http
+ }
+ end
+
+ def _test_send_request__GET(http)
+ res = http.send_request('GET', '/')
+ assert_kind_of Net::HTTPResponse, res
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ assert_kind_of String, res.body
+ assert_equal $test_net_http_data, res.body
+ end
+
+ def _test_send_request__HEAD(http)
+ res = http.send_request('HEAD', '/')
+ assert_kind_of Net::HTTPResponse, res
+ unless self.is_a?(TestNetHTTP_v1_2_chunked)
+ assert_not_nil res['content-length']
+ assert_equal $test_net_http_data.size, res['content-length'].to_i
+ end
+ assert_nil res.body
+ end
+
+ def _test_send_request__POST(http)
+ data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa'
+ res = http.send_request('POST', '/', data, 'content-type' => 'application/x-www-form-urlencoded')
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ assert_equal data.size, res.body.size
+ assert_equal data, res.body
+ end
+
+ def test_set_form
+ require 'tempfile'
+ Tempfile.create('ruby-test') {|file|
+ file << "\u{30c7}\u{30fc}\u{30bf}"
+ data = [
+ ['name', 'Gonbei Nanashi'],
+ ['name', "\u{540d}\u{7121}\u{3057}\u{306e}\u{6a29}\u{5175}\u{885b}"],
+ ['s"i\o', StringIO.new("\u{3042 3044 4e9c 925b}")],
+ ["file", file, filename: "ruby-test"]
+ ]
+ expected = <<"__EOM__".gsub(/\n/, "\r\n")
+--<boundary>
+Content-Disposition: form-data; name="name"
+
+Gonbei Nanashi
+--<boundary>
+Content-Disposition: form-data; name="name"
+
+\xE5\x90\x8D\xE7\x84\xA1\xE3\x81\x97\xE3\x81\xAE\xE6\xA8\xA9\xE5\x85\xB5\xE8\xA1\x9B
+--<boundary>
+Content-Disposition: form-data; name="s\\"i\\\\o"
+
+\xE3\x81\x82\xE3\x81\x84\xE4\xBA\x9C\xE9\x89\x9B
+--<boundary>
+Content-Disposition: form-data; name="file"; filename="ruby-test"
+Content-Type: application/octet-stream
+
+\xE3\x83\x87\xE3\x83\xBC\xE3\x82\xBF
+--<boundary>--
+__EOM__
+ start {|http|
+ _test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})
+ _test_set_form_multipart(http, false, data, expected)
+ _test_set_form_multipart(http, true, data, expected)
+ }
+ }
+ end
+
+ def _test_set_form_urlencoded(http, data)
+ req = Net::HTTP::Post.new('/')
+ req.set_form(data)
+ res = http.request req
+ assert_equal "name=Gonbei+Nanashi&name=%E5%90%8D%E7%84%A1%E3%81%97%E3%81%AE%E6%A8%A9%E5%85%B5%E8%A1%9B", res.body
+ end
+
+ def _test_set_form_multipart(http, chunked_p, data, expected)
+ data.each{|k,v|v.rewind rescue nil}
+ req = Net::HTTP::Post.new('/')
+ req.set_form(data, 'multipart/form-data')
+ req['Transfer-Encoding'] = 'chunked' if chunked_p
+ res = http.request req
+ body = res.body
+ assert_match(/\A--(?<boundary>\S+)/, body)
+ /\A--(?<boundary>\S+)/ =~ body
+ expected = expected.gsub(/<boundary>/, boundary)
+ assert_equal(expected, body)
+ end
+
+ def test_set_form_with_file
+ require 'tempfile'
+ Tempfile.create('ruby-test') {|file|
+ file.binmode
+ file << $test_net_http_data
+ filename = File.basename(file.to_path)
+ data = [['file', file]]
+ expected = <<"__EOM__".gsub(/\n/, "\r\n")
+--<boundary>
+Content-Disposition: form-data; name="file"; filename="<filename>"
+Content-Type: application/octet-stream
+
+<data>
+--<boundary>--
+__EOM__
+ expected.sub!(/<filename>/, filename)
+ expected.sub!(/<data>/, $test_net_http_data)
+ start {|http|
+ data.each{|k,v|v.rewind rescue nil}
+ req = Net::HTTP::Post.new('/')
+ req.set_form(data, 'multipart/form-data')
+ res = http.request req
+ body = res.body
+ header, _ = body.split(/\r\n\r\n/, 2)
+ assert_match(/\A--(?<boundary>\S+)/, body)
+ /\A--(?<boundary>\S+)/ =~ body
+ expected = expected.gsub(/<boundary>/, boundary)
+ assert_match(/^--(?<boundary>\S+)\r\n/, header)
+ assert_match(
+ /^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/,
+ header)
+ assert_equal(expected, body)
+
+ data.each{|k,v|v.rewind rescue nil}
+ req['Transfer-Encoding'] = 'chunked'
+ res = http.request req
+ #assert_equal(expected, res.body)
+ }
+ }
+ end
+end
+
+class TestNetHTTP_v1_2 < Test::Unit::TestCase
+ CONFIG = {
+ 'host' => '127.0.0.1',
+ 'proxy_host' => nil,
+ 'proxy_port' => nil,
+ }
+
+ include TestNetHTTPUtils
+ include TestNetHTTP_version_1_1_methods
+ include TestNetHTTP_version_1_2_methods
+
+ def new
+ Net::HTTP.version_1_2
+ super
+ end
+end
+
+class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase
+ CONFIG = {
+ 'host' => '127.0.0.1',
+ 'proxy_host' => nil,
+ 'proxy_port' => nil,
+ 'chunked' => true,
+ }
+
+ include TestNetHTTPUtils
+ include TestNetHTTP_version_1_1_methods
+ include TestNetHTTP_version_1_2_methods
+
+ def new
+ Net::HTTP.version_1_2
+ super
+ end
+
+ def test_chunked_break
+ assert_nothing_raised("[ruby-core:29229]") {
+ start {|http|
+ http.request_get('/') {|res|
+ res.read_body {|chunk|
+ break
+ }
+ }
+ }
+ }
+ end
+end
+
+class TestNetHTTPContinue < Test::Unit::TestCase
+ CONFIG = {
+ 'host' => '127.0.0.1',
+ 'proxy_host' => nil,
+ 'proxy_port' => nil,
+ 'chunked' => true,
+ }
+
+ include TestNetHTTPUtils
+
+ def logfile
+ @debug = StringIO.new('')
+ end
+
+ def mount_proc(&block)
+ @server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
+ end
+
+ def test_expect_continue
+ mount_proc {|req, res|
+ req.continue
+ res.body = req.query['body']
+ }
+ start {|http|
+ uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
+ http.continue_timeout = 0.2
+ http.request_post('/continue', 'body=BODY', uheader) {|res|
+ assert_equal('BODY', res.read_body)
+ }
+ }
+ assert_match(/Expect: 100-continue/, @debug.string)
+ assert_match(/HTTP\/1.1 100 continue/, @debug.string)
+ end
+
+ def test_expect_continue_timeout
+ mount_proc {|req, res|
+ sleep 0.2
+ req.continue # just ignored because it's '100'
+ res.body = req.query['body']
+ }
+ start {|http|
+ uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
+ http.continue_timeout = 0
+ http.request_post('/continue', 'body=BODY', uheader) {|res|
+ assert_equal('BODY', res.read_body)
+ }
+ }
+ assert_match(/Expect: 100-continue/, @debug.string)
+ assert_match(/HTTP\/1.1 100 continue/, @debug.string)
+ end
+
+ def test_expect_continue_error
+ mount_proc {|req, res|
+ res.status = 501
+ res.body = req.query['body']
+ }
+ start {|http|
+ uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
+ http.continue_timeout = 0
+ http.request_post('/continue', 'body=ERROR', uheader) {|res|
+ assert_equal('ERROR', res.read_body)
+ }
+ }
+ assert_match(/Expect: 100-continue/, @debug.string)
+ assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
+ end
+
+ def test_expect_continue_error_before_body
+ @log_tester = nil
+ mount_proc {|req, res|
+ raise WEBrick::HTTPStatus::Forbidden
+ }
+ start {|http|
+ uheader = {'content-length' => '5', 'expect' => '100-continue'}
+ http.continue_timeout = 1 # allow the server to respond before sending
+ http.request_post('/continue', 'data', uheader) {|res|
+ assert_equal(res.code, '403')
+ }
+ }
+ assert_match(/Expect: 100-continue/, @debug.string)
+ assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
+ end
+
+ def test_expect_continue_error_while_waiting
+ mount_proc {|req, res|
+ res.status = 501
+ res.body = req.query['body']
+ }
+ start {|http|
+ uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
+ http.continue_timeout = 0.5
+ http.request_post('/continue', 'body=ERROR', uheader) {|res|
+ assert_equal('ERROR', res.read_body)
+ }
+ }
+ assert_match(/Expect: 100-continue/, @debug.string)
+ assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
+ end
+end
+
+class TestNetHTTPSwitchingProtocols < Test::Unit::TestCase
+ CONFIG = {
+ 'host' => '127.0.0.1',
+ 'proxy_host' => nil,
+ 'proxy_port' => nil,
+ 'chunked' => true,
+ }
+
+ include TestNetHTTPUtils
+
+ def logfile
+ @debug = StringIO.new('')
+ end
+
+ def mount_proc(&block)
+ @server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
+ end
+
+ def test_info
+ mount_proc {|req, res|
+ req.instance_variable_get(:@socket) << "HTTP/1.1 101 Switching Protocols\r\n\r\n"
+ res.body = req.query['body']
+ }
+ start {|http|
+ http.continue_timeout = 0.2
+ http.request_post('/continue', 'body=BODY') {|res|
+ assert_equal('BODY', res.read_body)
+ }
+ }
+ assert_match(/HTTP\/1.1 101 Switching Protocols/, @debug.string)
+ end
+end
+
+class TestNetHTTPKeepAlive < Test::Unit::TestCase
+ CONFIG = {
+ 'host' => '127.0.0.1',
+ 'proxy_host' => nil,
+ 'proxy_port' => nil,
+ 'RequestTimeout' => 1,
+ }
+
+ include TestNetHTTPUtils
+
+ def test_keep_alive_get_auto_reconnect
+ start {|http|
+ res = http.get('/')
+ http.keep_alive_timeout = 1
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ sleep 1.5
+ assert_nothing_raised {
+ res = http.get('/')
+ }
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ }
+ end
+
+ def test_server_closed_connection_auto_reconnect
+ start {|http|
+ res = http.get('/')
+ http.keep_alive_timeout = 5
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ sleep 1.5
+ assert_nothing_raised {
+ # Net::HTTP should detect the closed connection before attempting the
+ # request, since post requests cannot be retried.
+ res = http.post('/', 'query=foo', 'content-type' => 'application/x-www-form-urlencoded')
+ }
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ }
+ end
+
+ def test_keep_alive_get_auto_retry
+ start {|http|
+ res = http.get('/')
+ http.keep_alive_timeout = 5
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ sleep 1.5
+ res = http.get('/')
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ }
+ end
+
+ class MockSocket
+ attr_reader :count
+ def initialize(success_after: nil)
+ @success_after = success_after
+ @count = 0
+ end
+ def close
+ end
+ def closed?
+ end
+ def write(_)
+ end
+ def readline
+ @count += 1
+ if @success_after && @success_after <= @count
+ "HTTP/1.1 200 OK"
+ else
+ raise Errno::ECONNRESET
+ end
+ end
+ def readuntil(*_)
+ ""
+ end
+ def read_all(_)
+ end
+ end
+
+ def test_http_retry_success
+ start {|http|
+ socket = MockSocket.new(success_after: 10)
+ http.instance_variable_get(:@socket).close
+ http.instance_variable_set(:@socket, socket)
+ assert_equal 0, socket.count
+ http.max_retries = 10
+ res = http.get('/')
+ assert_equal 10, socket.count
+ assert_kind_of Net::HTTPResponse, res
+ assert_kind_of String, res.body
+ }
+ end
+
+ def test_http_retry_failed
+ start {|http|
+ socket = MockSocket.new
+ http.instance_variable_get(:@socket).close
+ http.instance_variable_set(:@socket, socket)
+ http.max_retries = 10
+ assert_raise(Errno::ECONNRESET){ http.get('/') }
+ assert_equal 11, socket.count
+ }
+ end
+
+ def test_keep_alive_server_close
+ def @server.run(sock)
+ sock.close
+ end
+
+ start {|http|
+ assert_raise(EOFError, Errno::ECONNRESET, IOError) {
+ http.get('/')
+ }
+ }
+ end
+end
+
+class TestNetHTTPLocalBind < Test::Unit::TestCase
+ CONFIG = {
+ 'host' => 'localhost',
+ 'proxy_host' => nil,
+ 'proxy_port' => nil,
+ }
+
+ include TestNetHTTPUtils
+
+ def test_bind_to_local_host
+ @server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip }
+
+ http = Net::HTTP.new(config('host'), config('port'))
+ http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address
+ assert_not_nil(http.local_host)
+ assert_nil(http.local_port)
+
+ res = http.get('/show_ip')
+ assert_equal(http.local_host, res.body)
+ end
+
+ def test_bind_to_local_port
+ @server.mount_proc('/show_port') { |req, res| res.body = req.peeraddr[1].to_s }
+
+ http = Net::HTTP.new(config('host'), config('port'))
+ http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address
+ http.local_port = Addrinfo.tcp(config('host'), 0).bind {|s|
+ s.local_address.ip_port.to_s
+ }
+ assert_not_nil(http.local_host)
+ assert_not_nil(http.local_port)
+
+ res = http.get('/show_port')
+ assert_equal(http.local_port, res.body)
+ end
+end
+
diff --git a/test/net/http/test_http_request.rb b/test/net/http/test_http_request.rb
new file mode 100644
index 0000000000..c2144d86c7
--- /dev/null
+++ b/test/net/http/test_http_request.rb
@@ -0,0 +1,80 @@
+# frozen_string_literal: false
+require 'net/http'
+require 'test/unit'
+require 'stringio'
+
+class HTTPRequestTest < Test::Unit::TestCase
+
+ def test_initialize_GET
+ req = Net::HTTP::Get.new '/'
+
+ assert_equal 'GET', req.method
+ assert_not_predicate req, :request_body_permitted?
+ assert_predicate req, :response_body_permitted?
+
+ expected = {
+ 'accept' => %w[*/*],
+ 'user-agent' => %w[Ruby],
+ }
+
+ expected['accept-encoding'] = %w[gzip;q=1.0,deflate;q=0.6,identity;q=0.3] if
+ Net::HTTP::HAVE_ZLIB
+
+ assert_equal expected, req.to_hash
+ end
+
+ def test_initialize_GET_range
+ req = Net::HTTP::Get.new '/', 'Range' => 'bytes=0-9'
+
+ assert_equal 'GET', req.method
+ assert_not_predicate req, :request_body_permitted?
+ assert_predicate req, :response_body_permitted?
+
+ expected = {
+ 'accept' => %w[*/*],
+ 'user-agent' => %w[Ruby],
+ 'range' => %w[bytes=0-9],
+ }
+
+ assert_equal expected, req.to_hash
+ end
+
+ def test_initialize_HEAD
+ req = Net::HTTP::Head.new '/'
+
+ assert_equal 'HEAD', req.method
+ assert_not_predicate req, :request_body_permitted?
+ assert_not_predicate req, :response_body_permitted?
+
+ expected = {
+ 'accept' => %w[*/*],
+ 'user-agent' => %w[Ruby],
+ }
+
+ assert_equal expected, req.to_hash
+ end
+
+ def test_initialize_accept_encoding
+ req1 = Net::HTTP::Get.new '/'
+
+ assert req1.decode_content, 'Bug #7831 - automatically decode content'
+
+ req2 = Net::HTTP::Get.new '/', 'accept-encoding' => 'identity'
+
+ assert_not_predicate req2, :decode_content,
+ 'Bug #7381 - do not decode content if the user overrides'
+ end if Net::HTTP::HAVE_ZLIB
+
+ def test_header_set
+ req = Net::HTTP::Get.new '/'
+
+ assert req.decode_content, 'Bug #7831 - automatically decode content'
+
+ req['accept-encoding'] = 'identity'
+
+ assert_not_predicate req, :decode_content,
+ 'Bug #7831 - do not decode content if the user overrides'
+ end if Net::HTTP::HAVE_ZLIB
+
+end
+
diff --git a/test/net/http/test_httpheader.rb b/test/net/http/test_httpheader.rb
index 3ba1b217c0..f8778522eb 100644
--- a/test/net/http/test_httpheader.rb
+++ b/test/net/http/test_httpheader.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
require 'net/http'
require 'test/unit'
@@ -15,6 +16,21 @@ class HTTPHeaderTest < Test::Unit::TestCase
@c = C.new
end
+ def test_initialize
+ @c.initialize_http_header("foo"=>"abc")
+ assert_equal "abc", @c["foo"]
+ @c.initialize_http_header("foo"=>"abc", "bar"=>"xyz")
+ assert_equal "xyz", @c["bar"]
+ @c.initialize_http_header([["foo", "abc"]])
+ assert_equal "abc", @c["foo"]
+ @c.initialize_http_header([["foo", "abc"], ["bar","xyz"]])
+ assert_equal "xyz", @c["bar"]
+ assert_raise(NoMethodError){ @c.initialize_http_header("foo"=>[]) }
+ assert_raise(ArgumentError){ @c.initialize_http_header("foo"=>"a\nb") }
+ assert_raise(ArgumentError){ @c.initialize_http_header("foo"=>"a\rb") }
+ assert_raise(ArgumentError){ @c.initialize_http_header("foo"=>"a\xff") }
+ end
+
def test_size
assert_equal 0, @c.size
@c['a'] = 'a'
@@ -39,6 +55,16 @@ class HTTPHeaderTest < Test::Unit::TestCase
@c['aaA'] = 'aaa'
@c['AAa'] = 'aaa'
assert_equal 2, @c.length
+
+ @c['aaa'] = ['aaa', ['bbb', [3]]]
+ assert_equal 2, @c.length
+ assert_equal ['aaa', 'bbb', '3'], @c.get_fields('aaa')
+
+ @c['aaa'] = "aaa\xff"
+ assert_equal 2, @c.length
+
+ assert_raise(ArgumentError){ @c['foo'] = "a\nb" }
+ assert_raise(ArgumentError){ @c['foo'] = ["a\nb"] }
end
def test_AREF
@@ -64,6 +90,10 @@ class HTTPHeaderTest < Test::Unit::TestCase
@c.add_field 'My-Header', 'd, d'
assert_equal 'a, b, c, d, d', @c['My-Header']
assert_equal ['a', 'b', 'c', 'd, d'], @c.get_fields('My-Header')
+ assert_raise(ArgumentError){ @c.add_field 'My-Header', "d\nd" }
+ @c.add_field 'My-Header', ['e', ["\xff", 7]]
+ assert_equal "a, b, c, d, d, e, \xff, 7", @c['My-Header']
+ assert_equal ['a', 'b', 'c', 'd, d', 'e', "\xff", '7'], @c.get_fields('My-Header')
end
def test_get_fields
@@ -81,6 +111,12 @@ class HTTPHeaderTest < Test::Unit::TestCase
assert_equal ['test string'], @c.get_fields('my-header')
end
+ class D; include Net::HTTPHeader; end
+
+ def test_nil_variable_header
+ assert_nothing_raised { D.new.initialize_http_header({Authorization: nil}) }
+ end
+
def test_delete
@c['My-Header'] = 'test'
assert_equal 'test', @c['My-Header']
@@ -104,6 +140,12 @@ class HTTPHeaderTest < Test::Unit::TestCase
assert_equal 'my-header', k
assert_equal 'test', v
end
+ e = @c.each
+ assert_equal 1, e.size
+ e.each do |k, v|
+ assert_equal 'my-header', k
+ assert_equal 'test', v
+ end
end
def test_each_key
@@ -114,6 +156,26 @@ class HTTPHeaderTest < Test::Unit::TestCase
@c.each_key do |k|
assert_equal 'my-header', k
end
+ e = @c.each_key
+ assert_equal 1, e.size
+ e.each do |k|
+ assert_equal 'my-header', k
+ end
+ end
+
+ def test_each_capitalized_name
+ @c['my-header'] = 'test'
+ @c.each_capitalized_name do |k|
+ assert_equal 'My-Header', k
+ end
+ @c.each_capitalized_name do |k|
+ assert_equal 'My-Header', k
+ end
+ e = @c.each_capitalized_name
+ assert_equal 1, e.size
+ e.each do |k|
+ assert_equal 'My-Header', k
+ end
end
def test_each_value
@@ -124,6 +186,11 @@ class HTTPHeaderTest < Test::Unit::TestCase
@c.each_value do |v|
assert_equal 'test', v
end
+ e = @c.each_value
+ assert_equal 1, e.size
+ e.each do |v|
+ assert_equal 'test', v
+ end
end
def test_canonical_each
@@ -132,6 +199,12 @@ class HTTPHeaderTest < Test::Unit::TestCase
assert_equal 'My-Header', k
assert_equal 'a, b', v
end
+ e = @c.canonical_each
+ assert_equal 1, e.size
+ e.each do |k,v|
+ assert_equal 'My-Header', k
+ assert_equal 'a, b', v
+ end
end
def test_each_capitalized
@@ -140,6 +213,26 @@ class HTTPHeaderTest < Test::Unit::TestCase
assert_equal 'My-Header', k
assert_equal 'a, b', v
end
+ e = @c.each_capitalized
+ assert_equal 1, e.size
+ e.each do |k,v|
+ assert_equal 'My-Header', k
+ assert_equal 'a, b', v
+ end
+ end
+
+ def test_each_capitalized_with_symbol
+ @c[:my_header] = ['a', 'b']
+ @c.each_capitalized do |k,v|
+ assert_equal "My_header", k
+ assert_equal 'a, b', v
+ end
+ e = @c.each_capitalized
+ assert_equal 1, e.size
+ e.each do |k,v|
+ assert_equal 'My_header', k
+ assert_equal 'a, b', v
+ end
end
def test_key?
@@ -156,15 +249,32 @@ class HTTPHeaderTest < Test::Unit::TestCase
end
def test_range
- try_range(1..5, '1-5')
- try_range(234..567, '234-567')
- try_range(-5..-1, '-5')
- try_range(1..-1, '1-')
+ try_range([1..5], '1-5')
+ try_invalid_range('5-1')
+ try_range([234..567], '234-567')
+ try_range([-5..-1], '-5')
+ try_invalid_range('-0')
+ try_range([1..-1], '1-')
+ try_range([0..0,-1..-1], '0-0,-1')
+ try_range([1..2, 3..4], '1-2,3-4')
+ try_range([1..2, 3..4], '1-2 , 3-4')
+ try_range([1..2, 1..4], '1-2,1-4')
+
+ try_invalid_range('invalid')
+ try_invalid_range(' 12-')
+ try_invalid_range('12- ')
+ try_invalid_range('123-abc')
+ try_invalid_range('abc-123')
end
def try_range(r, s)
@c['range'] = "bytes=#{s}"
- assert_equal r, Array(@c.range)[0]
+ assert_equal r, @c.range
+ end
+
+ def try_invalid_range(s)
+ @c['range'] = "bytes=#{s}"
+ assert_raise(Net::HTTPHeaderSyntaxError, s){ @c.range }
end
def test_range=
@@ -215,7 +325,7 @@ class HTTPHeaderTest < Test::Unit::TestCase
try_content_length 123, ' 123'
try_content_length 1, '1 23'
try_content_length 500, '(OK)500'
- assert_raises(Net::HTTPHeaderSyntaxError, 'here is no digit, but') {
+ assert_raise(Net::HTTPHeaderSyntaxError, 'here is no digit, but') {
@c['content-length'] = 'no digit'
@c.content_length
}
diff --git a/test/net/http/test_httpresponse.rb b/test/net/http/test_httpresponse.rb
new file mode 100644
index 0000000000..d99c3611c5
--- /dev/null
+++ b/test/net/http/test_httpresponse.rb
@@ -0,0 +1,437 @@
+# coding: US-ASCII
+# frozen_string_literal: false
+require 'net/http'
+require 'test/unit'
+require 'stringio'
+
+class HTTPResponseTest < Test::Unit::TestCase
+ def test_singleline_header
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Content-Length: 5
+Connection: close
+
+hello
+EOS
+ res = Net::HTTPResponse.read_new(io)
+ assert_equal('5', res['content-length'])
+ assert_equal('close', res['connection'])
+ end
+
+ def test_multiline_header
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+X-Foo: XXX
+ YYY
+X-Bar:
+ XXX
+\tYYY
+
+hello
+EOS
+ res = Net::HTTPResponse.read_new(io)
+ assert_equal('XXX YYY', res['x-foo'])
+ assert_equal('XXX YYY', res['x-bar'])
+ end
+
+ def test_read_body
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Length: 5
+
+hello
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ assert_equal 'hello', body
+ end
+
+ def test_read_body_block
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Length: 5
+
+hello
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+
+ body = ''
+
+ res.reading_body io, true do
+ res.read_body do |chunk|
+ body << chunk
+ end
+ end
+
+ assert_equal 'hello', body
+ end
+
+ def test_read_body_block_mod
+ IO.pipe do |r, w|
+ buf = 'x' * 1024
+ buf.freeze
+ n = 1024
+ len = n * buf.size
+ th = Thread.new do
+ w.write("HTTP/1.1 200 OK\r\nContent-Length: #{len}\r\n\r\n")
+ n.times { w.write(buf) }
+ :ok
+ end
+ io = Net::BufferedIO.new(r)
+ res = Net::HTTPResponse.read_new(io)
+ nr = 0
+ res.reading_body io, true do
+ # should be allowed to modify the chunk given to them:
+ res.read_body do |chunk|
+ nr += chunk.size
+ chunk.clear
+ end
+ end
+ assert_equal len, nr
+ assert_equal :ok, th.value
+ end
+ end
+
+ def test_read_body_content_encoding_deflate
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Encoding: deflate
+Content-Length: 13
+
+x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ res.decode_content = true
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ if Net::HTTP::HAVE_ZLIB
+ assert_equal nil, res['content-encoding']
+ assert_equal 'hello', body
+ else
+ assert_equal 'deflate', res['content-encoding']
+ assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
+ end
+ end
+
+ def test_read_body_content_encoding_deflate_uppercase
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Encoding: DEFLATE
+Content-Length: 13
+
+x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ res.decode_content = true
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ if Net::HTTP::HAVE_ZLIB
+ assert_equal nil, res['content-encoding']
+ assert_equal 'hello', body
+ else
+ assert_equal 'DEFLATE', res['content-encoding']
+ assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
+ end
+ end
+
+ def test_read_body_content_encoding_deflate_chunked
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Encoding: deflate
+Transfer-Encoding: chunked
+
+6
+x\x9C\xCBH\xCD\xC9
+7
+\xC9\a\x00\x06,\x02\x15
+0
+
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ res.decode_content = true
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ if Net::HTTP::HAVE_ZLIB
+ assert_equal nil, res['content-encoding']
+ assert_equal 'hello', body
+ else
+ assert_equal 'deflate', res['content-encoding']
+ assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body
+ end
+ end
+
+ def test_read_body_content_encoding_deflate_disabled
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Encoding: deflate
+Content-Length: 13
+
+x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ res.decode_content = false # user set accept-encoding in request
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ assert_equal 'deflate', res['content-encoding'], 'Bug #7831'
+ assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15", body, 'Bug #7381'
+ end
+
+ def test_read_body_content_encoding_deflate_no_length
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Encoding: deflate
+
+x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ res.decode_content = true
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ if Net::HTTP::HAVE_ZLIB
+ assert_equal nil, res['content-encoding']
+ assert_equal 'hello', body
+ else
+ assert_equal 'deflate', res['content-encoding']
+ assert_equal "x\x9C\xCBH\xCD\xC9\xC9\a\x00\x06,\x02\x15\r\n", body
+ end
+ end
+
+ def test_read_body_content_encoding_deflate_content_range
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Accept-Ranges: bytes
+Connection: close
+Content-Encoding: gzip
+Content-Length: 10
+Content-Range: bytes 0-9/55
+
+\x1F\x8B\b\x00\x00\x00\x00\x00\x00\x03
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ assert_equal "\x1F\x8B\b\x00\x00\x00\x00\x00\x00\x03", body
+ end
+
+ def test_read_body_content_encoding_deflate_empty_body
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Encoding: deflate
+Content-Length: 0
+
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ res.decode_content = true
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ if Net::HTTP::HAVE_ZLIB
+ assert_equal nil, res['content-encoding']
+ assert_equal '', body
+ else
+ assert_equal 'deflate', res['content-encoding']
+ assert_equal '', body
+ end
+ end
+
+ def test_read_body_content_encoding_deflate_empty_body_no_length
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Encoding: deflate
+
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ res.decode_content = true
+
+ body = nil
+
+ res.reading_body io, true do
+ body = res.read_body
+ end
+
+ if Net::HTTP::HAVE_ZLIB
+ assert_equal nil, res['content-encoding']
+ assert_equal '', body
+ else
+ assert_equal 'deflate', res['content-encoding']
+ assert_equal '', body
+ end
+ end
+
+ def test_read_body_string
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Connection: close
+Content-Length: 5
+
+hello
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+
+ body = ''
+
+ res.reading_body io, true do
+ res.read_body body
+ end
+
+ assert_equal 'hello', body
+ end
+
+ def test_uri_equals
+ uri = URI 'http://example'
+
+ response = Net::HTTPResponse.new '1.1', 200, 'OK'
+
+ response.uri = nil
+
+ assert_nil response.uri
+
+ response.uri = uri
+
+ assert_equal uri, response.uri
+ assert_not_same uri, response.uri
+ end
+
+ def test_ensure_zero_space_does_not_regress
+ io = dummy_io(<<EOS)
+HTTP/1.1 200OK
+Content-Length: 5
+Connection: close
+
+hello
+EOS
+
+ assert_raise Net::HTTPBadResponse do
+ Net::HTTPResponse.read_new(io)
+ end
+ end
+
+ def test_allow_trailing_space_after_status
+ io = dummy_io(<<EOS)
+HTTP/1.1 200\s
+Content-Length: 5
+Connection: close
+
+hello
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ assert_equal('1.1', res.http_version)
+ assert_equal('200', res.code)
+ assert_equal('', res.message)
+ end
+
+ def test_normal_status_line
+ io = dummy_io(<<EOS)
+HTTP/1.1 200 OK
+Content-Length: 5
+Connection: close
+
+hello
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ assert_equal('1.1', res.http_version)
+ assert_equal('200', res.code)
+ assert_equal('OK', res.message)
+ end
+
+ def test_allow_empty_reason_code
+ io = dummy_io(<<EOS)
+HTTP/1.1 200
+Content-Length: 5
+Connection: close
+
+hello
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ assert_equal('1.1', res.http_version)
+ assert_equal('200', res.code)
+ assert_equal(nil, res.message)
+ end
+
+ def test_raises_exception_with_missing_reason
+ io = dummy_io(<<EOS)
+HTTP/1.1 404
+Content-Length: 5
+Connection: close
+
+hello
+EOS
+
+ res = Net::HTTPResponse.read_new(io)
+ assert_equal(nil, res.message)
+ assert_raise Net::HTTPServerException do
+ res.error!
+ end
+ end
+
+private
+
+ def dummy_io(str)
+ str = str.gsub(/\n/, "\r\n")
+
+ Net::BufferedIO.new(StringIO.new(str))
+ end
+end
diff --git a/test/net/http/test_httpresponses.rb b/test/net/http/test_httpresponses.rb
new file mode 100644
index 0000000000..b389e163cf
--- /dev/null
+++ b/test/net/http/test_httpresponses.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: false
+require 'net/http'
+require 'test/unit'
+
+class HTTPResponsesTest < Test::Unit::TestCase
+ def test_status_code_classes
+ Net::HTTPResponse::CODE_TO_OBJ.each_pair { |code, klass|
+ case code
+ when /\A1\d\d\z/
+ group = Net::HTTPInformation
+ when /\A2\d\d\z/
+ group = Net::HTTPSuccess
+ when /\A3\d\d\z/
+ group = Net::HTTPRedirection
+ when /\A4\d\d\z/
+ group = Net::HTTPClientError
+ when /\A5\d\d\z/
+ group = Net::HTTPServerError
+ else
+ flunk "Unknown HTTP status code: #{code} => #{klass.name}"
+ end
+ assert(klass < group, "#{klass.name} (#{code}) must inherit from #{group.name}")
+ }
+ end
+end
diff --git a/test/net/http/test_https.rb b/test/net/http/test_https.rb
new file mode 100644
index 0000000000..3a23410241
--- /dev/null
+++ b/test/net/http/test_https.rb
@@ -0,0 +1,217 @@
+# frozen_string_literal: false
+require "test/unit"
+begin
+ require 'net/https'
+ require 'stringio'
+ require 'timeout'
+ require File.expand_path("utils", File.dirname(__FILE__))
+rescue LoadError
+ # should skip this test
+end
+
+class TestNetHTTPS < Test::Unit::TestCase
+ include TestNetHTTPUtils
+
+ def self.fixture(key)
+ File.read(File.expand_path("../fixtures/#{key}", __dir__))
+ end
+
+ CA_CERT = OpenSSL::X509::Certificate.new(fixture("cacert.pem"))
+ SERVER_KEY = OpenSSL::PKey.read(fixture("server.key"))
+ SERVER_CERT = OpenSSL::X509::Certificate.new(fixture("server.crt"))
+ DHPARAMS = OpenSSL::PKey::DH.new(fixture("dhparams.pem"))
+ TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
+
+ CONFIG = {
+ 'host' => '127.0.0.1',
+ 'proxy_host' => nil,
+ 'proxy_port' => nil,
+ 'ssl_enable' => true,
+ 'ssl_certificate' => SERVER_CERT,
+ 'ssl_private_key' => SERVER_KEY,
+ 'ssl_tmp_dh_callback' => proc { DHPARAMS },
+ }
+
+ def test_get
+ http = Net::HTTP.new("localhost", config("port"))
+ http.use_ssl = true
+ http.cert_store = TEST_STORE
+ certs = []
+ http.verify_callback = Proc.new do |preverify_ok, store_ctx|
+ certs << store_ctx.current_cert
+ preverify_ok
+ end
+ http.request_get("/") {|res|
+ assert_equal($test_net_http_data, res.body)
+ }
+ # TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
+ certs.zip([SERVER_CERT, CA_CERT]) do |actual, expected|
+ assert_equal(expected.to_der, actual.to_der)
+ end
+ rescue SystemCallError
+ skip $!
+ end
+
+ def test_post
+ http = Net::HTTP.new("localhost", config("port"))
+ http.use_ssl = true
+ http.cert_store = TEST_STORE
+ data = config('ssl_private_key').to_der
+ http.request_post("/", data, {'content-type' => 'application/x-www-form-urlencoded'}) {|res|
+ assert_equal(data, res.body)
+ }
+ rescue SystemCallError
+ skip $!
+ end
+
+ def test_session_reuse
+ # FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
+ # See https://github.com/openssl/openssl/pull/5967 for details.
+ skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
+
+ http = Net::HTTP.new("localhost", config("port"))
+ http.use_ssl = true
+ http.cert_store = TEST_STORE
+
+ http.start
+ http.get("/")
+ http.finish
+
+ http.start
+ http.get("/")
+
+ socket = http.instance_variable_get(:@socket).io
+ assert_equal true, socket.session_reused?
+
+ http.finish
+ rescue SystemCallError
+ skip $!
+ end
+
+ def test_session_reuse_but_expire
+ # FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
+ skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
+
+ http = Net::HTTP.new("localhost", config("port"))
+ http.use_ssl = true
+ http.cert_store = TEST_STORE
+
+ http.ssl_timeout = -1
+ http.start
+ http.get("/")
+ http.finish
+
+ http.start
+ http.get("/")
+
+ socket = http.instance_variable_get(:@socket).io
+ assert_equal false, socket.session_reused?
+
+ http.finish
+ rescue SystemCallError
+ skip $!
+ end
+
+ if ENV["RUBY_OPENSSL_TEST_ALL"]
+ def test_verify
+ http = Net::HTTP.new("ssl.netlab.jp", 443)
+ http.use_ssl = true
+ assert(
+ (http.request_head("/"){|res| } rescue false),
+ "The system may not have default CA certificate store."
+ )
+ end
+ end
+
+ def test_verify_none
+ http = Net::HTTP.new("localhost", config("port"))
+ http.use_ssl = true
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+ http.request_get("/") {|res|
+ assert_equal($test_net_http_data, res.body)
+ }
+ rescue SystemCallError
+ skip $!
+ end
+
+ def test_certificate_verify_failure
+ http = Net::HTTP.new("localhost", config("port"))
+ http.use_ssl = true
+ ex = assert_raise(OpenSSL::SSL::SSLError){
+ begin
+ http.request_get("/") {|res| }
+ rescue SystemCallError
+ skip $!
+ end
+ }
+ assert_match(/certificate verify failed/, ex.message)
+ unless /mswin|mingw/ =~ RUBY_PLATFORM
+ # on Windows, Errno::ECONNRESET will be raised, and it'll be eaten by
+ # WEBrick
+ @log_tester = lambda {|log|
+ assert_equal(1, log.length)
+ assert_match(/ERROR OpenSSL::SSL::SSLError:/, log[0])
+ }
+ end
+ end
+
+ def test_identity_verify_failure
+ # the certificate's subject has CN=localhost
+ http = Net::HTTP.new("127.0.0.1", config("port"))
+ http.use_ssl = true
+ http.cert_store = TEST_STORE
+ @log_tester = lambda {|_| }
+ ex = assert_raise(OpenSSL::SSL::SSLError){
+ http.request_get("/") {|res| }
+ }
+ re_msg = /certificate verify failed|hostname \"127.0.0.1\" does not match/
+ assert_match(re_msg, ex.message)
+ end
+
+ def test_timeout_during_SSL_handshake
+ bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]"
+
+ # listen for connections... but deliberately do not complete SSL handshake
+ TCPServer.open('localhost', 0) {|server|
+ port = server.addr[1]
+
+ conn = Net::HTTP.new('localhost', port)
+ conn.use_ssl = true
+ conn.read_timeout = 0.01
+ conn.open_timeout = 0.01
+
+ th = Thread.new do
+ assert_raise(Net::OpenTimeout) {
+ conn.get('/')
+ }
+ end
+ assert th.join(10), bug4246
+ }
+ end
+
+ def test_min_version
+ http = Net::HTTP.new("localhost", config("port"))
+ http.use_ssl = true
+ http.min_version = :TLS1
+ http.cert_store = TEST_STORE
+ http.request_get("/") {|res|
+ assert_equal($test_net_http_data, res.body)
+ }
+ end
+
+ def test_max_version
+ http = Net::HTTP.new("127.0.0.1", config("port"))
+ http.use_ssl = true
+ http.max_version = :SSL2
+ http.verify_callback = Proc.new do |preverify_ok, store_ctx|
+ true
+ end
+ @log_tester = lambda {|_| }
+ ex = assert_raise(OpenSSL::SSL::SSLError){
+ http.request_get("/") {|res| }
+ }
+ re_msg = /\ASSL_connect returned=1 errno=0 |SSL_CTX_set_max_proto_version/
+ assert_match(re_msg, ex.message)
+ end
+
+end if defined?(OpenSSL::SSL)
diff --git a/test/net/http/test_https_proxy.rb b/test/net/http/test_https_proxy.rb
index 4f26342339..f833f1a1e3 100644
--- a/test/net/http/test_https_proxy.rb
+++ b/test/net/http/test_https_proxy.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
begin
require 'net/https'
rescue LoadError
@@ -6,10 +7,15 @@ require 'test/unit'
class HTTPSProxyTest < Test::Unit::TestCase
def test_https_proxy_authentication
- t = nil
+ begin
+ OpenSSL
+ rescue LoadError
+ skip 'autoload problem. see [ruby-dev:45021][Bug #5786]'
+ end
+
TCPServer.open("127.0.0.1", 0) {|serv|
_, port, _, _ = serv.addr
- t = Thread.new {
+ client_thread = Thread.new {
proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password')
http = proxy.new("foo.example.org", 8000)
http.use_ssl = true
@@ -19,19 +25,23 @@ class HTTPSProxyTest < Test::Unit::TestCase
rescue EOFError
end
}
- sock = serv.accept
- proxy_request = sock.gets("\r\n\r\n")
- assert_equal(
- "CONNECT foo.example.org:8000 HTTP/1.1\r\n" +
- "Host: foo.example.org:8000\r\n" +
- "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" +
- "\r\n",
- proxy_request,
- "[ruby-dev:25673]")
- sock.close
+ server_thread = Thread.new {
+ sock = serv.accept
+ begin
+ proxy_request = sock.gets("\r\n\r\n")
+ assert_equal(
+ "CONNECT foo.example.org:8000 HTTP/1.1\r\n" +
+ "Host: foo.example.org:8000\r\n" +
+ "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" +
+ "\r\n",
+ proxy_request,
+ "[ruby-dev:25673]")
+ ensure
+ sock.close
+ end
+ }
+ assert_join_threads([client_thread, server_thread])
}
- ensure
- t.join if t
end
end if defined?(OpenSSL)
-
+
diff --git a/test/net/http/utils.rb b/test/net/http/utils.rb
new file mode 100644
index 0000000000..dbfd112f31
--- /dev/null
+++ b/test/net/http/utils.rb
@@ -0,0 +1,110 @@
+# frozen_string_literal: false
+require 'webrick'
+begin
+ require "webrick/https"
+rescue LoadError
+ # SSL features cannot be tested
+end
+require 'webrick/httpservlet/abstract'
+
+module TestNetHTTPUtils
+ def start(&block)
+ new().start(&block)
+ end
+
+ def new
+ klass = Net::HTTP::Proxy(config('proxy_host'), config('proxy_port'))
+ http = klass.new(config('host'), config('port'))
+ http.set_debug_output logfile()
+ http
+ end
+
+ def config(key)
+ @config ||= self.class::CONFIG
+ @config[key]
+ end
+
+ def logfile
+ $DEBUG ? $stderr : NullWriter.new
+ end
+
+ def setup
+ spawn_server
+ end
+
+ def teardown
+ if @server
+ @server.shutdown
+ @server_thread.join
+ WEBrick::Utils::TimeoutHandler.terminate
+ end
+ @log_tester.call(@log) if @log_tester
+ # resume global state
+ Net::HTTP.version_1_2
+ end
+
+ def spawn_server
+ @log = []
+ @log_tester = lambda {|log| assert_equal([], log ) }
+ @config = self.class::CONFIG
+ server_config = {
+ :BindAddress => config('host'),
+ :Port => 0,
+ :Logger => WEBrick::Log.new(@log, WEBrick::BasicLog::WARN),
+ :AccessLog => [],
+ :ServerType => Thread,
+ }
+ server_config[:OutputBufferSize] = 4 if config('chunked')
+ server_config[:RequestTimeout] = config('RequestTimeout') if config('RequestTimeout')
+ if defined?(OpenSSL) and config('ssl_enable')
+ server_config.update({
+ :SSLEnable => true,
+ :SSLCertificate => config('ssl_certificate'),
+ :SSLPrivateKey => config('ssl_private_key'),
+ :SSLTmpDhCallback => config('ssl_tmp_dh_callback'),
+ })
+ end
+ @server = WEBrick::HTTPServer.new(server_config)
+ @server.mount('/', Servlet, config('chunked'))
+ @server_thread = @server.start
+ @config['port'] = @server[:Port]
+ end
+
+ $test_net_http = nil
+ $test_net_http_data = (0...256).to_a.map {|i| i.chr }.join('') * 64
+ $test_net_http_data.force_encoding("ASCII-8BIT")
+ $test_net_http_data_type = 'application/octet-stream'
+
+ class Servlet < WEBrick::HTTPServlet::AbstractServlet
+ def initialize(this, chunked = false)
+ @chunked = chunked
+ end
+
+ def do_GET(req, res)
+ res['Content-Type'] = $test_net_http_data_type
+ res.body = $test_net_http_data
+ res.chunked = @chunked
+ end
+
+ # echo server
+ def do_POST(req, res)
+ res['Content-Type'] = req['Content-Type']
+ res['X-request-uri'] = req.request_uri.to_s
+ res.body = req.body
+ res.chunked = @chunked
+ end
+
+ def do_PATCH(req, res)
+ res['Content-Type'] = req['Content-Type']
+ res.body = req.body
+ res.chunked = @chunked
+ end
+ end
+
+ class NullWriter
+ def <<(s) end
+ def puts(*args) end
+ def print(*args) end
+ def printf(*args) end
+ end
+end
diff --git a/test/net/imap/Makefile b/test/net/imap/Makefile
new file mode 100644
index 0000000000..b2bc9c7368
--- /dev/null
+++ b/test/net/imap/Makefile
@@ -0,0 +1,15 @@
+all:
+
+regen_certs:
+ touch server.key
+ make server.crt
+
+cacert.pem: server.key
+ openssl req -new -x509 -days 1825 -key server.key -out cacert.pem -text -subj "/C=JP/ST=Shimane/L=Matz-e city/O=Ruby Core Team/CN=Ruby Test CA/emailAddress=security@ruby-lang.org"
+
+server.csr:
+ openssl req -new -key server.key -out server.csr -text -subj "/C=JP/ST=Shimane/O=Ruby Core Team/OU=Ruby Test/CN=localhost"
+
+server.crt: server.csr cacert.pem
+ openssl x509 -days 1825 -CA cacert.pem -CAkey server.key -set_serial 00 -in server.csr -req -text -out server.crt
+ rm server.csr
diff --git a/test/net/imap/test_imap.rb b/test/net/imap/test_imap.rb
index 0ee217965c..41f25fe1c7 100644
--- a/test/net/imap/test_imap.rb
+++ b/test/net/imap/test_imap.rb
@@ -1,11 +1,770 @@
+# frozen_string_literal: true
+
require "net/imap"
require "test/unit"
class IMAPTest < Test::Unit::TestCase
- def test_parse_nomodesq
- parser = Net::IMAP::ResponseParser.new
- r = parser.parse(%Q'* OK [NOMODSEQ] Sorry, modsequences have not been enabled on this mailbox\r\n')
- assert_equal("OK", r.name)
- assert_equal("NOMODSEQ", r.data.code.name)
+ CA_FILE = File.expand_path("../fixtures/cacert.pem", __dir__)
+ SERVER_KEY = File.expand_path("../fixtures/server.key", __dir__)
+ SERVER_CERT = File.expand_path("../fixtures/server.crt", __dir__)
+
+ def setup
+ @do_not_reverse_lookup = Socket.do_not_reverse_lookup
+ Socket.do_not_reverse_lookup = true
+ @threads = []
+ end
+
+ def teardown
+ if !@threads.empty?
+ assert_join_threads(@threads)
+ end
+ ensure
+ Socket.do_not_reverse_lookup = @do_not_reverse_lookup
+ end
+
+ def test_encode_utf7
+ assert_equal("foo", Net::IMAP.encode_utf7("foo"))
+ assert_equal("&-", Net::IMAP.encode_utf7("&"))
+
+ utf8 = "\357\274\241\357\274\242\357\274\243".dup.force_encoding("UTF-8")
+ s = Net::IMAP.encode_utf7(utf8)
+ assert_equal("&,yH,Iv8j-", s)
+ s = Net::IMAP.encode_utf7("foo&#{utf8}-bar".encode("EUC-JP"))
+ assert_equal("foo&-&,yH,Iv8j--bar", s)
+
+ utf8 = "\343\201\202&".dup.force_encoding("UTF-8")
+ s = Net::IMAP.encode_utf7(utf8)
+ assert_equal("&MEI-&-", s)
+ s = Net::IMAP.encode_utf7(utf8.encode("EUC-JP"))
+ assert_equal("&MEI-&-", s)
+ end
+
+ def test_decode_utf7
+ assert_equal("&", Net::IMAP.decode_utf7("&-"))
+ assert_equal("&-", Net::IMAP.decode_utf7("&--"))
+
+ s = Net::IMAP.decode_utf7("&,yH,Iv8j-")
+ utf8 = "\357\274\241\357\274\242\357\274\243".dup.force_encoding("UTF-8")
+ assert_equal(utf8, s)
+ end
+
+ def test_format_date
+ time = Time.mktime(2009, 7, 24)
+ s = Net::IMAP.format_date(time)
+ assert_equal("24-Jul-2009", s)
+ end
+
+ def test_format_datetime
+ time = Time.mktime(2009, 7, 24, 1, 23, 45)
+ s = Net::IMAP.format_datetime(time)
+ assert_match(/\A24-Jul-2009 01:23 [+\-]\d{4}\z/, s)
+ end
+
+ if defined?(OpenSSL::SSL::SSLError)
+ def test_imaps_unknown_ca
+ assert_raise(OpenSSL::SSL::SSLError) do
+ imaps_test do |port|
+ begin
+ Net::IMAP.new("localhost",
+ :port => port,
+ :ssl => true)
+ rescue SystemCallError
+ skip $!
+ end
+ end
+ end
+ end
+
+ def test_imaps_with_ca_file
+ assert_nothing_raised do
+ imaps_test do |port|
+ begin
+ Net::IMAP.new("localhost",
+ :port => port,
+ :ssl => { :ca_file => CA_FILE })
+ rescue SystemCallError
+ skip $!
+ end
+ end
+ end
+ end
+
+ def test_imaps_verify_none
+ assert_nothing_raised do
+ imaps_test do |port|
+ Net::IMAP.new(server_addr,
+ :port => port,
+ :ssl => { :verify_mode => OpenSSL::SSL::VERIFY_NONE })
+ end
+ end
+ end
+
+ def test_imaps_post_connection_check
+ assert_raise(OpenSSL::SSL::SSLError) do
+ imaps_test do |port|
+ # server_addr is different from the hostname in the certificate,
+ # so the following code should raise a SSLError.
+ Net::IMAP.new(server_addr,
+ :port => port,
+ :ssl => { :ca_file => CA_FILE })
+ end
+ end
+ end
+ end
+
+ if defined?(OpenSSL::SSL)
+ def test_starttls
+ imap = nil
+ starttls_test do |port|
+ imap = Net::IMAP.new("localhost", :port => port)
+ imap.starttls(:ca_file => CA_FILE)
+ imap
+ end
+ rescue SystemCallError
+ skip $!
+ ensure
+ if imap && !imap.disconnected?
+ imap.disconnect
+ end
+ end
+ end
+
+ def test_unexpected_eof
+ server = create_tcp_server
+ port = server.addr[1]
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ sock.gets
+# sock.print("* BYE terminating connection\r\n")
+# sock.print("RUBY0001 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ assert_raise(EOFError) do
+ imap.logout
+ end
+ ensure
+ imap.disconnect if imap
+ end
+ end
+
+ def test_idle
+ server = create_tcp_server
+ port = server.addr[1]
+ requests = []
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ requests.push(sock.gets)
+ sock.print("+ idling\r\n")
+ sock.print("* 3 EXISTS\r\n")
+ sock.print("* 2 EXPUNGE\r\n")
+ requests.push(sock.gets)
+ sock.print("RUBY0001 OK IDLE terminated\r\n")
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0002 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ responses = []
+ imap.idle do |res|
+ responses.push(res)
+ if res.name == "EXPUNGE"
+ imap.idle_done
+ end
+ end
+ assert_equal(3, responses.length)
+ assert_instance_of(Net::IMAP::ContinuationRequest, responses[0])
+ assert_equal("EXISTS", responses[1].name)
+ assert_equal(3, responses[1].data)
+ assert_equal("EXPUNGE", responses[2].name)
+ assert_equal(2, responses[2].data)
+ assert_equal(2, requests.length)
+ assert_equal("RUBY0001 IDLE\r\n", requests[0])
+ assert_equal("DONE\r\n", requests[1])
+ imap.logout
+ ensure
+ imap.disconnect if imap
+ end
+ end
+
+ def test_exception_during_idle
+ server = create_tcp_server
+ port = server.addr[1]
+ requests = []
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ requests.push(sock.gets)
+ sock.print("+ idling\r\n")
+ sock.print("* 3 EXISTS\r\n")
+ sock.print("* 2 EXPUNGE\r\n")
+ requests.push(sock.gets)
+ sock.print("RUBY0001 OK IDLE terminated\r\n")
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0002 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ begin
+ th = Thread.current
+ m = Monitor.new
+ in_idle = false
+ exception_raised = false
+ c = m.new_cond
+ @threads << Thread.start do
+ m.synchronize do
+ until in_idle
+ c.wait(0.1)
+ end
+ end
+ th.raise(Interrupt)
+ m.synchronize do
+ exception_raised = true
+ c.signal
+ end
+ end
+ imap.idle do |res|
+ m.synchronize do
+ in_idle = true
+ c.signal
+ until exception_raised
+ c.wait(0.1)
+ end
+ end
+ end
+ rescue Interrupt
+ end
+ assert_equal(2, requests.length)
+ assert_equal("RUBY0001 IDLE\r\n", requests[0])
+ assert_equal("DONE\r\n", requests[1])
+ imap.logout
+ ensure
+ imap.disconnect if imap
+ end
+ end
+
+ def test_idle_done_not_during_idle
+ server = create_tcp_server
+ port = server.addr[1]
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ assert_raise(Net::IMAP::Error) do
+ imap.idle_done
+ end
+ ensure
+ imap.disconnect if imap
+ end
+ end
+
+ def test_idle_timeout
+ server = create_tcp_server
+ port = server.addr[1]
+ requests = []
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ requests.push(sock.gets)
+ sock.print("+ idling\r\n")
+ sock.print("* 3 EXISTS\r\n")
+ sock.print("* 2 EXPUNGE\r\n")
+ requests.push(sock.gets)
+ sock.print("RUBY0001 OK IDLE terminated\r\n")
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0002 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ responses = []
+ Thread.pass
+ imap.idle(0.2) do |res|
+ responses.push(res)
+ end
+ # There is no guarantee that this thread has received all the responses,
+ # so check the response length.
+ if responses.length > 0
+ assert_instance_of(Net::IMAP::ContinuationRequest, responses[0])
+ if responses.length > 1
+ assert_equal("EXISTS", responses[1].name)
+ assert_equal(3, responses[1].data)
+ if responses.length > 2
+ assert_equal("EXPUNGE", responses[2].name)
+ assert_equal(2, responses[2].data)
+ end
+ end
+ end
+ # Also, there is no guarantee that the server thread has stored
+ # all the requests into the array, so check the length.
+ if requests.length > 0
+ assert_equal("RUBY0001 IDLE\r\n", requests[0])
+ if requests.length > 1
+ assert_equal("DONE\r\n", requests[1])
+ end
+ end
+ imap.logout
+ ensure
+ imap.disconnect if imap
+ end
+ end
+
+ def test_unexpected_bye
+ server = create_tcp_server
+ port = server.addr[1]
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK Gimap ready for requests from 75.101.246.151 33if2752585qyk.26\r\n")
+ sock.gets
+ sock.print("* BYE System Error 33if2752585qyk.26\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ assert_raise(Net::IMAP::ByeResponseError) do
+ imap.login("user", "password")
+ end
+ end
+ end
+
+ def test_exception_during_shutdown
+ server = create_tcp_server
+ port = server.addr[1]
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0001 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ imap.instance_eval do
+ def @sock.shutdown(*args)
+ super
+ ensure
+ raise "error"
+ end
+ end
+ imap.logout
+ ensure
+ assert_raise(RuntimeError) do
+ imap.disconnect
+ end
+ end
+ end
+
+ def test_connection_closed_during_idle
+ server = create_tcp_server
+ port = server.addr[1]
+ requests = []
+ sock = nil
+ threads = []
+ threads << Thread.start do
+ begin
+ sock = server.accept
+ sock.print("* OK test server\r\n")
+ requests.push(sock.gets)
+ sock.print("+ idling\r\n")
+ rescue IOError # sock is closed by another thread
+ ensure
+ server.close
+ end
+ end
+ threads << Thread.start do
+ imap = Net::IMAP.new(server_addr, :port => port)
+ begin
+ m = Monitor.new
+ in_idle = false
+ closed = false
+ c = m.new_cond
+ threads << Thread.start do
+ m.synchronize do
+ until in_idle
+ c.wait(0.1)
+ end
+ end
+ sock.close
+ m.synchronize do
+ closed = true
+ c.signal
+ end
+ end
+ assert_raise(EOFError) do
+ imap.idle do |res|
+ m.synchronize do
+ in_idle = true
+ c.signal
+ until closed
+ c.wait(0.1)
+ end
+ end
+ end
+ end
+ assert_equal(1, requests.length)
+ assert_equal("RUBY0001 IDLE\r\n", requests[0])
+ ensure
+ imap.disconnect if imap
+ end
+ end
+ assert_join_threads(threads)
+ ensure
+ if sock && !sock.closed?
+ sock.close
+ end
+ end
+
+ def test_connection_closed_without_greeting
+ server = create_tcp_server
+ port = server.addr[1]
+ @threads << Thread.start do
+ begin
+ sock = server.accept
+ sock.close
+ ensure
+ server.close
+ end
+ end
+ assert_raise(Net::IMAP::Error) do
+ Net::IMAP.new(server_addr, :port => port)
+ end
+ end
+
+ def test_default_port
+ assert_equal(143, Net::IMAP.default_port)
+ assert_equal(143, Net::IMAP.default_imap_port)
+ assert_equal(993, Net::IMAP.default_tls_port)
+ assert_equal(993, Net::IMAP.default_ssl_port)
+ assert_equal(993, Net::IMAP.default_imaps_port)
+ end
+
+ def test_send_invalid_number
+ server = create_tcp_server
+ port = server.addr[1]
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ sock.gets
+ sock.print("RUBY0001 OK TEST completed\r\n")
+ sock.gets
+ sock.print("RUBY0002 OK TEST completed\r\n")
+ sock.gets
+ sock.print("RUBY0003 OK TEST completed\r\n")
+ sock.gets
+ sock.print("RUBY0004 OK TEST completed\r\n")
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0005 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ assert_raise(Net::IMAP::DataFormatError) do
+ imap.send(:send_command, "TEST", -1)
+ end
+ imap.send(:send_command, "TEST", 0)
+ imap.send(:send_command, "TEST", 4294967295)
+ assert_raise(Net::IMAP::DataFormatError) do
+ imap.send(:send_command, "TEST", 4294967296)
+ end
+ assert_raise(Net::IMAP::DataFormatError) do
+ imap.send(:send_command, "TEST", Net::IMAP::MessageSet.new(-1))
+ end
+ assert_raise(Net::IMAP::DataFormatError) do
+ imap.send(:send_command, "TEST", Net::IMAP::MessageSet.new(0))
+ end
+ imap.send(:send_command, "TEST", Net::IMAP::MessageSet.new(1))
+ imap.send(:send_command, "TEST", Net::IMAP::MessageSet.new(4294967295))
+ assert_raise(Net::IMAP::DataFormatError) do
+ imap.send(:send_command, "TEST", Net::IMAP::MessageSet.new(4294967296))
+ end
+ imap.logout
+ ensure
+ imap.disconnect
+ end
+ end
+
+ def test_send_literal
+ server = create_tcp_server
+ port = server.addr[1]
+ requests = []
+ literal = nil
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ line = sock.gets
+ requests.push(line)
+ size = line.slice(/{(\d+)}\r\n/, 1).to_i
+ sock.print("+ Ready for literal data\r\n")
+ literal = sock.read(size)
+ requests.push(sock.gets)
+ sock.print("RUBY0001 OK TEST completed\r\n")
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0002 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ imap.send(:send_command, "TEST", ["\xDE\xAD\xBE\xEF".b])
+ assert_equal(2, requests.length)
+ assert_equal("RUBY0001 TEST ({4}\r\n", requests[0])
+ assert_equal("\xDE\xAD\xBE\xEF".b, literal)
+ assert_equal(")\r\n", requests[1])
+ imap.logout
+ ensure
+ imap.disconnect
+ end
+ end
+
+ def test_disconnect
+ server = create_tcp_server
+ port = server.addr[1]
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0001 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ imap.logout
+ imap.disconnect
+ assert_equal(true, imap.disconnected?)
+ imap.disconnect
+ assert_equal(true, imap.disconnected?)
+ ensure
+ imap.disconnect if imap && !imap.disconnected?
+ end
+ end
+
+ def test_append
+ server = create_tcp_server
+ port = server.addr[1]
+ mail = <<EOF.gsub(/\n/, "\r\n")
+From: shugo@example.com
+To: matz@example.com
+Subject: hello
+
+hello world
+EOF
+ requests = []
+ received_mail = nil
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ line = sock.gets
+ requests.push(line)
+ size = line.slice(/{(\d+)}\r\n/, 1).to_i
+ sock.print("+ Ready for literal data\r\n")
+ received_mail = sock.read(size)
+ sock.gets
+ sock.print("RUBY0001 OK APPEND completed\r\n")
+ requests.push(sock.gets)
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0002 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ resp = imap.append("INBOX", mail)
+ assert_equal(1, requests.length)
+ assert_equal("RUBY0001 APPEND INBOX {#{mail.size}}\r\n", requests[0])
+ assert_equal(mail, received_mail)
+ imap.logout
+ assert_equal(2, requests.length)
+ assert_equal("RUBY0002 LOGOUT\r\n", requests[1])
+ ensure
+ imap.disconnect if imap
+ end
+ end
+
+ def test_append_fail
+ server = create_tcp_server
+ port = server.addr[1]
+ mail = <<EOF.gsub(/\n/, "\r\n")
+From: shugo@example.com
+To: matz@example.com
+Subject: hello
+
+hello world
+EOF
+ requests = []
+ received_mail = nil
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ requests.push(sock.gets)
+ sock.print("RUBY0001 NO Mailbox doesn't exist\r\n")
+ requests.push(sock.gets)
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0002 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+
+ begin
+ imap = Net::IMAP.new(server_addr, :port => port)
+ assert_raise(Net::IMAP::NoResponseError) do
+ imap.append("INBOX", mail)
+ end
+ assert_equal(1, requests.length)
+ assert_equal("RUBY0001 APPEND INBOX {#{mail.size}}\r\n", requests[0])
+ imap.logout
+ assert_equal(2, requests.length)
+ assert_equal("RUBY0002 LOGOUT\r\n", requests[1])
+ ensure
+ imap.disconnect if imap
+ end
+ end
+
+ private
+
+ def imaps_test
+ server = create_tcp_server
+ port = server.addr[1]
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx)
+ ths = Thread.start do
+ Thread.current.report_on_exception = false # always join-ed
+ begin
+ sock = ssl_server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0001 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ end
+ rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNABORTED
+ end
+ end
+ begin
+ begin
+ imap = yield(port)
+ imap.logout
+ ensure
+ imap.disconnect if imap
+ end
+ ensure
+ ssl_server.close
+ ths.join
+ end
+ end
+
+ def starttls_test
+ server = create_tcp_server
+ port = server.addr[1]
+ @threads << Thread.start do
+ sock = server.accept
+ begin
+ sock.print("* OK test server\r\n")
+ sock.gets
+ sock.print("RUBY0001 OK completed\r\n")
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+ sock.sync_close = true
+ sock.accept
+ sock.gets
+ sock.print("* BYE terminating connection\r\n")
+ sock.print("RUBY0002 OK LOGOUT completed\r\n")
+ ensure
+ sock.close
+ server.close
+ end
+ end
+ begin
+ imap = yield(port)
+ imap.logout if !imap.disconnected?
+ ensure
+ imap.disconnect if imap && !imap.disconnected?
+ end
+ end
+
+ def create_tcp_server
+ return TCPServer.new(server_addr, 0)
+ end
+
+ def server_addr
+ Addrinfo.tcp("localhost", 0).ip_address
end
end
diff --git a/test/net/imap/test_imap_response_parser.rb b/test/net/imap/test_imap_response_parser.rb
new file mode 100644
index 0000000000..12c8a27017
--- /dev/null
+++ b/test/net/imap/test_imap_response_parser.rb
@@ -0,0 +1,322 @@
+# frozen_string_literal: true
+
+require "net/imap"
+require "test/unit"
+
+class IMAPResponseParserTest < Test::Unit::TestCase
+ def setup
+ @do_not_reverse_lookup = Socket.do_not_reverse_lookup
+ Socket.do_not_reverse_lookup = true
+ if Net::IMAP.respond_to?(:max_flag_count)
+ @max_flag_count = Net::IMAP.max_flag_count
+ Net::IMAP.max_flag_count = 3
+ end
+ end
+
+ def teardown
+ Socket.do_not_reverse_lookup = @do_not_reverse_lookup
+ if Net::IMAP.respond_to?(:max_flag_count)
+ Net::IMAP.max_flag_count = @max_flag_count
+ end
+ end
+
+ def test_flag_list_safe
+ parser = Net::IMAP::ResponseParser.new
+ response = lambda {
+ $SAFE = 1
+ parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* LIST (\\HasChildren) "." "INBOX"
+EOF
+ }.call
+ assert_equal [:Haschildren], response.data.attr
+ end
+
+ def test_flag_list_too_many_flags
+ parser = Net::IMAP::ResponseParser.new
+ assert_nothing_raised do
+ 3.times do |i|
+ parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* LIST (\\Foo#{i}) "." "INBOX"
+EOF
+ end
+ end
+ assert_raise(Net::IMAP::FlagCountError) do
+ parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* LIST (\\Foo3) "." "INBOX"
+EOF
+ end
+ end
+
+ def test_flag_list_many_same_flags
+ parser = Net::IMAP::ResponseParser.new
+ assert_nothing_raised do
+ 100.times do
+ parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* LIST (\\Foo) "." "INBOX"
+EOF
+ end
+ end
+ end
+
+ def test_flag_xlist_inbox
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* XLIST (\\Inbox) "." "INBOX"
+EOF
+ assert_equal [:Inbox], response.data.attr
+ end
+
+ def test_resp_text_code
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* OK [CLOSED] Previous mailbox closed.
+EOF
+ assert_equal "CLOSED", response.data.code.name
+ end
+
+ def test_search_response
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* SEARCH
+EOF
+ assert_equal [], response.data
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* SEARCH 1
+EOF
+ assert_equal [1], response.data
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* SEARCH 1 2 3
+EOF
+ assert_equal [1, 2, 3], response.data
+ end
+
+ def test_search_response_of_yahoo
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* SEARCH 1\s
+EOF
+ assert_equal [1], response.data
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* SEARCH 1 2 3\s
+EOF
+ assert_equal [1, 2, 3], response.data
+ end
+
+ def test_msg_att_extra_space
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* 1 FETCH (UID 92285)
+EOF
+ assert_equal 92285, response.data.attr["UID"]
+
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* 1 FETCH (UID 92285 )
+EOF
+ assert_equal 92285, response.data.attr["UID"]
+ end
+
+ def test_msg_att_parse_error
+ parser = Net::IMAP::ResponseParser.new
+ e = assert_raise(Net::IMAP::ResponseParseError) {
+ parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* 123 FETCH (UNKNOWN 92285)
+EOF
+ }
+ assert_match(/ for \{123\}/, e.message)
+ end
+
+ def test_msg_att_rfc822_text
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* 123 FETCH (RFC822 {5}
+foo
+)
+EOF
+ assert_equal("foo\r\n", response.data.attr["RFC822"])
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* 123 FETCH (RFC822[] {5}
+foo
+)
+EOF
+ assert_equal("foo\r\n", response.data.attr["RFC822"])
+ end
+
+ # [Bug #6397] [ruby-core:44849]
+ def test_body_type_attachment
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* 980 FETCH (UID 2862 BODYSTRUCTURE ((("TEXT" "PLAIN" ("CHARSET" "iso-8859-1") NIL NIL "7BIT" 416 21 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "iso-8859-1") NIL NIL "7BIT" 1493 32 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "Boundary_(ID_IaecgfnXwG5bn3x8lIeGIQ)") NIL NIL)("MESSAGE" "RFC822" ("NAME" "Fw_ ____ _____ ____.eml") NIL NIL "7BIT" 1980088 NIL ("ATTACHMENT" ("FILENAME" "Fw_ ____ _____ ____.eml")) NIL) "MIXED" ("BOUNDARY" "Boundary_(ID_eDdLc/j0mBIzIlR191pHjA)") NIL NIL))
+EOF
+ assert_equal("Fw_ ____ _____ ____.eml",
+ response.data.attr["BODYSTRUCTURE"].parts[1].body.param["FILENAME"])
+ end
+
+ def assert_parseable(s)
+ parser = Net::IMAP::ResponseParser.new
+ parser.parse(s.gsub(/\n/, "\r\n").taint)
+ end
+
+ # [Bug #7146]
+ def test_msg_delivery_status
+ # This was part of a larger response that caused crashes, but this was the
+ # minimal test case to demonstrate it
+ assert_parseable <<EOF
+* 4902 FETCH (BODY (("MESSAGE" "DELIVERY-STATUS" NIL NIL NIL "7BIT" 324) "REPORT"))
+EOF
+ end
+
+ # [Bug #7147]
+ def test_msg_with_message_rfc822_attachment
+ assert_parseable <<EOF
+* 5441 FETCH (BODY ((("TEXT" "PLAIN" ("CHARSET" "iso-8859-1") NIL NIL "QUOTED-PRINTABLE" 69 1)("TEXT" "HTML" ("CHARSET" "iso-8859-1") NIL NIL "QUOTED-PRINTABLE" 455 12) "ALTERNATIVE")("MESSAGE" "RFC822" ("NAME" "ATT00026.eml") NIL NIL "7BIT" 4079755) "MIXED"))
+EOF
+ end
+
+ # [Bug #7153]
+ def test_msg_body_mixed
+ assert_parseable <<EOF
+* 1038 FETCH (BODY ("MIXED"))
+EOF
+ end
+
+ # [Bug #8167]
+ def test_msg_delivery_status_with_extra_data
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* 29021 FETCH (RFC822.SIZE 3162 UID 113622 RFC822.HEADER {1155}
+Return-path: <>
+Envelope-to: info@xxxxxxxx.si
+Delivery-date: Tue, 26 Mar 2013 12:42:58 +0100
+Received: from mail by xxxx.xxxxxxxxxxx.net with spam-scanned (Exim 4.76)
+ id 1UKSHI-000Cwl-AR
+ for info@xxxxxxxx.si; Tue, 26 Mar 2013 12:42:58 +0100
+X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on xxxx.xxxxxxxxxxx.net
+X-Spam-Level: **
+X-Spam-Status: No, score=2.1 required=7.0 tests=DKIM_ADSP_NXDOMAIN,RDNS_NONE
+ autolearn=no version=3.3.1
+Received: from [xx.xxx.xxx.xx] (port=56890 helo=xxxxxx.localdomain)
+ by xxxx.xxxxxxxxxxx.net with esmtp (Exim 4.76)
+ id 1UKSHI-000Cwi-9j
+ for info@xxxxxxxx.si; Tue, 26 Mar 2013 12:42:56 +0100
+Received: by xxxxxx.localdomain (Postfix)
+ id 72725BEA64A; Tue, 26 Mar 2013 12:42:55 +0100 (CET)
+Date: Tue, 26 Mar 2013 12:42:55 +0100 (CET)
+From: MAILER-DAEMON@xxxxxx.localdomain (Mail Delivery System)
+Subject: Undelivered Mail Returned to Sender
+To: info@xxxxxxxx.si
+Auto-Submitted: auto-replied
+MIME-Version: 1.0
+Content-Type: multipart/report; report-type=delivery-status;
+ boundary="27797BEA649.1364298175/xxxxxx.localdomain"
+Message-Id: <20130326114255.72725BEA64A@xxxxxx.localdomain>
+
+ BODYSTRUCTURE (("text" "plain" ("charset" "us-ascii") NIL "Notification" "7bit" 510 14 NIL NIL NIL NIL)("message" "delivery-status" NIL NIL "Delivery report" "7bit" 410 NIL NIL NIL NIL)("text" "rfc822-headers" ("charset" "us-ascii") NIL "Undelivered Message Headers" "7bit" 612 15 NIL NIL NIL NIL) "report" ("report-type" "delivery-status" "boundary" "27797BEA649.1364298175/xxxxxx.localdomain") NIL NIL NIL))
+EOF
+ delivery_status = response.data.attr["BODYSTRUCTURE"].parts[1]
+ assert_equal("MESSAGE", delivery_status.media_type)
+ assert_equal("DELIVERY-STATUS", delivery_status.subtype)
+ assert_equal(nil, delivery_status.param)
+ assert_equal(nil, delivery_status.content_id)
+ assert_equal("Delivery report", delivery_status.description)
+ assert_equal("7BIT", delivery_status.encoding)
+ assert_equal(410, delivery_status.size)
+ end
+
+ # [Bug #8281]
+ def test_acl
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse(<<EOF.gsub(/\n/, "\r\n").taint)
+* ACL "INBOX/share" "imshare2copy1366146467@xxxxxxxxxxxxxxxxxx.com" lrswickxteda
+EOF
+ assert_equal("ACL", response.name)
+ assert_equal(1, response.data.length)
+ assert_equal("INBOX/share", response.data[0].mailbox)
+ assert_equal("imshare2copy1366146467@xxxxxxxxxxxxxxxxxx.com",
+ response.data[0].user)
+ assert_equal("lrswickxteda", response.data[0].rights)
+ end
+
+ # [Bug #8415]
+ def test_capability
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse("* CAPABILITY st11p00mm-iscream009 1Q49 XAPPLEPUSHSERVICE IMAP4 IMAP4rev1 SASL-IR AUTH=ATOKEN AUTH=PLAIN\r\n")
+ assert_equal("CAPABILITY", response.name)
+ assert_equal("AUTH=PLAIN", response.data.last)
+ response = parser.parse("* CAPABILITY st11p00mm-iscream009 1Q49 XAPPLEPUSHSERVICE IMAP4 IMAP4rev1 SASL-IR AUTH=ATOKEN AUTH=PLAIN \r\n")
+ assert_equal("CAPABILITY", response.name)
+ assert_equal("AUTH=PLAIN", response.data.last)
+ end
+
+ def test_mixed_boundary
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse("* 2688 FETCH (UID 179161 BODYSTRUCTURE (" \
+ "(\"TEXT\" \"PLAIN\" (\"CHARSET\" \"iso-8859-1\") NIL NIL \"QUOTED-PRINTABLE\" 200 4 NIL NIL NIL)" \
+ "(\"MESSAGE\" \"DELIVERY-STATUS\" NIL NIL NIL \"7BIT\" 318 NIL NIL NIL)" \
+ "(\"MESSAGE\" \"RFC822\" NIL NIL NIL \"7BIT\" 2177" \
+ " (\"Tue, 11 May 2010 18:28:16 -0400\" \"Re: Welcome letter\" (" \
+ "(\"David\" NIL \"info\" \"xxxxxxxx.si\")) " \
+ "((\"David\" NIL \"info\" \"xxxxxxxx.si\")) " \
+ "((\"David\" NIL \"info\" \"xxxxxxxx.si\")) " \
+ "((\"Doretha\" NIL \"doretha.info\" \"xxxxxxxx.si\")) " \
+ "NIL NIL " \
+ "\"<AC1D15E06EA82F47BDE18E851CC32F330717704E@localdomain>\" " \
+ "\"<AANLkTikKMev1I73L2E7XLjRs67IHrEkb23f7ZPmD4S_9@localdomain>\")" \
+ " (\"MIXED\" (\"BOUNDARY\" \"000e0cd29212e3e06a0486590ae2\") NIL NIL)" \
+ " 37 NIL NIL NIL)" \
+ " \"REPORT\" (\"BOUNDARY\" \"16DuG.4XbaNOvCi.9ggvq.8Ipnyp3\" \"REPORT-TYPE\" \"delivery-status\") NIL NIL))\r\n")
+ empty_part = response.data.attr['BODYSTRUCTURE'].parts[2]
+ assert_equal(empty_part.lines, 37)
+ assert_equal(empty_part.body.media_type, 'MULTIPART')
+ assert_equal(empty_part.body.subtype, 'MIXED')
+ assert_equal(empty_part.body.param['BOUNDARY'], '000e0cd29212e3e06a0486590ae2')
+ end
+
+ # [Bug #10112]
+ def test_search_modseq
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse("* SEARCH 87216 87221 (MODSEQ 7667567)\r\n")
+ assert_equal("SEARCH", response.name)
+ assert_equal([87216, 87221], response.data)
+ end
+
+ # [Bug #11128]
+ def test_body_ext_mpart_without_lang
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse("* 4 FETCH (BODY (((\"text\" \"plain\" (\"charset\" \"utf-8\") NIL NIL \"7bit\" 257 9 NIL NIL NIL NIL)(\"text\" \"html\" (\"charset\" \"utf-8\") NIL NIL \"quoted-printable\" 655 9 NIL NIL NIL NIL) \"alternative\" (\"boundary\" \"001a1137a5047848dd05157ddaa1\") NIL)(\"application\" \"pdf\" (\"name\" \"test.xml\" \"x-apple-part-url\" \"9D00D9A2-98AB-4EFB-85BA-FB255F8BF3D7\") NIL NIL \"base64\" 4383638 NIL (\"attachment\" (\"filename\" \"test.xml\")) NIL NIL) \"mixed\" (\"boundary\" \"001a1137a5047848e405157ddaa3\") NIL))\r\n")
+ assert_equal("FETCH", response.name)
+ body = response.data.attr["BODY"]
+ assert_equal(nil, body.parts[0].disposition)
+ assert_equal(nil, body.parts[0].language)
+ assert_equal("ATTACHMENT", body.parts[1].disposition.dsp_type)
+ assert_equal("test.xml", body.parts[1].disposition.param["FILENAME"])
+ assert_equal(nil, body.parts[1].language)
+ end
+
+ # [Bug #13649]
+ def test_status
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse("* STATUS INBOX (UIDNEXT 1 UIDVALIDITY 1234)\r\n")
+ assert_equal("STATUS", response.name)
+ assert_equal("INBOX", response.data.mailbox)
+ assert_equal(1234, response.data.attr["UIDVALIDITY"])
+ response = parser.parse("* STATUS INBOX (UIDNEXT 1 UIDVALIDITY 1234) \r\n")
+ assert_equal("STATUS", response.name)
+ assert_equal("INBOX", response.data.mailbox)
+ assert_equal(1234, response.data.attr["UIDVALIDITY"])
+ end
+
+ # [Bug #10119]
+ def test_msg_att_modseq_data
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse("* 1 FETCH (FLAGS (\Seen) MODSEQ (12345) UID 5)\r\n")
+ assert_equal(12345, response.data.attr["MODSEQ"])
+ end
+
+ def test_continuation_request_without_response_text
+ parser = Net::IMAP::ResponseParser.new
+ response = parser.parse("+\r\n")
+ assert_instance_of(Net::IMAP::ContinuationRequest, response)
+ assert_equal(nil, response.data.code)
+ assert_equal("", response.data.text)
+ end
+end
diff --git a/test/net/pop/test_pop.rb b/test/net/pop/test_pop.rb
new file mode 100644
index 0000000000..f4c807a7a8
--- /dev/null
+++ b/test/net/pop/test_pop.rb
@@ -0,0 +1,166 @@
+# frozen_string_literal: true
+require 'net/pop'
+require 'test/unit'
+require 'digest/md5'
+
+class TestPOP < Test::Unit::TestCase
+ def setup
+ @users = {'user' => 'pass' }
+ @ok_user = 'user'
+ @stamp_base = "#{$$}.#{Time.now.to_i}@localhost"
+ end
+
+ def test_pop_auth_ok
+ pop_test(false) do |pop|
+ assert_instance_of Net::POP3, pop
+ assert_nothing_raised do
+ pop.start(@ok_user, @users[@ok_user])
+ end
+ end
+ end
+
+ def test_pop_auth_ng
+ pop_test(false) do |pop|
+ assert_instance_of Net::POP3, pop
+ assert_raise Net::POPAuthenticationError do
+ pop.start(@ok_user, 'bad password')
+ end
+ end
+ end
+
+ def test_apop_ok
+ pop_test(@stamp_base) do |pop|
+ assert_instance_of Net::APOP, pop
+ assert_nothing_raised do
+ pop.start(@ok_user, @users[@ok_user])
+ end
+ end
+ end
+
+ def test_apop_ng
+ pop_test(@stamp_base) do |pop|
+ assert_instance_of Net::APOP, pop
+ assert_raise Net::POPAuthenticationError do
+ pop.start(@ok_user, 'bad password')
+ end
+ end
+ end
+
+ def test_apop_invalid
+ pop_test("\x80"+@stamp_base) do |pop|
+ assert_instance_of Net::APOP, pop
+ assert_raise Net::POPAuthenticationError do
+ pop.start(@ok_user, @users[@ok_user])
+ end
+ end
+ end
+
+ def test_apop_invalid_at
+ pop_test(@stamp_base.sub('@', '.')) do |pop|
+ assert_instance_of Net::APOP, pop
+ assert_raise Net::POPAuthenticationError do
+ pop.start(@ok_user, @users[@ok_user])
+ end
+ end
+ end
+
+ def test_popmail
+ # totally not representative of real messages, but
+ # enough to test frozen bugs
+ lines = [ "[ruby-core:85210]" , "[Bug #14416]" ].freeze
+ command = Object.new
+ command.instance_variable_set(:@lines, lines)
+
+ def command.retr(n)
+ @lines.each { |l| yield "#{l}\r\n" }
+ end
+
+ def command.top(number, nl)
+ @lines.each do |l|
+ yield "#{l}\r\n"
+ break if (nl -= 1) <= 0
+ end
+ end
+
+ net_pop = :unused
+ popmail = Net::POPMail.new(1, 123, net_pop, command)
+ res = popmail.pop
+ assert_equal "[ruby-core:85210]\r\n[Bug #14416]\r\n", res
+ assert_not_predicate res, :frozen?
+
+ res = popmail.top(1)
+ assert_equal "[ruby-core:85210]\r\n", res
+ assert_not_predicate res, :frozen?
+ end
+
+ def pop_test(apop=false)
+ host = 'localhost'
+ server = TCPServer.new(host, 0)
+ port = server.addr[1]
+ server_thread = Thread.start do
+ sock = server.accept
+ begin
+ pop_server_loop(sock, apop)
+ ensure
+ sock.close
+ end
+ end
+ client_thread = Thread.start do
+ begin
+ begin
+ pop = Net::POP3::APOP(apop).new(host, port)
+ #pop.set_debug_output $stderr
+ yield pop
+ ensure
+ begin
+ pop.finish
+ rescue IOError
+ raise unless $!.message == "POP session not yet started"
+ end
+ end
+ ensure
+ server.close
+ end
+ end
+ assert_join_threads([client_thread, server_thread])
+ end
+
+ def pop_server_loop(sock, apop)
+ if apop
+ sock.print "+OK ready <#{apop}>\r\n"
+ else
+ sock.print "+OK ready\r\n"
+ end
+ user = nil
+ while line = sock.gets
+ case line
+ when /^USER (.+)\r\n/
+ user = $1
+ if @users.key?(user)
+ sock.print "+OK\r\n"
+ else
+ sock.print "-ERR unknown user\r\n"
+ end
+ when /^PASS (.+)\r\n/
+ if @users[user] == $1
+ sock.print "+OK\r\n"
+ else
+ sock.print "-ERR invalid password\r\n"
+ end
+ when /^APOP (.+) (.+)\r\n/
+ user = $1
+ if apop && Digest::MD5.hexdigest("<#{apop}>#{@users[user]}") == $2
+ sock.print "+OK\r\n"
+ else
+ sock.print "-ERR authentication failed\r\n"
+ end
+ when /^QUIT/
+ sock.print "+OK bye\r\n"
+ return
+ else
+ sock.print "-ERR command not recognized\r\n"
+ return
+ end
+ end
+ end
+end
diff --git a/test/net/protocol/test_protocol.rb b/test/net/protocol/test_protocol.rb
new file mode 100644
index 0000000000..048526b1c7
--- /dev/null
+++ b/test/net/protocol/test_protocol.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+require "test/unit"
+require "net/protocol"
+require "stringio"
+
+class TestProtocol < Test::Unit::TestCase
+ def test_should_properly_dot_stuff_period_with_no_endline
+ bug9627 = '[ruby-core:61441] [Bug #9627]'
+ sio = StringIO.new("".dup)
+ imio = Net::InternetMessageIO.new(sio)
+ email = "To: bob@aol.com\nlook, a period with no endline\n."
+ imio.write_message(email)
+ assert_equal("To: bob@aol.com\r\nlook, a period with no endline\r\n..\r\n.\r\n", sio.string, bug9627)
+ end
+
+ def test_each_crlf_line
+ assert_output('', '') do
+ sio = StringIO.new("".dup)
+ imio = Net::InternetMessageIO.new(sio)
+ assert_equal(23, imio.write_message("\u3042\r\u3044\n\u3046\r\n\u3048"))
+ assert_equal("\u3042\r\n\u3044\r\n\u3046\r\n\u3048\r\n.\r\n", sio.string)
+
+ sio = StringIO.new("".dup)
+ imio = Net::InternetMessageIO.new(sio)
+ assert_equal(8, imio.write_message("\u3042\r"))
+ assert_equal("\u3042\r\n.\r\n", sio.string)
+ end
+ end
+end
diff --git a/test/net/smtp/test_response.rb b/test/net/smtp/test_response.rb
new file mode 100644
index 0000000000..3cf909a762
--- /dev/null
+++ b/test/net/smtp/test_response.rb
@@ -0,0 +1,100 @@
+# frozen_string_literal: true
+require 'net/smtp'
+require 'test/unit'
+
+module Net
+ class SMTP
+ class TestResponse < Test::Unit::TestCase
+ def test_capabilities
+ res = Response.parse("250-ubuntu-desktop\n250-PIPELINING\n250-SIZE 10240000\n250-VRFY\n250-ETRN\n250-STARTTLS\n250-ENHANCEDSTATUSCODES\n250 DSN\n")
+
+ capabilities = res.capabilities
+ %w{ PIPELINING SIZE VRFY STARTTLS ENHANCEDSTATUSCODES DSN}.each do |str|
+ assert capabilities.key?(str), str
+ end
+ end
+
+ def test_capabilities_default
+ res = Response.parse("250-ubuntu-desktop\n250-PIPELINING\n250 DSN\n")
+ assert_equal [], res.capabilities['PIPELINING']
+ end
+
+ def test_capabilities_value
+ res = Response.parse("250-ubuntu-desktop\n250-SIZE 1234\n250 DSN\n")
+ assert_equal ['1234'], res.capabilities['SIZE']
+ end
+
+ def test_capabilities_multi
+ res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n")
+ assert_equal %w{1 2 3}, res.capabilities['SIZE']
+ end
+
+ def test_bad_string
+ res = Response.parse("badstring")
+ assert_equal({}, res.capabilities)
+ end
+
+ def test_success?
+ res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n")
+ assert res.success?
+ assert !res.continue?
+ end
+
+ # RFC 2821, Section 4.2.1
+ def test_continue?
+ res = Response.parse("3yz-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n")
+ assert !res.success?
+ assert res.continue?
+ end
+
+ def test_status_type_char
+ res = Response.parse("3yz-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n")
+ assert_equal '3', res.status_type_char
+
+ res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n")
+ assert_equal '2', res.status_type_char
+ end
+
+ def test_message
+ res = Response.parse("250-ubuntu-desktop\n250-SIZE 1 2 3\n250 DSN\n")
+ assert_equal "250-ubuntu-desktop\n", res.message
+ end
+
+ def test_server_busy_exception
+ res = Response.parse("400 omg busy")
+ assert_equal Net::SMTPServerBusy, res.exception_class
+ res = Response.parse("410 omg busy")
+ assert_equal Net::SMTPServerBusy, res.exception_class
+ end
+
+ def test_syntax_error_exception
+ res = Response.parse("500 omg syntax error")
+ assert_equal Net::SMTPSyntaxError, res.exception_class
+
+ res = Response.parse("501 omg syntax error")
+ assert_equal Net::SMTPSyntaxError, res.exception_class
+ end
+
+ def test_authentication_exception
+ res = Response.parse("530 omg auth error")
+ assert_equal Net::SMTPAuthenticationError, res.exception_class
+
+ res = Response.parse("531 omg auth error")
+ assert_equal Net::SMTPAuthenticationError, res.exception_class
+ end
+
+ def test_fatal_error
+ res = Response.parse("510 omg fatal error")
+ assert_equal Net::SMTPFatalError, res.exception_class
+
+ res = Response.parse("511 omg fatal error")
+ assert_equal Net::SMTPFatalError, res.exception_class
+ end
+
+ def test_default_exception
+ res = Response.parse("250 omg fatal error")
+ assert_equal Net::SMTPUnknownError, res.exception_class
+ end
+ end
+ end
+end
diff --git a/test/net/smtp/test_smtp.rb b/test/net/smtp/test_smtp.rb
new file mode 100644
index 0000000000..23e1542d8f
--- /dev/null
+++ b/test/net/smtp/test_smtp.rb
@@ -0,0 +1,200 @@
+# frozen_string_literal: true
+require 'net/smtp'
+require 'stringio'
+require 'test/unit'
+
+module Net
+ class TestSMTP < Test::Unit::TestCase
+ CA_FILE = File.expand_path("../fixtures/cacert.pem", __dir__)
+ SERVER_KEY = File.expand_path("../fixtures/server.key", __dir__)
+ SERVER_CERT = File.expand_path("../fixtures/server.crt", __dir__)
+
+ class FakeSocket
+ attr_reader :write_io
+
+ def initialize out = "250 OK\n"
+ @write_io = StringIO.new
+ @read_io = StringIO.new out
+ end
+
+ def writeline line
+ @write_io.write "#{line}\r\n"
+ end
+
+ def readline
+ line = @read_io.gets
+ raise 'ran out of input' unless line
+ line.chop
+ end
+ end
+
+ def test_critical
+ smtp = Net::SMTP.new 'localhost', 25
+
+ assert_raise RuntimeError do
+ smtp.send :critical do
+ raise 'fail on purpose'
+ end
+ end
+
+ assert_kind_of Net::SMTP::Response, smtp.send(:critical),
+ '[Bug #9125]'
+ end
+
+ def test_esmtp
+ smtp = Net::SMTP.new 'localhost', 25
+ assert smtp.esmtp
+ assert smtp.esmtp?
+
+ smtp.esmtp = 'omg'
+ assert_equal 'omg', smtp.esmtp
+ assert_equal 'omg', smtp.esmtp?
+ end
+
+ def test_rset
+ smtp = Net::SMTP.new 'localhost', 25
+ smtp.instance_variable_set :@socket, FakeSocket.new
+
+ assert smtp.rset
+ end
+
+ def test_mailfrom
+ sock = FakeSocket.new
+ smtp = Net::SMTP.new 'localhost', 25
+ smtp.instance_variable_set :@socket, sock
+ assert smtp.mailfrom("foo@example.com").success?
+ assert_equal "MAIL FROM:<foo@example.com>\r\n", sock.write_io.string
+ end
+
+ def test_rcptto
+ sock = FakeSocket.new
+ smtp = Net::SMTP.new 'localhost', 25
+ smtp.instance_variable_set :@socket, sock
+ assert smtp.rcptto("foo@example.com").success?
+ assert_equal "RCPT TO:<foo@example.com>\r\n", sock.write_io.string
+ end
+
+ def test_auth_plain
+ sock = FakeSocket.new
+ smtp = Net::SMTP.new 'localhost', 25
+ smtp.instance_variable_set :@socket, sock
+ assert smtp.auth_plain("foo", "bar").success?
+ assert_equal "AUTH PLAIN AGZvbwBiYXI=\r\n", sock.write_io.string
+ end
+
+ def test_crlf_injection
+ smtp = Net::SMTP.new 'localhost', 25
+ smtp.instance_variable_set :@socket, FakeSocket.new
+
+ assert_raise(ArgumentError) do
+ smtp.mailfrom("foo\r\nbar")
+ end
+
+ assert_raise(ArgumentError) do
+ smtp.mailfrom("foo\rbar")
+ end
+
+ assert_raise(ArgumentError) do
+ smtp.mailfrom("foo\nbar")
+ end
+
+ assert_raise(ArgumentError) do
+ smtp.rcptto("foo\r\nbar")
+ end
+ end
+
+ def test_tls_connect
+ servers = Socket.tcp_server_sockets("localhost", 0)
+ ctx = OpenSSL::SSL::SSLContext.new
+ ctx.ca_file = CA_FILE
+ ctx.key = File.open(SERVER_KEY) { |f|
+ OpenSSL::PKey::RSA.new(f)
+ }
+ ctx.cert = File.open(SERVER_CERT) { |f|
+ OpenSSL::X509::Certificate.new(f)
+ }
+ begin
+ sock = nil
+ Thread.start do
+ s = accept(servers)
+ sock = OpenSSL::SSL::SSLSocket.new(s, ctx)
+ sock.sync_close = true
+ sock.accept
+ sock.write("220 localhost Service ready\r\n")
+ sock.gets
+ sock.write("250 localhost\r\n")
+ sock.gets
+ sock.write("221 localhost Service closing transmission channel\r\n")
+ end
+ smtp = Net::SMTP.new("localhost", servers[0].local_address.ip_port)
+ smtp.enable_tls
+ smtp.open_timeout = 1
+ smtp.start do
+ end
+ ensure
+ sock.close if sock
+ servers.each(&:close)
+ end
+ rescue LoadError
+ # skip (require openssl)
+ end
+
+ def test_tls_connect_timeout
+ servers = Socket.tcp_server_sockets("localhost", 0)
+ begin
+ sock = nil
+ Thread.start do
+ sock = accept(servers)
+ end
+ smtp = Net::SMTP.new("localhost", servers[0].local_address.ip_port)
+ smtp.enable_tls
+ smtp.open_timeout = 0.1
+ assert_raise(Net::OpenTimeout) do
+ smtp.start do
+ end
+ end
+ rescue LoadError
+ # skip (require openssl)
+ ensure
+ sock.close if sock
+ servers.each(&:close)
+ end
+ end
+
+ def test_eof_error_backtrace
+ bug13018 = '[ruby-core:78550] [Bug #13018]'
+ servers = Socket.tcp_server_sockets("localhost", 0)
+ begin
+ sock = nil
+ t = Thread.start do
+ sock = accept(servers)
+ sock.close
+ end
+ smtp = Net::SMTP.new("localhost", servers[0].local_address.ip_port)
+ e = assert_raise(EOFError, bug13018) do
+ smtp.start do
+ end
+ end
+ assert_equal(EOFError, e.class, bug13018)
+ assert(e.backtrace.grep(%r"\bnet/smtp\.rb:").size > 0, bug13018)
+ ensure
+ sock.close if sock
+ servers.each(&:close)
+ t.join
+ end
+ end
+
+ private
+
+ def accept(servers)
+ loop do
+ readable, = IO.select(servers.map(&:to_io))
+ readable.each do |r|
+ sock, addr = r.accept_nonblock(exception: false)
+ next if sock == :wait_readable
+ return sock
+ end
+ end
+ end
+ end
+end
diff --git a/test/net/smtp/test_ssl_socket.rb b/test/net/smtp/test_ssl_socket.rb
new file mode 100644
index 0000000000..342391f159
--- /dev/null
+++ b/test/net/smtp/test_ssl_socket.rb
@@ -0,0 +1,97 @@
+# frozen_string_literal: true
+require 'net/smtp'
+require 'test/unit'
+
+module Net
+ class TestSSLSocket < Test::Unit::TestCase
+ class MySMTP < SMTP
+ attr_accessor :fake_tcp, :fake_ssl
+
+ def initialize(*args)
+ super(*args)
+ @open_timeout = nil
+ end
+
+ def tcp_socket address, port
+ fake_tcp
+ end
+
+ def ssl_socket socket, context
+ fake_ssl
+ end
+ end
+
+ require 'stringio'
+ class SSLSocket < StringIO
+ attr_accessor :sync_close, :connected, :closed
+
+ def initialize(*args)
+ @connected = false
+ @closed = true
+ super
+ end
+
+ def connect
+ self.connected = true
+ self.closed = false
+ end
+
+ def close
+ self.closed = true
+ end
+
+ def post_connection_check omg
+ end
+ end
+
+ def test_ssl_socket_close_on_post_connection_check_fail
+ tcp_socket = StringIO.new success_response
+
+ ssl_socket = SSLSocket.new.extend Module.new {
+ def post_connection_check omg
+ raise OpenSSL::SSL::SSLError, 'hostname was not match with the server certificate'
+ end
+ }
+
+ connection = MySMTP.new('localhost', 25)
+ connection.enable_starttls_auto
+ connection.fake_tcp = tcp_socket
+ connection.fake_ssl = ssl_socket
+
+ assert_raise(OpenSSL::SSL::SSLError) do
+ connection.start
+ end
+ assert_equal true, ssl_socket.closed
+ end
+
+ def test_ssl_socket_open_on_post_connection_check_success
+ tcp_socket = StringIO.new success_response
+
+ ssl_socket = SSLSocket.new success_response
+
+ connection = MySMTP.new('localhost', 25)
+ connection.enable_starttls_auto
+ connection.fake_tcp = tcp_socket
+ connection.fake_ssl = ssl_socket
+
+ connection.start
+ assert_equal false, ssl_socket.closed
+ end
+
+ def success_response
+ [
+ '220 smtp.example.com ESMTP Postfix',
+ "250-ubuntu-desktop",
+ "250-PIPELINING",
+ "250-SIZE 10240000",
+ "250-VRFY",
+ "250-ETRN",
+ "250-STARTTLS",
+ "250-ENHANCEDSTATUSCODES",
+ "250-8BITMIME",
+ "250 DSN",
+ "220 2.0.0 Ready to start TLS",
+ ].join("\r\n") + "\r\n"
+ end
+ end
+end if defined?(OpenSSL)