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