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