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 |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 121 | let(:cert) { create_test_cert } |
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) |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 125 | s.add_http2_port('localhost:0', cert) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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 | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 134 | blk = proc { s.add_http2_port('localhost:0', cert) } |
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 |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 142 | expect { Server.new(@cq, nil) }.to_not raise_error |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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 |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame^] | 155 | blk = construct_with_args('1' => {}) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 199 | def start_a_server |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 200 | s = Server.new(@cq, nil) |
Tim Emiola | 61ddba3 | 2015-01-26 18:33:20 -0800 | [diff] [blame] | 201 | s.add_http2_port('0.0.0.0:0') |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 202 | s.start |
| 203 | s |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 204 | end |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 205 | end |