Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 1 | #include "dns_updater.hpp" |
| 2 | |
| 3 | #include <gtest/gtest.h> |
| 4 | |
| 5 | #include <fstream> |
| 6 | #include <experimental/filesystem> |
| 7 | |
Andrew Jeffery | 6b371a1 | 2018-03-13 22:38:17 +1030 | [diff] [blame] | 8 | static constexpr auto IN_FILE = "/tmp/" __BASE_FILE__ "netif_state"; |
| 9 | static constexpr auto OUT_FILE = "/tmp/" __BASE_FILE__ "resolv.conf"; |
| 10 | static constexpr auto COMPARE_FILE = "/tmp/" __BASE_FILE__ "resolv_compare.conf"; |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 11 | static constexpr auto DNS_ENTRY_1 = "DNS=1.2.3.4\n"; |
| 12 | static constexpr auto DNS_ENTRY_2 = "DNS=5.6.7.8\n"; |
| 13 | |
| 14 | namespace fs = std::experimental::filesystem; |
| 15 | |
| 16 | class 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 | */ |
| 54 | TEST_F(DnsUpdateTest, validateOutFile) |
| 55 | { |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 56 | phosphor::network::dns::updater::updateDNSEntries(IN_FILE, OUT_FILE); |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 57 | |
| 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 | } |