|
| 1 | +require 'rex/proto/http/client' |
| 2 | + |
| 3 | +# Note: Some of these tests require a failed |
| 4 | +# connection to 127.0.0.1:1. If you have some crazy local |
| 5 | +# firewall that is dropping packets to this, your tests |
| 6 | +# might be slow. I wonder how Travis-CI will react to this... |
| 7 | +describe Rex::Proto::Http::Client do |
| 8 | + |
| 9 | + class << self |
| 10 | + |
| 11 | + # Set a standard excuse that indicates that the method |
| 12 | + # under test needs to be first examined to figure out |
| 13 | + # what's sane and what's not. |
| 14 | + def excuse_lazy(test_method=nil) |
| 15 | + ret = "need to determine pass/fail criteria" |
| 16 | + test_method ? ret << " for #{test_method.inspect}" : ret |
| 17 | + end |
| 18 | + |
| 19 | + # Complain about not having a "real" connection (can be mocked) |
| 20 | + def excuse_needs_connection |
| 21 | + "need to actually set up an HTTP server to test" |
| 22 | + end |
| 23 | + |
| 24 | + # Complain about not having a real auth server (can be mocked) |
| 25 | + def excuse_needs_auth |
| 26 | + "need to set up an HTTP authentication challenger" |
| 27 | + end |
| 28 | + |
| 29 | + end |
| 30 | + |
| 31 | + before(:all) do |
| 32 | + @ip = "1.2.3.4" |
| 33 | + @cli = Rex::Proto::Http::Client.new(@ip) |
| 34 | + end |
| 35 | + |
| 36 | + it "should respond to intialize" do |
| 37 | + @cli.should be |
| 38 | + end |
| 39 | + |
| 40 | + it "should have a set of default instance variables" do |
| 41 | + @cli.instance_variable_get(:@hostname).should == @ip |
| 42 | + @cli.instance_variable_get(:@port).should == 80 |
| 43 | + @cli.instance_variable_get(:@context).should == {} |
| 44 | + @cli.instance_variable_get(:@ssl).should be_false |
| 45 | + @cli.instance_variable_get(:@proxies).should be_nil |
| 46 | + # @cli.instance_variable_get(:@username).should be_empty |
| 47 | + # @cli.instance_variable_get(:@password).should be_empty |
| 48 | + @cli.config.should be_a_kind_of Hash |
| 49 | + @cli.config_types.should be_a_kind_of Hash |
| 50 | + end |
| 51 | + |
| 52 | + it "should produce a raw HTTP request", :pending => "Waiting for PR #1500" do |
| 53 | + @cli.request_raw.should be_a_kind_of Rex::Proto::Http::Request |
| 54 | + end |
| 55 | + |
| 56 | + it "should produce a CGI HTTP request", :pending => "Waiting for PR #1500" do |
| 57 | + @cli.request_cgi.should be_a_kind_of Rex::Proto::Http::Request |
| 58 | + end |
| 59 | + |
| 60 | + it "should attempt to connect to a server" do |
| 61 | + this_cli = Rex::Proto::Http::Client.new("127.0.0.1", 1) |
| 62 | + expect { this_cli.connect(1) }.to raise_error ::Rex::ConnectionRefused |
| 63 | + end |
| 64 | + |
| 65 | + it "should be able to close a connection" do |
| 66 | + @cli.close.should be_nil |
| 67 | + end |
| 68 | + |
| 69 | + it "should send a request and receive a response", :pending => excuse_needs_connection do |
| 70 | + |
| 71 | + end |
| 72 | + |
| 73 | + it "should send a request and receive a response without auth handling", :pending => excuse_needs_connection do |
| 74 | + |
| 75 | + end |
| 76 | + |
| 77 | + it "should send a request", :pending => excuse_needs_connection do |
| 78 | + |
| 79 | + end |
| 80 | + |
| 81 | + it "should test for credentials" do |
| 82 | + # @cli.should_not have_creds |
| 83 | + # this_cli = Rex::Proto::Http::Client.new("127.0.0.1", 1, {}, false, nil, nil, "user1", "pass1" ) |
| 84 | + # this_cli.should have_creds |
| 85 | + pending "Should actually respond to :has_creds" |
| 86 | + end |
| 87 | + |
| 88 | + it "should send authentication", :pending => excuse_needs_connection |
| 89 | + |
| 90 | + it "should produce a basic authentication header", :pending => "Waiting for #1500" do |
| 91 | + u = "user1" |
| 92 | + p = "pass1" |
| 93 | + b64 = ["#{u}:#{p}"].pack("m*").strip |
| 94 | + @cli.basic_auth_header("user1","pass1").should == "Basic #{b64}" |
| 95 | + end |
| 96 | + |
| 97 | + it "should perform digest authentication", :pending => excuse_needs_auth do |
| 98 | + |
| 99 | + end |
| 100 | + |
| 101 | + it "should perform negotiate authentication", :pending => excuse_needs_auth do |
| 102 | + |
| 103 | + end |
| 104 | + |
| 105 | + it "should get a response", :pending => excuse_needs_connection do |
| 106 | + |
| 107 | + end |
| 108 | + |
| 109 | + it "should end a connection with a stop" do |
| 110 | + @cli.stop.should be_nil |
| 111 | + end |
| 112 | + |
| 113 | + it "should test if a connection is valid" do |
| 114 | + @cli.conn?.should be_false |
| 115 | + end |
| 116 | + |
| 117 | + it "should tell if pipelining is enabled" do |
| 118 | + @cli.pipelining?.should be_false |
| 119 | + this_cli = Rex::Proto::Http::Client.new("127.0.0.1", 1) |
| 120 | + this_cli.pipeline = true |
| 121 | + this_cli.pipelining?.should be_true |
| 122 | + end |
| 123 | + |
| 124 | + it "should return an encoded URI", :pending => excuse_lazy(:set_encode_uri) do |
| 125 | + |
| 126 | + end |
| 127 | + |
| 128 | + it "should return an encoded query string", :pending => excuse_lazy(:set_encode_qa) do |
| 129 | + |
| 130 | + end |
| 131 | + |
| 132 | + # These set_ methods all exercise the evasion opts, looks like |
| 133 | + |
| 134 | + it "should set and return the URI", :pending => excuse_lazy(:set_uri) do |
| 135 | + |
| 136 | + end |
| 137 | + |
| 138 | + it "should set and return the CGI", :pending => excuse_lazy(:set_cgi) do |
| 139 | + |
| 140 | + end |
| 141 | + |
| 142 | + it "should set and return the HTTP verb", :pending => excuse_lazy(:set_method) do |
| 143 | + |
| 144 | + end |
| 145 | + |
| 146 | + it "should set and return the version string", :pending => excuse_lazy(:set_version) do |
| 147 | + |
| 148 | + end |
| 149 | + |
| 150 | + it "should set and return the HTTP seperator and body string", :pending => excuse_lazy(:set_body) do |
| 151 | + |
| 152 | + end |
| 153 | + |
| 154 | + it "should set and return the path", :pending => excuse_lazy(:set_path_info) do |
| 155 | + |
| 156 | + end |
| 157 | + |
| 158 | + it "should set and return the whitespace between method and URI", :pending => excuse_lazy(:set_method_uri_spacer) do |
| 159 | + |
| 160 | + end |
| 161 | + |
| 162 | + it "should set and return the whitespace between the version and URI", :pending => excuse_lazy(:set_uri_version_spacer) do |
| 163 | + |
| 164 | + end |
| 165 | + |
| 166 | + it "should set and return padding before the URI", :pending => excuse_lazy(:set_uri_prepend) do |
| 167 | + |
| 168 | + end |
| 169 | + |
| 170 | + it "should set and return padding after the URI" do |
| 171 | + @cli.set_uri_append.should be_empty |
| 172 | + end |
| 173 | + |
| 174 | + it "should set and return the host header", :pending => excuse_lazy(:set_host_header) do |
| 175 | + |
| 176 | + end |
| 177 | + |
| 178 | + it "should set and return the agent header", :pending => excuse_lazy(:set_agent_header) do |
| 179 | + |
| 180 | + end |
| 181 | + |
| 182 | + it "should set and return the cookie header", :pending => excuse_lazy(:set_cookie_header) do |
| 183 | + |
| 184 | + end |
| 185 | + |
| 186 | + it "should set and return the content-type header", :pending => excuse_lazy(:set_cookie_header) do |
| 187 | + |
| 188 | + end |
| 189 | + |
| 190 | + it "should set and return the content-length header", :pending => excuse_lazy(:set_content_len_header) do |
| 191 | + |
| 192 | + end |
| 193 | + |
| 194 | + it "should set and return the basic authentication header", :pending => excuse_lazy(:set_basic_auth_header) do |
| 195 | + |
| 196 | + end |
| 197 | + |
| 198 | + it "should set and return any extra headers", :pending => excuse_lazy(:set_extra_headers) do |
| 199 | + |
| 200 | + end |
| 201 | + |
| 202 | + it "should set the chunked encoding header", :pending => excuse_lazy(:set_chunked_header) do |
| 203 | + |
| 204 | + end |
| 205 | + |
| 206 | + it "should set and return raw_headers", :pending => "#set_raw_headers() doesn't seem to actually do anything" do |
| 207 | + |
| 208 | + end |
| 209 | + |
| 210 | + it "should set and return a formatted header", :pending => excuse_lazy(:set_formatted_header) do |
| 211 | + |
| 212 | + end |
| 213 | + |
| 214 | + it "should respond to its various accessors" do |
| 215 | + @cli.should respond_to :config |
| 216 | + @cli.should respond_to :config_types |
| 217 | + @cli.should respond_to :pipeline |
| 218 | + @cli.should respond_to :local_host |
| 219 | + @cli.should respond_to :local_port |
| 220 | + @cli.should respond_to :conn |
| 221 | + @cli.should respond_to :context |
| 222 | + @cli.should respond_to :proxies |
| 223 | + # @cli.should respond_to :username |
| 224 | + # @cli.should respond_to :password |
| 225 | + @cli.should respond_to :junk_pipeline |
| 226 | + # These are supposed to be protected |
| 227 | + @cli.should respond_to :ssl |
| 228 | + @cli.should respond_to :ssl_version |
| 229 | + @cli.should respond_to :hostname |
| 230 | + @cli.should respond_to :port |
| 231 | + end |
| 232 | + |
| 233 | + # Not super sure why these are protected... |
| 234 | + it "should refuse access to its protected accessors" do |
| 235 | + expect {@cli.ssl}.to raise_error NoMethodError |
| 236 | + expect {@cli.ssl_version}.to raise_error NoMethodError |
| 237 | + expect {@cli.hostname}.to raise_error NoMethodError |
| 238 | + expect {@cli.port}.to raise_error NoMethodError |
| 239 | + end |
| 240 | + |
| 241 | +end |
0 commit comments