jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
| 20 | require 'spec_helper' |
| 21 | require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared") |
| 22 | |
| 23 | describe 'SSLSocket' do |
| 24 | |
| 25 | describe Thrift::SSLSocket do |
| 26 | before(:each) do |
| 27 | @context = OpenSSL::SSL::SSLContext.new |
| 28 | @socket = Thrift::SSLSocket.new |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 29 | @simple_socket_handle = double("Handle", :closed? => false) |
| 30 | allow(@simple_socket_handle).to receive(:close) |
| 31 | allow(@simple_socket_handle).to receive(:connect_nonblock) |
| 32 | allow(@simple_socket_handle).to receive(:setsockopt) |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 33 | |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 34 | @handle = double(double("SSLHandle", :connect_nonblock => true, :post_connection_check => true), :closed? => false) |
| 35 | allow(@handle).to receive(:connect_nonblock) |
| 36 | allow(@handle).to receive(:close) |
| 37 | allow(@handle).to receive(:post_connection_check) |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 38 | |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 39 | allow(::Socket).to receive(:new).and_return(@simple_socket_handle) |
| 40 | allow(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(@handle) |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 41 | end |
| 42 | |
| 43 | it_should_behave_like "a socket" |
| 44 | |
| 45 | it "should raise a TransportException when it cannot open a ssl socket" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 46 | expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]]) |
| 47 | expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) } |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 48 | end |
| 49 | |
| 50 | it "should open a ::Socket with default args" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 51 | expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(@simple_socket_handle, nil).and_return(@handle) |
| 52 | expect(@handle).to receive(:post_connection_check).with('localhost') |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 53 | @socket.open |
| 54 | end |
| 55 | |
| 56 | it "should accept host/port options" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 57 | handle = double("Handle", :connect_nonblock => true, :setsockopt => nil) |
| 58 | allow(::Socket).to receive(:new).and_return(handle) |
| 59 | expect(::Socket).to receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]]) |
| 60 | expect(::Socket).to receive(:sockaddr_in) |
| 61 | expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(handle, nil).and_return(@handle) |
| 62 | expect(@handle).to receive(:post_connection_check).with('my.domain') |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 63 | Thrift::SSLSocket.new('my.domain', 1234, 6000, nil).open |
| 64 | end |
| 65 | |
| 66 | it "should accept an optional timeout" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 67 | expect(Thrift::SSLSocket.new('localhost', 8080, 5).timeout).to eq(5) |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 68 | end |
| 69 | |
| 70 | it "should accept an optional context" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 71 | expect(Thrift::SSLSocket.new('localhost', 8080, 5, @context).ssl_context).to eq(@context) |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 72 | end |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 73 | |
| 74 | it "should provide a reasonable to_s" do |
James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 75 | expect(Thrift::SSLSocket.new('myhost', 8090).to_s).to eq("ssl(socket(myhost:8090))") |
James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 76 | end |
jfarrell | 04e6f62 | 2016-09-20 15:27:54 -0400 | [diff] [blame] | 77 | end |
| 78 | end |