blob: eb68bcff770a5bf629739cca1f9222258bfb2bd9 [file] [log] [blame]
jfarrell04e6f622016-09-20 15:27:54 -04001#
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
20require 'spec_helper'
21require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
22
23describe '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 III27247072018-03-22 20:50:23 -040029 @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)
jfarrell04e6f622016-09-20 15:27:54 -040033
James E. King III27247072018-03-22 20:50:23 -040034 @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)
Dmytro Shteflyuk697910f2026-02-13 14:12:43 -050038 allow(@handle).to receive(:to_io).and_return(@simple_socket_handle)
jfarrell04e6f622016-09-20 15:27:54 -040039
James E. King III27247072018-03-22 20:50:23 -040040 allow(::Socket).to receive(:new).and_return(@simple_socket_handle)
41 allow(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(@handle)
jfarrell04e6f622016-09-20 15:27:54 -040042 end
43
44 it_should_behave_like "a socket"
45
46 it "should raise a TransportException when it cannot open a ssl socket" do
James E. King III27247072018-03-22 20:50:23 -040047 expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
48 expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
jfarrell04e6f622016-09-20 15:27:54 -040049 end
50
51 it "should open a ::Socket with default args" do
James E. King III27247072018-03-22 20:50:23 -040052 expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(@simple_socket_handle, nil).and_return(@handle)
53 expect(@handle).to receive(:post_connection_check).with('localhost')
jfarrell04e6f622016-09-20 15:27:54 -040054 @socket.open
55 end
56
57 it "should accept host/port options" do
James E. King III27247072018-03-22 20:50:23 -040058 handle = double("Handle", :connect_nonblock => true, :setsockopt => nil)
59 allow(::Socket).to receive(:new).and_return(handle)
60 expect(::Socket).to receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]])
61 expect(::Socket).to receive(:sockaddr_in)
62 expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(handle, nil).and_return(@handle)
63 expect(@handle).to receive(:post_connection_check).with('my.domain')
jfarrell04e6f622016-09-20 15:27:54 -040064 Thrift::SSLSocket.new('my.domain', 1234, 6000, nil).open
65 end
66
67 it "should accept an optional timeout" do
James E. King III27247072018-03-22 20:50:23 -040068 expect(Thrift::SSLSocket.new('localhost', 8080, 5).timeout).to eq(5)
jfarrell04e6f622016-09-20 15:27:54 -040069 end
70
71 it "should accept an optional context" do
James E. King III27247072018-03-22 20:50:23 -040072 expect(Thrift::SSLSocket.new('localhost', 8080, 5, @context).ssl_context).to eq(@context)
jfarrell04e6f622016-09-20 15:27:54 -040073 end
James E. King III9aaf2952018-03-20 15:06:08 -040074
Dmytro Shteflyuk697910f2026-02-13 14:12:43 -050075 it "should delegate to_io to the underlying SSL socket handle" do
76 @socket.open
77 expect(@socket.to_io).to eq(@simple_socket_handle)
78 end
79
80 it "should raise IOError when to_io is called on a closed stream" do
81 expect { @socket.to_io }.to raise_error(IOError, 'closed stream')
82 end
83
James E. King III9aaf2952018-03-20 15:06:08 -040084 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -040085 expect(Thrift::SSLSocket.new('myhost', 8090).to_s).to eq("ssl(socket(myhost:8090))")
James E. King III9aaf2952018-03-20 15:06:08 -040086 end
jfarrell04e6f622016-09-20 15:27:54 -040087 end
88end