summaryrefslogtreecommitdiffstats
path: root/test/test_dns_updater.cpp
blob: 2a425c0c4b9280aff8017e06e8e202e8da65448d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "dns_updater.hpp"

#include <experimental/filesystem>
#include <fstream>

#include <gtest/gtest.h>

static constexpr auto IN_FILE = "/tmp/" __BASE_FILE__ "netif_state";
static constexpr auto OUT_FILE = "/tmp/" __BASE_FILE__ "resolv.conf";
static constexpr auto COMPARE_FILE =
    "/tmp/" __BASE_FILE__ "resolv_compare.conf";
static constexpr auto DNS_ENTRY_1 = "DNS=1.2.3.4\n";
static constexpr auto DNS_ENTRY_2 = "DNS=5.6.7.8\n";

namespace fs = std::experimental::filesystem;

class DnsUpdateTest : public ::testing::Test
{
  public:
    // Gets called as part of each TEST_F construction
    DnsUpdateTest()
    {
        // Create a file containing DNS entries like in netif/state
        std::ofstream file(IN_FILE);
        file << DNS_ENTRY_1;
        file << DNS_ENTRY_2;

        // Create a file to compare the results against
        std::ofstream compare(COMPARE_FILE);
        compare << "### Generated by phosphor-networkd ###\n";
        compare << "nameserver 1.2.3.4\n";
        compare << "nameserver 5.6.7.8\n";
    }

    // Gets called as part of each TEST_F destruction
    ~DnsUpdateTest()
    {
        if (fs::exists(IN_FILE))
        {
            fs::remove(IN_FILE);
        }
        if (fs::exists(OUT_FILE))
        {
            fs::remove(OUT_FILE);
        }
        if (fs::exists(COMPARE_FILE))
        {
            fs::remove(COMPARE_FILE);
        }
    }
};

/** @brief Makes outfile is updated with right contents
 */
TEST_F(DnsUpdateTest, validateOutFile)
{
    phosphor::network::dns::updater::updateDNSEntries(IN_FILE, OUT_FILE);

    // Read files and compare
    std::ifstream resolv(OUT_FILE);
    std::ifstream compare(COMPARE_FILE);

    // From actual file
    std::string resolvEntry{};
    std::string resolvContent{};
    while (std::getline(resolv, resolvEntry))
    {
        resolvContent += resolvEntry;
    }

    // From compare file
    std::string compareEntry{};
    std::string compareContent{};
    while (std::getline(compare, compareEntry))
    {
        compareContent += compareEntry;
    }
    EXPECT_EQ(resolvContent, compareContent);
}
OpenPOWER on IntegriCloud