net-snmp 3.15.3 → 3.16.1

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.
@@ -15,7 +15,7 @@
15
15
  "args": [
16
16
  "-v", "2c",
17
17
  "-c", "public",
18
- "-s", "5678",
18
+ "-s", "1162",
19
19
  "127.0.0.1",
20
20
  "1.3.6.1.2.1.31.1.1.1.6.1"
21
21
  ]
package/README.md CHANGED
@@ -3428,6 +3428,14 @@ Example programs are included under the module's `example` directory.
3428
3428
 
3429
3429
  * Fix agent SNMP SetRequest for OIDs
3430
3430
 
3431
+ ## Version 3.16.0 - 23/12/2024
3432
+
3433
+ * Add MIB object validation to set/add MIB API calls
3434
+
3435
+ ## Version 3.16.1 - 01/01/2025
3436
+
3437
+ * Fix loading of MIB directory paths with periods
3438
+
3431
3439
  # License
3432
3440
 
3433
3441
  Copyright (c) 2020 Mark Abrahams <mark@abrahams.co.nz>
@@ -57,15 +57,15 @@ var ifEntryRow1 = mib.getTableRowCells ("ifEntry", [2]);
57
57
 
58
58
  // ifXEntry
59
59
  // AUGMENTS ifEntry - meaning a single integer foreign key
60
- mib.addTableRow ("ifXEntry", [1, "eth0", 10, 2, 20, 4, counter64(1000), counter64(100), counter64(50), counter64(20),
61
- counter64(2000), counter64(200), counter64(100), counter64(40), 1, 1000, 0, 1, "myeth0", 10]);
62
- var ifXEntryRow1 = mib.getTableRowCells ("ifXEntry", [1]);
60
+ // mib.addTableRow ("ifXEntry", [1, "eth0", 10, 2, 20, 4, counter64(1000), counter64(100), counter64(50), counter64(20),
61
+ // counter64(2000), counter64(200), counter64(100), counter64(40), 1, 1000, 0, 1, "myeth0", 10]);
62
+ // var ifXEntryRow1 = mib.getTableRowCells ("ifXEntry", [1]);
63
63
  // console.log (ifXEntryRow1);
64
- var ifXEntryData1 = mib.getTableCells ("ifXEntry", false, false);
65
- var ifXEntryData2 = mib.getTableCells ("ifXEntry", true, false);
66
- var ifXEntryData3 = mib.getTableCells ("ifXEntry", false, true);
67
- var ifXEntryData4 = mib.getTableCells ("ifXEntry", true, true);
68
- mib.setTableSingleCell ("ifXEntry", 1, [1], "another0");
64
+ // var ifXEntryData1 = mib.getTableCells ("ifXEntry", false, false);
65
+ // var ifXEntryData2 = mib.getTableCells ("ifXEntry", true, false);
66
+ // var ifXEntryData3 = mib.getTableCells ("ifXEntry", false, true);
67
+ // var ifXEntryData4 = mib.getTableCells ("ifXEntry", true, true);
68
+ // mib.setTableSingleCell ("ifXEntry", 1, [1], "another0");
69
69
 
70
70
  // ifStackEntry
71
71
  // Composite index - two local columns
@@ -61,6 +61,9 @@ if ( command.includes('snmp-set') ) {
61
61
  type: snmp.ObjectType[options._[2]],
62
62
  value: options._[3]
63
63
  }];
