Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 1 | #include "dns_updater.hpp" |
| 2 | |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 3 | #include <experimental/filesystem> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 4 | #include <fstream> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 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"; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 10 | static constexpr auto COMPARE_FILE = |
| 11 | "/tmp/" __BASE_FILE__ "resolv_compare.conf"; |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 12 | static constexpr auto DNS_ENTRY_1 = "DNS=1.2.3.4\n"; |
| 13 | static constexpr auto DNS_ENTRY_2 = "DNS=5.6.7.8\n"; |
| 14 | |
| 15 | namespace fs = std::experimental::filesystem; |
| 16 | |
| 17 | class DnsUpdateTest : public ::testing::Test |
| 18 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 19 | 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 Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 27 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 28 | // 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 Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 34 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 35 | // Gets called as part of each TEST_F destruction |
| 36 | ~DnsUpdateTest() |
| 37 | { |
| 38 | if (fs::exists(IN_FILE)) |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 39 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 40 | fs::remove(IN_FILE); |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 41 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 42 | 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 Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /** @brief Makes outfile is updated with right contents |
| 54 | */ |
| 55 | TEST_F(DnsUpdateTest, validateOutFile) |
| 56 | { |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 57 | phosphor::network::dns::updater::updateDNSEntries(IN_FILE, OUT_FILE); |
Vishwanatha Subbanna | f00182e | 2017-10-16 19:08:59 +0530 | [diff] [blame] | 58 | |
| 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 | } |