blob: 5538449670254fe69e20be502c3e2fd85ad50ba5 [file] [log] [blame]
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06001#include "phosphor-ldap-config/utils.hpp"
Patrick Williams9638afb2021-02-22 17:16:24 -06002
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06003#include <ldap.h>
Patrick Williams9638afb2021-02-22 17:16:24 -06004#include <netinet/in.h>
5
6#include <gtest/gtest.h>
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -06007
8namespace phosphor
9{
10namespace ldap
11{
Nan Zhou78d85042022-08-29 17:50:22 +000012constexpr auto ldapScheme = "ldap";
13constexpr auto ldapsScheme = "ldaps";
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060014
15class TestUtil : public testing::Test
16{
17 public:
18 TestUtil()
19 {
20 // Empty
21 }
22};
23
24TEST_F(TestUtil, URIValidation)
25{
Nan Zhou78d85042022-08-29 17:50:22 +000026 std::string ipAddress = "ldap://0.0.0.0";
27 EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060028
Nan Zhou78d85042022-08-29 17:50:22 +000029 ipAddress = "ldap://9.3.185.83";
30 EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060031
Nan Zhou78d85042022-08-29 17:50:22 +000032 ipAddress = "ldaps://9.3.185.83";
33 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060034
Nan Zhou78d85042022-08-29 17:50:22 +000035 ipAddress = "ldap://9.3.a.83";
36 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060037
Nan Zhou78d85042022-08-29 17:50:22 +000038 ipAddress = "ldap://9.3.185.a";
39 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060040
Nan Zhou78d85042022-08-29 17:50:22 +000041 ipAddress = "ldap://x.x.x.x";
42 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060043
Nan Zhou78d85042022-08-29 17:50:22 +000044 ipAddress = "ldaps://0.0.0.0";
45 EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060046
Nan Zhou78d85042022-08-29 17:50:22 +000047 ipAddress = "ldap://0.0.0.0";
48 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060049
Nan Zhou78d85042022-08-29 17:50:22 +000050 ipAddress = "ldaps://9.3.185.83";
51 EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060052
Nan Zhou78d85042022-08-29 17:50:22 +000053 ipAddress = "ldap://9.3.185.83";
54 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060055
Nan Zhou78d85042022-08-29 17:50:22 +000056 ipAddress = "ldaps://9.3.185.83";
57 EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060058
Nan Zhou78d85042022-08-29 17:50:22 +000059 ipAddress = "ldaps://9.3.185.a";
60 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060061
Nan Zhou78d85042022-08-29 17:50:22 +000062 ipAddress = "ldaps://9.3.a.83";
63 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060064
Nan Zhou78d85042022-08-29 17:50:22 +000065 ipAddress = "ldaps://x.x.x.x";
66 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
Asmitha Karunanithi2f64e422022-03-10 01:35:21 -060067
Nan Zhou78d85042022-08-29 17:50:22 +000068 ipAddress = "ldap://9.3.185.83:70000";
69 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Asmitha Karunanithi2f64e422022-03-10 01:35:21 -060070
Nan Zhou78d85042022-08-29 17:50:22 +000071 ipAddress = "ldap://9.3.185.83:-3";
72 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Asmitha Karunanithi2f64e422022-03-10 01:35:21 -060073
Nan Zhou78d85042022-08-29 17:50:22 +000074 ipAddress = "ldap://9.3.185.83:221";
75 EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Asmitha Karunanithi2f64e422022-03-10 01:35:21 -060076
Nan Zhou78d85042022-08-29 17:50:22 +000077 ipAddress = "ldap://9.3.185.83:0";
78 EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
Nagaraju Gorugantid514e5d2018-11-08 03:07:25 -060079}
80} // namespace ldap
81} // namespace phosphor