Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 1 | #include "phosphor-ldap-config/utils.hpp" |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 2 | |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 3 | #include <ldap.h> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 4 | #include <netinet/in.h> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace ldap |
| 11 | { |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 12 | constexpr auto ldapScheme = "ldap"; |
| 13 | constexpr auto ldapsScheme = "ldaps"; |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 14 | |
| 15 | class TestUtil : public testing::Test |
| 16 | { |
| 17 | public: |
| 18 | TestUtil() |
| 19 | { |
| 20 | // Empty |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | TEST_F(TestUtil, URIValidation) |
| 25 | { |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 26 | std::string ipAddress = "ldap://0.0.0.0"; |
| 27 | EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 28 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 29 | ipAddress = "ldap://9.3.185.83"; |
| 30 | EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 31 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 32 | ipAddress = "ldaps://9.3.185.83"; |
| 33 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 34 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 35 | ipAddress = "ldap://9.3.a.83"; |
| 36 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 37 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 38 | ipAddress = "ldap://9.3.185.a"; |
| 39 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 40 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 41 | ipAddress = "ldap://x.x.x.x"; |
| 42 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 43 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 44 | ipAddress = "ldaps://0.0.0.0"; |
| 45 | EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 46 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 47 | ipAddress = "ldap://0.0.0.0"; |
| 48 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 49 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 50 | ipAddress = "ldaps://9.3.185.83"; |
| 51 | EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 52 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 53 | ipAddress = "ldap://9.3.185.83"; |
| 54 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 55 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 56 | ipAddress = "ldaps://9.3.185.83"; |
| 57 | EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 58 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 59 | ipAddress = "ldaps://9.3.185.a"; |
| 60 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 61 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 62 | ipAddress = "ldaps://9.3.a.83"; |
| 63 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 64 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 65 | ipAddress = "ldaps://x.x.x.x"; |
| 66 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme)); |
Asmitha Karunanithi | 2f64e42 | 2022-03-10 01:35:21 -0600 | [diff] [blame] | 67 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 68 | ipAddress = "ldap://9.3.185.83:70000"; |
| 69 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Asmitha Karunanithi | 2f64e42 | 2022-03-10 01:35:21 -0600 | [diff] [blame] | 70 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 71 | ipAddress = "ldap://9.3.185.83:-3"; |
| 72 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Asmitha Karunanithi | 2f64e42 | 2022-03-10 01:35:21 -0600 | [diff] [blame] | 73 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 74 | ipAddress = "ldap://9.3.185.83:221"; |
| 75 | EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Asmitha Karunanithi | 2f64e42 | 2022-03-10 01:35:21 -0600 | [diff] [blame] | 76 | |
Nan Zhou | 78d8504 | 2022-08-29 17:50:22 +0000 | [diff] [blame] | 77 | ipAddress = "ldap://9.3.185.83:0"; |
| 78 | EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme)); |
Nagaraju Goruganti | d514e5d | 2018-11-08 03:07:25 -0600 | [diff] [blame] | 79 | } |
| 80 | } // namespace ldap |
| 81 | } // namespace phosphor |