nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1 | # 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 | |
| 30 | require 'grpc' |
| 31 | require 'port_picker' |
| 32 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 33 | def 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 } |
| 37 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 38 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 39 | Server = GRPC::Core::Server |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 40 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 41 | describe Server do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 42 | 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 |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 51 | it 'runs without failing' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 52 | blk = proc { Server.new(@cq, nil).start } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 53 | expect(&blk).to_not raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 54 | end |
| 55 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 56 | 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) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 60 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 61 | end |
| 62 | |
| 63 | describe '#destroy' do |
| 64 | it 'destroys a server ok' do |
| 65 | s = start_a_server |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 66 | blk = proc { s.destroy } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 67 | 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 Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 73 | blk = proc { s.destroy } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 74 | expect(&blk).to_not raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 75 | blk.call |
| 76 | expect(&blk).to_not raise_error |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 77 | 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 Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 87 | blk = proc { s.close } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 88 | expect(&blk).to_not raise_error |
| 89 | ensure |
| 90 | s.close |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 91 | end |
| 92 | end |
| 93 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 94 | it 'can be called more than once without error' do |
| 95 | s = start_a_server |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 96 | blk = proc { s.close } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 97 | 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 |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 104 | describe 'for insecure servers' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 105 | it 'runs without failing' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 106 | blk = proc do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 107 | 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 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 119 | end |
| 120 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 121 | describe 'for secure servers' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 122 | it 'runs without failing' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 123 | blk = proc do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 124 | s = Server.new(@cq, nil) |
| 125 | s.add_http2_port('localhost:0', true) |
| 126 | s.close |
| 127 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 128 | expect(&blk).to_not raise_error |
| 129 | end |
| 130 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 131 | it 'fails if the server is closed' do |
| 132 | s = Server.new(@cq, nil) |
| 133 | s.close |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 134 | blk = proc { s.add_http2_port('localhost:0', true) } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 135 | expect(&blk).to raise_error(RuntimeError) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 136 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 137 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 138 | end |
| 139 | |
| 140 | shared_examples '#new' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 141 | 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 Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 153 | blk = construct_with_args(symbol: Object.new) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 154 | 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 Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 160 | blk = construct_with_args(a_symbol: 1) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 161 | 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 Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 170 | blk = construct_with_args(a_symbol: '1') |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 171 | expect(&blk).to_not raise_error |
| 172 | end |
| 173 | |
| 174 | it 'can take a hash with a symbol value as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 175 | blk = construct_with_args(a_symbol: :another_symbol) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 176 | expect(&blk).to_not raise_error |
| 177 | end |
| 178 | |
| 179 | it 'can take a hash with a numeric value as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 180 | blk = construct_with_args(a_symbol: 1) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 181 | expect(&blk).to_not raise_error |
| 182 | end |
| 183 | |
| 184 | it 'can take a hash with many args as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 185 | args = Hash[127.times.collect { |x| [x.to_s, x] }] |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 186 | blk = construct_with_args(args) |
| 187 | expect(&blk).to_not raise_error |
| 188 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 189 | end |
| 190 | |
| 191 | describe '#new with an insecure channel' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 192 | def construct_with_args(a) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 193 | proc { Server.new(@cq, a) } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 194 | end |
| 195 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 196 | it_behaves_like '#new' |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 197 | end |
| 198 | |
| 199 | describe '#new with a secure channel' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 200 | def construct_with_args(a) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 201 | proc { Server.new(@cq, a, create_test_cert) } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 202 | end |
| 203 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 204 | it_behaves_like '#new' |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 205 | 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 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 214 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 215 | end |