blob: bb566d1b1fb6e68c8b1fc367b053bd8e0ccbbae9 [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']
35 files.map { |f| File.open(File.join(test_root, f)).read }
36end
nnoble097ef9b2014-12-01 17:06:10 -080037
nnoble0c475f02014-12-05 15:37:39 -080038Server = GRPC::Core::Server
nnoble097ef9b2014-12-01 17:06:10 -080039
nnoble0c475f02014-12-05 15:37:39 -080040describe Server do
nnoble0c475f02014-12-05 15:37:39 -080041 def create_test_cert
42 GRPC::Core::ServerCredentials.new(*load_test_certs)
43 end
44
45 before(:each) do
46 @cq = GRPC::Core::CompletionQueue.new
47 end
48
49 describe '#start' do
nnoble0c475f02014-12-05 15:37:39 -080050 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -080051 blk = proc { Server.new(@cq, nil).start }
nnoble0c475f02014-12-05 15:37:39 -080052 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080053 end
54
nnoble0c475f02014-12-05 15:37:39 -080055 it 'fails if the server is closed' do
56 s = Server.new(@cq, nil)
57 s.close
58 expect { s.start }.to raise_error(RuntimeError)
nnoble097ef9b2014-12-01 17:06:10 -080059 end
nnoble0c475f02014-12-05 15:37:39 -080060 end
61
62 describe '#destroy' do
63 it 'destroys a server ok' do
64 s = start_a_server
Tim Emiolae2860c52015-01-16 02:58:41 -080065 blk = proc { s.destroy }
nnoble0c475f02014-12-05 15:37:39 -080066 expect(&blk).to_not raise_error
67 end
68
69 it 'can be called more than once without error' do
70 s = start_a_server
71 begin
Tim Emiolae2860c52015-01-16 02:58:41 -080072 blk = proc { s.destroy }
nnoble097ef9b2014-12-01 17:06:10 -080073 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080074 blk.call
75 expect(&blk).to_not raise_error
nnoble0c475f02014-12-05 15:37:39 -080076 ensure
77 s.close
78 end
79 end
80 end
81
82 describe '#close' do
83 it 'closes a server ok' do
84 s = start_a_server
85 begin
Tim Emiolae2860c52015-01-16 02:58:41 -080086 blk = proc { s.close }
nnoble0c475f02014-12-05 15:37:39 -080087 expect(&blk).to_not raise_error
88 ensure
89 s.close
nnoble097ef9b2014-12-01 17:06:10 -080090 end
91 end
92
nnoble0c475f02014-12-05 15:37:39 -080093 it 'can be called more than once without error' do
94 s = start_a_server
Tim Emiolae2860c52015-01-16 02:58:41 -080095 blk = proc { s.close }
nnoble0c475f02014-12-05 15:37:39 -080096 expect(&blk).to_not raise_error
97 blk.call
98 expect(&blk).to_not raise_error
99 end
100 end
101
102 describe '#add_http_port' do
nnoble0c475f02014-12-05 15:37:39 -0800103 describe 'for insecure servers' do
nnoble097ef9b2014-12-01 17:06:10 -0800104 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800105 blk = proc do
nnoble097ef9b2014-12-01 17:06:10 -0800106 s = Server.new(@cq, nil)
107 s.add_http2_port('localhost:0')
108 s.close
109 end
110 expect(&blk).to_not raise_error
111 end
112
113 it 'fails if the server is closed' do
114 s = Server.new(@cq, nil)
115 s.close
116 expect { s.add_http2_port('localhost:0') }.to raise_error(RuntimeError)
117 end
nnoble097ef9b2014-12-01 17:06:10 -0800118 end
119
nnoble0c475f02014-12-05 15:37:39 -0800120 describe 'for secure servers' do
Tim Emiola0ce8edc2015-03-05 15:17:30 -0800121 let(:cert) { create_test_cert }
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)
Tim Emiola0ce8edc2015-03-05 15:17:30 -0800125 s.add_http2_port('localhost:0', cert)
nnoble0c475f02014-12-05 15:37:39 -0800126 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 Emiola0ce8edc2015-03-05 15:17:30 -0800134 blk = proc { s.add_http2_port('localhost:0', cert) }
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
Tim Emiola0ce8edc2015-03-05 15:17:30 -0800142 expect { Server.new(@cq, nil) }.to_not raise_error
nnoble0c475f02014-12-05 15:37:39 -0800143 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
Tim Emiola1e098122015-04-14 09:42:09 -0700155 blk = construct_with_args('1' => {})
nnoble0c475f02014-12-05 15:37:39 -0800156 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
nnoble0c475f02014-12-05 15:37:39 -0800199 def start_a_server
nnoble0c475f02014-12-05 15:37:39 -0800200 s = Server.new(@cq, nil)
Tim Emiola61ddba32015-01-26 18:33:20 -0800201 s.add_http2_port('0.0.0.0:0')
nnoble0c475f02014-12-05 15:37:39 -0800202 s.start
203 s
nnoble097ef9b2014-12-01 17:06:10 -0800204 end
Craig Tiller190d3602015-02-18 09:23:38 -0800205end