1--TEST--
2Programming errors (Value/Type errors) for ldap_add(_ext)(), ldap_mod_replace(_ext)(), ldap_mod_add(_ext)(), and ldap_mod_del(_ext)()
3--EXTENSIONS--
4ldap
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 = [];
23try {
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];
37try {
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];
50try {
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];
63try {
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];
76try {
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];
86try {
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];
100try {
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];
113try {
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];
126try {
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--EXPECT--
135ValueError: ldap_add(): Argument #3 ($entry) must not be empty
136ValueError: ldap_add(): Argument #3 ($entry) must be an associative array of attribute => values
137ValueError: ldap_add(): Argument #3 ($entry) key must not be empty
138ValueError: ldap_add(): Argument #3 ($entry) key must not contain any null bytes
139Error: Object of class stdClass could not be converted to string
140ValueError: ldap_add(): Argument #3 ($entry) list of attribute values must not be empty
141ValueError: ldap_add(): Argument #3 ($entry) must be a list of attribute values
142TypeError: LDAP value must be of type string|int|bool, array given
143Error: Object of class stdClass could not be converted to string
144