<commit_before>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// ssl_test.cpp
//
// Identification: test/network/ssl_test.cpp
//
// Copyright (c) 2016-17, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include "common/harness.h"
#include "gtest/gtest.h"
#include "common/logger.h"
#include "network/peloton_server.h"
#include "network/protocol_handler_factory.h"
#include "util/string_util.h"
#include <pqxx/pqxx> /* libpqxx is used to instantiate C++ client */
#include "network/postgres_protocol_handler.h"
#include "peloton_config.h"
#include "network/connection_handle_factory.h"

#define NUM_THREADS 1

namespace peloton {
namespace test {

//===--------------------------------------------------------------------===//
// SSL TESTS
//===--------------------------------------------------------------------===//

class SSLTests : public PelotonTest {};

/**
 * Basic SSL connection test:  Tested with valid certificats and key files
 */
void *BasicTest(int port) {
  try {
    // forcing the factory to generate psql protocol handler
    pqxx::connection C(StringUtil::Format(
        "host=127.0.0.1 port=%d user=default_database application_name=psql sslmode=require", port));
    pqxx::work txn1(C);

    peloton::network::ConnectionHandle *conn =
        peloton::network::ConnectionHandleFactory::GetInstance().ConnectionHandleAt(
            peloton::network::PelotonServer::recent_connfd).get();

    network::PostgresProtocolHandler *handler =
        dynamic_cast<network::PostgresProtocolHandler *>(conn->GetProtocolHandler().get());
    EXPECT_NE(handler, nullptr);

    // basic test
    // create table and insert some data
    txn1.exec("DROP TABLE IF EXISTS employee;");
    txn1.exec("CREATE TABLE employee(id INT, name VARCHAR(100));");
    txn1.commit();

    pqxx::work txn2(C);
    txn2.exec("INSERT INTO employee VALUES (1, '<NAME>');");
    txn2.exec("INSERT INTO employee VALUES (2, '<NAME>');");
    txn2.exec("INSERT INTO employee VALUES (3, '<NAME>');");

    pqxx::result R = txn2.exec("SELECT name FROM employee where id=1;");
    txn2.commit();
    
    EXPECT_EQ(R.size(), 1);

    // SSL large write test
    pqxx::work txn3(C);
    txn3.exec("DROP TABLE IF EXISTS template;");
    txn3.exec("CREATE TABLE template(id INT);");
    txn3.commit();

    pqxx::work txn4(C);
    for (int i = 0; i < 1000; i++) {
      std::string s = "INSERT INTO template VALUES (" + std::to_string(i) + ")";
      txn4.exec(s);
    }

    R = txn4.exec("SELECT * from template;");
    txn4.commit();

    EXPECT_EQ(R.size(), 1000);

  } catch (const std::exception &e) {
    LOG_INFO("[SSLTest] Exception occurred: %s", e.what());
    EXPECT_TRUE(false);
  }

  LOG_INFO("[SSLTest] Client has closed");
  return NULL;
}

/**
 * Use std::thread to initiate peloton server and pqxx client in separate
 * threads
 * Simple query test to guarantee both sides run correctly
 * Callback method to close server after client finishes
 */
TEST_F(SSLTests, BasicTest) {
  peloton::PelotonInit::Initialize();
  LOG_INFO("Server initialized");
  peloton::network::PelotonServer peloton_server;
  int port = 15721;
  try {
      std::string server_crt = "/test/network/ssl/server_test.crt";
      std::string server_key = "/test/network/ssl/server_test.key";
      std::string root_crt = "/test/network/ssl/root_test.crt";
      peloton::network::PelotonServer::certificate_file_ = SOURCE_FOLDER + server_crt;
      peloton::network::PelotonServer::private_key_file_ = SOURCE_FOLDER + server_key;
      peloton::network::PelotonServer::root_cert_file_ = SOURCE_FOLDER + root_crt;
      peloton::network::PelotonServer::SSLInit();

      peloton_server.SetPort(port);
      peloton_server.SetupServer();
  } catch (peloton::ConnectionException &exception) {
    LOG_INFO("[LaunchServer] exception in thread");
  }

  std::thread serverThread([&] { peloton_server.ServerLoop(); });

  // server & client running correctly
  BasicTest(port);
<commit_msg>try to fix ssl crt permission denied on client side<commit_after>//===----------------------------------------------------------------------===//
//
//                         Peloton
//
// ssl_test.cpp
//
// Identification: test/network/ssl_test.cpp
//
// Copyright (c) 2016-17, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

#include <pqxx/pqxx> /* libpqxx is used to instantiate C++ client */
#include "common/harness.h"
#include "common/logger.h"
#include "gtest/gtest.h"
#include "network/connection_handle_factory.h"
#include "network/peloton_server.h"
#include "network/postgres_protocol_handler.h"
#include "network/protocol_handler_factory.h"
#include "peloton_config.h"
#include "util/string_util.h"

#define NUM_THREADS 1

namespace peloton {
namespace test {

//===--------------------------------------------------------------------===//
// SSL TESTS
//===--------------------------------------------------------------------===//

class SSLTests : public PelotonTest {};

std::string server_crt = std::string(StringUtil::Format(
    "%s%s", SOURCE_FOLDER, "/test/network/ssl/server_test.crt"));
std::string server_key = std::string(StringUtil::Format(
    "%s%s", SOURCE_FOLDER, "/test/network/ssl/server_test.key"));
std::string root_crt = std::string(StringUtil::Format(
    "%s%s", SOURCE_FOLDER, "/test/network/ssl/root_test.crt"));

/**
 * Basic SSL connection test:  Tested with valid certificats and key files
 */
void *BasicTest(int port) {
  try {
    // forcing the factory to generate psql protocol handler
    pqxx::connection C(StringUtil::Format(
        "host=127.0.0.1 port=%d user=default_database application_name=psql "
        "sslmode=require sslcert=%s sslkey=%s sslrootcert=%s ",
        port, server_crt, server_key, root_crt));

    pqxx::work txn1(C);

    peloton::network::ConnectionHandle *conn =
        peloton::network::ConnectionHandleFactory::GetInstance()
            .ConnectionHandleAt(peloton::network::PelotonServer::recent_connfd)
            .get();

    network::PostgresProtocolHandler *handler =
        dynamic_cast<network::PostgresProtocolHandler *>(
            conn->GetProtocolHandler().get());
    EXPECT_NE(handler, nullptr);

    // basic test
    // create table and insert some data
    txn1.exec("DROP TABLE IF EXISTS employee;");
    txn1.exec("CREATE TABLE employee(id INT, name VARCHAR(100));");
    txn1.commit();

    pqxx::work txn2(C);
    txn2.exec("INSERT INTO employee VALUES (1, '<NAME>');");
    txn2.exec("INSERT INTO employee VALUES (2, '<NAME>');");
    txn2.exec("INSERT INTO employee VALUES (3, '<NAME>');");

    pqxx::result R = txn2.exec("SELECT name FROM employee where id=1;");
    txn2.commit();

    EXPECT_EQ(R.size(), 1);

    // SSL large write test
    pqxx::work txn3(C);
    txn3.exec("DROP TABLE IF EXISTS template;");
    txn3.exec("CREATE TABLE template(id INT);");
    txn3.commit();

    pqxx::work txn4(C);
    for (int i = 0; i < 1000; i++) {
      std::string s = "INSERT INTO template VALUES (" + std::to_string(i) + ")";
      txn4.exec(s);
    }

    R = txn4.exec("SELECT * from template;");
    txn4.commit();

    EXPECT_EQ(R.size(), 1000);

  } catch (const std::exception &e) {
    LOG_INFO("[SSLTest] Exception occurred: %s", e.what());
    EXPECT_TRUE(false);
  }

  LOG_INFO("[SSLTest] Client has closed");
  return NULL;
}

/**
 * Use std::thread to initiate peloton server and pqxx client in separate
 * threads
 * Simple query test to guarantee both sides run correctly
 * Callback method to close server after client finishes
 */
TEST_F(SSLTests, BasicTest) {
  peloton::PelotonInit::Initialize();
  LOG_INFO("Server initialized");
  peloton::network::PelotonServer peloton_server;
  int port = 15721;
  try {
    peloton::network::PelotonServer::certificate_file_ = server_crt;
    peloton::network::PelotonServer::private_key_file_ = server_key;
    peloton::network::PelotonServer::root_cert_file_ = root_crt;
    peloton::network::PelotonServer::SSLInit();

    peloton_server.SetPort(port);
    peloton_server.SetupServer();
  } catch (peloton::ConnectionException &exception) {
    LOG_INFO("[LaunchServer] exception in thread");
  }

  std::thread serverThread([&] { peloton_server.ServerLoop(); });

  // server & client running correctly
  BasicTest(port);

{'fraction_non_alphanumeric': 0.20804340134449817, 'fraction_numerical': 0.012501474230451705, 'mean_word_length': 2.9168591224018474, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '88653', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2577, 'n_words': 580}