Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / src / android / test / interop / app / src / main / cpp / grpc-interop.cc
1 /*
2  *
3  * Copyright 2018 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 <grpcpp/grpcpp.h>
20 #include <jni.h>
21
22 #include "src/core/lib/security/security_connector/ssl_utils.h"
23 #include "test/cpp/interop/interop_client.h"
24
25 extern "C" JNIEXPORT void JNICALL
26 Java_io_grpc_interop_cpp_InteropActivity_configureSslRoots(JNIEnv* env,
27                                                            jobject obj_this,
28                                                            jstring path_raw) {
29   const char* path = env->GetStringUTFChars(path_raw, (jboolean*)0);
30
31   GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, path);
32 }
33
34 std::shared_ptr<grpc::testing::InteropClient> GetClient(const char* host,
35                                                         int port,
36                                                         bool use_tls) {
37   const int host_port_buf_size = 1024;
38   char host_port[host_port_buf_size];
39   snprintf(host_port, host_port_buf_size, "%s:%d", host, port);
40
41   std::shared_ptr<grpc::ChannelCredentials> credentials;
42   if (use_tls) {
43     credentials = grpc::SslCredentials(grpc::SslCredentialsOptions());
44   } else {
45     credentials = grpc::InsecureChannelCredentials();
46   }
47
48   grpc::testing::ChannelCreationFunc channel_creation_func =
49       std::bind(grpc::CreateChannel, host_port, credentials);
50   return std::shared_ptr<grpc::testing::InteropClient>(
51       new grpc::testing::InteropClient(channel_creation_func, true, false));
52 }
53
54 extern "C" JNIEXPORT jboolean JNICALL
55 Java_io_grpc_interop_cpp_InteropActivity_doEmpty(JNIEnv* env, jobject obj_this,
56                                                  jstring host_raw,
57                                                  jint port_raw,
58                                                  jboolean use_tls_raw) {
59   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
60   int port = static_cast<int>(port_raw);
61   bool use_tls = static_cast<bool>(use_tls_raw);
62
63   return GetClient(host, port, use_tls)->DoEmpty();
64 }
65
66 extern "C" JNIEXPORT jboolean JNICALL
67 Java_io_grpc_interop_cpp_InteropActivity_doLargeUnary(JNIEnv* env,
68                                                       jobject obj_this,
69                                                       jstring host_raw,
70                                                       jint port_raw,
71                                                       jboolean use_tls_raw) {
72   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
73   int port = static_cast<int>(port_raw);
74   bool use_tls = static_cast<bool>(use_tls_raw);
75
76   return GetClient(host, port, use_tls)->DoLargeUnary();
77 }
78
79 extern "C" JNIEXPORT jboolean JNICALL
80 Java_io_grpc_interop_cpp_InteropActivity_doEmptyStream(JNIEnv* env,
81                                                        jobject obj_this,
82                                                        jstring host_raw,
83                                                        jint port_raw,
84                                                        jboolean use_tls_raw) {
85   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
86   int port = static_cast<int>(port_raw);
87   bool use_tls = static_cast<bool>(use_tls_raw);
88
89   return GetClient(host, port, use_tls)->DoEmptyStream();
90 }
91
92 extern "C" JNIEXPORT jboolean JNICALL
93 Java_io_grpc_interop_cpp_InteropActivity_doRequestStreaming(
94     JNIEnv* env, jobject obj_this, jstring host_raw, jint port_raw,
95     jboolean use_tls_raw) {
96   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
97   int port = static_cast<int>(port_raw);
98   bool use_tls = static_cast<bool>(use_tls_raw);
99
100   return GetClient(host, port, use_tls)->DoRequestStreaming();
101 }
102
103 extern "C" JNIEXPORT jboolean JNICALL
104 Java_io_grpc_interop_cpp_InteropActivity_doResponseStreaming(
105     JNIEnv* env, jobject obj_this, jstring host_raw, jint port_raw,
106     jboolean use_tls_raw) {
107   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
108   int port = static_cast<int>(port_raw);
109   bool use_tls = static_cast<bool>(use_tls_raw);
110
111   return GetClient(host, port, use_tls)->DoResponseStreaming();
112 }
113
114 extern "C" JNIEXPORT jboolean JNICALL
115 Java_io_grpc_interop_cpp_InteropActivity_doPingPong(JNIEnv* env,
116                                                     jobject obj_this,
117                                                     jstring host_raw,
118                                                     jint port_raw,
119                                                     jboolean use_tls_raw) {
120   const char* host = env->GetStringUTFChars(host_raw, (jboolean*)0);
121   int port = static_cast<int>(port_raw);
122   bool use_tls = static_cast<bool>(use_tls_raw);
123
124   return GetClient(host, port, use_tls)->DoPingPong();
125 }