client_server_spec.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. # Copyright 2015, 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. require 'grpc'
  30. require 'spec_helper'
  31. include GRPC::Core
  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
  37. shared_context 'setup: tags' do
  38. let(:sent_message) { 'sent message' }
  39. let(:reply_text) { 'the reply' }
  40. before(:example) do
  41. @client_tag = Object.new
  42. @server_tag = Object.new
  43. end
  44. def deadline
  45. Time.now + 2
  46. end
  47. def server_allows_client_to_proceed
  48. recvd_rpc = @server.request_call(@server_queue, @server_tag, deadline)
  49. expect(recvd_rpc).to_not eq nil
  50. server_call = recvd_rpc.call
  51. ops = { CallOps::SEND_INITIAL_METADATA => {} }
  52. svr_batch = server_call.run_batch(@server_queue, @server_tag, deadline, ops)
  53. expect(svr_batch.send_metadata).to be true
  54. server_call
  55. end
  56. def new_client_call
  57. @ch.create_call(@client_queue, '/method', 'foo.test.google.fr', deadline)
  58. end
  59. end
  60. shared_examples 'basic GRPC message delivery is OK' do
  61. include GRPC::Core
  62. include_context 'setup: tags'
  63. it 'servers receive requests from clients and can respond' do
  64. call = new_client_call
  65. server_call = nil
  66. server_thread = Thread.new do
  67. server_call = server_allows_client_to_proceed
  68. end
  69. client_ops = {
  70. CallOps::SEND_INITIAL_METADATA => {},
  71. CallOps::SEND_MESSAGE => sent_message
  72. }
  73. batch_result = call.run_batch(@client_queue, @client_tag, deadline,
  74. client_ops)
  75. expect(batch_result.send_metadata).to be true
  76. expect(batch_result.send_message).to be true
  77. # confirm the server can read the inbound message
  78. server_thread.join
  79. server_ops = {
  80. CallOps::RECV_MESSAGE => nil
  81. }
  82. svr_batch = server_call.run_batch(@server_queue, @server_tag, deadline,
  83. server_ops)
  84. expect(svr_batch.message).to eq(sent_message)
  85. end
  86. it 'responses written by servers are received by the client' do
  87. call = new_client_call
  88. server_call = nil
  89. server_thread = Thread.new do
  90. server_call = server_allows_client_to_proceed
  91. end
  92. client_ops = {
  93. CallOps::SEND_INITIAL_METADATA => {},
  94. CallOps::SEND_MESSAGE => sent_message
  95. }
  96. batch_result = call.run_batch(@client_queue, @client_tag, deadline,
  97. client_ops)
  98. expect(batch_result.send_metadata).to be true
  99. expect(batch_result.send_message).to be true
  100. # confirm the server can read the inbound message
  101. server_thread.join
  102. server_ops = {
  103. CallOps::RECV_MESSAGE => nil,
  104. CallOps::SEND_MESSAGE => reply_text
  105. }
  106. svr_batch = server_call.run_batch(@server_queue, @server_tag, deadline,
  107. server_ops)
  108. expect(svr_batch.message).to eq(sent_message)
  109. expect(svr_batch.send_message).to be true
  110. end
  111. it 'servers can ignore a client write and send a status' do
  112. call = new_client_call
  113. server_call = nil
  114. server_thread = Thread.new do
  115. server_call = server_allows_client_to_proceed
  116. end
  117. client_ops = {
  118. CallOps::SEND_INITIAL_METADATA => {},
  119. CallOps::SEND_MESSAGE => sent_message
  120. }
  121. batch_result = call.run_batch(@client_queue, @client_tag, deadline,
  122. client_ops)
  123. expect(batch_result.send_metadata).to be true
  124. expect(batch_result.send_message).to be true
  125. # confirm the server can read the inbound message
  126. the_status = Struct::Status.new(StatusCodes::OK, 'OK')
  127. server_thread.join
  128. server_ops = {
  129. CallOps::SEND_STATUS_FROM_SERVER => the_status
  130. }
  131. svr_batch = server_call.run_batch(@server_queue, @server_tag, deadline,
  132. server_ops)
  133. expect(svr_batch.message).to eq nil
  134. expect(svr_batch.send_status).to be true
  135. end
  136. it 'completes calls by sending status to client and server' do
  137. call = new_client_call
  138. server_call = nil
  139. server_thread = Thread.new do
  140. server_call = server_allows_client_to_proceed
  141. end
  142. client_ops = {
  143. CallOps::SEND_INITIAL_METADATA => {},
  144. CallOps::SEND_MESSAGE => sent_message
  145. }
  146. batch_result = call.run_batch(@client_queue, @client_tag, deadline,
  147. client_ops)
  148. expect(batch_result.send_metadata).to be true
  149. expect(batch_result.send_message).to be true
  150. # confirm the server can read the inbound message and respond
  151. the_status = Struct::Status.new(StatusCodes::OK, 'OK', {})
  152. server_thread.join
  153. server_ops = {
  154. CallOps::RECV_MESSAGE => nil,
  155. CallOps::SEND_MESSAGE => reply_text,
  156. CallOps::SEND_STATUS_FROM_SERVER => the_status
  157. }
  158. svr_batch = server_call.run_batch(@server_queue, @server_tag, deadline,
  159. server_ops)
  160. expect(svr_batch.message).to eq sent_message
  161. expect(svr_batch.send_status).to be true
  162. expect(svr_batch.send_message).to be true
  163. # confirm the client can receive the server response and status.
  164. client_ops = {
  165. CallOps::SEND_CLOSE_FROM_CLIENT => nil,
  166. CallOps::RECV_MESSAGE => nil,
  167. CallOps::RECV_STATUS_ON_CLIENT => nil
  168. }
  169. batch_result = call.run_batch(@client_queue, @client_tag, deadline,
  170. client_ops)
  171. expect(batch_result.send_close).to be true
  172. expect(batch_result.message).to eq reply_text
  173. expect(batch_result.status).to eq the_status
  174. # confirm the server can receive the client close.
  175. server_ops = {
  176. CallOps::RECV_CLOSE_ON_SERVER => nil
  177. }
  178. svr_batch = server_call.run_batch(@server_queue, @server_tag, deadline,
  179. server_ops)
  180. expect(svr_batch.send_close).to be true
  181. end
  182. end
  183. shared_examples 'GRPC metadata delivery works OK' do
  184. include_context 'setup: tags'
  185. describe 'from client => server' do
  186. before(:example) do
  187. n = 7 # arbitrary number of metadata
  188. diff_keys_fn = proc { |i| [format('k%d', i), format('v%d', i)] }
  189. diff_keys = Hash[n.times.collect { |x| diff_keys_fn.call x }]
  190. null_vals_fn = proc { |i| [format('k%d', i), format('v\0%d', i)] }
  191. null_vals = Hash[n.times.collect { |x| null_vals_fn.call x }]
  192. same_keys_fn = proc { |i| [format('k%d', i), [format('v%d', i)] * n] }
  193. same_keys = Hash[n.times.collect { |x| same_keys_fn.call x }]
  194. symbol_key = { a_key: 'a val' }
  195. @valid_metadata = [diff_keys, same_keys, null_vals, symbol_key]
  196. @bad_keys = []
  197. @bad_keys << { Object.new => 'a value' }
  198. @bad_keys << { 1 => 'a value' }
  199. end
  200. it 'raises an exception if a metadata key is invalid' do
  201. @bad_keys.each do |md|
  202. call = new_client_call
  203. client_ops = {
  204. CallOps::SEND_INITIAL_METADATA => md
  205. }
  206. blk = proc do
  207. call.run_batch(@client_queue, @client_tag, deadline,
  208. client_ops)
  209. end
  210. expect(&blk).to raise_error
  211. end
  212. end
  213. it 'sends all the metadata pairs when keys and values are valid' do
  214. @valid_metadata.each do |md|
  215. recvd_rpc = nil
  216. rcv_thread = Thread.new do
  217. recvd_rpc = @server.request_call(@server_queue, @server_tag, deadline)
  218. end
  219. call = new_client_call
  220. client_ops = {
  221. CallOps::SEND_INITIAL_METADATA => md
  222. }
  223. batch_result = call.run_batch(@client_queue, @client_tag, deadline,
  224. client_ops)
  225. expect(batch_result.send_metadata).to be true
  226. # confirm the server can receive the client metadata
  227. rcv_thread.join
  228. expect(recvd_rpc).to_not eq nil
  229. recvd_md = recvd_rpc.metadata
  230. replace_symbols = Hash[md.each_pair.collect { |x, y| [x.to_s, y] }]
  231. expect(recvd_md).to eq(recvd_md.merge(replace_symbols))
  232. end
  233. end
  234. end
  235. describe 'from server => client' do
  236. before(:example) do
  237. n = 7 # arbitrary number of metadata
  238. diff_keys_fn = proc { |i| [format('k%d', i), format('v%d', i)] }
  239. diff_keys = Hash[n.times.collect { |x| diff_keys_fn.call x }]
  240. null_vals_fn = proc { |i| [format('k%d', i), format('v\0%d', i)] }
  241. null_vals = Hash[n.times.collect { |x| null_vals_fn.call x }]
  242. same_keys_fn = proc { |i| [format('k%d', i), [format('v%d', i)] * n] }
  243. same_keys = Hash[n.times.collect { |x| same_keys_fn.call x }]
  244. symbol_key = { a_key: 'a val' }
  245. @valid_metadata = [diff_keys, same_keys, null_vals, symbol_key]
  246. @bad_keys = []
  247. @bad_keys << { Object.new => 'a value' }
  248. @bad_keys << { 1 => 'a value' }
  249. end
  250. it 'raises an exception if a metadata key is invalid' do
  251. @bad_keys.each do |md|
  252. recvd_rpc = nil
  253. rcv_thread = Thread.new do
  254. recvd_rpc = @server.request_call(@server_queue, @server_tag, deadline)
  255. end
  256. call = new_client_call
  257. # client signals that it's done sending metadata to allow server to
  258. # respond
  259. client_ops = {
  260. CallOps::SEND_INITIAL_METADATA => nil
  261. }
  262. call.run_batch(@client_queue, @client_tag, deadline, client_ops)
  263. # server gets the invocation
  264. rcv_thread.join
  265. expect(recvd_rpc).to_not eq nil
  266. server_ops = {
  267. CallOps::SEND_INITIAL_METADATA => md
  268. }
  269. blk = proc do
  270. recvd_rpc.call.run_batch(@server_queue, @server_tag, deadline,
  271. server_ops)
  272. end
  273. expect(&blk).to raise_error
  274. end
  275. end
  276. it 'sends an empty hash if no metadata is added' do
  277. recvd_rpc = nil
  278. rcv_thread = Thread.new do
  279. recvd_rpc = @server.request_call(@server_queue, @server_tag, deadline)
  280. end
  281. call = new_client_call
  282. # client signals that it's done sending metadata to allow server to
  283. # respond
  284. client_ops = {
  285. CallOps::SEND_INITIAL_METADATA => nil
  286. }
  287. call.run_batch(@client_queue, @client_tag, deadline, client_ops)
  288. # server gets the invocation but sends no metadata back
  289. rcv_thread.join
  290. expect(recvd_rpc).to_not eq nil
  291. server_call = recvd_rpc.call
  292. server_ops = {
  293. CallOps::SEND_INITIAL_METADATA => nil
  294. }
  295. server_call.run_batch(@server_queue, @server_tag, deadline, server_ops)
  296. # client receives nothing as expected
  297. client_ops = {
  298. CallOps::RECV_INITIAL_METADATA => nil
  299. }
  300. batch_result = call.run_batch(@client_queue, @client_tag, deadline,
  301. client_ops)
  302. expect(batch_result.metadata).to eq({})
  303. end
  304. it 'sends all the pairs when keys and values are valid' do
  305. @valid_metadata.each do |md|
  306. recvd_rpc = nil
  307. rcv_thread = Thread.new do
  308. recvd_rpc = @server.request_call(@server_queue, @server_tag, deadline)
  309. end
  310. call = new_client_call
  311. # client signals that it's done sending metadata to allow server to
  312. # respond
  313. client_ops = {
  314. CallOps::SEND_INITIAL_METADATA => nil
  315. }
  316. call.run_batch(@client_queue, @client_tag, deadline, client_ops)
  317. # server gets the invocation but sends no metadata back
  318. rcv_thread.join
  319. expect(recvd_rpc).to_not eq nil
  320. server_call = recvd_rpc.call
  321. server_ops = {
  322. CallOps::SEND_INITIAL_METADATA => md
  323. }
  324. server_call.run_batch(@server_queue, @server_tag, deadline, server_ops)
  325. # client receives nothing as expected
  326. client_ops = {
  327. CallOps::RECV_INITIAL_METADATA => nil
  328. }
  329. batch_result = call.run_batch(@client_queue, @client_tag, deadline,
  330. client_ops)
  331. replace_symbols = Hash[md.each_pair.collect { |x, y| [x.to_s, y] }]
  332. expect(batch_result.metadata).to eq(replace_symbols)
  333. end
  334. end
  335. end
  336. end
  337. describe 'the http client/server' do
  338. before(:example) do
  339. server_host = '0.0.0.0:0'
  340. @client_queue = GRPC::Core::CompletionQueue.new
  341. @server_queue = GRPC::Core::CompletionQueue.new
  342. @server = GRPC::Core::Server.new(@server_queue, nil)
  343. server_port = @server.add_http2_port(server_host)
  344. @server.start
  345. @ch = Channel.new("0.0.0.0:#{server_port}", nil)
  346. end
  347. after(:example) do
  348. @ch.close
  349. @server.close(@server_queue, deadline)
  350. end
  351. it_behaves_like 'basic GRPC message delivery is OK' do
  352. end
  353. it_behaves_like 'GRPC metadata delivery works OK' do
  354. end
  355. end
  356. describe 'the secure http client/server' do
  357. before(:example) do
  358. certs = load_test_certs
  359. server_host = '0.0.0.0:0'
  360. @client_queue = GRPC::Core::CompletionQueue.new
  361. @server_queue = GRPC::Core::CompletionQueue.new
  362. server_creds = GRPC::Core::ServerCredentials.new(nil, certs[1], certs[2])
  363. @server = GRPC::Core::Server.new(@server_queue, nil)
  364. server_port = @server.add_http2_port(server_host, server_creds)
  365. @server.start
  366. args = { Channel::SSL_TARGET => 'foo.test.google.fr' }
  367. @ch = Channel.new("0.0.0.0:#{server_port}", args,
  368. GRPC::Core::Credentials.new(certs[0], nil, nil))
  369. end
  370. after(:example) do
  371. @server.close(@server_queue, deadline)
  372. end
  373. it_behaves_like 'basic GRPC message delivery is OK' do
  374. end
  375. it_behaves_like 'GRPC metadata delivery works OK' do
  376. end
  377. end