64
+ if ( varbinds[0].type == snmp.ObjectType.Integer ) {
65
+ varbinds[0].value = Number(varbinds[0].value);
66
+ }
64
67
  } else if ( command.includes('snmp-table-columns') ) {
65
68
  oids = [options._[1]];
66
69
  columns = [];
package/index.js CHANGED
@@ -585,6 +585,107 @@ function writeVarbinds (buffer, varbinds) {
585
585
  buffer.endSequence ();
586
586
  }
587
587
 
588
+ const ObjectTypeUtil = {};
589
+
590
+ ObjectTypeUtil.castSetValue = function (type, value) {
591
+
592
+ switch (type) {
593
+ case ObjectType.Boolean:
594
+ return !! value;
595
+
596
+ case ObjectType.Integer:
597
+ if ( typeof value != "number" && typeof value != "string" ) {
598
+ throw new Error("Invalid Integer", value);
599
+ }
600
+ return typeof value == "number" ? value : parseInt(value, 10);
601
+
602
+ case ObjectType.OctetString:
603
+ if ( value instanceof Buffer) {
604
+ return value.toString();
605
+ } else if ( typeof value != "string" ) {
606
+ throw new Error("Invalid OctetString", value);
607
+ } else {
608
+ return value;
609
+ }
610
+
611
+ case ObjectType.OID:
612
+ if ( typeof value != "string" || ! value.match(/^([0-9]+)(\.[0-9]+)+$/) ) {
613
+ throw new Error("Invalid OID", value);
614
+ }
615
+ return value;
616
+
617
+ case ObjectType.Counter:
618
+ case ObjectType.Counter64:
619
+ // Counters should be initialized to 0 (RFC2578, end of section 7.9)
620
+ // We'll do so.
621
+ return 0;
622
+
623
+ case ObjectType.IpAddress:
624
+ // A 32-bit internet address represented as OCTET STRING of length 4
625
+ var bytes = value.split(".");
626
+ if ( typeof value != "string" || bytes.length != 4 ) {
627
+ throw new Error("Invalid IpAddress", value);
628
+ }
629
+ return value;
630
+
631
+ default :
632
+ // Assume the caller knows what he's doing
633
+ return value;
634
+ }
635
+
636
+ };
637
+
638
+ ObjectTypeUtil.isValid = function (type, value) {
639
+
640
+ switch (type) {
641
+ case ObjectType.Boolean: {
642
+ return typeof value == "boolean";
643
+ }
644
+ case ObjectType.Integer: {
645
+ // Allow strings that can be parsed as integers
646
+ const parsed = Number(value);
647
+ return ! isNaN (parsed) && Number.isInteger (parsed) && parsed >= MIN_SIGNED_INT32 && parsed <= MAX_SIGNED_INT32;
648
+ }
649
+ case ObjectType.OctetString: {
650
+ // Allow string or buffer
651
+ return typeof value == "string" || value instanceof Buffer;
652
+ }
653
+ case ObjectType.OID: {
654
+ return typeof value == "string" && value.match (/^([0-9]+)(\.[0-9]+)+$/);
655
+ }
656
+ case ObjectType.Counter: {
657
+ // Allow strings that can be parsed as integers
658
+ const parsed = Number(value);
659
+ return ! isNaN (parsed) && Number.isInteger (parsed) && parsed >= 0 && parsed <= MAX_UNSIGNED_INT32;
660
+ }
661
+ case ObjectType.Counter64: {
662
+ // Allow strings that can be parsed as integers
663
+ const parsed = Number(value);
664
+ return ! isNaN (parsed) && Number.isInteger (parsed) && parsed >= 0;
665
+ }
666
+ case ObjectType.IpAddress: {
667
+ const octets = value.split(".");
668
+ if ( octets.length !== 4 ) {
669
+ return false;
670
+ }
671
+ for ( const octet of octets ) {
672
+ if ( isNaN (octet) ) {
673
+ return false;
674
+ }
675
+ if ( parseInt (octet) < 0 || parseInt (octet) > 255) {
676
+ return false;
677
+ }
678
+ }
679
+ return true;
680
+ }
681
+ // return true for all other types until we are certain all object types are covered with specific rules
682
+ default: {
683
+ return true;
684
+ }
685
+ }
686
+
687
+ };
688
+
588
689
  /*****************************************************************************
589
690
  ** PDU class definitions
590
691
  **/
@@ -3966,14 +4067,8 @@ Mib.prototype.getProviderNodeForInstance = function (instanceNode) {
3966
4067
  };
3967
4068
 
3968
4069
  Mib.prototype.addProviderToNode = function (provider) {
3969
- var node = this.addNodesForOid (provider.oid);
3970
-
4070
+ const node = this.addNodesForOid (provider.oid);
3971
4071
  node.provider = provider;
3972
- if ( provider.type == MibProviderType.Table ) {
3973
- if ( ! provider.tableIndex ) {
3974
- provider.tableIndex = [1];
3975
- }
3976
- }
3977
4072
  this.providerNodes[provider.name] = node;
3978
4073
  return node;
3979
4074
  };
@@ -4183,8 +4278,11 @@ Mib.prototype.setScalarValue = function (scalarName, newValue) {
4183
4278
  instanceNode = this.lookup (instanceAddress);
4184
4279
  instanceNode.valueType = provider.scalarType;
4185
4280
  }
4281
+ const isValidValue = ObjectTypeUtil.isValid(instanceNode.valueType, newValue);
4282
+ if ( ! isValidValue ) {
4283
+ throw new TypeError(`Invalid value for ${scalarName} of type ${instanceNode.valueType}: ${newValue}`);
4284
+ }
4186
4285
  instanceNode.value = newValue;
4187
- // return instanceNode.setValue (newValue);
4188
4286
  };
