xref: /PHP-8.4/ext/snmp/tests/snmpset.phpt (revision b5a14e6c)
1--TEST--
2Function snmpset
3--CREDITS--
4Olivier Doucet Olivier Doucet Boris 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 = snmpset($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 = snmpset($hostname, $communityWrite, 'SNMPv2-MIB::sysLocation.0', 'q', $timeout, $retries);
30var_dump($z);
31
32echo "Bogus OID\n";
33$z = snmpset($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 = snmpset($hostname, $communityWrite, $oid1, 's', $newvalue1, $timeout, $retries);
46var_dump($z);
47var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $newvalue1));
48$z = snmpset($hostname, $communityWrite, $oid1, 's', $oldvalue1, $timeout, $retries);
49var_dump($z);
50var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
51
52echo "Multiple OID\n";
53$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('s','s'), array($newvalue1, $newvalue2), $timeout, $retries);
54var_dump($z);
55var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $newvalue1));
56var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $newvalue2));
57$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('s','s'), array($oldvalue1, $oldvalue2), $timeout, $retries);
58var_dump($z);
59var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
60var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
61
62echo "Multiple OID, single type & value\n";
63$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), 's', $newvalue1, $timeout, $retries);
64var_dump($z);
65var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $newvalue1));
66var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $newvalue1));
67$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('s','s'), array($oldvalue1, $oldvalue2), $timeout, $retries);
68var_dump($z);
69var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
70var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
71
72echo "Multiple OID, single type, multiple value\n";
73$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), 's', array($newvalue1, $newvalue2), $timeout, $retries);
74var_dump($z);
75var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $newvalue1));
76var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $newvalue2));
77$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('s','s'), array($oldvalue1, $oldvalue2), $timeout, $retries);
78var_dump($z);
79var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
80var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
81
82
83echo "More error handing\n";
84echo "Single OID, single type in array, single value\n";
85try {
86    $z = snmpset($hostname, $communityWrite, $oid1, array('s'), $newvalue1, $timeout, $retries);
87    var_dump($z);
88} catch (\TypeError $e) {
89    echo $e->getMessage() . \PHP_EOL;
90}
91var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
92var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
93
94echo "Single OID, single type, single value in array\n";
95try {
96    $z = snmpset($hostname, $communityWrite, $oid1, 's', array($newvalue1), $timeout, $retries);
97    var_dump($z);
98} catch (\TypeError $e) {
99    echo $e->getMessage() . \PHP_EOL;
100}
101var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
102var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
103
104echo "Multiple OID, 1st wrong type\n";
105try {
106    $z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('sw','s'), array($newvalue1, $newvalue2), $timeout, $retries);
107    var_dump($z);
108} catch (\ValueError $e) {
109    echo $e->getMessage() . \PHP_EOL;
110}
111var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
112var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
113
114echo "Multiple OID, 2nd wrong type\n";
115try {
116    $z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('s','sb'), array($newvalue1, $newvalue2), $timeout, $retries);
117    var_dump($z);
118} catch (\ValueError $e) {
119    echo $e->getMessage() . \PHP_EOL;
120}
121var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
122var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
123
124echo "Multiple OID, single type in array, multiple value\n";
125$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('s'), array($newvalue1, $newvalue2), $timeout, $retries);
126var_dump($z);
127var_dump((snmpget($hostname, $communityWrite, $oid1, $timeout, $retries) === $oldvalue1));
128var_dump((snmpget($hostname, $communityWrite, $oid2, $timeout, $retries) === $oldvalue2));
129
130echo "Multiple OID & type, single value in array\n";
131$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('s', 's'), array($newvalue1), $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, 1st bogus, single type, multiple value\n";
137$z = snmpset($hostname, $communityWrite, array($oid1 . '44.55.66.77', $oid2), 's', array($newvalue1, $newvalue2), $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, 2nd bogus, single type, multiple value\n";
143$z = snmpset($hostname, $communityWrite, array($oid1, $oid2 . '44.55.66.77'), '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, single multiple type (1st bogus), multiple value\n";
149$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('q', '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 (2nd bogus), multiple value\n";
155$z = snmpset($hostname, $communityWrite, array($oid1, $oid2), array('s', 'w'), 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
160?>
161--EXPECTF--
162Check error handing
163No type & no value (timeout & retries instead)
164Type must be a single character
165No value (timeout instead), retries instead of timeout
166
167Warning: snmpset(): Could not add variable: OID='%s' type='q' value='%i': Bad variable type ("q") in %s on line %d
168bool(false)
169Bogus OID
170
171Warning: snmpset(): Error in packet at '%s': (noSuchName) There is no such variable name in this MIB. in %s on line %d
172bool(false)
173Checking working
174Single OID
175bool(true)
176bool(true)
177bool(true)
178bool(true)
179Multiple OID
180bool(true)
181bool(true)
182bool(true)
183bool(true)
184bool(true)
185bool(true)
186Multiple OID, single type & value
187bool(true)
188bool(true)
189bool(true)
190bool(true)
191bool(true)
192bool(true)
193Multiple OID, single type, multiple value
194bool(true)
195bool(true)
196bool(true)
197bool(true)
198bool(true)
199bool(true)
200More error handing
201Single OID, single type in array, single value
202Type must be of type string when object ID is a string
203bool(true)
204bool(true)
205Single OID, single type, single value in array
206Value must be of type string when object ID is a string
207bool(true)
208bool(true)
209Multiple OID, 1st wrong type
210Type must be a single character
211bool(true)
212bool(true)
213Multiple OID, 2nd wrong type
214Type must be a single character
215bool(true)
216bool(true)
217Multiple OID, single type in array, multiple value
218
219Warning: snmpset(): '%s': no type set in %s on line %d
220bool(false)
221bool(true)
222bool(true)
223Multiple OID & type, single value in array
224
225Warning: snmpset(): '%s': no value set in %s on line %d
226bool(false)
227bool(true)
228bool(true)
229Multiple OID, 1st bogus, single type, multiple value
230
231Warning: snmpset(): Error in packet at '%s': (noSuchName) There is no such variable name in this MIB. in %s on line %d
232bool(false)
233bool(true)
234bool(true)
235Multiple OID, 2nd bogus, single type, multiple value
236
237Warning: snmpset(): Error in packet at '%s': (noSuchName) There is no such variable name in this MIB. in %s on line %d
238bool(false)
239bool(true)
240bool(true)
241Multiple OID, single multiple type (1st bogus), multiple value
242
243Warning: snmpset(): Could not add variable: OID='%s' type='q' value='%s': Bad variable type ("q") in %s on line %d
244bool(false)
245bool(true)
246bool(true)
247Multiple OID, single multiple type (2nd bogus), multiple value
248
249Warning: snmpset(): Could not add variable: OID='%s' type='w' value='%s': Bad variable type ("w") in %s on line %d
250bool(false)
251bool(true)
252bool(true)
253