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