Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 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' |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 31 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 32 | def 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 } |
| 36 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 37 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 38 | Server = GRPC::Core::Server |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 39 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 40 | describe Server do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 41 | 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 |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 50 | it 'runs without failing' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 51 | blk = proc { Server.new(@cq, nil).start } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 52 | expect(&blk).to_not raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 53 | end |
| 54 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 55 | 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) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 59 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 60 | end |
| 61 | |
| 62 | describe '#destroy' do |
| 63 | it 'destroys a server ok' do |
| 64 | s = start_a_server |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 65 | blk = proc { s.destroy } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 66 | 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 Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 72 | blk = proc { s.destroy } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 73 | expect(&blk).to_not raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 74 | blk.call |
| 75 | expect(&blk).to_not raise_error |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 76 | 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 Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 86 | blk = proc { s.close } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 87 | expect(&blk).to_not raise_error |
| 88 | ensure |
| 89 | s.close |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 90 | end |
| 91 | end |
| 92 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 93 | it 'can be called more than once without error' do |
| 94 | s = start_a_server |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 95 | blk = proc { s.close } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 96 | 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 |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 103 | describe 'for insecure servers' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 104 | it 'runs without failing' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 105 | blk = proc do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 106 | 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 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 118 | end |
| 119 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 120 | describe 'for secure servers' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 121 | it 'runs without failing' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 122 | blk = proc do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 123 | s = Server.new(@cq, nil) |
| 124 | s.add_http2_port('localhost:0', true) |
| 125 | s.close |
| 126 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 127 | expect(&blk).to_not raise_error |
| 128 | end |
| 129 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 130 | it 'fails if the server is closed' do |
| 131 | s = Server.new(@cq, nil) |
| 132 | s.close |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 133 | blk = proc { s.add_http2_port('localhost:0', true) } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 134 | expect(&blk).to raise_error(RuntimeError) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 135 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 136 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 137 | end |
| 138 | |
| 139 | shared_examples '#new' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 140 | it 'takes a completion queue with nil channel args' do |
| 141 | expect { Server.new(@cq, nil, create_test_cert) }.to_not raise_error |
| 142 | end |
| 143 | |
| 144 | it 'does not take a hash with bad keys as channel args' do |
| 145 | blk = construct_with_args(Object.new => 1) |
| 146 | expect(&blk).to raise_error TypeError |
| 147 | blk = construct_with_args(1 => 1) |
| 148 | expect(&blk).to raise_error TypeError |
| 149 | end |
| 150 | |
| 151 | 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] | 152 | blk = construct_with_args(symbol: Object.new) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 153 | expect(&blk).to raise_error TypeError |
| 154 | blk = construct_with_args('1' => Hash.new) |
| 155 | expect(&blk).to raise_error TypeError |
| 156 | end |
| 157 | |
| 158 | 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] | 159 | blk = construct_with_args(a_symbol: 1) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 160 | expect(&blk).to_not raise_error |
| 161 | end |
| 162 | |
| 163 | it 'can take a hash with a string key as channel args' do |
| 164 | blk = construct_with_args('a_symbol' => 1) |
| 165 | expect(&blk).to_not raise_error |
| 166 | end |
| 167 | |
| 168 | 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] | 169 | blk = construct_with_args(a_symbol: '1') |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 170 | expect(&blk).to_not raise_error |
| 171 | end |
| 172 | |
| 173 | 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] | 174 | blk = construct_with_args(a_symbol: :another_symbol) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 175 | expect(&blk).to_not raise_error |
| 176 | end |
| 177 | |
| 178 | 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] | 179 | blk = construct_with_args(a_symbol: 1) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 180 | expect(&blk).to_not raise_error |
| 181 | end |
| 182 | |
| 183 | it 'can take a hash with many args as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 184 | args = Hash[127.times.collect { |x| [x.to_s, x] }] |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 185 | blk = construct_with_args(args) |
| 186 | expect(&blk).to_not raise_error |
| 187 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 188 | end |
| 189 | |
| 190 | describe '#new with an insecure channel' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 191 | def construct_with_args(a) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 192 | proc { Server.new(@cq, a) } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 193 | end |
| 194 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 195 | it_behaves_like '#new' |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 196 | end |
| 197 | |
| 198 | describe '#new with a secure channel' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 199 | def construct_with_args(a) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 200 | proc { Server.new(@cq, a, create_test_cert) } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 201 | end |
| 202 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 203 | it_behaves_like '#new' |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 204 | end |
| 205 | |
| 206 | def start_a_server |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 207 | s = Server.new(@cq, nil) |
Tim Emiola | 61ddba3 | 2015-01-26 18:33:20 -0800 | [diff] [blame] | 208 | s.add_http2_port('0.0.0.0:0') |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 209 | s.start |
| 210 | s |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 211 | end |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 212 | end |