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