Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
authenticate_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <string>
4 
5 #include <curl/curl.h>
6 #include <boost/process.hpp>
7 
8 #include "authenticate.h"
9 #include "server_credentials.h"
10 #include "test_utils.h"
11 #include "treehub_server.h"
12 #include "utilities/utils.h"
13 
14 boost::filesystem::path certs_dir;
15 
16 /* Authenticate with OAuth2.
17  * Parse authentication information from treehub.json. */
18 TEST(authenticate, good_zip) {
19  // Authenticates with the ATS portal to the SaaS instance.
20  boost::filesystem::path filepath = "tests/sota_tools/auth_test_good.zip";
21  ServerCredentials creds(filepath);
22  EXPECT_EQ(creds.GetMethod(), AuthMethod::kOauth2);
23  TreehubServer treehub;
24  int r = authenticate("", creds, treehub);
25  EXPECT_EQ(0, r);
26 }
27 
28 /* Authenticate with TLS credentials.
29  * Parse Image repository URL from a provided archive. */
30 TEST(authenticate, good_cert_zip) {
31  // Authenticates with tls_server on port 1443.
32  boost::filesystem::path filepath = certs_dir / "good.zip";
33  boost::filesystem::path capath = certs_dir / "server.crt";
34  ServerCredentials creds(filepath);
35  EXPECT_EQ(creds.GetMethod(), AuthMethod::kTls);
36  TreehubServer treehub;
37  int r = authenticate(capath.string(), creds, treehub);
38  EXPECT_EQ(0, r);
39  CurlEasyWrapper curl_handle;
40  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_VERBOSE, 1);
41  treehub.InjectIntoCurl("test.txt", curl_handle.get());
42  CURLcode rc = curl_easy_perform(curl_handle.get());
43  EXPECT_EQ(CURLE_OK, rc);
44 }
45 
46 /* Authenticate with nothing (no auth).
47  * Parse authentication information from treehub.json.
48  * Parse Image repository URL from a provided archive. */
49 TEST(authenticate, good_cert_noauth_zip) {
50  // Authenticates with tls_noauth_server on port 2443.
51  boost::filesystem::path filepath = "tests/sota_tools/auth_test_noauth_good.zip";
52  boost::filesystem::path capath = certs_dir / "server.crt";
53  ServerCredentials creds(filepath);
54  EXPECT_EQ(creds.GetMethod(), AuthMethod::kNone);
55  TreehubServer treehub;
56  int r = authenticate(capath.string(), creds, treehub);
57  EXPECT_EQ(0, r);
58  CurlEasyWrapper curl_handle;
59  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_VERBOSE, 1);
60  treehub.InjectIntoCurl("test.txt", curl_handle.get());
61  CURLcode rc = curl_easy_perform(curl_handle.get());
62 
63  EXPECT_EQ(CURLE_OK, rc);
64 }
65 
66 TEST(authenticate, bad_cert_zip) {
67  // Tries to authenticates with tls_server on port 1443.
68  // Fails because the intermediate cert that signed the client cert was signed
69  // by a different root cert.
70  boost::filesystem::path filepath = certs_dir / "bad.zip";
71  ServerCredentials creds(filepath);
72  EXPECT_EQ(creds.GetMethod(), AuthMethod::kTls);
73  TreehubServer treehub;
74  int r = authenticate("", creds, treehub);
75  EXPECT_EQ(0, r);
76  CurlEasyWrapper curl_handle;
77  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_VERBOSE, 1);
78  treehub.InjectIntoCurl("test.txt", curl_handle.get());
79  CURLcode rc = curl_easy_perform(curl_handle.get());
80 
81  EXPECT_NE(CURLE_OK, rc);
82 }
83 
84 /* Reject a provided archive file with bogus credentials. */
85 TEST(authenticate, bad_zip) {
86  boost::filesystem::path filepath = "tests/sota_tools/auth_test_bad.zip";
87  TreehubServer treehub;
88  int r = authenticate("", ServerCredentials(filepath), treehub);
89  EXPECT_EQ(1, r);
90 }
91 
92 /* Reject a provided archive file without a treehub.json. */
93 TEST(authenticate, no_json_zip) {
94  boost::filesystem::path filepath = "tests/sota_tools/auth_test_no_json.zip";
95  EXPECT_THROW(ServerCredentials creds(filepath), BadCredentialsContent);
96 }
97 
98 /* Extract credentials from a provided JSON file. */
99 TEST(authenticate, good_json) {
100  // Authenticates with the ATS portal to the SaaS instance.
101  boost::filesystem::path filepath = "tests/sota_tools/auth_test_good.json";
102  TreehubServer treehub;
103  int r = authenticate("", ServerCredentials(filepath), treehub);
104  EXPECT_EQ(0, r);
105 }
106 
107 /* Reject a bogus provided JSON file. */
108 TEST(authenticate, bad_json) {
109  boost::filesystem::path filepath = "tests/sota_tools/auth_test_bad.json";
110  TreehubServer treehub;
111  int r = authenticate("", ServerCredentials(filepath), treehub);
112  EXPECT_EQ(1, r);
113 }
114 
115 /* Reject a bogus provided file. */
116 TEST(authenticate, invalid_file) {
117  boost::filesystem::path filepath = "tests/sota_tools/auth_test.cc";
118  EXPECT_THROW(ServerCredentials creds(filepath), BadCredentialsJson);
119 }
120 
121 /* Check if credentials support offline signing. */
122 TEST(authenticate, offline_sign_creds) {
123  // Note that these credentials point to the old CI infrastructure that is now
124  // defunct. However, for the sake of this test, that doesn't matter.
125  boost::filesystem::path auth_offline = "tests/sota_tools/auth_test_good_offline.zip";
126  ServerCredentials creds_offline(auth_offline);
127  EXPECT_TRUE(creds_offline.CanSignOffline());
128 }
129 
130 /* Check if credentials do not support offline signing. */
131 TEST(authenticate, online_sign_creds) {
132  // Authenticates with tls_server on port 1443.
133  boost::filesystem::path auth_online = certs_dir / "good.zip";
134  ServerCredentials creds_online(auth_online);
135  EXPECT_FALSE(creds_online.CanSignOffline());
136 }
137 
138 #ifndef __NO_MAIN__
139 int main(int argc, char **argv) {
140  ::testing::InitGoogleTest(&argc, argv);
141  if (argc != 2) {
142  std::cerr << "Error: " << argv[0] << " requires the path to the directory with generated certificates.\n";
143  return EXIT_FAILURE;
144  }
145  certs_dir = argv[1];
146 
147  boost::process::child server_process("tests/sota_tools/authentication/tls_server.py", "1443", certs_dir);
148  boost::process::child server_noauth_process("tests/sota_tools/authentication/tls_server.py", "--noauth", "2443",
149  certs_dir);
150  // TODO: this do not work because the server expects auth! Let's sleep for now.
151  // (could be replaced by a check with raw tcp)
152  // TestUtils::waitForServer("https://localhost:1443/");
153  sleep(4);
154  TestUtils::waitForServer("https://localhost:2443/");
155  return RUN_ALL_TESTS();
156 }
157 #endif
158 
159 // vim: set tabstop=2 shiftwidth=2 expandtab: