1d514e5dcSNagaraju Goruganti #include "phosphor-ldap-config/utils.hpp"
29638afb9SPatrick Williams 
3d514e5dcSNagaraju Goruganti #include <ldap.h>
49638afb9SPatrick Williams #include <netinet/in.h>
59638afb9SPatrick Williams 
69638afb9SPatrick Williams #include <gtest/gtest.h>
7d514e5dcSNagaraju Goruganti 
8d514e5dcSNagaraju Goruganti namespace phosphor
9d514e5dcSNagaraju Goruganti {
10d514e5dcSNagaraju Goruganti namespace ldap
11d514e5dcSNagaraju Goruganti {
12*78d85042SNan Zhou constexpr auto ldapScheme = "ldap";
13*78d85042SNan Zhou constexpr auto ldapsScheme = "ldaps";
14d514e5dcSNagaraju Goruganti 
15d514e5dcSNagaraju Goruganti class TestUtil : public testing::Test
16d514e5dcSNagaraju Goruganti {
17d514e5dcSNagaraju Goruganti   public:
TestUtil()18d514e5dcSNagaraju Goruganti     TestUtil()
19d514e5dcSNagaraju Goruganti     {
20d514e5dcSNagaraju Goruganti         // Empty
21d514e5dcSNagaraju Goruganti     }
22d514e5dcSNagaraju Goruganti };
23d514e5dcSNagaraju Goruganti 
TEST_F(TestUtil,URIValidation)24d514e5dcSNagaraju Goruganti TEST_F(TestUtil, URIValidation)
25d514e5dcSNagaraju Goruganti {
26*78d85042SNan Zhou     std::string ipAddress = "ldap://0.0.0.0";
27*78d85042SNan Zhou     EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
28d514e5dcSNagaraju Goruganti 
29*78d85042SNan Zhou     ipAddress = "ldap://9.3.185.83";
30*78d85042SNan Zhou     EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
31d514e5dcSNagaraju Goruganti 
32*78d85042SNan Zhou     ipAddress = "ldaps://9.3.185.83";
33*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
34d514e5dcSNagaraju Goruganti 
35*78d85042SNan Zhou     ipAddress = "ldap://9.3.a.83";
36*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
37d514e5dcSNagaraju Goruganti 
38*78d85042SNan Zhou     ipAddress = "ldap://9.3.185.a";
39*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
40d514e5dcSNagaraju Goruganti 
41*78d85042SNan Zhou     ipAddress = "ldap://x.x.x.x";
42*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
43d514e5dcSNagaraju Goruganti 
44*78d85042SNan Zhou     ipAddress = "ldaps://0.0.0.0";
45*78d85042SNan Zhou     EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
46d514e5dcSNagaraju Goruganti 
47*78d85042SNan Zhou     ipAddress = "ldap://0.0.0.0";
48*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
49d514e5dcSNagaraju Goruganti 
50*78d85042SNan Zhou     ipAddress = "ldaps://9.3.185.83";
51*78d85042SNan Zhou     EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
52d514e5dcSNagaraju Goruganti 
53*78d85042SNan Zhou     ipAddress = "ldap://9.3.185.83";
54*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
55d514e5dcSNagaraju Goruganti 
56*78d85042SNan Zhou     ipAddress = "ldaps://9.3.185.83";
57*78d85042SNan Zhou     EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
58d514e5dcSNagaraju Goruganti 
59*78d85042SNan Zhou     ipAddress = "ldaps://9.3.185.a";
60*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
61d514e5dcSNagaraju Goruganti 
62*78d85042SNan Zhou     ipAddress = "ldaps://9.3.a.83";
63*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
64d514e5dcSNagaraju Goruganti 
65*78d85042SNan Zhou     ipAddress = "ldaps://x.x.x.x";
66*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapsScheme));
672f64e420SAsmitha Karunanithi 
68*78d85042SNan Zhou     ipAddress = "ldap://9.3.185.83:70000";
69*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
702f64e420SAsmitha Karunanithi 
71*78d85042SNan Zhou     ipAddress = "ldap://9.3.185.83:-3";
72*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
732f64e420SAsmitha Karunanithi 
74*78d85042SNan Zhou     ipAddress = "ldap://9.3.185.83:221";
75*78d85042SNan Zhou     EXPECT_EQ(true, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
762f64e420SAsmitha Karunanithi 
77*78d85042SNan Zhou     ipAddress = "ldap://9.3.185.83:0";
78*78d85042SNan Zhou     EXPECT_EQ(false, isValidLDAPURI(ipAddress.c_str(), ldapScheme));
79d514e5dcSNagaraju Goruganti }
80d514e5dcSNagaraju Goruganti } // namespace ldap
81d514e5dcSNagaraju Goruganti } // namespace phosphor
82