blob: 6e5bb523de03c2d1319bc79763ce4d3158d401b2 [file] [log] [blame]
nnoble097ef9b2014-12-01 17:06:10 -08001# Copyright 2014, Google Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30require 'grpc'
31require 'port_picker'
32
nnoble0c475f02014-12-05 15:37:39 -080033def load_test_certs
34 test_root = File.join(File.dirname(__FILE__), 'testdata')
35 files = ['ca.pem', 'server1.key', 'server1.pem']
36 files.map { |f| File.open(File.join(test_root, f)).read }
37end
nnoble097ef9b2014-12-01 17:06:10 -080038
nnoble0c475f02014-12-05 15:37:39 -080039Server = GRPC::Core::Server
nnoble097ef9b2014-12-01 17:06:10 -080040
nnoble0c475f02014-12-05 15:37:39 -080041describe Server do
nnoble0c475f02014-12-05 15:37:39 -080042 def create_test_cert
43 GRPC::Core::ServerCredentials.new(*load_test_certs)
44 end
45
46 before(:each) do
47 @cq = GRPC::Core::CompletionQueue.new
48 end
49
50 describe '#start' do
nnoble0c475f02014-12-05 15:37:39 -080051 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -080052 blk = proc { Server.new(@cq, nil).start }
nnoble0c475f02014-12-05 15:37:39 -080053 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080054 end
55
nnoble0c475f02014-12-05 15:37:39 -080056 it 'fails if the server is closed' do
57 s = Server.new(@cq, nil)
58 s.close
59 expect { s.start }.to raise_error(RuntimeError)
nnoble097ef9b2014-12-01 17:06:10 -080060 end
nnoble0c475f02014-12-05 15:37:39 -080061 end
62
63 describe '#destroy' do
64 it 'destroys a server ok' do
65 s = start_a_server
Tim Emiolae2860c52015-01-16 02:58:41 -080066 blk = proc { s.destroy }
nnoble0c475f02014-12-05 15:37:39 -080067 expect(&blk).to_not raise_error
68 end
69
70 it 'can be called more than once without error' do
71 s = start_a_server
72 begin
Tim Emiolae2860c52015-01-16 02:58:41 -080073 blk = proc { s.destroy }
nnoble097ef9b2014-12-01 17:06:10 -080074 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080075 blk.call
76 expect(&blk).to_not raise_error
nnoble0c475f02014-12-05 15:37:39 -080077 ensure
78 s.close
79 end
80 end
81 end
82
83 describe '#close' do
84 it 'closes a server ok' do
85 s = start_a_server
86 begin
Tim Emiolae2860c52015-01-16 02:58:41 -080087 blk = proc { s.close }
nnoble0c475f02014-12-05 15:37:39 -080088 expect(&blk).to_not raise_error
89 ensure
90 s.close
nnoble097ef9b2014-12-01 17:06:10 -080091 end
92 end
93
nnoble0c475f02014-12-05 15:37:39 -080094 it 'can be called more than once without error' do
95 s = start_a_server
Tim Emiolae2860c52015-01-16 02:58:41 -080096 blk = proc { s.close }
nnoble0c475f02014-12-05 15:37:39 -080097 expect(&blk).to_not raise_error
98 blk.call
99 expect(&blk).to_not raise_error
100 end
101 end
102
103 describe '#add_http_port' do
nnoble0c475f02014-12-05 15:37:39 -0800104 describe 'for insecure servers' do
nnoble097ef9b2014-12-01 17:06:10 -0800105 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800106 blk = proc do
nnoble097ef9b2014-12-01 17:06:10 -0800107 s = Server.new(@cq, nil)
108 s.add_http2_port('localhost:0')
109 s.close
110 end
111 expect(&blk).to_not raise_error
112 end
113
114 it 'fails if the server is closed' do
115 s = Server.new(@cq, nil)
116 s.close
117 expect { s.add_http2_port('localhost:0') }.to raise_error(RuntimeError)
118 end
nnoble097ef9b2014-12-01 17:06:10 -0800119 end
120
nnoble0c475f02014-12-05 15:37:39 -0800121 describe 'for secure servers' do
nnoble0c475f02014-12-05 15:37:39 -0800122 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800123 blk = proc do
nnoble0c475f02014-12-05 15:37:39 -0800124 s = Server.new(@cq, nil)
125 s.add_http2_port('localhost:0', true)
126 s.close
127 end
nnoble097ef9b2014-12-01 17:06:10 -0800128 expect(&blk).to_not raise_error
129 end
130
nnoble0c475f02014-12-05 15:37:39 -0800131 it 'fails if the server is closed' do
132 s = Server.new(@cq, nil)
133 s.close
Tim Emiolae2860c52015-01-16 02:58:41 -0800134 blk = proc { s.add_http2_port('localhost:0', true) }
nnoble0c475f02014-12-05 15:37:39 -0800135 expect(&blk).to raise_error(RuntimeError)
nnoble097ef9b2014-12-01 17:06:10 -0800136 end
nnoble097ef9b2014-12-01 17:06:10 -0800137 end
nnoble0c475f02014-12-05 15:37:39 -0800138 end
139
140 shared_examples '#new' do
nnoble0c475f02014-12-05 15:37:39 -0800141 it 'takes a completion queue with nil channel args' do
142 expect { Server.new(@cq, nil, create_test_cert) }.to_not raise_error
143 end
144
145 it 'does not take a hash with bad keys as channel args' do
146 blk = construct_with_args(Object.new => 1)
147 expect(&blk).to raise_error TypeError
148 blk = construct_with_args(1 => 1)
149 expect(&blk).to raise_error TypeError
150 end
151
152 it 'does not take a hash with bad values as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800153 blk = construct_with_args(symbol: Object.new)
nnoble0c475f02014-12-05 15:37:39 -0800154 expect(&blk).to raise_error TypeError
155 blk = construct_with_args('1' => Hash.new)
156 expect(&blk).to raise_error TypeError
157 end
158
159 it 'can take a hash with a symbol key as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800160 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -0800161 expect(&blk).to_not raise_error
162 end
163
164 it 'can take a hash with a string key as channel args' do
165 blk = construct_with_args('a_symbol' => 1)
166 expect(&blk).to_not raise_error
167 end
168
169 it 'can take a hash with a string value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800170 blk = construct_with_args(a_symbol: '1')
nnoble0c475f02014-12-05 15:37:39 -0800171 expect(&blk).to_not raise_error
172 end
173
174 it 'can take a hash with a symbol value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800175 blk = construct_with_args(a_symbol: :another_symbol)
nnoble0c475f02014-12-05 15:37:39 -0800176 expect(&blk).to_not raise_error
177 end
178
179 it 'can take a hash with a numeric value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800180 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -0800181 expect(&blk).to_not raise_error
182 end
183
184 it 'can take a hash with many args as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800185 args = Hash[127.times.collect { |x| [x.to_s, x] }]
nnoble0c475f02014-12-05 15:37:39 -0800186 blk = construct_with_args(args)
187 expect(&blk).to_not raise_error
188 end
nnoble0c475f02014-12-05 15:37:39 -0800189 end
190
191 describe '#new with an insecure channel' do
nnoble097ef9b2014-12-01 17:06:10 -0800192 def construct_with_args(a)
Tim Emiolae2860c52015-01-16 02:58:41 -0800193 proc { Server.new(@cq, a) }
nnoble097ef9b2014-12-01 17:06:10 -0800194 end
195
nnoble0c475f02014-12-05 15:37:39 -0800196 it_behaves_like '#new'
nnoble0c475f02014-12-05 15:37:39 -0800197 end
198
199 describe '#new with a secure channel' do
nnoble0c475f02014-12-05 15:37:39 -0800200 def construct_with_args(a)
Tim Emiolae2860c52015-01-16 02:58:41 -0800201 proc { Server.new(@cq, a, create_test_cert) }
nnoble097ef9b2014-12-01 17:06:10 -0800202 end
203
nnoble0c475f02014-12-05 15:37:39 -0800204 it_behaves_like '#new'
nnoble0c475f02014-12-05 15:37:39 -0800205 end
206
207 def start_a_server
208 port = find_unused_tcp_port
209 host = "localhost:#{port}"
210 s = Server.new(@cq, nil)
211 s.add_http2_port(host)
212 s.start
213 s
nnoble097ef9b2014-12-01 17:06:10 -0800214 end
nnoble097ef9b2014-12-01 17:06:10 -0800215end