|
2 | 2 |
|
3 | 3 | require "test_helper" |
4 | 4 | require "httpx" |
| 5 | +require "fileutils" |
5 | 6 |
|
6 | 7 | class TestHttp < HyperRubyTest |
7 | 8 | ACCEPT_RESPONSE = HyperRuby::Response.new(202, { 'Content-Type' => 'text/plain' }, '').freeze |
@@ -103,6 +104,117 @@ def test_http2_request |
103 | 104 | end |
104 | 105 | end |
105 | 106 |
|
| 107 | + def test_address_binding_error |
| 108 | + # First server binds to the port |
| 109 | + server1 = HyperRuby::Server.new |
| 110 | + server1.configure({ bind_address: "127.0.0.1:3020" }) |
| 111 | + server1.start |
| 112 | + |
| 113 | + begin |
| 114 | + # Try to start a second server on the same port, which should fail |
| 115 | + server2 = HyperRuby::Server.new |
| 116 | + server2.configure({ bind_address: "127.0.0.1:3020" }) |
| 117 | + |
| 118 | + # This should raise an exception |
| 119 | + error = assert_raises(RuntimeError) do |
| 120 | + server2.start |
| 121 | + end |
| 122 | + |
| 123 | + # Verify that the error message contains information about the binding failure |
| 124 | + assert_match(/Failed to bind to address/, error.message) |
| 125 | + ensure |
| 126 | + # Clean up |
| 127 | + server1.stop if server1 |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + def test_unix_socket_cleanup |
| 132 | + # Create a temporary path for the socket |
| 133 | + socket_path = "/tmp/hyper_ruby_test_cleanup.sock" |
| 134 | + |
| 135 | + # Clean up any leftover socket file from previous test runs |
| 136 | + File.unlink(socket_path) if File.exist?(socket_path) |
| 137 | + |
| 138 | + # First ensure that automatic cleanup works when a socket exists |
| 139 | + # but is deletable - this should work without errors |
| 140 | + FileUtils.touch(socket_path) |
| 141 | + File.chmod(0644, socket_path) # Ensure we have permissions |
| 142 | + |
| 143 | + begin |
| 144 | + server = HyperRuby::Server.new |
| 145 | + server.configure({ bind_address: "unix:#{socket_path}" }) |
| 146 | + server.start |
| 147 | + |
| 148 | + # If we get here, the server started correctly |
| 149 | + assert File.exist?(socket_path), "Socket file should exist after server starts" |
| 150 | + ensure |
| 151 | + server.stop if server |
| 152 | + File.unlink(socket_path) if File.exist?(socket_path) |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + # This test requires root permissions to create a file that can't be deleted. |
| 157 | + # Skip it unless we're running with proper permissions. |
| 158 | + def test_unix_socket_undeletable |
| 159 | + # Skip if we're not root or can't modify file permissions |
| 160 | + skip unless Process.uid == 0 || system("sudo -n true 2>/dev/null") |
| 161 | + |
| 162 | + socket_path = "/tmp/hyper_ruby_test_undeletable.sock" |
| 163 | + |
| 164 | + # Clean up any leftover socket file |
| 165 | + File.unlink(socket_path) if File.exist?(socket_path) |
| 166 | + |
| 167 | + # Create a file at the socket path that can't be deleted by the current user |
| 168 | + system("sudo touch #{socket_path} && sudo chmod 0000 #{socket_path}") |
| 169 | + |
| 170 | + begin |
| 171 | + server = HyperRuby::Server.new |
| 172 | + server.configure({ bind_address: "unix:#{socket_path}" }) |
| 173 | + |
| 174 | + # This should raise an exception about not being able to remove the file |
| 175 | + error = assert_raises(RuntimeError) do |
| 176 | + server.start |
| 177 | + end |
| 178 | + |
| 179 | + # Verify the error message |
| 180 | + assert_match(/Failed to remove existing Unix socket file/, error.message) |
| 181 | + ensure |
| 182 | + # Clean up with sudo |
| 183 | + system("sudo rm -f #{socket_path}") if File.exist?(socket_path) |
| 184 | + end |
| 185 | + end |
| 186 | + |
| 187 | + def test_unix_socket_directory_error |
| 188 | + # Create a directory instead of a socket file |
| 189 | + socket_dir = "/tmp/hyper_ruby_test_dir" |
| 190 | + |
| 191 | + # Ensure the directory exists |
| 192 | + FileUtils.mkdir_p(socket_dir) |
| 193 | + |
| 194 | + # Try to bind to the directory (which should fail) |
| 195 | + begin |
| 196 | + server = HyperRuby::Server.new |
| 197 | + server.configure({ bind_address: "unix:#{socket_dir}" }) |
| 198 | + |
| 199 | + # This should raise an exception |
| 200 | + error = assert_raises(RuntimeError) do |
| 201 | + server.start |
| 202 | + end |
| 203 | + |
| 204 | + # The error is from trying to remove the directory, not from binding |
| 205 | + assert_match(/Failed to remove existing Unix socket file/, error.message) |
| 206 | + |
| 207 | + # It should include something about "Operation not permitted" or similar |
| 208 | + assert(error.message.include?("Operation not permitted") || |
| 209 | + error.message.include?("Permission denied") || |
| 210 | + error.message.include?("not a socket"), |
| 211 | + "Error should indicate issue with removing directory: #{error.message}") |
| 212 | + ensure |
| 213 | + # Clean up |
| 214 | + FileUtils.rm_rf(socket_dir) if Dir.exist?(socket_dir) |
| 215 | + end |
| 216 | + end |
| 217 | + |
106 | 218 | private |
107 | 219 |
|
108 | 220 | def handler_simple(request) |
|
0 commit comments