Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 1 | #include <sys/stat.h> |
| 2 | #include <string> |
| 3 | #include <fstream> |
| 4 | #include <experimental/filesystem> |
| 5 | #include <gtest/gtest.h> |
| 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include "user.hpp" |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace user |
| 11 | { |
| 12 | |
| 13 | namespace fs = std::experimental::filesystem; |
| 14 | |
| 15 | constexpr auto path = "/dummy/user"; |
| 16 | constexpr auto testShadow = "/tmp/__tshadow__"; |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 17 | constexpr auto shadowCompare = "/tmp/__tshadowCompare__"; |
| 18 | |
| 19 | // New password |
| 20 | constexpr auto password = "passw0rd"; |
| 21 | |
| 22 | constexpr auto MD5 = "1"; |
| 23 | constexpr auto SHA512 = "6"; |
| 24 | constexpr auto salt = "1G.cK/YP"; |
| 25 | |
| 26 | // Example entry matching /etc/shadow structure |
| 27 | constexpr auto spPwdp = "$1$1G.cK/YP$JI5t0oliPxZveXOvLcZ/H.:17344:1:90:7:::"; |
| 28 | |
| 29 | class UserTest : public ::testing::Test |
| 30 | { |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 31 | public: |
| 32 | const std::string md5Salt = |
| 33 | '$' + std::string(MD5) + '$' + std::string(salt) + '$'; |
| 34 | const std::string shaSalt = |
| 35 | '$' + std::string(SHA512) + '$' + std::string(salt) + '$'; |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 36 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 37 | const std::string entry = |
| 38 | fs::path(path).filename().string() + ':' + std::string(spPwdp); |
| 39 | sdbusplus::bus::bus bus; |
| 40 | phosphor::user::User user; |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 41 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 42 | // Gets called as part of each TEST_F construction |
| 43 | UserTest() : bus(sdbusplus::bus::new_default()), user(bus, path) |
| 44 | { |
| 45 | // Create a shadow file entry |
| 46 | std::ofstream file(testShadow); |
| 47 | file << entry; |
| 48 | file.close(); |
| 49 | |
| 50 | // File to compare against |
| 51 | std::ofstream compare(shadowCompare); |
| 52 | compare << entry; |
| 53 | compare.close(); |
| 54 | } |
| 55 | |
| 56 | // Gets called as part of each TEST_F destruction |
| 57 | ~UserTest() |
| 58 | { |
| 59 | if (fs::exists(testShadow)) |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 60 | { |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 61 | fs::remove(testShadow); |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 62 | } |
| 63 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 64 | if (fs::exists(shadowCompare)) |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 65 | { |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 66 | fs::remove(shadowCompare); |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 67 | } |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 68 | } |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 69 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 70 | /** @brief wrapper for get crypt field */ |
| 71 | auto getCryptField(char* data) |
| 72 | { |
| 73 | return User::getCryptField(std::forward<decltype(data)>(data)); |
| 74 | } |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 75 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 76 | /** @brief wrapper for getSaltString */ |
| 77 | auto getSaltString(const std::string& crypt, const std::string& salt) |
| 78 | { |
| 79 | return User::getSaltString(std::forward<decltype(crypt)>(crypt), |
| 80 | std::forward<decltype(salt)>(salt)); |
| 81 | } |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 82 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 83 | /** @brief wrapper for generateHash */ |
| 84 | auto generateHash(const std::string& password, const std::string& salt) |
| 85 | { |
| 86 | return User::generateHash(std::forward<decltype(password)>(password), |
| 87 | std::forward<decltype(salt)>(salt)); |
| 88 | } |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 89 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 90 | /** @brief Applies the new password */ |
| 91 | auto applyPassword() |
| 92 | { |
| 93 | return user.applyPassword(testShadow, password, salt); |
| 94 | } |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /** @brief Makes sure that SHA512 crypt field is extracted |
| 98 | */ |
| 99 | TEST_F(UserTest, sha512GetCryptField) |
| 100 | { |
| 101 | auto salt = const_cast<char*>(shaSalt.c_str()); |
| 102 | EXPECT_EQ(SHA512, this->getCryptField(salt)); |
| 103 | } |
| 104 | |
| 105 | /** @brief Makes sure that MD5 crypt field is extracted as default |
| 106 | */ |
| 107 | TEST_F(UserTest, md55GetCryptFieldDefault) |
| 108 | { |
| 109 | auto salt = const_cast<char*>("hello"); |
| 110 | EXPECT_EQ(MD5, this->getCryptField(salt)); |
| 111 | } |
| 112 | |
| 113 | /** @brief Makes sure that MD5 crypt field is extracted |
| 114 | */ |
| 115 | TEST_F(UserTest, md55GetCryptField) |
| 116 | { |
| 117 | auto salt = const_cast<char*>(md5Salt.c_str()); |
| 118 | EXPECT_EQ(MD5, this->getCryptField(salt)); |
| 119 | } |
| 120 | |
| 121 | /** @brief Makes sure that salt string is put within $$ |
| 122 | */ |
| 123 | TEST_F(UserTest, getSaltString) |
| 124 | { |
| 125 | EXPECT_EQ(md5Salt, this->getSaltString(MD5, salt)); |
| 126 | } |
| 127 | |
| 128 | /** @brief Makes sure hash is generated correctly |
| 129 | */ |
| 130 | TEST_F(UserTest, generateHash) |
| 131 | { |
| 132 | std::string sample = crypt(password, md5Salt.c_str()); |
| 133 | std::string actual = generateHash(password, md5Salt); |
| 134 | EXPECT_EQ(sample, actual); |
| 135 | } |
| 136 | |
| 137 | /** @brief Verifies that the correct password is written to file |
| 138 | */ |
| 139 | TEST_F(UserTest, applyPassword) |
| 140 | { |
| 141 | // Update the password |
| 142 | applyPassword(); |
| 143 | |
| 144 | // Read files and compare |
| 145 | std::ifstream shadow(testShadow); |
| 146 | std::ifstream copy(shadowCompare); |
| 147 | |
| 148 | std::string shadowEntry; |
| 149 | shadow >> shadowEntry; |
| 150 | |
| 151 | std::string shadowCompareEntry; |
| 152 | copy >> shadowCompareEntry; |
| 153 | |
| 154 | EXPECT_EQ(shadowEntry, shadowCompareEntry); |
| 155 | } |
| 156 | |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 157 | /** @brief Verifies the permissions are correct |
| 158 | */ |
| 159 | TEST_F(UserTest, verifyShadowPermission) |
| 160 | { |
| 161 | // Change the permission to 400-> -r-------- |
| 162 | chmod(testShadow, S_IRUSR); |
| 163 | chmod(shadowCompare, S_IRUSR); |
| 164 | |
| 165 | // Update the password so that the temp file is in action |
| 166 | applyPassword(); |
| 167 | |
| 168 | // Compare the permission of 2 files. |
| 169 | // file rename would make sure that the permissions |
| 170 | // of old are moved to new |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 171 | struct stat shadow |
| 172 | { |
| 173 | }; |
| 174 | struct stat compare |
| 175 | { |
| 176 | }; |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 177 | |
| 178 | stat(testShadow, &shadow); |
| 179 | stat(shadowCompare, &compare); |
| 180 | EXPECT_EQ(shadow.st_mode, compare.st_mode); |
| 181 | } |
| 182 | |
| 183 | } // namespace user |
| 184 | } // namespace phosphor |