blob: 5d92cd3f22d8593fa57f0d1196c586b716c085ef [file] [log] [blame]
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +05301#include "dns_updater.hpp"
2
3#include <gtest/gtest.h>
4
5#include <fstream>
6#include <experimental/filesystem>
7
8static constexpr auto IN_FILE = "/tmp/netif_state";
9static constexpr auto OUT_FILE = "/tmp/resolv.conf";
10static constexpr auto COMPARE_FILE = "/tmp/resolv_compare.conf";
11static constexpr auto DNS_ENTRY_1 = "DNS=1.2.3.4\n";
12static constexpr auto DNS_ENTRY_2 = "DNS=5.6.7.8\n";
13
14namespace fs = std::experimental::filesystem;
15
16class DnsUpdateTest : public ::testing::Test
17{
18 public:
19 // Gets called as part of each TEST_F construction
20 DnsUpdateTest()
21 {
22 // Create a file containing DNS entries like in netif/state
23 std::ofstream file(IN_FILE);
24 file << DNS_ENTRY_1;
25 file << DNS_ENTRY_2;
26
27 // Create a file to compare the results against
28 std::ofstream compare(COMPARE_FILE);
29 compare << "### Generated by phosphor-networkd ###\n";
30 compare << "nameserver 1.2.3.4\n";
31 compare << "nameserver 5.6.7.8\n";
32 }
33
34 // Gets called as part of each TEST_F destruction
35 ~DnsUpdateTest()
36 {
37 if (fs::exists(IN_FILE))
38 {
39 fs::remove(IN_FILE);
40 }
41 if (fs::exists(OUT_FILE))
42 {
43 fs::remove(OUT_FILE);
44 }
45 if (fs::exists(COMPARE_FILE))
46 {
47 fs::remove(COMPARE_FILE);
48 }
49 }
50};
51
52/** @brief Makes outfile is updated with right contents
53 */
54TEST_F(DnsUpdateTest, validateOutFile)
55{
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +053056 phosphor::network::dns::updater::updateDNSEntries(IN_FILE, OUT_FILE);
Vishwanatha Subbannaf00182e2017-10-16 19:08:59 +053057
58 // Read files and compare
59 std::ifstream resolv(OUT_FILE);
60 std::ifstream compare(COMPARE_FILE);
61
62 // From actual file
63 std::string resolvEntry{};
64 std::string resolvContent{};
65 while (std::getline(resolv, resolvEntry))
66 {
67 resolvContent += resolvEntry;
68 }
69
70 // From compare file
71 std::string compareEntry{};
72 std::string compareContent{};
73 while (std::getline(compare, compareEntry))
74 {
75 compareContent += compareEntry;
76 }
77 EXPECT_EQ(resolvContent, compareContent);
78}