Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
cert_provider_shared_cred_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <boost/format.hpp>
4 
5 #include "cert_provider_test.h"
6 #include "config/config.h"
7 #include "utilities/utils.h"
8 
9 static boost::filesystem::path CERT_PROVIDER_PATH;
10 static boost::filesystem::path CREDENTIALS_PATH;
11 
12 class AktualizrCertProviderTest : public ::testing::Test {
13  protected:
14  struct TestArgs {
15  TestArgs(const TemporaryDirectory& tmp_dir, const std::string& cred_path)
16  : test_dir{tmp_dir.PathString()}, credentials_path(cred_path) {}
17 
18  const std::string test_dir;
19  const std::string fleet_ca_cert = "tests/test_data/CAcert.pem";
20  const std::string fleet_ca_private_key = "tests/test_data/CApkey.pem";
21  const std::string credentials_path;
22  };
23 
24  protected:
25  TemporaryDirectory tmp_dir_;
26  TestArgs test_args_{tmp_dir_, CREDENTIALS_PATH.string()};
27  DeviceCredGenerator device_cred_gen_{CERT_PROVIDER_PATH.string()};
28 };
29 
30 /**
31  * Verifies that cert-provider works when given shared provisioning credentials
32  * and the fleet CA and private key are not specified.
33  *
34  * - [x] Use shared provisioning credentials if fleet CA and private key are not provided
35  * - [x] Provision with shared credentials
36  * - [x] Read server root CA from p12
37  * - [x] Provide root CA if requested
38  * - [x] Provide server URL if requested
39  */
40 TEST_F(AktualizrCertProviderTest, SharedCredProvisioning) {
41  if (test_args_.credentials_path.empty()) {
42  // GTEST_SKIP() was introduced in recent gtest version;
43  SUCCEED() << "A path to the credentials file hasn't been proided, so skip the test";
44  return;
45  }
46 
48 
49  args.credentialFile = test_args_.credentials_path;
50  args.localDir = test_args_.test_dir;
51  args.provideRootCA.set();
52  args.provideServerURL.set();
53 
54  device_cred_gen_.run(args);
55  ASSERT_EQ(device_cred_gen_.lastExitCode(), 0) << device_cred_gen_.lastStdErr();
56 
57  DeviceCredGenerator::OutputPath device_cred_path(test_args_.test_dir);
58 
59  ASSERT_TRUE(boost::filesystem::exists(device_cred_path.privateKeyFileFullPath))
60  << device_cred_path.privateKeyFileFullPath;
61  ASSERT_TRUE(boost::filesystem::exists(device_cred_path.certFileFullPath)) << device_cred_path.certFileFullPath;
62 
63  ASSERT_TRUE(boost::filesystem::exists(device_cred_path.serverRootCAFullPath))
64  << device_cred_path.serverRootCAFullPath;
65  ASSERT_TRUE(boost::filesystem::exists(device_cred_path.gtwURLFileFullPath)) << device_cred_path.gtwURLFileFullPath;
66 
67  Process openssl("/usr/bin/openssl");
68 
69  openssl.run({"verify", "-verbose", "-CAfile", device_cred_path.serverRootCAFullPath.string(),
70  device_cred_path.certFileFullPath.string()});
71  ASSERT_EQ(openssl.lastExitCode(), 0) << openssl.lastStdErr();
72  ASSERT_EQ(openssl.lastStdOut(), str(boost::format("%1%: OK\n") % device_cred_path.certFileFullPath.string()));
73 }
74 
75 #ifndef __NO_MAIN__
76 int main(int argc, char** argv) {
77  ::testing::InitGoogleTest(&argc, argv);
78 
79  if (argc < 3) {
80  std::cerr << "Two arguments are required: <path-to-cert-provider> <path-to-credentials>" << std::endl;
81  return EXIT_FAILURE;
82  }
83 
84  CERT_PROVIDER_PATH = argv[1];
85  std::cout << "Path to the cert-provider executable: " << CERT_PROVIDER_PATH << std::endl;
86 
87  CREDENTIALS_PATH = argv[2];
88  std::cout << "Path to the shared provisioning credentials: " << CREDENTIALS_PATH << std::endl;
89 
90  int test_run_res = RUN_ALL_TESTS();
91 
92  return test_run_res;
93 }
94 #endif
DeviceCredGenerator::OutputPath
Definition: cert_provider_test.h:79
DeviceCredGenerator
Definition: cert_provider_test.h:9
Process
Definition: test_utils.h:19
AktualizrCertProviderTest::TestArgs
Definition: cert_provider_shared_cred_test.cc:14
TemporaryDirectory
Definition: utils.h:82
DeviceCredGenerator::ArgSet
Definition: cert_provider_test.h:13
AktualizrCertProviderTest
Definition: cert_provider_shared_cred_test.cc:12