net-snmp 3.19.1 → 3.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/index.js +50 -9
- package/package.json +1 -1
- package/test/cast-set-value.test.js +200 -0
package/README.md
CHANGED
@@ -3469,6 +3469,14 @@ Example programs are included under the module's `example` directory.
|
|
3469
3469
|
|
3470
3470
|
* Fix integer constraints check
|
3471
3471
|
|
3472
|
+
# Version 3.19.2 - 06/03/2025
|
3473
|
+
|
3474
|
+
* Fix integer and string constraints check on cast
|
3475
|
+
|
3476
|
+
# Version 3.20.0 - 06/03/2025
|
3477
|
+
|
3478
|
+
* Fix set value for counter, gauge and unsigned integer types
|
3479
|
+
|
3472
3480
|
# License
|
3473
3481
|
|
3474
3482
|
Copyright (c) 2020 Mark Abrahams <mark@abrahams.co.nz>
|
package/index.js
CHANGED
@@ -15,6 +15,7 @@ var MIN_SIGNED_INT32 = -2147483648;
|
|
15
15
|
var MAX_SIGNED_INT32 = 2147483647;
|
16
16
|
var MIN_UNSIGNED_INT32 = 0;
|
17
17
|
var MAX_UNSIGNED_INT32 = 4294967295;
|
18
|
+
var MAX_UNSIGNED_INT64 = 18446744073709551615;
|
18
19
|
|
19
20
|
function debug (line) {
|
20
21
|
if ( DEBUG ) {
|
@@ -604,17 +605,17 @@ ObjectTypeUtil.castSetValue = function (type, value, constraints) {
|
|
604
605
|
if ( isNaN(parsedValue) ) {
|
605
606
|
throw new Error("Invalid Integer", value);
|
606
607
|
}
|
607
|
-
if ( constraints && ObjectTypeUtil.doesIntegerMeetConstraints (parsedValue, constraints) ) {
|
608
|
+
if ( constraints && ! ObjectTypeUtil.doesIntegerMeetConstraints (parsedValue, constraints) ) {
|
608
609
|
throw new Error("Integer does not meet constraints", value);
|
609
610
|
}
|
610
611
|
return parsedValue;
|
611
612
|
}
|
612
613
|
|
613
614
|
case ObjectType.OctetString: {
|
614
|
-
if ( ! value instanceof Buffer
|
615
|
+
if ( ! ( value instanceof Buffer || typeof value == "string" ) ) {
|
615
616
|
throw new Error("Invalid OctetString", value);
|
616
617
|
}
|
617
|
-
if ( constraints && ObjectTypeUtil.doesStringMeetConstraints (value, constraints) ) {
|
618
|
+
if ( constraints && ! ObjectTypeUtil.doesStringMeetConstraints (value, constraints) ) {
|
618
619
|
throw new Error("OctetString does not meet constraints", value);
|
619
620
|
}
|
620
621
|
if ( value instanceof Buffer ) {
|
@@ -635,19 +636,59 @@ ObjectTypeUtil.castSetValue = function (type, value, constraints) {
|
|
635
636
|
case ObjectType.Counter32:
|
636
637
|
case ObjectType.Gauge:
|
637
638
|
case ObjectType.Gauge32:
|
638
|
-
case ObjectType.Unsigned32:
|
639
|
-
case ObjectType.Counter64: {
|
639
|
+
case ObjectType.Unsigned32: {
|
640
640
|
// Counters should be initialized to 0 (RFC2578, end of section 7.9)
|
641
641
|
// We'll do so.
|
642
|
-
return 0;
|
642
|
+
// return 0;
|
643
|
+
// That ^^^ was fine when castSetValue was used only for DEFVAL
|
644
|
+
// But now it's used in other set value scenarios
|
645
|
+
// So we need to cast the given value to a whole number
|
646
|
+
const parsedValue = parseInt(value, 10);
|
647
|
+
if ( isNaN(parsedValue) ) {
|
648
|
+
throw new Error(`Invalid Integer for ${type}`, value);
|
649
|
+
}
|
650
|
+
if ( parsedValue < 0 ) {
|
651
|
+
throw new Error(`Integer is negative for ${type}`, value);
|
652
|
+
}
|
653
|
+
if ( parsedValue > MAX_UNSIGNED_INT32 ) {
|
654
|
+
throw new Error(`Integer is greater than max unsigned int32 for ${type}`, value);
|
655
|
+
}
|
656
|
+
return parsedValue;
|
657
|
+
}
|
658
|
+
|
659
|
+
case ObjectType.Counter64: {
|
660
|
+
if ( value instanceof Buffer ) {
|
661
|
+
if ( value.length !== 8 ) {
|
662
|
+
throw new Error(`Counter64 buffer is not 8 bytes`, value);
|
663
|
+
}
|
664
|
+
return value;
|
665
|
+
}
|
666
|
+
const parsedValue = parseInt(value, 10);
|
667
|
+
if ( isNaN(parsedValue) ) {
|
668
|
+
throw new Error(`Invalid Integer for Counter64`, value);
|
669
|
+
}
|
670
|
+
if ( parsedValue < 0 ) {
|
671
|
+
throw new Error(`Integer is negative for Counter64`, value);
|
672
|
+
}
|
673
|
+
if ( parsedValue > MAX_UNSIGNED_INT64 ) {
|
674
|
+
throw new Error(`Integer is greater than max unsigned int64 for Counter64`, value);
|
675
|
+
}
|
676
|
+
return parsedValue;
|
643
677
|
}
|
644
678
|
|
645
679
|
case ObjectType.IpAddress: {
|
646
|
-
|
647
|
-
|
648
|
-
if ( typeof value != "string" || bytes.length != 4 ) {
|
680
|
+
const octets = value.split (".");
|
681
|
+
if ( typeof value != "string" || octets.length != 4 ) {
|
649
682
|
throw new Error("Invalid IpAddress", value);
|
650
683
|
}
|
684
|
+
for ( const octet of octets ) {
|
685
|
+
if ( isNaN (octet) ) {
|
686
|
+
throw new Error("Invalid IpAddress", value);
|
687
|
+
}
|
688
|
+
if ( parseInt (octet) < 0 || parseInt (octet) > 255) {
|
689
|
+
throw new Error("Invalid IpAddress", value);
|
690
|
+
}
|
691
|
+
}
|
651
692
|
return value;
|
652
693
|
}
|
653
694
|
|
package/package.json
CHANGED
@@ -0,0 +1,200 @@
|
|
1
|
+
const assert = require('assert');
|
2
|
+
const snmp = require('..');
|
3
|
+
|
4
|
+
describe('Cast Set Value', function() {
|
5
|
+
|
6
|
+
describe('Boolean', function() {
|
7
|
+
it('casts truthy values to true', function() {
|
8
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Boolean, 1), true);
|
9
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Boolean, "true"), true);
|
10
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Boolean, {}), true);
|
11
|
+
});
|
12
|
+
|
13
|
+
it('casts falsy values to false', function() {
|
14
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Boolean, 0), false);
|
15
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Boolean, ""), false);
|
16
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Boolean, null), false);
|
17
|
+
});
|
18
|
+
});
|
19
|
+
|
20
|
+
describe('Integer', function() {
|
21
|
+
it('accepts valid integers', function() {
|
22
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, 42), 42);
|
23
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, -42), -42);
|
24
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, "42"), 42);
|
25
|
+
});
|
26
|
+
|
27
|
+
it('throws on invalid integers', function() {
|
28
|
+
assert.throws(() => {
|
29
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, "not a number");
|
30
|
+
}, /Invalid Integer/);
|
31
|
+
|
32
|
+
assert.throws(() => {
|
33
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, {});
|
34
|
+
}, /Invalid Integer/);
|
35
|
+
});
|
36
|
+
|
37
|
+
it('respects enumeration constraints', function() {
|
38
|
+
const constraints = {
|
39
|
+
enumeration: {
|
40
|
+
1: 'one',
|
41
|
+
2: 'two'
|
42
|
+
}
|
43
|
+
};
|
44
|
+
|
45
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, 1, constraints), 1);
|
46
|
+
|
47
|
+
assert.throws(() => {
|
48
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, 3, constraints);
|
49
|
+
}, /Integer does not meet constraints/);
|
50
|
+
});
|
51
|
+
|
52
|
+
it('respects range constraints', function() {
|
53
|
+
const constraints = {
|
54
|
+
ranges: [
|
55
|
+
{ min: 0, max: 10 }
|
56
|
+
]
|
57
|
+
};
|
58
|
+
|
59
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, 5, constraints), 5);
|
60
|
+
|
61
|
+
assert.throws(() => {
|
62
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Integer, 11, constraints);
|
63
|
+
}, /Integer does not meet constraints/);
|
64
|
+
});
|
65
|
+
});
|
66
|
+
|
67
|
+
describe('OctetString', function() {
|
68
|
+
it('accepts strings', function() {
|
69
|
+
assert.strictEqual(
|
70
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.OctetString, "test string"),
|
71
|
+
"test string"
|
72
|
+
);
|
73
|
+
});
|
74
|
+
|
75
|
+
it('converts buffers to strings', function() {
|
76
|
+
const buf = Buffer.from("test buffer");
|
77
|
+
assert.strictEqual(
|
78
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.OctetString, buf),
|
79
|
+
buf.toString()
|
80
|
+
);
|
81
|
+
});
|
82
|
+
|
83
|
+
it('throws on invalid types', function() {
|
84
|
+
assert.throws(() => {
|
85
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.OctetString, 123);
|
86
|
+
}, /Invalid OctetString/);
|
87
|
+
});
|
88
|
+
|
89
|
+
it('respects size constraints', function() {
|
90
|
+
const constraints = {
|
91
|
+
sizes: [
|
92
|
+
{ min: 2, max: 5 }
|
93
|
+
]
|
94
|
+
};
|
95
|
+
|
96
|
+
assert.strictEqual(
|
97
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.OctetString, "123", constraints),
|
98
|
+
"123"
|
99
|
+
);
|
100
|
+
|
101
|
+
assert.throws(() => {
|
102
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.OctetString, "too long", constraints);
|
103
|
+
}, /OctetString does not meet constraints/);
|
104
|
+
});
|
105
|
+
});
|
106
|
+
|
107
|
+
describe('OID', function() {
|
108
|
+
it('accepts valid OIDs', function() {
|
109
|
+
assert.strictEqual(
|
110
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.OID, "1.3.6.1.2.1"),
|
111
|
+
"1.3.6.1.2.1"
|
112
|
+
);
|
113
|
+
});
|
114
|
+
|
115
|
+
it('throws on invalid OIDs', function() {
|
116
|
+
assert.throws(() => {
|
117
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.OID, "not.an.oid");
|
118
|
+
}, /Invalid OID/);
|
119
|
+
|
120
|
+
assert.throws(() => {
|
121
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.OID, "1.2.3.");
|
122
|
+
}, /Invalid OID/);
|
123
|
+
});
|
124
|
+
});
|
125
|
+
|
126
|
+
describe('Counter/Gauge/Unsigned32', function() {
|
127
|
+
const types = [
|
128
|
+
snmp.ObjectType.Counter,
|
129
|
+
snmp.ObjectType.Counter32,
|
130
|
+
snmp.ObjectType.Gauge,
|
131
|
+
snmp.ObjectType.Gauge32,
|
132
|
+
snmp.ObjectType.Unsigned32
|
133
|
+
];
|
134
|
+
|
135
|
+
types.forEach(type => {
|
136
|
+
it(`accepts valid unsigned integers for ${type}`, function() {
|
137
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(type, 42), 42);
|
138
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(type, "42"), 42);
|
139
|
+
});
|
140
|
+
|
141
|
+
it(`throws on negative numbers for ${type}`, function() {
|
142
|
+
assert.throws(() => {
|
143
|
+
snmp.ObjectTypeUtil.castSetValue(type, -1);
|
144
|
+
}, /Integer is negative/);
|
145
|
+
});
|
146
|
+
|
147
|
+
it(`throws on values exceeding unsigned 32-bit max for ${type}`, function() {
|
148
|
+
assert.throws(() => {
|
149
|
+
snmp.ObjectTypeUtil.castSetValue(type, 4294967296); // MAX_UNSIGNED_INT32 + 1
|
150
|
+
}, /Integer is greater than max unsigned int32/);
|
151
|
+
});
|
152
|
+
});
|
153
|
+
});
|
154
|
+
|
155
|
+
describe('Counter64', function() {
|
156
|
+
it('accepts valid unsigned integers', function() {
|
157
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Counter64, 42), 42);
|
158
|
+
assert.strictEqual(snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Counter64, "42"), 42);
|
159
|
+
});
|
160
|
+
|
161
|
+
it('accepts valid 8-byte buffers', function() {
|
162
|
+
const buf = Buffer.alloc(8);
|
163
|
+
assert.strictEqual(
|
164
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Counter64, buf),
|
165
|
+
buf
|
166
|
+
);
|
167
|
+
});
|
168
|
+
|
169
|
+
it('throws on invalid buffer length', function() {
|
170
|
+
assert.throws(() => {
|
171
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Counter64, Buffer.alloc(7));
|
172
|
+
}, /Counter64 buffer is not 8 bytes/);
|
173
|
+
});
|
174
|
+
|
175
|
+
it('throws on negative numbers', function() {
|
176
|
+
assert.throws(() => {
|
177
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.Counter64, -1);
|
178
|
+
}, /Integer is negative for Counter64/);
|
179
|
+
});
|
180
|
+
});
|
181
|
+
|
182
|
+
describe('IpAddress', function() {
|
183
|
+
it('accepts valid IP addresses', function() {
|
184
|
+
assert.strictEqual(
|
185
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.IpAddress, "192.168.1.1"),
|
186
|
+
"192.168.1.1"
|
187
|
+
);
|
188
|
+
});
|
189
|
+
|
190
|
+
it('throws on invalid IP addresses', function() {
|
191
|
+
assert.throws(() => {
|
192
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.IpAddress, "not.an.ip.address");
|
193
|
+
}, /Invalid IpAddress/);
|
194
|
+
|
195
|
+
assert.throws(() => {
|
196
|
+
snmp.ObjectTypeUtil.castSetValue(snmp.ObjectType.IpAddress, "192.168.1");
|
197
|
+
}, /Invalid IpAddress/);
|
198
|
+
});
|
199
|
+
});
|
200
|
+
});
|