xref: /PHP-8.3/ext/snmp/tests/snmp2_set.phpt (revision b5a14e6c)
1--TEST--
2Function snmp2_set
3--CREDITS--
4Boris Lytochkin
5--EXTENSIONS--
6snmp
7--SKIPIF--
8<?php
9require_once(__DIR__.'/skipif.inc');
10?>
11--FILE--
12<?php
13require_once(__DIR__.'/snmp_include.inc');
14
15//EXPECTF format is quickprint OFF
16snmp_set_quick_print(false);
17snmp_set_valueretrieval(SNMP_VALUE_PLAIN);
18
19echo "Check error handing\n";
20echo "No type & no value (timeout & retries instead)\n";
21try {
22    $z = snmp2_set($hostname, $communityWrite, 'SNMPv2-MIB::sysLocation.0', $timeout, $retries);
23    var_dump($z);
24} catch (\ValueError $e) {
25    echo $e->getMessage() . \PHP_EOL;
26}
27
28echo "No value (timeout instead), retries instead of timeout\n";
29$z = snmp2_set($hostname, $communityWrite, 'SNMPv2-MIB::sysLocation.0', 'q', $timeout, $retries);
30var_dump($z);
31
32echo "Bogus OID\n";
33$z = snmp2_set($hostname, $communityWrite, '.1.3.6.777.888.999.444.0', 's', 'bbb', $timeout, $retries);
34var_dump($z);
35
36echo "Checking working\n";
37$oid1 = 'SNMPv2-MIB::sysContact.0';
38$oldvalue1 = snmpget($hostname, $communityWrite, $oid1, $timeout, $retries);
39$newvalue1 = $oldvalue1 . '0';
40$oid2 = 'SNMPv2-MIB::sysLocation.0';
41$oldvalue2 = snmpget($hostname, $communityWrite, $oid1, $timeout, $retries);
42$newvalue2 = $oldvalue2 . '0';
43
44echo "Single OID\n";
45$z = snmp2_set($hostname, $communityWrite, $oid1, 's', $newvalue1, $timeout, $retries);
46var_dump($z);
47var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $newvalue1));
48
49$z = snmp2_set($hostname, $communityWrite, $oid1, 's', $oldvalue1, $timeout, $retries);
50var_dump($z);
51var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
52
53echo "Multiple OID\n";
54$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('s','s'), array($newvalue1, $newvalue2), $timeout, $retries);
55var_dump($z);
56var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $newvalue1));
57var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $newvalue2));
58
59$z = snmp2_set($hostname, $communityWrite, '.1.3.6.777.888.999.444.0', 's', 'bbb', $timeout, $retries);
60var_dump($z);
61
62$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('s','s'), array($oldvalue1, $oldvalue2), $timeout, $retries);
63var_dump($z);
64var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
65var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
66
67echo "Multiple OID, single type & value\n";
68$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), 's', $newvalue1, $timeout, $retries);
69var_dump($z);
70var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $newvalue1));
71var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $newvalue1));
72
73$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('s','s'), array($oldvalue1, $oldvalue2), $timeout, $retries);
74var_dump($z);
75var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
76var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2 ));
77
78echo "Multiple OID, single type, multiple value\n";
79$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), 's', array($newvalue1, $newvalue2), $timeout, $retries);
80var_dump($z);
81var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $newvalue1));
82var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $newvalue2));
83
84$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('s','s'), array($oldvalue1, $oldvalue2), $timeout, $retries);
85var_dump($z);
86var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
87var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
88
89echo "More error handing\n";
90echo "Single OID, single type in array, single value\n";
91try {
92    $z = snmp2_set($hostname, $communityWrite, $oid1, array('s'), $newvalue1, $timeout, $retries);
93    var_dump($z);
94} catch (\TypeError $e) {
95    echo $e->getMessage() . \PHP_EOL;
96}
97var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
98var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
99
100echo "Single OID, single type, single value in array\n";
101try {
102    $z = snmp2_set($hostname, $communityWrite, $oid1, 's', array($newvalue1), $timeout, $retries);
103    var_dump($z);
104} catch (\TypeError $e) {
105    echo $e->getMessage() . \PHP_EOL;
106}
107var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
108var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
109
110echo "Multiple OID, 1st wrong type\n";
111try {
112    $z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('sw','s'), array($newvalue1, $newvalue2), $timeout, $retries);
113    var_dump($z);
114} catch (\ValueError $e) {
115    echo $e->getMessage() . \PHP_EOL;
116}
117var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
118var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
119
120echo "Multiple OID, 2nd wrong type\n";
121try {
122    $z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('s','sb'), array($newvalue1, $newvalue2), $timeout, $retries);
123    var_dump($z);
124} catch (\ValueError $e) {
125    echo $e->getMessage() . \PHP_EOL;
126}
127var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
128var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
129
130echo "Multiple OID, single type in array, multiple value\n";
131$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('s'), array($newvalue1, $newvalue2), $timeout, $retries);
132var_dump($z);
133var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
134var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
135
136echo "Multiple OID & type, single value in array\n";
137$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('s', 's'), array($newvalue1), $timeout, $retries);
138var_dump($z);
139var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
140var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
141
142echo "Multiple OID, 1st bogus, single type, multiple value\n";
143$z = snmp2_set($hostname, $communityWrite, array($oid1 . '44.55.66.77', $oid2), 's', array($newvalue1, $newvalue2), $timeout, $retries);
144var_dump($z);
145var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
146var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
147
148echo "Multiple OID, 2nd bogus, single type, multiple value\n";
149$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2 . '44.55.66.77'), 's', array($newvalue1, $newvalue2), $timeout, $retries);
150var_dump($z);
151var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
152var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
153
154echo "Multiple OID, single multiple type (1st bogus), multiple value\n";
155$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('q', 's'), array($newvalue1, $newvalue2), $timeout, $retries);
156var_dump($z);
157var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
158var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
159
160echo "Multiple OID, single multiple type (2nd bogus), multiple value\n";
161$z = snmp2_set($hostname, $communityWrite, array($oid1, $oid2), array('s', 'w'), array($newvalue1, $newvalue2), $timeout, $retries);
162var_dump($z);
163var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
164var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
165
166?>
167--EXPECTF--
168Check error handing
169No type & no value (timeout & retries instead)
170Type must be a single character
171No value (timeout instead), retries instead of timeout
172
173Warning: snmp2_set(): Could not add variable: OID='%s' type='q' value='%i': Bad variable type ("q") in %s on line %d
174bool(false)
175Bogus OID
176
177Warning: snmp2_set(): Error in packet at '%s': notWritable (That object does not support modification) in %s on line %d
178bool(false)
179Checking working
180Single OID
181bool(true)
182bool(true)
183bool(true)
184bool(true)
185Multiple OID
186bool(true)
187bool(true)
188bool(true)
189
190Warning: snmp2_set(): Error in packet at '%s': notWritable (That object does not support modification) in %s on line %d
191bool(false)
192bool(true)
193bool(true)
194bool(true)
195Multiple OID, single type & value
196bool(true)
197bool(true)
198bool(true)
199bool(true)
200bool(true)
201bool(true)
202Multiple OID, single type, multiple value
203bool(true)
204bool(true)
205bool(true)
206bool(true)
207bool(true)
208bool(true)
209More error handing
210Single OID, single type in array, single value
211Type must be of type string when object ID is a string
212bool(true)
213bool(true)
214Single OID, single type, single value in array
215Value must be of type string when object ID is a string
216bool(true)
217bool(true)
218Multiple OID, 1st wrong type
219Type must be a single character
220bool(true)
221bool(true)
222Multiple OID, 2nd wrong type
223Type must be a single character
224bool(true)
225bool(true)
226Multiple OID, single type in array, multiple value
227
228Warning: snmp2_set(): '%s': no type set in %s on line %d
229bool(false)
230bool(true)
231bool(true)
232Multiple OID & type, single value in array
233
234Warning: snmp2_set(): '%s': no value set in %s on line %d
235bool(false)
236bool(true)
237bool(true)
238Multiple OID, 1st bogus, single type, multiple value
239
240Warning: snmp2_set(): Error in packet at '%s': %rnoCreation|notWritable%r (%s) in %s on line %d
241bool(false)
242bool(true)
243bool(true)
244Multiple OID, 2nd bogus, single type, multiple value
245
246Warning: snmp2_set(): Error in packet at '%s': %rnoCreation|notWritable%r (%s) in %s on line %d
247bool(false)
248bool(true)
249bool(true)
250Multiple OID, single multiple type (1st bogus), multiple value
251
252Warning: snmp2_set(): Could not add variable: OID='%s' type='q' value='%s': Bad variable type ("q") in %s on line %d
253bool(false)
254bool(true)
255bool(true)
256Multiple OID, single multiple type (2nd bogus), multiple value
257
258Warning: snmp2_set(): Could not add variable: OID='%s' type='w' value='%s': Bad variable type ("w") in %s on line %d
259bool(false)
260bool(true)
261bool(true)
262