Skip to content

Commit 3a2a86b

Browse files
committed
ext/ldap: Add tests for php_ldap_do_modify()
And also amend some existing tests which would duplicate coverage
1 parent e609a21 commit 3a2a86b

7 files changed

+300
-90
lines changed

ext/ldap/tests/ldap_add_error.phpt

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,73 +13,55 @@ require "connect.inc";
1313

1414
$link = ldap_connect_and_bind($uri, $user, $passwd, $protocol_version);
1515

16-
var_dump(ldap_add($link, "$base", array()));
17-
1816
// Invalid DN
1917
var_dump(
20-
ldap_add($link, "weirdAttribute=val", array(
21-
"weirdAttribute" => "val",
22-
)),
18+
ldap_add(
19+
$link,
20+
"weirdAttribute=val",
21+
["weirdAttribute" => "val"],
22+
),
2323
ldap_error($link),
2424
ldap_errno($link)
2525
);
2626

2727
// Duplicate entry
28-
for ($i = 0; $i < 2; $i++)
28+
for ($i = 0; $i < 2; $i++) {
2929
var_dump(
30-
ldap_add($link, "dc=my-domain,$base", array(
31-
"objectClass" => array(
32-
"top",
33-
"dcObject",
34-
"organization"),
35-
"dc" => "my-domain",
36-
"o" => "my-domain",
37-
))
30+
ldap_add(
31+
$link,
32+
"dc=my-domain,$base",
33+
[
34+
"objectClass" => [
35+
"top",
36+
"dcObject",
37+
"organization",
38+
],
39+
"dc" => "my-domain",
40+
"o" => "my-domain",
41+
],
42+
)
3843
);
39-
var_dump(ldap_error($link), ldap_errno($link));
40-
41-
// Wrong array indexes
42-
try {
43-
ldap_add($link, "dc=my-domain2,dc=com", array(
44-
"objectClass" => array(
45-
0 => "top",
46-
2 => "dcObject",
47-
5 => "organization"),
48-
"dc" => "my-domain",
49-
"o" => "my-domain",
50-
));
51-
/* Is this correct behaviour to still have "Already exists" as error/errno?
52-
,
53-
ldap_error($link),
54-
ldap_errno($link)
55-
*/
56-
} catch (ValueError $exception) {
57-
echo $exception->getMessage() . "\n";
5844
}
45+
var_dump(ldap_error($link), ldap_errno($link));
5946

6047
// Invalid attribute
6148
var_dump(
62-
ldap_add($link, "$base", array(
63-
"objectClass" => array(
64-
"top",
65-
"dcObject",
66-
"organization"),
67-
"dc" => "my-domain",
68-
"o" => "my-domain",
69-
"weirdAttr" => "weirdVal",
70-
)),
71-
ldap_error($link),
72-
ldap_errno($link)
73-
);
74-
75-
var_dump(
76-
ldap_add($link, "$base", array(array( "Oops"
77-
)))
78-
/* Is this correct behaviour to still have "Undefined attribute type" as error/errno?
79-
,
49+
ldap_add(
50+
$link,
51+
"dc=my-domain,$base",
52+
[
53+
"objectClass" => [
54+
"top",
55+
"dcObject",
56+
"organization",
57+
],
58+
"dc" => "my-domain",
59+
"o" => "my-domain",
60+
"weirdAttr" => "weirdVal",
61+
],
62+
),
8063
ldap_error($link),
81-
ldap_errno($link)
82-
*/
64+
ldap_errno($link),
8365
);
8466
?>
8567
--CLEAN--
@@ -91,9 +73,6 @@ $link = ldap_connect_and_bind($uri, $user, $passwd, $protocol_version);
9173
ldap_delete($link, "dc=my-domain,$base");
9274
?>
9375
--EXPECTF--
94-
Warning: ldap_add(): Add: Protocol error in %s on line %d
95-
bool(false)
96-
9776
Warning: ldap_add(): Add: Invalid DN syntax in %s on line %d
9877
bool(false)
9978
string(17) "Invalid DN syntax"
@@ -104,12 +83,8 @@ Warning: ldap_add(): Add: Already exists in %s on line %d
10483
bool(false)
10584
string(14) "Already exists"
10685
int(68)
107-
ldap_add(): Argument #3 ($entry) must contain arrays with consecutive integer indices starting from 0
10886

10987
Warning: ldap_add(): Add: Undefined attribute type in %s on line %d
11088
bool(false)
11189
string(24) "Undefined attribute type"
11290
int(17)
113-
114-
Warning: ldap_add(): Unknown attribute in the data in %s on line %d
115-
bool(false)
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
--TEST--
2+
Programming errors (Value/Type errors) for ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)()
3+
--EXTENSIONS--
4+
ldap
5+
--FILE--
6+
<?php
7+
8+
/* ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)() share an underlying C function */
9+
/* We are assuming 3333 is not connectable */
10+
$ldap = ldap_connect('ldap://127.0.0.1:3333');
11+
$valid_dn = "cn=userA,something";
12+
13+
/* Taken from the existing ldap_add() PHP documentation */
14+
$valid_entries = [
15+
'attribute1' => 'value',
16+
'attribute2' => [
17+
'value1',
18+
'value2',
19+
],
20+
];
21+
22+
$empty_dict = [];
23+
try {
24+
var_dump(ldap_add($ldap, $valid_dn, $empty_dict));
25+
} catch (Throwable $e) {
26+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
27+
}
28+
29+
$not_dict_of_attributes = [
30+
'attribute1' => 'value',
31+
'not_key_entry',
32+
'attribute3' => [
33+
'value1',
34+
'value2',
35+
],
36+
];
37+
try {
38+
var_dump(ldap_add($ldap, $valid_dn, $not_dict_of_attributes));
39+
} catch (Throwable $e) {
40+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
41+
}
42+
43+
$dict_key_with_empty_key = [
44+
'attribute1' => 'value',
45+
'' => [
46+
'value1',
47+
'value2',
48+
],
49+
];
50+
try {
51+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_with_empty_key));
52+
} catch (Throwable $e) {
53+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
54+
}
55+
56+
$dict_key_with_nul_bytes = [
57+
"attrib1\0with\0nul\0byte" => 'value',
58+
"attrib2\0with\0nul\0byte" => [
59+
'value1',
60+
'value2',
61+
],
62+
];
63+
try {
64+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_with_nul_bytes));
65+
} catch (Throwable $e) {
66+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
67+
}
68+
69+
$dict_key_value_not_string = [
70+
'attribute1' => new stdClass(),
71+
'attribute2' => [
72+
'value1',
73+
'value2',
74+
],
75+
];
76+
try {
77+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_value_not_string));
78+
} catch (Throwable $e) {
79+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
80+
}
81+
82+
$dict_key_multi_value_empty_list = [
83+
'attribute1' => 'value',
84+
'attribute2' => [],
85+
];
86+
try {
87+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_multi_value_empty_list));
88+
} catch (Throwable $e) {
89+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
90+
}
91+
92+
$dict_key_multi_value_not_list = [
93+
'attribute1' => 'value',
94+
'attribute2' => [
95+
'value1',
96+
'wat' => 'nosense',
97+
'value2',
98+
],
99+
];
100+
try {
101+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_multi_value_not_list));
102+
} catch (Throwable $e) {
103+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
104+
}
105+
106+
$dict_key_multi_value_not_list_of_strings = [
107+
'attribute1' => 'value',
108+
'attribute2' => [
109+
'value1',
110+
[],
111+
],
112+
];
113+
try {
114+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_multi_value_not_list_of_strings));
115+
} catch (Throwable $e) {
116+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
117+
}
118+
119+
$dict_key_multi_value_not_list_of_strings2 = [
120+
'attribute1' => 'value',
121+
'attribute2' => [
122+
'value1',
123+
new stdClass(),
124+
],
125+
];
126+
try {
127+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_multi_value_not_list_of_strings2));
128+
} catch (Throwable $e) {
129+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
130+
}
131+
/* We don't check that values have nul bytes as the length of the string is passed to LDAP */
132+
133+
?>
134+
--EXPECTF--
135+
Warning: ldap_add(): Add: Can't contact LDAP server in %s on line %d
136+
bool(false)
137+
138+
Warning: ldap_add(): Unknown attribute in the data in %s on line %d
139+
bool(false)
140+
141+
Warning: ldap_add(): Add: Can't contact LDAP server in %s on line %d
142+
bool(false)
143+
144+
Warning: ldap_add(): Add: Can't contact LDAP server in %s on line %d
145+
bool(false)
146+
Error: Object of class stdClass could not be converted to string
147+
148+
Warning: ldap_add(): Add: Can't contact LDAP server in %s on line %d
149+
bool(false)
150+
ValueError: ldap_add(): Argument #3 ($entry) must contain arrays with consecutive integer indices starting from 0
151+
152+
Warning: Array to string conversion in %s on line %d
153+
154+
Warning: ldap_add(): Add: Can't contact LDAP server in %s on line %d
155+
bool(false)
156+
Error: Object of class stdClass could not be converted to string
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
--TEST--
2+
Programming errors (Value/Type errors) for ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)() with references
3+
--EXTENSIONS--
4+
ldap
5+
--FILE--
6+
<?php
7+
8+
/* ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)() share an underlying C function */
9+
/* We are assuming 3333 is not connectable */
10+
$ldap = ldap_connect('ldap://127.0.0.1:3333');
11+
$valid_dn = "cn=userA,something";
12+
13+
/* Taken from the existing ldap_add() PHP documentation */
14+
$valid_entries = [
15+
'attribute1' => 'value',
16+
'attribute2' => [
17+
'value1',
18+
'value2',
19+
],
20+
];
21+
22+
$obj = new stdClass();
23+
$dict_key_value_not_string = [
24+
'attribute1' => &$obj,
25+
'attribute2' => [
26+
'value1',
27+
'value2',
28+
],
29+
];
30+
try {
31+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_value_not_string));
32+
} catch (Throwable $e) {
33+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
34+
}
35+
36+
$empty_list = [];
37+
$dict_key_multi_value_empty_list = [
38+
'attribute1' => 'value',
39+
'attribute2' => &$empty_list,
40+
];
41+
try {
42+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_multi_value_empty_list));
43+
} catch (Throwable $e) {
44+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
45+
}
46+
47+
$empty_list = [];
48+
$dict_key_multi_value_not_list_of_strings = [
49+
'attribute1' => 'value',
50+
'attribute2' => [
51+
'value1',
52+
&$empty_list,
53+
],
54+
];
55+
try {
56+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_multi_value_not_list_of_strings));
57+
} catch (Throwable $e) {
58+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
59+
}
60+
61+
$obj = new stdClass();
62+
$dict_key_multi_value_not_list_of_strings2 = [
63+
'attribute1' => 'value',
64+
'attribute2' => [
65+
'value1',
66+
&$obj,
67+
],
68+
];
69+
try {
70+
var_dump(ldap_add($ldap, $valid_dn, $dict_key_multi_value_not_list_of_strings2));
71+
} catch (Throwable $e) {
72+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
73+
}
74+
/* We don't check that values have nul bytes as the length of the string is passed to LDAP */
75+
76+
?>
77+
--EXPECTF--
78+
Error: Object of class stdClass could not be converted to string
79+
80+
Warning: ldap_add(): Add: Can't contact LDAP server in %s on line %d
81+
bool(false)
82+
83+
Warning: Array to string conversion in %s on line %d
84+
85+
Warning: ldap_add(): Add: Can't contact LDAP server in %s on line %d
86+
bool(false)
87+
Error: Object of class stdClass could not be converted to string

0 commit comments

Comments
 (0)