blob: c52fe0d9b6e3ee61d36424ec33fae5941fd4abd7 [file] [log] [blame]
Craig Tiller06059952015-02-18 08:34:56 -08001# Copyright 2015, Google Inc.
nnoble097ef9b2014-12-01 17:06:10 -08002# 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'
nnoble097ef9b2014-12-01 17:06:10 -080031
nnoble0c475f02014-12-05 15:37:39 -080032def load_test_certs
33 test_root = File.join(File.dirname(__FILE__), 'testdata')
34 files = ['ca.pem', 'server1.key', 'server1.pem']
Tim Emiola73a540a2015-08-28 18:56:17 -070035 contents = files.map { |f| File.open(File.join(test_root, f)).read }
36 [contents[0], [{ private_key: contents[1], cert_chain: contents[2] }], false]
nnoble0c475f02014-12-05 15:37:39 -080037end
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)
Tim Emiolab1fa5d42015-06-11 09:35:06 -070058 s.close(@cq)
nnoble0c475f02014-12-05 15:37:39 -080059 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 Emiolab1fa5d42015-06-11 09:35:06 -070066 blk = proc { s.destroy(@cq) }
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 Emiolab1fa5d42015-06-11 09:35:06 -070073 blk = proc { s.destroy(@cq) }
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
Tim Emiolab1fa5d42015-06-11 09:35:06 -070078 s.close(@cq)
nnoble0c475f02014-12-05 15:37:39 -080079 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 Emiolab1fa5d42015-06-11 09:35:06 -070087 blk = proc { s.close(@cq) }
nnoble0c475f02014-12-05 15:37:39 -080088 expect(&blk).to_not raise_error
89 ensure
Tim Emiolab1fa5d42015-06-11 09:35:06 -070090 s.close(@cq)
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 Emiolab1fa5d42015-06-11 09:35:06 -070096 blk = proc { s.close(@cq) }
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')
Tim Emiolab1fa5d42015-06-11 09:35:06 -0700109 s.close(@cq)
nnoble097ef9b2014-12-01 17:06:10 -0800110 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)
Tim Emiolab1fa5d42015-06-11 09:35:06 -0700116 s.close(@cq)
nnoble097ef9b2014-12-01 17:06:10 -0800117 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
Tim Emiola0ce8edc2015-03-05 15:17:30 -0800122 let(:cert) { create_test_cert }
nnoble0c475f02014-12-05 15:37:39 -0800123 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800124 blk = proc do
nnoble0c475f02014-12-05 15:37:39 -0800125 s = Server.new(@cq, nil)
Tim Emiola0ce8edc2015-03-05 15:17:30 -0800126 s.add_http2_port('localhost:0', cert)
Tim Emiolab1fa5d42015-06-11 09:35:06 -0700127 s.close(@cq)
nnoble0c475f02014-12-05 15:37:39 -0800128 end
nnoble097ef9b2014-12-01 17:06:10 -0800129 expect(&blk).to_not raise_error
130 end
131
nnoble0c475f02014-12-05 15:37:39 -0800132 it 'fails if the server is closed' do
133 s = Server.new(@cq, nil)
Tim Emiolab1fa5d42015-06-11 09:35:06 -0700134 s.close(@cq)
Tim Emiola0ce8edc2015-03-05 15:17:30 -0800135 blk = proc { s.add_http2_port('localhost:0', cert) }
nnoble0c475f02014-12-05 15:37:39 -0800136 expect(&blk).to raise_error(RuntimeError)
nnoble097ef9b2014-12-01 17:06:10 -0800137 end
nnoble097ef9b2014-12-01 17:06:10 -0800138 end
nnoble0c475f02014-12-05 15:37:39 -0800139 end
140
141 shared_examples '#new' do
nnoble0c475f02014-12-05 15:37:39 -0800142 it 'takes a completion queue with nil channel args' do
Tim Emiola0ce8edc2015-03-05 15:17:30 -0800143 expect { Server.new(@cq, nil) }.to_not raise_error
nnoble0c475f02014-12-05 15:37:39 -0800144 end
145
146 it 'does not take a hash with bad keys as channel args' do
147 blk = construct_with_args(Object.new => 1)
148 expect(&blk).to raise_error TypeError
149 blk = construct_with_args(1 => 1)
150 expect(&blk).to raise_error TypeError
151 end
152
153 it 'does not take a hash with bad values as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800154 blk = construct_with_args(symbol: Object.new)
nnoble0c475f02014-12-05 15:37:39 -0800155 expect(&blk).to raise_error TypeError
Tim Emiola1e098122015-04-14 09:42:09 -0700156 blk = construct_with_args('1' => {})
nnoble0c475f02014-12-05 15:37:39 -0800157 expect(&blk).to raise_error TypeError
158 end
159
160 it 'can take a hash with a symbol key as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800161 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -0800162 expect(&blk).to_not raise_error
163 end
164
165 it 'can take a hash with a string key as channel args' do
166 blk = construct_with_args('a_symbol' => 1)
167 expect(&blk).to_not raise_error
168 end
169
170 it 'can take a hash with a string value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800171 blk = construct_with_args(a_symbol: '1')
nnoble0c475f02014-12-05 15:37:39 -0800172 expect(&blk).to_not raise_error
173 end
174
175 it 'can take a hash with a symbol value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800176 blk = construct_with_args(a_symbol: :another_symbol)
nnoble0c475f02014-12-05 15:37:39 -0800177 expect(&blk).to_not raise_error
178 end
179
180 it 'can take a hash with a numeric value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800181 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -0800182 expect(&blk).to_not raise_error
183 end
184
185 it 'can take a hash with many args as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800186 args = Hash[127.times.collect { |x| [x.to_s, x] }]
nnoble0c475f02014-12-05 15:37:39 -0800187 blk = construct_with_args(args)
188 expect(&blk).to_not raise_error
189 end
nnoble0c475f02014-12-05 15:37:39 -0800190 end
191
192 describe '#new with an insecure channel' do
nnoble097ef9b2014-12-01 17:06:10 -0800193 def construct_with_args(a)
Tim Emiolae2860c52015-01-16 02:58:41 -0800194 proc { Server.new(@cq, a) }
nnoble097ef9b2014-12-01 17:06:10 -0800195 end
196
nnoble0c475f02014-12-05 15:37:39 -0800197 it_behaves_like '#new'
nnoble0c475f02014-12-05 15:37:39 -0800198 end
199
nnoble0c475f02014-12-05 15:37:39 -0800200 def start_a_server
nnoble0c475f02014-12-05 15:37:39 -0800201 s = Server.new(@cq, nil)
Tim Emiola61ddba32015-01-26 18:33:20 -0800202 s.add_http2_port('0.0.0.0:0')
nnoble0c475f02014-12-05 15:37:39 -0800203 s.start
204 s
nnoble097ef9b2014-12-01 17:06:10 -0800205 end
Craig Tiller190d3602015-02-18 09:23:38 -0800206end