From 2bcdf164e9d4c98882d55b293861b3b698d06bdf Mon Sep 17 00:00:00 2001 From: Tim Trippel Date: Mon, 14 Oct 2024 16:49:35 -0700 Subject: [PATCH] [ate,cp] add reference CP test program boilerplate This adds reference CP test program boilerplate code. This partially addresses #6. Signed-off-by: Tim Trippel --- src/ate/test_programs/BUILD.bazel | 19 +++++ src/ate/test_programs/cp.cc | 117 ++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 src/ate/test_programs/BUILD.bazel create mode 100644 src/ate/test_programs/cp.cc diff --git a/src/ate/test_programs/BUILD.bazel b/src/ate/test_programs/BUILD.bazel new file mode 100644 index 0000000..edc8b2b --- /dev/null +++ b/src/ate/test_programs/BUILD.bazel @@ -0,0 +1,19 @@ +# Copyright lowRISC contributors (OpenTitan project). +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 + +package(default_visibility = ["//visibility:public"]) + +cc_binary( + name = "cp", + srcs = ["cp.cc"], + deps = [ + "//src/ate:ate_client", + "//src/version", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/flags:parse", + "@com_google_absl//absl/log", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:statusor", + ], +) diff --git a/src/ate/test_programs/cp.cc b/src/ate/test_programs/cp.cc new file mode 100644 index 0000000..68774e9 --- /dev/null +++ b/src/ate/test_programs/cp.cc @@ -0,0 +1,117 @@ +// Copyright lowRISC contributors (OpenTitan project). +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 + +#include + +#include +#include +#include +#include +#include + +#include "absl/flags/flag.h" +#include "absl/flags/parse.h" +#include "absl/flags/usage_config.h" +#include "absl/log/log.h" +#include "absl/status/status.h" +#include "absl/status/statusor.h" +#include "src/ate/ate_client.h" +#include "src/pa/proto/pa.grpc.pb.h" +#include "src/version/version.h" + +/** + * PA configuration flags. + */ +ABSL_FLAG(std::string, pa_socket, "", "host:port of the PA server."); +ABSL_FLAG(std::string, sku, "", "SKU string to initialize the PA session."); +ABSL_FLAG(std::string, sku_auth_pw, "", + "SKU authorization password string to initialize the PA session."); + +/** + * mTLS configuration flags. + */ +ABSL_FLAG(bool, enable_mtls, false, "Enable mTLS secure channel."); +ABSL_FLAG(std::string, client_key, "", + "File path to the PEM encoding of the client's private key."); +ABSL_FLAG(std::string, client_cert, "", + "File path to the PEM encoding of the client's certificate chain."); +ABSL_FLAG(std::string, ca_root_certs, "", + "File path to the PEM encoding of the server root certificates."); + +namespace { +using provisioning::VersionFormatted; +using provisioning::ate::AteClient; + +// Returns `filename` content in a std::string format +absl::StatusOr ReadFile(const std::string &filename) { + auto output_stream = std::ostringstream(); + std::ifstream file_stream(filename); + if (!file_stream.is_open()) { + return absl::InvalidArgumentError( + absl::StrCat("Unable to open file: \"", filename, "\"")); + } + output_stream << file_stream.rdbuf(); + return output_stream.str(); +} + +} // namespace + +int main(int argc, char **argv) { + // Parse cmd line args. + absl::FlagsUsageConfig config; + config.version_string = &VersionFormatted; + absl::SetFlagsUsageConfig(config); + absl::ParseCommandLine(argc, argv); + LOG(INFO) << VersionFormatted(); + + // Extract and validate ATE client options. + AteClient::Options options; + options.pa_socket = absl::GetFlag(FLAGS_pa_socket); + options.enable_mtls = absl::GetFlag(FLAGS_enable_mtls); + if (options.enable_mtls) { + std::unordered_map *, std::string *> pem_options = { + {&FLAGS_client_key, &options.pem_private_key}, + {&FLAGS_client_cert, &options.pem_cert_chain}, + {&FLAGS_ca_root_certs, &options.pem_root_certs}, + }; + + for (auto opt : pem_options) { + std::string filename = absl::GetFlag(*opt.first); + if (filename.empty()) { + LOG(ERROR) << "--" << absl::GetFlagReflectionHandle(*opt.first).Name() + << " not set. This is a required argument when " + << " --enable_mtls is set to true." << std::endl; + return -1; + } + auto result = ReadFile(filename); + if (!result.ok()) { + LOG(ERROR) << "--" << absl::GetFlagReflectionHandle(*opt.first).Name() + << " " << result.status() << std::endl; + return -1; + } + *opt.second = result.value(); + } + } + + // Instantiate a client. + auto ate = AteClient::Create(options); + + // Init/Close session. + grpc::Status status = ate->InitSession(absl::GetFlag(FLAGS_sku), + absl::GetFlag(FLAGS_sku_auth_pw)); + if (!status.ok()) { + LOG(ERROR) << "InitSession failed with " << status.error_code() << ": " + << status.error_message() << std::endl; + } + + // TODO(#6): add CP test code here. + + status = ate->CloseSession(); + if (!status.ok()) { + LOG(ERROR) << "CloseSession failed with " << status.error_code() << ": " + << status.error_message() << std::endl; + } + + return 0; +}