1 #include <gtest/gtest.h>
3 #include <boost/process.hpp>
5 #include "authenticate.h"
8 #include "ostree_http_repo.h"
9 #include "ostree_ref.h"
10 #include "server_credentials.h"
11 #include "test_utils.h"
16 TEST(http_repo, valid_repo) {
18 server.root_url(
"http://localhost:" + port);
19 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
20 EXPECT_TRUE(src_repo->LooksValid());
24 TEST(http_repo, invalid_repo) {
26 server.root_url(
"http://wronghost");
27 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
28 EXPECT_FALSE(src_repo->LooksValid());
32 TEST(http_repo, getRef) {
34 server.root_url(
"http://localhost:" + port);
35 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
36 EXPECT_EQ(src_repo->GetRef(
"master").GetHash().string(),
37 std::string(
"b9ac1e45f9227df8ee191b6e51e09417bd36c6ebbeff999431e3073ac50f0563"));
42 TEST(http_repo, GetObject) {
44 server.root_url(
"http://localhost:" + port);
45 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
46 const uint8_t hash[32] = {0x44, 0x6a, 0x0e, 0xf1, 0x1b, 0x7c, 0xc1, 0x67, 0xf3, 0xb6, 0x03,
47 0xe5, 0x85, 0xc7, 0xee, 0xee, 0xb6, 0x75, 0xfa, 0xa4, 0x12, 0xd5,
48 0xec, 0x73, 0xf6, 0x29, 0x88, 0xeb, 0x0b, 0x6c, 0x54, 0x88};
49 auto object = src_repo->GetObject(hash, OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_META);
52 EXPECT_EQ(s.str(), std::string(
"44/6a0ef11b7cc167f3b603e585c7eeeeb675faa412d5ec73f62988eb0b6c5488.dirmeta"));
56 TEST(http_repo, GetWrongObject) {
58 server.root_url(
"http://localhost:" + port);
59 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
60 const uint8_t hash[32] = {0x00, 0x28, 0xda, 0xc4, 0x2b, 0x76, 0xc2, 0x01, 0x5e, 0xe3, 0xc4,
61 0x1c, 0xc4, 0x18, 0x3b, 0xb8, 0xb5, 0xc7, 0x90, 0xfd, 0x21, 0xfa,
62 0x5c, 0xfa, 0x08, 0x02, 0xc6, 0xe1, 0x1f, 0xd0, 0xed, 0xbe};
63 EXPECT_THROW(src_repo->GetObject(hash, OstreeObjectType::OSTREE_OBJECT_TYPE_DIR_META),
OSTreeObjectMissing);
71 TEST(http_repo, bad_connection) {
73 std::string sp = TestUtils::getFreePort();
75 boost::process::child server_process(
"tests/sota_tools/treehub_server.py", std::string(
"-p"), sp, std::string(
"-d"),
76 src_dir.PathString(), std::string(
"-f2"), std::string(
"--create"));
77 TestUtils::waitForServer(
"http://localhost:" + sp +
"/");
80 server.root_url(
"http://localhost:" + sp);
81 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
83 std::string dp = TestUtils::getFreePort();
85 auth[
"ostree"][
"server"] = std::string(
"https://localhost:") + dp;
86 Utils::writeFile(dst_dir.Path() /
"auth.json", auth);
87 boost::process::child deploy_server_process(
"tests/sota_tools/treehub_server.py", std::string(
"-p"), dp,
88 std::string(
"-d"), dst_dir.PathString(), std::string(
"-f2"),
89 std::string(
"--tls"));
90 TestUtils::waitForServer(
"https://localhost:" + dp +
"/");
92 boost::filesystem::path filepath = (dst_dir.Path() /
"auth.json").
string();
93 boost::filesystem::path cert_path =
"tests/fake_http_server/server.crt";
95 auto hash =
OSTreeHash::Parse(
"b9ac1e45f9227df8ee191b6e51e09417bd36c6ebbeff999431e3073ac50f0563");
97 EXPECT_EQ(authenticate(cert_path.string(),
ServerCredentials(filepath), push_server), EXIT_SUCCESS);
100 std::string diff(
"diff -r ");
101 std::string src_path((src_dir.Path() /
"objects").string() +
" ");
102 std::string repo_path((src_repo->root() /
"objects").string() +
" ");
103 std::string dst_path((dst_dir.Path() /
"objects").string() +
" ");
105 int result = system((diff + src_path + repo_path).c_str());
106 result |= system((diff + repo_path + dst_path).c_str());
108 EXPECT_EQ(
result, 0) <<
"Diff between source and destination repos is nonzero.";
111 TEST(http_repo, root) {
113 server.root_url(
"http://localhost:" + port);
114 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&server);
115 EXPECT_TRUE(src_repo->LooksValid());
116 std::string conf = Utils::readFile(src_repo->root() /
"config");
117 EXPECT_EQ(conf, std::string(
"[core]\nrepo_version=1\nmode=archive-z2\n"));
121 int main(
int argc,
char **argv) {
122 ::testing::InitGoogleTest(&argc, argv);
124 std::string server =
"tests/sota_tools/treehub_server.py";
125 port = TestUtils::getFreePort();
127 boost::process::child server_process(server, std::string(
"-p"), port, std::string(
"--create"));
128 TestUtils::waitForServer(
"http://localhost:" + port +
"/");
130 return RUN_ALL_TESTS();