Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
treehub_server_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <curl/curl.h>
4 #include <boost/process.hpp>
5 #include <thread>
6 #include "ostree_http_repo.h"
7 #include "ostree_ref.h"
8 #include "test_utils.h"
9 std::string port;
10 
11 static size_t writeString(void *contents, size_t size, size_t nmemb, void *userp) {
12  assert(userp);
13  // append the writeback data to the provided string
14  (static_cast<std::string *>(userp))->append(static_cast<char *>(contents), size * nmemb);
15 
16  // return size of written data
17  return size * nmemb;
18 }
19 
20 /* Authenticate with OAuth2. */
21 TEST(treehub_server, token_auth) {
22  TreehubServer server;
23  CurlEasyWrapper curl_handle;
24  server.root_url(std::string("http://127.0.0.1:") + port);
25  server.SetToken("test_token");
26  server.InjectIntoCurl("/", curl_handle.get());
27  std::string response;
28  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEFUNCTION, writeString);
29  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEDATA, static_cast<void *>(&response));
30  curl_easy_perform(curl_handle.get());
31 
32  long rescode; // NOLINT(google-runtime-int)
33  curl_easy_getinfo(curl_handle.get(), CURLINFO_RESPONSE_CODE, &rescode);
34  if (rescode == 200) {
35  auto response_json = Utils::parseJSON(response);
36  EXPECT_EQ(response_json["Authorization"], "Bearer test_token");
37  } else {
38  FAIL() << "Failed to authenticate token with server";
39  }
40 }
41 
42 /* Authenticate with username and password (basic auth). */
43 TEST(treehub_server, basic_auth) {
44  TreehubServer server;
45  CurlEasyWrapper curl_handle;
46  server.root_url(std::string("http://127.0.0.1:") + port);
47  server.SetAuthBasic("login", "password");
48  server.InjectIntoCurl("/", curl_handle.get());
49  std::string response;
50  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEFUNCTION, writeString);
51  curlEasySetoptWrapper(curl_handle.get(), CURLOPT_WRITEDATA, static_cast<void *>(&response));
52  curl_easy_perform(curl_handle.get());
53 
54  long rescode; // NOLINT(google-runtime-int)
55  curl_easy_getinfo(curl_handle.get(), CURLINFO_RESPONSE_CODE, &rescode);
56  if (rescode == 200) {
57  auto response_json = Utils::parseJSON(response);
58  EXPECT_EQ(response_json["Authorization"], "Basic bG9naW46cGFzc3dvcmQ=");
59  } else {
60  FAIL() << "Failed to authenticate login/password with server";
61  }
62 }
63 
64 #ifndef __NO_MAIN__
65 int main(int argc, char **argv) {
66  ::testing::InitGoogleTest(&argc, argv);
67  std::string server = "tests/sota_tools/headers_response_server.py";
68  port = TestUtils::getFreePort();
69 
70  boost::process::child server_process(server, port);
71  TestUtils::waitForServer("http://127.0.0.1:" + port + "/");
72 
73  return RUN_ALL_TESTS();
74 }
75 #endif
76 
77 // vim: set tabstop=2 shiftwidth=2 expandtab:
CurlEasyWrapper
Definition: utils.h:146
TreehubServer
Definition: treehub_server.h:11