blob: 28f520a2f6811f81113d90382e2e803e37ed3896 [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
42
43 def create_test_cert
44 GRPC::Core::ServerCredentials.new(*load_test_certs)
45 end
46
47 before(:each) do
48 @cq = GRPC::Core::CompletionQueue.new
49 end
50
51 describe '#start' do
52
53 it 'runs without failing' do
54 blk = Proc.new do
55 s = Server.new(@cq, nil).start
56 end
57 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080058 end
59
nnoble0c475f02014-12-05 15:37:39 -080060 it 'fails if the server is closed' do
61 s = Server.new(@cq, nil)
62 s.close
63 expect { s.start }.to raise_error(RuntimeError)
nnoble097ef9b2014-12-01 17:06:10 -080064 end
65
nnoble0c475f02014-12-05 15:37:39 -080066 end
67
68 describe '#destroy' do
69 it 'destroys a server ok' do
70 s = start_a_server
71 blk = Proc.new { s.destroy }
72 expect(&blk).to_not raise_error
73 end
74
75 it 'can be called more than once without error' do
76 s = start_a_server
77 begin
nnoble097ef9b2014-12-01 17:06:10 -080078 blk = Proc.new { s.destroy }
79 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080080 blk.call
81 expect(&blk).to_not raise_error
nnoble0c475f02014-12-05 15:37:39 -080082 ensure
83 s.close
84 end
85 end
86 end
87
88 describe '#close' do
89 it 'closes a server ok' do
90 s = start_a_server
91 begin
92 blk = Proc.new { s.close }
93 expect(&blk).to_not raise_error
94 ensure
95 s.close
nnoble097ef9b2014-12-01 17:06:10 -080096 end
97 end
98
nnoble0c475f02014-12-05 15:37:39 -080099 it 'can be called more than once without error' do
100 s = start_a_server
101 blk = Proc.new { s.close }
102 expect(&blk).to_not raise_error
103 blk.call
104 expect(&blk).to_not raise_error
105 end
106 end
107
108 describe '#add_http_port' do
109
110 describe 'for insecure servers' do
nnoble097ef9b2014-12-01 17:06:10 -0800111
112 it 'runs without failing' do
113 blk = Proc.new do
114 s = Server.new(@cq, nil)
115 s.add_http2_port('localhost:0')
116 s.close
117 end
118 expect(&blk).to_not raise_error
119 end
120
121 it 'fails if the server is closed' do
122 s = Server.new(@cq, nil)
123 s.close
124 expect { s.add_http2_port('localhost:0') }.to raise_error(RuntimeError)
125 end
126
127 end
128
nnoble0c475f02014-12-05 15:37:39 -0800129 describe 'for secure servers' do
nnoble097ef9b2014-12-01 17:06:10 -0800130
nnoble0c475f02014-12-05 15:37:39 -0800131 it 'runs without failing' do
132 blk = Proc.new do
133 s = Server.new(@cq, nil)
134 s.add_http2_port('localhost:0', true)
135 s.close
136 end
nnoble097ef9b2014-12-01 17:06:10 -0800137 expect(&blk).to_not raise_error
138 end
139
nnoble0c475f02014-12-05 15:37:39 -0800140 it 'fails if the server is closed' do
141 s = Server.new(@cq, nil)
142 s.close
143 blk = Proc.new { s.add_http2_port('localhost:0', true) }
144 expect(&blk).to raise_error(RuntimeError)
nnoble097ef9b2014-12-01 17:06:10 -0800145 end
146
147 end
148
nnoble0c475f02014-12-05 15:37:39 -0800149 end
150
151 shared_examples '#new' do
152
153 it 'takes a completion queue with nil channel args' do
154 expect { Server.new(@cq, nil, create_test_cert) }.to_not raise_error
155 end
156
157 it 'does not take a hash with bad keys as channel args' do
158 blk = construct_with_args(Object.new => 1)
159 expect(&blk).to raise_error TypeError
160 blk = construct_with_args(1 => 1)
161 expect(&blk).to raise_error TypeError
162 end
163
164 it 'does not take a hash with bad values as channel args' do
165 blk = construct_with_args(:symbol => Object.new)
166 expect(&blk).to raise_error TypeError
167 blk = construct_with_args('1' => Hash.new)
168 expect(&blk).to raise_error TypeError
169 end
170
171 it 'can take a hash with a symbol key as channel args' do
172 blk = construct_with_args(:a_symbol => 1)
173 expect(&blk).to_not raise_error
174 end
175
176 it 'can take a hash with a string key as channel args' do
177 blk = construct_with_args('a_symbol' => 1)
178 expect(&blk).to_not raise_error
179 end
180
181 it 'can take a hash with a string value as channel args' do
182 blk = construct_with_args(:a_symbol => '1')
183 expect(&blk).to_not raise_error
184 end
185
186 it 'can take a hash with a symbol value as channel args' do
187 blk = construct_with_args(:a_symbol => :another_symbol)
188 expect(&blk).to_not raise_error
189 end
190
191 it 'can take a hash with a numeric value as channel args' do
192 blk = construct_with_args(:a_symbol => 1)
193 expect(&blk).to_not raise_error
194 end
195
196 it 'can take a hash with many args as channel args' do
197 args = Hash[127.times.collect { |x| [x.to_s, x] } ]
198 blk = construct_with_args(args)
199 expect(&blk).to_not raise_error
200 end
201
202 end
203
204 describe '#new with an insecure channel' do
205
nnoble097ef9b2014-12-01 17:06:10 -0800206 def construct_with_args(a)
207 Proc.new { Server.new(@cq, a) }
208 end
209
nnoble0c475f02014-12-05 15:37:39 -0800210 it_behaves_like '#new'
211
212 end
213
214 describe '#new with a secure channel' do
215
216 def construct_with_args(a)
217 Proc.new { Server.new(@cq, a, create_test_cert) }
nnoble097ef9b2014-12-01 17:06:10 -0800218 end
219
nnoble0c475f02014-12-05 15:37:39 -0800220 it_behaves_like '#new'
221
222 end
223
224 def start_a_server
225 port = find_unused_tcp_port
226 host = "localhost:#{port}"
227 s = Server.new(@cq, nil)
228 s.add_http2_port(host)
229 s.start
230 s
nnoble097ef9b2014-12-01 17:06:10 -0800231 end
232
233end