github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/SimpleClientCpp/junkyard/client_test.cc.old (about)

     1  //
     2  // Copyright 2016, Google Corporation , All Rights Reserved.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  // or in the the file LICENSE-2.0.txt in the top level sourcedirectory
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License
    14  // Project: New Cloudproxy Crypto
    15  // File: client_test.cc
    16  
    17  #include "gtest/gtest.h"
    18  
    19  #include <gtest/gtest.h>
    20  #include <gflags/gflags.h>
    21  #include <stdio.h>
    22  #include <string>
    23  
    24  #include "helpers.h"
    25  
    26  
    27  int main(int an, char** av) {
    28    SslChannel channel;
    29    string path;
    30  
    31    string key_path("/Domains/test_keys");
    32    string ca_cert_string;
    33    string client_cert_string;
    34    string server_cert_string;
    35    string ca_key_string;
    36    string client_key_string;
    37    string server_key_string;
    38    string ca_key_type;
    39    string server_key_type;
    40    string client_key_type;
    41  
    42    // CA
    43    string ca_cert_file_name = key_path + "/ca_cert";
    44    string ca_key_file_name = key_path + "/ca_key";
    45    if(!ReadFile(ca_cert_file_name, &ca_cert_string)) {
    46      printf("can't read ca_cert.\n");
    47      return 1;
    48    }
    49    if(!ReadFile(ca_key_file_name, &ca_key_string)) {
    50      printf("can't read ca key.\n");
    51      return 1;
    52    }
    53    byte* ca_ptr = (byte*)ca_cert_string.data();
    54    X509* ca_cert = d2i_X509(nullptr, (const byte**)&ca_ptr,
    55          ca_cert_string.size());
    56  
    57    // client cert and keys
    58    string client_cert_file_name = key_path + "/client_cert";
    59    string client_key_file_name = key_path + "/client_key";
    60    if(!ReadFile(client_cert_file_name, &client_cert_string)) {
    61      printf("can't read client_cert.\n");
    62      return 1;
    63    }
    64    if(!ReadFile(client_key_file_name, &client_key_string)) {
    65      printf("Can't read client key.\n");
    66      return 1;
    67    }
    68    byte* client_ptr = (byte*)client_cert_string.data();
    69    X509* client_cert = d2i_X509(nullptr, (const byte**)&client_ptr,
    70          client_cert_string.size());
    71    if (client_cert == nullptr) {
    72      printf("client_cert doesnt translate.\n");
    73      return 1;
    74    }
    75  
    76    EVP_PKEY* client_key = nullptr;
    77  
    78    if (!DeserializePrivateKey(client_key_string, &client_key_type, &client_key)) {
    79      printf("Can't deserialize client key\n");
    80      return 1;
    81    }
    82  
    83    string network("tcp");
    84    string address("127.0.0.1");
    85    string port("2015");
    86    string key_type;
    87  
    88    printf("Calling InitClientSslChannel\n");
    89    if (!channel.InitClientSslChannel(network, address, port, ca_cert,
    90                                      client_cert, key_type, client_key,
    91                                      SSL_SERVER_VERIFY_CLIENT_VERIFY)) {
    92      printf("Can't InitClientSslChannel\n");
    93      return 1;
    94    }
    95  
    96    int size_send_buf = 4096;
    97    byte send_buf[4096];
    98    int size_get_buf = 4096;
    99    byte get_buf[4096];
   100    int msg_num = 1;
   101  
   102    // write/read
   103    printf("Client transcript\n\n");
   104    sprintf((char*)send_buf, "Client message %d\n", msg_num++);
   105    size_send_buf = SslWrite(channel.GetSslChannel(),
   106                             strlen((const char*)send_buf) + 1,
   107                             send_buf);
   108    size_get_buf = SslRead(channel.GetSslChannel(), 4096, get_buf);
   109    printf("server reply %d, %s\n", size_get_buf, (const char*)get_buf);
   110    sprintf((char*)send_buf, "Client message %d\n", msg_num++);
   111    size_send_buf = SslWrite(channel.GetSslChannel(),
   112                        strlen((const char*)send_buf) + 1, send_buf);
   113    size_get_buf = SslRead(channel.GetSslChannel(), 4096, get_buf);
   114    printf("server reply %d, %s\n", size_get_buf, (const char*)get_buf);
   115  
   116    return 0;
   117  }
   118