4189
4287
 
4190
4288
  Mib.prototype.getProviderNodeForTable = function (table) {
@@ -4312,6 +4410,23 @@ Mib.prototype.getTableRowInstanceFromRowIndex = function (provider, rowIndex) {
4312
4410
  return rowIndexOid;
4313
4411
  };
4314
4412
 
4413
+ Mib.prototype.validateTableRow = function (table, row) {
4414
+ const provider = this.providers[table];
4415
+ const tableIndex = provider.tableIndex;
4416
+ const foreignIndexOffset = tableIndex.filter ( indexPart => indexPart.foreign ).length;
4417
+ for ( let i = 0; i < provider.tableColumns.length ; i++ ) {
4418
+ const column = provider.tableColumns[i];
4419
+ const isColumnIndex = tableIndex.some ( indexPart => indexPart.columnNumber == column.number );
4420
+ if ( ! isColumnIndex || ! (column.maxAccess === MaxAccess['not-accessible'] || column.maxAccess === MaxAccess['accessible-for-notify']) ) {
4421
+ const rowValueIndex = foreignIndexOffset + i;
4422
+ const isValidValue = ObjectTypeUtil.isValid(column.type, row[rowValueIndex]);
4423
+ if ( ! isValidValue ) {
4424
+ throw new TypeError(`Invalid value for ${table} column ${column.name} (index ${rowValueIndex}): ${row[rowValueIndex]} (in row [${row}])`);
4425
+ }
4426
+ }
4427
+ }
4428
+ };
4429
+
4315
4430
  Mib.prototype.addTableRow = function (table, row) {
4316
4431
  var providerNode;
4317
4432
  var provider;
@@ -4320,7 +4435,11 @@ Mib.prototype.addTableRow = function (table, row) {
4320
4435
  var instanceNode;
4321
4436
  var rowValueOffset;
4322
4437
 
4323
- if ( this.providers[table] && ! this.providerNodes[table] ) {
4438
+ if ( ! this.providers[table] ) {
4439
+ throw new ReferenceError ("Provider " + table + " not registered with this MIB");
4440
+ }
4441
+ this.validateTableRow (table, row);
4442
+ if ( ! this.providerNodes[table] ) {
4324
4443
  this.addProviderToNode (this.providers[table]);
4325
4444
  }
4326
4445
  providerNode = this.getProviderNodeForTable (table);
@@ -4450,19 +4569,16 @@ Mib.prototype.getTableSingleCell = function (table, columnNumber, rowIndex) {
4450
4569
  };
4451
4570
 
4452
4571
  Mib.prototype.setTableSingleCell = function (table, columnNumber, rowIndex, value) {
4453
- var provider;
4454
- var providerNode;
4455
- var columnNode;
4456
- var instanceNode;
4457
- var instanceAddress;
4458
-
4459
- provider = this.providers[table];
4460
- providerNode = this.getProviderNodeForTable (table);
4461
- instanceAddress = this.getTableRowInstanceFromRowIndex (provider, rowIndex);
4462
- columnNode = providerNode.children[columnNumber];
4463
- instanceNode = columnNode.getInstanceNodeForTableRowIndex (instanceAddress);
4572
+ const provider = this.providers[table];
4573
+ const providerNode = this.getProviderNodeForTable (table);
4574
+ const instanceAddress = this.getTableRowInstanceFromRowIndex (provider, rowIndex);
4575
+ const columnNode = providerNode.children[columnNumber];
4576
+ const instanceNode = columnNode.getInstanceNodeForTableRowIndex (instanceAddress);
4577
+ const isValidValue = ObjectTypeUtil.isValid(instanceNode.valueType, value);
4578
+ if ( ! isValidValue ) {
4579
+ throw new TypeError(`Invalid value for ${table} column ${columnNumber} of type ${instanceNode.valueType}: ${value}`);
4580
+ }
4464
4581
  instanceNode.value = value;
4465
- // return instanceNode.setValue (value);
4466
4582
  };
4467
4583
 
4468
4584
  Mib.prototype.deleteTableRow = function (table, rowIndex) {
@@ -4742,53 +4858,6 @@ Agent.prototype.onMsg = function (socket, buffer, rinfo) {
4742
4858
  }
4743
4859
  };
4744
4860
 
4745
- Agent.prototype.castSetValue = function ( type, value ) {
4746
- switch (type) {
4747
- case ObjectType.Boolean:
4748
- return !! value;
4749
-
4750
- case ObjectType.Integer:
4751
- if ( typeof value != "number" && typeof value != "string" ) {
4752
- throw new Error("Invalid Integer", value);
4753
- }
4754
- return typeof value == "number" ? value : parseInt(value, 10);
4755
-
4756
- case ObjectType.OctetString:
4757
- if ( value instanceof Buffer) {
4758
- return value.toString();
4759
- } else if ( typeof value != "string" ) {
4760
- throw new Error("Invalid OctetString", value);
4761
- } else {
4762
- return value;
4763
- }
4764
-
4765
- case ObjectType.OID:
4766
- if ( typeof value != "string" || ! value.match(/^([0-9]+)(\.[0-9]+)+$/) ) {
4767
- throw new Error("Invalid OID", value);
4768
- }
4769
- return value;
4770
-
4771
- case ObjectType.Counter:
4772
- case ObjectType.Counter64:
4773
- // Counters should be initialized to 0 (RFC2578, end of section 7.9)
4774
- // We'll do so.
4775
- return 0;
4776
-
4777
- case ObjectType.IpAddress:
4778
- // A 32-bit internet address represented as OCTET STRING of length 4
4779
- var bytes = value.split(".");
4780
- if ( typeof value != "string" || bytes.length != 4 ) {
4781
- throw new Error("Invalid IpAddress", value);
4782
- }
4783
- return value;
4784
-
4785
- default :
4786
- // Assume the caller knows what he's doing
4787
- return value;
4788
- }
4789
- };
4790
-
4791
-
4792
4861
  Agent.prototype.tryCreateInstance = function (varbind, requestType) {
4793
4862
  var row;
4794
4863
  var column;
@@ -4841,7 +4910,7 @@ Agent.prototype.tryCreateInstance = function (varbind, requestType) {
4841
4910
  }
4842
4911
 
4843
4912
  // Ensure the value is of the correct type, and save it
4844
- value = this.castSetValue ( provider.scalarType, value );
4913
+ value = ObjectTypeUtil.castSetValue ( provider.scalarType, value );
4845
4914
  this.mib.setScalarValue ( provider.name, value );
4846
4915
 
4847
4916
  // Now there should be an instanceNode available.
@@ -4901,7 +4970,7 @@ Agent.prototype.tryCreateInstance = function (varbind, requestType) {
4901
4970
  }
4902
4971
 
4903
4972
  // Map each column's value to the appropriate type
4904
- value = value.map( (v, i) => this.castSetValue ( provider.tableColumns[i].type, v ) );
4973
+ value = value.map( (v, i) => ObjectTypeUtil.castSetValue ( provider.tableColumns[i].type, v ) );
4905
4974
 
4906
4975
  // Add the table row
4907
4976
  this.mib.addTableRow ( provider.name, value );
@@ -5078,7 +5147,7 @@ Agent.prototype.request = function (socket, requestMessage, rinfo) {
5078
5147
  });
5079
5148
  };
5080
5149
 
5081
- requestPdu.varbinds[i].requestValue = this.castSetValue (requestPdu.varbinds[i].type, requestPdu.varbinds[i].value);
5150
+ requestPdu.varbinds[i].requestValue = ObjectTypeUtil.castSetValue (requestPdu.varbinds[i].type, requestPdu.varbinds[i].value);
5082
5151
  switch ( requestPdu.varbinds[i].value ) {
5083
5152
  case RowStatus["active"]:
5084
5153
  case RowStatus["notInService"]:
@@ -5218,7 +5287,7 @@ Agent.prototype.request = function (socket, requestMessage, rinfo) {
5218
5287
 
5219
5288
  } else {
5220
5289
  // No special handling required. Just save the new value.
5221
- let setResult = mibRequests[savedIndex].instanceNode.setValue (me.castSetValue (
5290
+ let setResult = mibRequests[savedIndex].instanceNode.setValue (ObjectTypeUtil.castSetValue (
5222
5291
  requestPdu.varbinds[savedIndex].type,
5223
5292
  requestPdu.varbinds[savedIndex].value
5224
5293
  ));
@@ -5254,9 +5323,9 @@ Agent.prototype.request = function (socket, requestMessage, rinfo) {
5254
5323
  }
5255
5324
  responseVarbind.requestType = requestPdu.varbinds[savedIndex].type;
5256
5325
  if ( requestPdu.varbinds[savedIndex].requestValue ) {
5257
- responseVarbind.requestValue = me.castSetValue (requestPdu.varbinds[savedIndex].type, requestPdu.varbinds[savedIndex].requestValue);
5326
+ responseVarbind.requestValue = ObjectTypeUtil.castSetValue (requestPdu.varbinds[savedIndex].type, requestPdu.varbinds[savedIndex].requestValue);
5258
5327
  } else {
5259
- responseVarbind.requestValue = me.castSetValue (requestPdu.varbinds[savedIndex].type, requestPdu.varbinds[savedIndex].value);
5328
+ responseVarbind.requestValue = ObjectTypeUtil.castSetValue (requestPdu.varbinds[savedIndex].type, requestPdu.varbinds[savedIndex].value);
5260
5329
  }
5261
5330
  }
5262
5331
  if ( createResult[savedIndex] ) {
@@ -6425,5 +6494,6 @@ exports.ObjectParser = {
6425
6494
  readUint32: readUint32,
6426
6495
  readVarbindValue: readVarbindValue
6427
6496
  };
6497
+ exports.ObjectTypeUtil = ObjectTypeUtil;
6428
6498
  exports.Authentication = Authentication;
6429
6499
  exports.Encryption = Encryption;
package/lib/mib.js CHANGED
@@ -1,6 +1,7 @@
1
- var fs = require('fs');
1
+ const fs = require('fs');
2
+ const path = require('path');
2
3
 
3
- var MIB = function (dir) {
4
+ const MIB = function (dir) {
4
5
 
5
6
  var initializeBuffer = function (buffer) {
6
7
  return Object.assign(buffer, {
@@ -127,7 +128,7 @@ var MIB = function (dir) {
127
128
  }
128
129
  },
129
130
  Import: function (FileName) {
130
- this.ParseModule(FileName.split('/')[FileName.split('/').length - 1].split('.')[0], fs.readFileSync(FileName).toString());
131
+ this.ParseModule(path.basename(FileName, path.extname(FileName)), fs.readFileSync(FileName).toString());
131
132
  },
132
133
  ParseModule: function (FileName, Contents) {
133
134
  initializeBuffer(this.CharBuffer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "net-snmp",
3
- "version": "3.15.3",
3
+ "version": "3.16.1",
4
4
  "description": "JavaScript implementation of the Simple Network Management Protocol (SNMP)",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -12,7 +12,8 @@
12
12
  },
13
13
  "devDependencies": {
14
14
  "eslint": "^9.9.1",
15
- "getopts": "*"
15
+ "getopts": "^2.3.0",
16
+ "mocha": "^11.0.1"
16
17
  },
17
18
  "contributors": [
18
19
  {
@@ -47,6 +48,7 @@
47
48
  "author": "Mark Abrahams <mark@abrahams.co.nz>",
48
49
  "license": "MIT",
49
50
  "scripts": {
50
- "lint": "eslint . ./**/*.js"
51
+ "lint": "eslint . ./**/*.js",
52
+ "test": "./node_modules/mocha/bin/mocha.js"
51
53
  }
52
54
  }
@@ -0,0 +1,238 @@
1
+ const assert = require('assert');
2
+ const snmp = require('../');
3
+
4
+ describe('isValid() - Boolean', function() {
5
+ describe('JS true is a Boolean type', function() {
6
+ it('returns true', function() {
7
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Boolean, true), true);
8
+ });
9
+ });
10
+ describe('JS false is a Boolean type', function() {
11
+ it('returns true', function() {
12
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Boolean, false), true);
13
+ });
14
+ });
15
+ });
16
+
17
+ describe('isValid() - Integer', function() {
18
+ describe('Zero is an Integer type', function() {
19
+ it('returns true', function() {
20
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, 0), true);
21
+ });
22
+ });
23
+ describe('An positive integer number is an Integer type', function() {
24
+ it('returns true', function() {
25
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, 44), true);
26
+ });
27
+ });
28
+ describe('An positive integer string is an Integer type', function() {
29
+ it('returns true', function() {
30
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, "44"), true);
31
+ });
32
+ });
33
+ describe('An negative integer number is an Integer type', function() {
34
+ it('returns true', function() {
35
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, -24), true);
36
+ });
37
+ });
38
+ describe('A negative integer string is an Integer type', function() {
39
+ it('returns true', function() {
40
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, "-24"), true);
41
+ });
42
+ });
43
+ describe('A non-integer decimal number is not an Integer type', function() {
44
+ it('returns false', function() {
45
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, 45.2), false);
46
+ });
47
+ });
48
+ describe('A non-integer decimal string is not an Integer type', function() {
49
+ it('returns false', function() {
50
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, "45.2"), false);
51
+ });
52
+ });
53
+ describe('An integer at the bottom of the valid range is an Integer type', function() {
54
+ it('returns true', function() {
55
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, -2147483648), true);
56
+ });
57
+ });
58
+ describe('An integer just lower than the valid range is not an Integer type', function() {
59
+ it('returns false', function() {
60
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, -2147483649), false);
61
+ });
62
+ });
63
+ describe('An integer at the top of the valid range is an Integer type', function() {
64
+ it('returns true', function() {
65
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, 2147483647), true);
66
+ });
67
+ });
68
+ describe('An integer just higher than the valid range is not an Integer type', function() {
69
+ it('returns false', function() {
70
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Integer, 2147483648), false);
71
+ });
72
+ });
73
+ });
74
+
75
+ describe('isValid() - OctetString', function() {
76
+ describe('A string is an OctetString type', function() {
77
+ it('returns true', function() {
78
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.OctetString, 'Better late than never'), true);
79
+ });
80
+ });
81
+ describe('A buffer is an OctetString type', function() {
82
+ it('returns true', function() {
83
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.OctetString, Buffer.from('1.3.6.1.2.3.4.5')), true);
84
+ });
85
+ });
86
+ });
87
+
88
+ describe('isValid() - OID', function() {
89
+ describe('A string OID is an OID type', function() {
90
+ it('returns true', function() {
91
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.OctetString, '1.3.6.1.2.33.4.5'), true);
92
+ });
93
+ });
94
+ describe('A string non-OID is not an OID type', function() {
95
+ it('returns true', function() {
96
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.OctetString, 'dog'), true);
97
+ });
98
+ });
99
+ describe('An OID string with a double-dot is not an OID type', function() {
100
+ it('returns true', function() {
101
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.OctetString, '1.3.6.1..2.33.4.5'), true);
102
+ });
103
+ });
104
+ describe('An OID string with a leading dot is not an OID type', function() {
105
+ it('returns true', function() {
106
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.OctetString, '.1.3.6.1..2.33.4.5'), true);
107
+ });
108
+ });
109
+ describe('An OID string with a trailing dot is not an OID type', function() {
110
+ it('returns true', function() {
111
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.OctetString, '1.3.6.1.2.33.4.5.'), true);
112
+ });
113
+ });
114
+ });
115
+
116
+ describe('isValid() - Counter', function() {
117
+ describe('Zero is a Counter type', function() {
118
+ it('returns true', function() {
119
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, 0), true);
120
+ });
121
+ });
122
+ describe('An positive integer number is a Counter type', function() {
123
+ it('returns true', function() {
124
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, 44), true);
125
+ });
126
+ });
127
+ describe('An positive integer string is a Counter type', function() {
128
+ it('returns true', function() {
129
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, "44"), true);
130
+ });
131
+ });
132
+ describe('An negative integer number is not a Counter type', function() {
133
+ it('returns false', function() {
134
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, -24), false);
135
+ });
136
+ });
137
+ describe('A negative integer string is not a Counter type', function() {
138
+ it('returns false', function() {
139
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, "-24"), false);
140
+ });
141
+ });
142
+ describe('A non-integer decimal number is not a Counter type', function() {
143
+ it('returns false', function() {
144
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, 45.2), false);
145
+ });
146
+ });
147
+ describe('A non-integer decimal string is not a Counter type', function() {
148
+ it('returns false', function() {
149
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, "45.2"), false);
150
+ });
151
+ });
152
+ describe('An integer at the bottom of the valid range is a Counter type', function() {
153
+ it('returns true', function() {
154
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, 0), true);
155
+ });
156
+ });
157
+ describe('An integer just lower than the valid range is not a Counter type', function() {
158
+ it('returns false', function() {
159
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, -1), false);
160
+ });
161
+ });
162
+ describe('An integer at the top of the valid range is a Counter type', function() {
163
+ it('returns true', function() {
164
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, 4294967295), true);
165
+ });
166
+ });
167
+ describe('An integer just higher than the valid range is not a Counter type', function() {
168
+ it('returns false', function() {
169
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter, 4294967296), false);
170
+ });
171
+ });
172
+ });
173
+
174
+ describe('isValid() - Counter64', function() {
175
+ describe('Zero is a Counter64 type', function() {
176
+ it('returns true', function() {
177
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, 0), true);
178
+ });
179
+ });
180
+ describe('An positive integer number is a Counter64 type', function() {
181
+ it('returns true', function() {
182
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, 44), true);
183
+ });
184
+ });
185
+ describe('An positive integer string is a Counter64 type', function() {
186
+ it('returns true', function() {
187
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, "44"), true);
188
+ });
189
+ });
190
+ describe('An negative integer number is not a Counter64 type', function() {
191
+ it('returns false', function() {
192
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, -24), false);
193
+ });
194
+ });
195
+ describe('A negative integer string is not a Counter64 type', function() {
196
+ it('returns false', function() {
197
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, "-24"), false);
198
+ });
199
+ });
200
+ describe('A non-integer decimal number is not a Counter64 type', function() {
201
+ it('returns false', function() {
202
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, 45.2), false);
203
+ });
204
+ });
205
+ describe('A non-integer decimal string is not a Counter64 type', function() {
206
+ it('returns false', function() {
207
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, "45.2"), false);
208
+ });
209
+ });
210
+ describe('An integer at the bottom of the valid range is a Counter64 type', function() {
211
+ it('returns true', function() {
212
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, 0), true);
213
+ });
214
+ });
215
+ describe('An integer just lower than the valid range is not a Counter64 type', function() {
216
+ it('returns false', function() {
217
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.Counter64, -1), false);
218
+ });
219
+ });
220
+ });
221
+
222
+ describe('isValid() - IPAddress', function() {
223
+ describe('Dotted quad of numbers is an IpAddress type', function() {
224
+ it('returns true', function() {
225
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.IpAddress, '10.3.147.4'), true);
226
+ });
227
+ });
228
+ describe('Dotted quin of numbers is not an IpAddress type', function() {
229
+ it('returns false', function() {
230
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.IpAddress, '10.3.147.22.4'), false);
231
+ });
232
+ });
233
+ describe('Dotted quad containing non-numbers is not an IpAddress type', function() {
234
+ it('returns false', function() {
235
+ assert.equal(snmp.ObjectTypeUtil.isValid(snmp.ObjectType.IpAddress, '10.word.147.4'), false);
236
+ });
237
+ });
238
+ });