Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | include GRPC::Core |
| 33 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 34 | shared_context 'setup: tags' do |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 35 | let(:sent_message) { 'sent message' } |
| 36 | let(:reply_text) { 'the reply' } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 37 | |
| 38 | def deadline |
Tim Emiola | 391664a | 2015-08-31 15:38:43 -0700 | [diff] [blame] | 39 | Time.now + 5 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 40 | end |
| 41 | |
yang-g | 2cecece | 2016-06-26 10:57:11 -0700 | [diff] [blame] | 42 | def server_allows_client_to_proceed(metadata = {}) |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 43 | recvd_rpc = @server.request_call |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 44 | expect(recvd_rpc).to_not eq nil |
| 45 | server_call = recvd_rpc.call |
yang-g | 2cecece | 2016-06-26 10:57:11 -0700 | [diff] [blame] | 46 | ops = { CallOps::SEND_INITIAL_METADATA => metadata } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 47 | svr_batch = server_call.run_batch(ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 48 | expect(svr_batch.send_metadata).to be true |
Tim Emiola | 75d7f9a | 2015-01-26 16:57:33 -0800 | [diff] [blame] | 49 | server_call |
| 50 | end |
| 51 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 52 | def new_client_call |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 53 | @ch.create_call(nil, nil, '/method', nil, deadline) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 54 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 55 | end |
| 56 | |
| 57 | shared_examples 'basic GRPC message delivery is OK' do |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 58 | include GRPC::Core |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 59 | include_context 'setup: tags' |
| 60 | |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 61 | context 'the test channel' do |
| 62 | it 'should have a target' do |
| 63 | expect(@ch.target).to be_a(String) |
| 64 | end |
| 65 | end |
| 66 | |
| 67 | context 'a client call' do |
| 68 | it 'should have a peer' do |
| 69 | expect(new_client_call.peer).to be_a(String) |
| 70 | end |
| 71 | end |
| 72 | |
| 73 | it 'calls have peer info' do |
| 74 | call = new_client_call |
| 75 | expect(call.peer).to be_a(String) |
| 76 | end |
| 77 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 78 | it 'servers receive requests from clients and can respond' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 79 | call = new_client_call |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 80 | server_call = nil |
| 81 | |
| 82 | server_thread = Thread.new do |
| 83 | server_call = server_allows_client_to_proceed |
| 84 | end |
| 85 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 86 | client_ops = { |
| 87 | CallOps::SEND_INITIAL_METADATA => {}, |
| 88 | CallOps::SEND_MESSAGE => sent_message |
| 89 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 90 | batch_result = call.run_batch(client_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 91 | expect(batch_result.send_metadata).to be true |
| 92 | expect(batch_result.send_message).to be true |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 93 | |
| 94 | # confirm the server can read the inbound message |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 95 | server_thread.join |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 96 | server_ops = { |
| 97 | CallOps::RECV_MESSAGE => nil |
| 98 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 99 | svr_batch = server_call.run_batch(server_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 100 | expect(svr_batch.message).to eq(sent_message) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 101 | end |
| 102 | |
| 103 | it 'responses written by servers are received by the client' do |
| 104 | call = new_client_call |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 105 | server_call = nil |
| 106 | |
| 107 | server_thread = Thread.new do |
| 108 | server_call = server_allows_client_to_proceed |
| 109 | end |
| 110 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 111 | client_ops = { |
| 112 | CallOps::SEND_INITIAL_METADATA => {}, |
| 113 | CallOps::SEND_MESSAGE => sent_message |
| 114 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 115 | batch_result = call.run_batch(client_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 116 | expect(batch_result.send_metadata).to be true |
| 117 | expect(batch_result.send_message).to be true |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 118 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 119 | # confirm the server can read the inbound message |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 120 | server_thread.join |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 121 | server_ops = { |
| 122 | CallOps::RECV_MESSAGE => nil, |
| 123 | CallOps::SEND_MESSAGE => reply_text |
| 124 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 125 | svr_batch = server_call.run_batch(server_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 126 | expect(svr_batch.message).to eq(sent_message) |
| 127 | expect(svr_batch.send_message).to be true |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 128 | end |
| 129 | |
yang-g | 2cecece | 2016-06-26 10:57:11 -0700 | [diff] [blame] | 130 | it 'compressed messages can be sent and received' do |
| 131 | call = new_client_call |
| 132 | server_call = nil |
| 133 | long_request_str = '0' * 2000 |
| 134 | long_response_str = '1' * 2000 |
| 135 | md = { 'grpc-internal-encoding-request' => 'gzip' } |
| 136 | |
| 137 | server_thread = Thread.new do |
| 138 | server_call = server_allows_client_to_proceed(md) |
| 139 | end |
| 140 | |
| 141 | client_ops = { |
| 142 | CallOps::SEND_INITIAL_METADATA => md, |
| 143 | CallOps::SEND_MESSAGE => long_request_str |
| 144 | } |
murgatroid99 | d9d9ba9 | 2016-06-29 14:28:39 -0700 | [diff] [blame] | 145 | batch_result = call.run_batch(client_ops) |
yang-g | 2cecece | 2016-06-26 10:57:11 -0700 | [diff] [blame] | 146 | expect(batch_result.send_metadata).to be true |
| 147 | expect(batch_result.send_message).to be true |
| 148 | |
| 149 | # confirm the server can read the inbound message |
| 150 | server_thread.join |
| 151 | server_ops = { |
| 152 | CallOps::RECV_MESSAGE => nil, |
| 153 | CallOps::SEND_MESSAGE => long_response_str |
| 154 | } |
murgatroid99 | d9d9ba9 | 2016-06-29 14:28:39 -0700 | [diff] [blame] | 155 | svr_batch = server_call.run_batch(server_ops) |
yang-g | 2cecece | 2016-06-26 10:57:11 -0700 | [diff] [blame] | 156 | expect(svr_batch.message).to eq(long_request_str) |
| 157 | expect(svr_batch.send_message).to be true |
| 158 | |
| 159 | client_ops = { |
| 160 | CallOps::SEND_CLOSE_FROM_CLIENT => nil, |
| 161 | CallOps::RECV_INITIAL_METADATA => nil, |
| 162 | CallOps::RECV_MESSAGE => nil |
| 163 | } |
murgatroid99 | d9d9ba9 | 2016-06-29 14:28:39 -0700 | [diff] [blame] | 164 | batch_result = call.run_batch(client_ops) |
yang-g | 2cecece | 2016-06-26 10:57:11 -0700 | [diff] [blame] | 165 | expect(batch_result.send_close).to be true |
| 166 | expect(batch_result.message).to eq long_response_str |
| 167 | end |
| 168 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 169 | it 'servers can ignore a client write and send a status' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 170 | call = new_client_call |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 171 | server_call = nil |
| 172 | |
| 173 | server_thread = Thread.new do |
| 174 | server_call = server_allows_client_to_proceed |
| 175 | end |
| 176 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 177 | client_ops = { |
| 178 | CallOps::SEND_INITIAL_METADATA => {}, |
| 179 | CallOps::SEND_MESSAGE => sent_message |
| 180 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 181 | batch_result = call.run_batch(client_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 182 | expect(batch_result.send_metadata).to be true |
| 183 | expect(batch_result.send_message).to be true |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 184 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 185 | # confirm the server can read the inbound message |
| 186 | the_status = Struct::Status.new(StatusCodes::OK, 'OK') |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 187 | server_thread.join |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 188 | server_ops = { |
| 189 | CallOps::SEND_STATUS_FROM_SERVER => the_status |
| 190 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 191 | svr_batch = server_call.run_batch(server_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 192 | expect(svr_batch.message).to eq nil |
| 193 | expect(svr_batch.send_status).to be true |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 194 | end |
| 195 | |
| 196 | it 'completes calls by sending status to client and server' do |
| 197 | call = new_client_call |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 198 | server_call = nil |
| 199 | |
| 200 | server_thread = Thread.new do |
| 201 | server_call = server_allows_client_to_proceed |
| 202 | end |
| 203 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 204 | client_ops = { |
| 205 | CallOps::SEND_INITIAL_METADATA => {}, |
| 206 | CallOps::SEND_MESSAGE => sent_message |
| 207 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 208 | batch_result = call.run_batch(client_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 209 | expect(batch_result.send_metadata).to be true |
| 210 | expect(batch_result.send_message).to be true |
| 211 | |
| 212 | # confirm the server can read the inbound message and respond |
| 213 | the_status = Struct::Status.new(StatusCodes::OK, 'OK', {}) |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 214 | server_thread.join |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 215 | server_ops = { |
| 216 | CallOps::RECV_MESSAGE => nil, |
| 217 | CallOps::SEND_MESSAGE => reply_text, |
| 218 | CallOps::SEND_STATUS_FROM_SERVER => the_status |
| 219 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 220 | svr_batch = server_call.run_batch(server_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 221 | expect(svr_batch.message).to eq sent_message |
| 222 | expect(svr_batch.send_status).to be true |
| 223 | expect(svr_batch.send_message).to be true |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 224 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 225 | # confirm the client can receive the server response and status. |
| 226 | client_ops = { |
| 227 | CallOps::SEND_CLOSE_FROM_CLIENT => nil, |
murgatroid99 | 5738c50 | 2016-02-04 10:15:42 -0800 | [diff] [blame] | 228 | CallOps::RECV_INITIAL_METADATA => nil, |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 229 | CallOps::RECV_MESSAGE => nil, |
| 230 | CallOps::RECV_STATUS_ON_CLIENT => nil |
| 231 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 232 | batch_result = call.run_batch(client_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 233 | expect(batch_result.send_close).to be true |
| 234 | expect(batch_result.message).to eq reply_text |
| 235 | expect(batch_result.status).to eq the_status |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 236 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 237 | # confirm the server can receive the client close. |
| 238 | server_ops = { |
| 239 | CallOps::RECV_CLOSE_ON_SERVER => nil |
| 240 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 241 | svr_batch = server_call.run_batch(server_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 242 | expect(svr_batch.send_close).to be true |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 243 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 244 | end |
| 245 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 246 | shared_examples 'GRPC metadata delivery works OK' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 247 | include_context 'setup: tags' |
| 248 | |
| 249 | describe 'from client => server' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 250 | before(:example) do |
| 251 | n = 7 # arbitrary number of metadata |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame] | 252 | diff_keys_fn = proc { |i| [format('k%d', i), format('v%d', i)] } |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 253 | diff_keys = Hash[n.times.collect { |x| diff_keys_fn.call x }] |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame] | 254 | null_vals_fn = proc { |i| [format('k%d', i), format('v\0%d', i)] } |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 255 | null_vals = Hash[n.times.collect { |x| null_vals_fn.call x }] |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame] | 256 | same_keys_fn = proc { |i| [format('k%d', i), [format('v%d', i)] * n] } |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 257 | same_keys = Hash[n.times.collect { |x| same_keys_fn.call x }] |
| 258 | symbol_key = { a_key: 'a val' } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 259 | @valid_metadata = [diff_keys, same_keys, null_vals, symbol_key] |
| 260 | @bad_keys = [] |
| 261 | @bad_keys << { Object.new => 'a value' } |
| 262 | @bad_keys << { 1 => 'a value' } |
| 263 | end |
| 264 | |
| 265 | it 'raises an exception if a metadata key is invalid' do |
| 266 | @bad_keys.each do |md| |
| 267 | call = new_client_call |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 268 | client_ops = { |
| 269 | CallOps::SEND_INITIAL_METADATA => md |
| 270 | } |
| 271 | blk = proc do |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 272 | call.run_batch(client_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 273 | end |
| 274 | expect(&blk).to raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 275 | end |
| 276 | end |
| 277 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 278 | it 'sends all the metadata pairs when keys and values are valid' do |
| 279 | @valid_metadata.each do |md| |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 280 | recvd_rpc = nil |
| 281 | rcv_thread = Thread.new do |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 282 | recvd_rpc = @server.request_call |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 283 | end |
| 284 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 285 | call = new_client_call |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 286 | client_ops = { |
| 287 | CallOps::SEND_INITIAL_METADATA => md |
| 288 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 289 | batch_result = call.run_batch(client_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 290 | expect(batch_result.send_metadata).to be true |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 291 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 292 | # confirm the server can receive the client metadata |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 293 | rcv_thread.join |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 294 | expect(recvd_rpc).to_not eq nil |
| 295 | recvd_md = recvd_rpc.metadata |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 296 | replace_symbols = Hash[md.each_pair.collect { |x, y| [x.to_s, y] }] |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 297 | expect(recvd_md).to eq(recvd_md.merge(replace_symbols)) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 298 | end |
| 299 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 300 | end |
| 301 | |
| 302 | describe 'from server => client' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 303 | before(:example) do |
| 304 | n = 7 # arbitrary number of metadata |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame] | 305 | diff_keys_fn = proc { |i| [format('k%d', i), format('v%d', i)] } |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 306 | diff_keys = Hash[n.times.collect { |x| diff_keys_fn.call x }] |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame] | 307 | null_vals_fn = proc { |i| [format('k%d', i), format('v\0%d', i)] } |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 308 | null_vals = Hash[n.times.collect { |x| null_vals_fn.call x }] |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame] | 309 | same_keys_fn = proc { |i| [format('k%d', i), [format('v%d', i)] * n] } |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 310 | same_keys = Hash[n.times.collect { |x| same_keys_fn.call x }] |
| 311 | symbol_key = { a_key: 'a val' } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 312 | @valid_metadata = [diff_keys, same_keys, null_vals, symbol_key] |
| 313 | @bad_keys = [] |
| 314 | @bad_keys << { Object.new => 'a value' } |
| 315 | @bad_keys << { 1 => 'a value' } |
| 316 | end |
| 317 | |
| 318 | it 'raises an exception if a metadata key is invalid' do |
| 319 | @bad_keys.each do |md| |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 320 | recvd_rpc = nil |
| 321 | rcv_thread = Thread.new do |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 322 | recvd_rpc = @server.request_call |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 323 | end |
| 324 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 325 | call = new_client_call |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 326 | # client signals that it's done sending metadata to allow server to |
| 327 | # respond |
| 328 | client_ops = { |
| 329 | CallOps::SEND_INITIAL_METADATA => nil |
| 330 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 331 | call.run_batch(client_ops) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 332 | |
| 333 | # server gets the invocation |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 334 | rcv_thread.join |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 335 | expect(recvd_rpc).to_not eq nil |
| 336 | server_ops = { |
| 337 | CallOps::SEND_INITIAL_METADATA => md |
| 338 | } |
| 339 | blk = proc do |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 340 | recvd_rpc.call.run_batch(server_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 341 | end |
| 342 | expect(&blk).to raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 343 | end |
| 344 | end |
| 345 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 346 | it 'sends an empty hash if no metadata is added' do |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 347 | recvd_rpc = nil |
| 348 | rcv_thread = Thread.new do |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 349 | recvd_rpc = @server.request_call |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 350 | end |
| 351 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 352 | call = new_client_call |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 353 | # client signals that it's done sending metadata to allow server to |
| 354 | # respond |
| 355 | client_ops = { |
| 356 | CallOps::SEND_INITIAL_METADATA => nil |
| 357 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 358 | call.run_batch(client_ops) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 359 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 360 | # server gets the invocation but sends no metadata back |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 361 | rcv_thread.join |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 362 | expect(recvd_rpc).to_not eq nil |
| 363 | server_call = recvd_rpc.call |
| 364 | server_ops = { |
| 365 | CallOps::SEND_INITIAL_METADATA => nil |
| 366 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 367 | server_call.run_batch(server_ops) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 368 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 369 | # client receives nothing as expected |
| 370 | client_ops = { |
| 371 | CallOps::RECV_INITIAL_METADATA => nil |
| 372 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 373 | batch_result = call.run_batch(client_ops) |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 374 | expect(batch_result.metadata).to eq({}) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 375 | end |
| 376 | |
Tim Emiola | 5d66df8 | 2015-02-09 12:35:10 -0800 | [diff] [blame] | 377 | it 'sends all the pairs when keys and values are valid' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 378 | @valid_metadata.each do |md| |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 379 | recvd_rpc = nil |
| 380 | rcv_thread = Thread.new do |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 381 | recvd_rpc = @server.request_call |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 382 | end |
| 383 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 384 | call = new_client_call |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 385 | # client signals that it's done sending metadata to allow server to |
| 386 | # respond |
| 387 | client_ops = { |
| 388 | CallOps::SEND_INITIAL_METADATA => nil |
| 389 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 390 | call.run_batch(client_ops) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 391 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 392 | # server gets the invocation but sends no metadata back |
Craig Tiller | 09600be | 2015-05-13 16:46:55 -0700 | [diff] [blame] | 393 | rcv_thread.join |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 394 | expect(recvd_rpc).to_not eq nil |
| 395 | server_call = recvd_rpc.call |
| 396 | server_ops = { |
| 397 | CallOps::SEND_INITIAL_METADATA => md |
| 398 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 399 | server_call.run_batch(server_ops) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 400 | |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 401 | # client receives nothing as expected |
| 402 | client_ops = { |
| 403 | CallOps::RECV_INITIAL_METADATA => nil |
| 404 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 405 | batch_result = call.run_batch(client_ops) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 406 | replace_symbols = Hash[md.each_pair.collect { |x, y| [x.to_s, y] }] |
Tim Emiola | 05e934f | 2015-03-28 15:19:34 -0700 | [diff] [blame] | 407 | expect(batch_result.metadata).to eq(replace_symbols) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 408 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 409 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 410 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 411 | end |
| 412 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 413 | describe 'the http client/server' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 414 | before(:example) do |
Tim Emiola | 75d7f9a | 2015-01-26 16:57:33 -0800 | [diff] [blame] | 415 | server_host = '0.0.0.0:0' |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 416 | @server = GRPC::Core::Server.new(nil) |
Tim Emiola | 934ae9a | 2015-08-31 09:42:59 -0700 | [diff] [blame] | 417 | server_port = @server.add_http2_port(server_host, :this_port_is_insecure) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 418 | @server.start |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 419 | @ch = Channel.new("0.0.0.0:#{server_port}", nil, :this_channel_is_insecure) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 420 | end |
| 421 | |
| 422 | after(:example) do |
Tim Emiola | 75d7f9a | 2015-01-26 16:57:33 -0800 | [diff] [blame] | 423 | @ch.close |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 424 | @server.close(deadline) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 425 | end |
| 426 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 427 | it_behaves_like 'basic GRPC message delivery is OK' do |
| 428 | end |
| 429 | |
| 430 | it_behaves_like 'GRPC metadata delivery works OK' do |
| 431 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 432 | end |
| 433 | |
| 434 | describe 'the secure http client/server' do |
murgatroid99 | cfa26e1 | 2015-12-04 14:36:52 -0800 | [diff] [blame] | 435 | include_context 'setup: tags' |
| 436 | |
Tim Emiola | 73a540a | 2015-08-28 18:56:17 -0700 | [diff] [blame] | 437 | def load_test_certs |
| 438 | test_root = File.join(File.dirname(__FILE__), 'testdata') |
| 439 | files = ['ca.pem', 'server1.key', 'server1.pem'] |
| 440 | files.map { |f| File.open(File.join(test_root, f)).read } |
| 441 | end |
| 442 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 443 | before(:example) do |
| 444 | certs = load_test_certs |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 445 | server_host = '0.0.0.0:0' |
Tim Emiola | 73a540a | 2015-08-28 18:56:17 -0700 | [diff] [blame] | 446 | server_creds = GRPC::Core::ServerCredentials.new( |
| 447 | nil, [{ private_key: certs[1], cert_chain: certs[2] }], false) |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 448 | @server = GRPC::Core::Server.new(nil) |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 449 | server_port = @server.add_http2_port(server_host, server_creds) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 450 | @server.start |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 451 | args = { Channel::SSL_TARGET => 'foo.test.google.fr' } |
Tim Emiola | 2b90e30 | 2015-01-26 17:37:15 -0800 | [diff] [blame] | 452 | @ch = Channel.new("0.0.0.0:#{server_port}", args, |
Tim Emiola | 43a7e4e | 2015-10-28 00:17:14 -0700 | [diff] [blame] | 453 | GRPC::Core::ChannelCredentials.new(certs[0], nil, nil)) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 454 | end |
| 455 | |
| 456 | after(:example) do |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 457 | @server.close(deadline) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 458 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 459 | |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 460 | it_behaves_like 'basic GRPC message delivery is OK' do |
| 461 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 462 | |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 463 | it_behaves_like 'GRPC metadata delivery works OK' do |
| 464 | end |
murgatroid99 | cfa26e1 | 2015-12-04 14:36:52 -0800 | [diff] [blame] | 465 | |
| 466 | it 'modifies metadata with CallCredentials' do |
| 467 | auth_proc = proc { { 'k1' => 'updated-v1' } } |
| 468 | call_creds = GRPC::Core::CallCredentials.new(auth_proc) |
| 469 | md = { 'k2' => 'v2' } |
| 470 | expected_md = { 'k1' => 'updated-v1', 'k2' => 'v2' } |
| 471 | recvd_rpc = nil |
| 472 | rcv_thread = Thread.new do |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 473 | recvd_rpc = @server.request_call |
murgatroid99 | cfa26e1 | 2015-12-04 14:36:52 -0800 | [diff] [blame] | 474 | end |
| 475 | |
| 476 | call = new_client_call |
| 477 | call.set_credentials! call_creds |
| 478 | client_ops = { |
| 479 | CallOps::SEND_INITIAL_METADATA => md |
| 480 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 481 | batch_result = call.run_batch(client_ops) |
murgatroid99 | cfa26e1 | 2015-12-04 14:36:52 -0800 | [diff] [blame] | 482 | expect(batch_result.send_metadata).to be true |
| 483 | |
| 484 | # confirm the server can receive the client metadata |
| 485 | rcv_thread.join |
| 486 | expect(recvd_rpc).to_not eq nil |
| 487 | recvd_md = recvd_rpc.metadata |
| 488 | replace_symbols = Hash[expected_md.each_pair.collect { |x, y| [x.to_s, y] }] |
| 489 | expect(recvd_md).to eq(recvd_md.merge(replace_symbols)) |
| 490 | end |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 491 | end |