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'] |
Tim Emiola | 73a540a | 2015-08-28 18:56:17 -0700 | [diff] [blame] | 35 | contents = files.map { |f| File.open(File.join(test_root, f)).read } |
| 36 | [contents[0], [{ private_key: contents[1], cert_chain: contents[2] }], false] |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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) |
Tim Emiola | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 58 | s.close(@cq) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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 | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 66 | blk = proc { s.destroy(@cq) } |
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 | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 73 | blk = proc { s.destroy(@cq) } |
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 |
Tim Emiola | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 78 | s.close(@cq) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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 | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 87 | blk = proc { s.close(@cq) } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 88 | expect(&blk).to_not raise_error |
| 89 | ensure |
Tim Emiola | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 90 | s.close(@cq) |
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 | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 96 | blk = proc { s.close(@cq) } |
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) |
Tim Emiola | 934ae9a | 2015-08-31 09:42:59 -0700 | [diff] [blame^] | 108 | s.add_http2_port('localhost:0', :this_port_is_insecure) |
Tim Emiola | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 109 | s.close(@cq) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 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) |
Tim Emiola | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 116 | s.close(@cq) |
Tim Emiola | 934ae9a | 2015-08-31 09:42:59 -0700 | [diff] [blame^] | 117 | blk = proc do |
| 118 | s.add_http2_port('localhost:0', :this_port_is_insecure) |
| 119 | end |
| 120 | expect(&blk).to raise_error(RuntimeError) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 121 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 122 | end |
| 123 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 124 | describe 'for secure servers' do |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 125 | let(:cert) { create_test_cert } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 126 | it 'runs without failing' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 127 | blk = proc do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 128 | s = Server.new(@cq, nil) |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 129 | s.add_http2_port('localhost:0', cert) |
Tim Emiola | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 130 | s.close(@cq) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 131 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 132 | expect(&blk).to_not raise_error |
| 133 | end |
| 134 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 135 | it 'fails if the server is closed' do |
| 136 | s = Server.new(@cq, nil) |
Tim Emiola | b1fa5d4 | 2015-06-11 09:35:06 -0700 | [diff] [blame] | 137 | s.close(@cq) |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 138 | blk = proc { s.add_http2_port('localhost:0', cert) } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 139 | expect(&blk).to raise_error(RuntimeError) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 140 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 141 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 142 | end |
| 143 | |
| 144 | shared_examples '#new' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 145 | it 'takes a completion queue with nil channel args' do |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 146 | expect { Server.new(@cq, nil) }.to_not raise_error |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 147 | end |
| 148 | |
| 149 | it 'does not take a hash with bad keys as channel args' do |
| 150 | blk = construct_with_args(Object.new => 1) |
| 151 | expect(&blk).to raise_error TypeError |
| 152 | blk = construct_with_args(1 => 1) |
| 153 | expect(&blk).to raise_error TypeError |
| 154 | end |
| 155 | |
| 156 | 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] | 157 | blk = construct_with_args(symbol: Object.new) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 158 | expect(&blk).to raise_error TypeError |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame] | 159 | blk = construct_with_args('1' => {}) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 160 | expect(&blk).to raise_error TypeError |
| 161 | end |
| 162 | |
| 163 | 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] | 164 | blk = construct_with_args(a_symbol: 1) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 165 | expect(&blk).to_not raise_error |
| 166 | end |
| 167 | |
| 168 | it 'can take a hash with a string key as channel args' do |
| 169 | blk = construct_with_args('a_symbol' => 1) |
| 170 | expect(&blk).to_not raise_error |
| 171 | end |
| 172 | |
| 173 | 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] | 174 | blk = construct_with_args(a_symbol: '1') |
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 symbol value as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 179 | blk = construct_with_args(a_symbol: :another_symbol) |
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 a numeric value as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 184 | blk = construct_with_args(a_symbol: 1) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 185 | expect(&blk).to_not raise_error |
| 186 | end |
| 187 | |
| 188 | it 'can take a hash with many args as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 189 | args = Hash[127.times.collect { |x| [x.to_s, x] }] |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 190 | blk = construct_with_args(args) |
| 191 | expect(&blk).to_not raise_error |
| 192 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 193 | end |
| 194 | |
| 195 | describe '#new with an insecure channel' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 196 | def construct_with_args(a) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 197 | proc { Server.new(@cq, a) } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 198 | end |
| 199 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 200 | it_behaves_like '#new' |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 201 | end |
| 202 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 203 | def start_a_server |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 204 | s = Server.new(@cq, nil) |
Tim Emiola | 934ae9a | 2015-08-31 09:42:59 -0700 | [diff] [blame^] | 205 | s.add_http2_port('0.0.0.0:0', :this_port_is_insecure) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 206 | s.start |
| 207 | s |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 208 | end |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 209 | end |