Home | History | Annotate | Download | only in bad_ssl
      1 /*
      2  *
      3  * Copyright 2015 gRPC authors.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  */
     18 
     19 #include <grpc/support/log.h>
     20 #include <signal.h>
     21 
     22 #include "test/core/bad_ssl/server_common.h"
     23 #include "test/core/util/cmdline.h"
     24 #include "test/core/util/test_config.h"
     25 
     26 /* Common server implementation details for all servers in servers/.
     27  * There's nothing *wrong* with these servers per-se, but they are
     28  * configured to cause some failure case in the SSL connection path.
     29  */
     30 
     31 static int got_sigint = 0;
     32 
     33 static void sigint_handler(int x) { got_sigint = 1; }
     34 
     35 const char* bad_ssl_addr(int argc, char** argv) {
     36   gpr_cmdline* cl;
     37   const char* addr = nullptr;
     38   cl = gpr_cmdline_create("test server");
     39   gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr);
     40   gpr_cmdline_parse(cl, argc, argv);
     41   gpr_cmdline_destroy(cl);
     42   GPR_ASSERT(addr);
     43   return addr;
     44 }
     45 
     46 void bad_ssl_run(grpc_server* server) {
     47   int shutdown_started = 0;
     48   int shutdown_finished = 0;
     49   grpc_event ev;
     50   grpc_call_error error;
     51   grpc_call* s = nullptr;
     52   grpc_call_details call_details;
     53   grpc_metadata_array request_metadata_recv;
     54 
     55   grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
     56   grpc_completion_queue* shutdown_cq;
     57 
     58   grpc_call_details_init(&call_details);
     59   grpc_metadata_array_init(&request_metadata_recv);
     60 
     61   grpc_server_register_completion_queue(server, cq, nullptr);
     62   grpc_server_start(server);
     63 
     64   error = grpc_server_request_call(server, &s, &call_details,
     65                                    &request_metadata_recv, cq, cq, (void*)1);
     66   GPR_ASSERT(GRPC_CALL_OK == error);
     67 
     68   signal(SIGINT, sigint_handler);
     69   while (!shutdown_finished) {
     70     if (got_sigint && !shutdown_started) {
     71       gpr_log(GPR_INFO, "Shutting down due to SIGINT");
     72       shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
     73       grpc_server_shutdown_and_notify(server, shutdown_cq, nullptr);
     74       GPR_ASSERT(grpc_completion_queue_pluck(
     75                      shutdown_cq, nullptr, grpc_timeout_seconds_to_deadline(5),
     76                      nullptr)
     77                      .type == GRPC_OP_COMPLETE);
     78       grpc_completion_queue_destroy(shutdown_cq);
     79       grpc_completion_queue_shutdown(cq);
     80       shutdown_started = 1;
     81     }
     82     ev = grpc_completion_queue_next(
     83         cq,
     84         gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
     85                      gpr_time_from_micros(1000000, GPR_TIMESPAN)),
     86         nullptr);
     87     switch (ev.type) {
     88       case GRPC_OP_COMPLETE:
     89         GPR_ASSERT(ev.tag == (void*)1);
     90         GPR_ASSERT(ev.success == 0);
     91         break;
     92       case GRPC_QUEUE_SHUTDOWN:
     93         GPR_ASSERT(shutdown_started);
     94         shutdown_finished = 1;
     95         break;
     96       case GRPC_QUEUE_TIMEOUT:
     97         break;
     98     }
     99   }
    100 
    101   GPR_ASSERT(s == nullptr);
    102   grpc_call_details_destroy(&call_details);
    103   grpc_metadata_array_destroy(&request_metadata_recv);
    104 }
    105