| 1 | /* Copyright (c) 2019, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include "my_config.h" |
| 25 | |
| 26 | #ifdef HAVE_GETPWNAM |
| 27 | #include <gtest/gtest.h> |
| 28 | #include "my_getpwnam.h" |
| 29 | |
| 30 | // Unit tests for mysys my_getpwnam() and my_getpwuid() |
| 31 | |
| 32 | namespace mysys_my_getpw { |
| 33 | |
| 34 | // Verify that a default-constructed PasswdValue is void |
| 35 | TEST(MysysMyGetpw, DefaultPasswdValue) { |
| 36 | PasswdValue pv; |
| 37 | EXPECT_TRUE(pv.IsVoid()); |
| 38 | } |
| 39 | // Verify basic value operations |
| 40 | TEST(MysysMyGetpw, BasicValueOperations) { |
| 41 | PasswdValue pv; |
| 42 | pv.pw_name = "root"; |
| 43 | PasswdValue copy = pv; |
| 44 | EXPECT_EQ(std::string{"root"}, copy.pw_name); |
| 45 | PasswdValue moved = std::move(copy); |
| 46 | EXPECT_EQ(std::string{"root"}, moved.pw_name); |
| 47 | PasswdValue assigned; |
| 48 | assigned = pv; |
| 49 | EXPECT_EQ(std::string{"root"}, assigned.pw_name); |
| 50 | } |
| 51 | |
| 52 | // Verify that it is possible to retrive entry for root using symbolic name |
| 53 | TEST(MysysMyGetpw, GetpwnamRoot) { |
| 54 | PasswdValue rootpw = my_getpwnam("root"); |
| 55 | EXPECT_EQ(std::string{"root"}, rootpw.pw_name); |
| 56 | EXPECT_EQ(0u, rootpw.pw_uid); |
| 57 | EXPECT_EQ(0u, rootpw.pw_gid); |
| 58 | } |
| 59 | |
| 60 | // Verify that it is possible to retrive entry for root using numeric id |
| 61 | TEST(MysysMyGetpw, GetpwuidRoot) { |
| 62 | PasswdValue rootpw = my_getpwuid(0); |
| 63 | EXPECT_EQ(std::string{"root"}, rootpw.pw_name); |
| 64 | EXPECT_EQ(0u, rootpw.pw_uid); |
| 65 | EXPECT_EQ(0u, rootpw.pw_gid); |
| 66 | } |
| 67 | |
| 68 | // Verify that non-existent user give void value |
| 69 | TEST(MysysMyGetpw, NonExistentUser) { |
| 70 | PasswdValue none = my_getpwnam("thereisnosuchuser___"); |
| 71 | EXPECT_TRUE(none.IsVoid()); |
| 72 | } |
| 73 | |
| 74 | } // namespace mysys_my_getpw |
| 75 | #endif /* HAVE_GETPWNAM */ |