net-snmp 3.26.3 → 3.28.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.
@@ -1,384 +0,0 @@
1
- const assert = require('assert');
2
- // crypto is used indirectly by Authentication and Encryption modules
3
- const snmp = require('../');
4
- const { SecurityLevel, AuthProtocols, PrivProtocols, Authentication, Encryption } = snmp;
5
-
6
- describe('SNMPv3 Authentication and Encryption', function () {
7
- // Sample data for tests
8
- const authPassword = 'test_auth_password';
9
- const privPassword = 'test_priv_password';
10
- const engineID = Buffer.from('8000B98380ABCDEF', 'hex');
11
-
12
- // Test data that will be encrypted/authenticated
13
- const testData = Buffer.from('Test data for SNMP authentication and privacy tests', 'utf8');
14
-
15
- describe('Authentication', function () {
16
- it('should support noAuthNoPriv security level', function () {
17
- // noAuthNoPriv doesn't use authentication, verify this is handled appropriately
18
- const user = {
19
- name: 'noAuthUser',
20
- level: SecurityLevel.noAuthNoPriv,
21
- };
22
-
23
- // No authentication should be required for this user
24
- assert.strictEqual(user.level, SecurityLevel.noAuthNoPriv);
25
- assert.strictEqual(user.authProtocol, undefined);
26
- assert.strictEqual(user.authKey, undefined);
27
- });
28
-
29
- it('should support MD5 authentication protocol', function () {
30
- // Generate authentication key using MD5
31
- const authKey = Authentication.passwordToKey(AuthProtocols.md5, authPassword, engineID);
32
-
33
- // Verify key length matches expected MD5 output length
34
- assert.strictEqual(authKey.length, Authentication.algorithms[AuthProtocols.md5].KEY_LENGTH);
35
-
36
- // Create a test digest
37
- const digest = Authentication.calculateDigest(testData, AuthProtocols.md5, authPassword, engineID);
38
-
39
- // Verify digest length
40
- assert.strictEqual(digest.length, Authentication.algorithms[AuthProtocols.md5].AUTHENTICATION_CODE_LENGTH);
41
-
42
- // Verify authentication works
43
- const testBuffer = Buffer.concat([testData]);
44
- const digestInMessage = Buffer.alloc(
45
- Authentication.algorithms[AuthProtocols.md5].AUTHENTICATION_CODE_LENGTH
46
- );
47
- Authentication.writeParameters(testBuffer, AuthProtocols.md5, authPassword, engineID, digestInMessage);
48
-
49
- assert.strictEqual(
50
- Authentication.isAuthentic(testBuffer, AuthProtocols.md5, authPassword, engineID, digestInMessage),
51
- true
52
- );
53
- });
54
-
55
- it('should support SHA authentication protocol', function () {
56
- // Generate authentication key using SHA
57
- const authKey = Authentication.passwordToKey(AuthProtocols.sha, authPassword, engineID);
58
-
59
- // Verify key length matches expected SHA output length
60
- assert.strictEqual(authKey.length, Authentication.algorithms[AuthProtocols.sha].KEY_LENGTH);
61
-
62
- // Create a test digest
63
- const digest = Authentication.calculateDigest(testData, AuthProtocols.sha, authPassword, engineID);
64
-
65
- // Verify digest length
66
- assert.strictEqual(digest.length, Authentication.algorithms[AuthProtocols.sha].AUTHENTICATION_CODE_LENGTH);
67
-
68
- // Verify authentication works
69
- const testBuffer = Buffer.concat([testData]);
70
- const digestInMessage = Buffer.alloc(
71
- Authentication.algorithms[AuthProtocols.sha].AUTHENTICATION_CODE_LENGTH
72
- );
73
- Authentication.writeParameters(testBuffer, AuthProtocols.sha, authPassword, engineID, digestInMessage);
74
-
75
- assert.strictEqual(
76
- Authentication.isAuthentic(testBuffer, AuthProtocols.sha, authPassword, engineID, digestInMessage),
77
- true
78
- );
79
- });
80
-
81
- it('should support SHA-256 authentication protocol', function () {
82
- // Generate authentication key using SHA-256
83
- const authKey = Authentication.passwordToKey(AuthProtocols.sha256, authPassword, engineID);
84
-
85
- // Verify key length matches expected SHA-256 output length
86
- assert.strictEqual(authKey.length, Authentication.algorithms[AuthProtocols.sha256].KEY_LENGTH);
87
-
88
- // Create a test digest
89
- const digest = Authentication.calculateDigest(testData, AuthProtocols.sha256, authPassword, engineID);
90
-
91
- // Verify digest length
92
- assert.strictEqual(
93
- digest.length,
94
- Authentication.algorithms[AuthProtocols.sha256].AUTHENTICATION_CODE_LENGTH
95
- );
96
-
97
- // Verify authentication works
98
- const testBuffer = Buffer.concat([testData]);
99
- const digestInMessage = Buffer.alloc(
100
- Authentication.algorithms[AuthProtocols.sha256].AUTHENTICATION_CODE_LENGTH
101
- );
102
- Authentication.writeParameters(testBuffer, AuthProtocols.sha256, authPassword, engineID, digestInMessage);
103
-
104
- assert.strictEqual(
105
- Authentication.isAuthentic(testBuffer, AuthProtocols.sha256, authPassword, engineID, digestInMessage),
106
- true
107
- );
108
- });
109
-
110
- it('should support SHA-512 authentication protocol', function () {
111
- // Generate authentication key using SHA-512
112
- const authKey = Authentication.passwordToKey(AuthProtocols.sha512, authPassword, engineID);
113
-
114
- // Verify key length matches expected SHA-512 output length
115
- assert.strictEqual(authKey.length, Authentication.algorithms[AuthProtocols.sha512].KEY_LENGTH);
116
-
117
- // Create a test digest
118
- const digest = Authentication.calculateDigest(testData, AuthProtocols.sha512, authPassword, engineID);
119
-
120
- // Verify digest length
121
- assert.strictEqual(
122
- digest.length,
123
- Authentication.algorithms[AuthProtocols.sha512].AUTHENTICATION_CODE_LENGTH
124
- );
125
-
126
- // Verify authentication works
127
- const testBuffer = Buffer.concat([testData]);
128
- const digestInMessage = Buffer.alloc(
129
- Authentication.algorithms[AuthProtocols.sha512].AUTHENTICATION_CODE_LENGTH
130
- );
131
- Authentication.writeParameters(testBuffer, AuthProtocols.sha512, authPassword, engineID, digestInMessage);
132
-
133
- assert.strictEqual(
134
- Authentication.isAuthentic(testBuffer, AuthProtocols.sha512, authPassword, engineID, digestInMessage),
135
- true
136
- );
137
- });
138
- });
139
-
140
- describe('Encryption', function () {
141
- // Create a mock engine for encryption tests
142
- const engine = {
143
- engineID: engineID,
144
- engineBoots: 1,
145
- engineTime: 123,
146
- };
147
-
148
- it('should support DES encryption protocol', function () {
149
- // Test DES encryption/decryption with SHA authentication
150
- const authProtocol = AuthProtocols.sha;
151
- const privProtocol = PrivProtocols.des;
152
-
153
- // Encrypt the test data
154
- const { encryptedPdu, msgPrivacyParameters } = Encryption.encryptPdu(
155
- privProtocol,
156
- testData,
157
- privPassword,
158
- authProtocol,
159
- engine
160
- );
161
-
162
- // Verify encryption was done (output should be different from input)
163
- assert.notDeepStrictEqual(encryptedPdu, testData);
164
-
165
- // Decrypt the data
166
- const decryptedPdu = Encryption.decryptPdu(
167
- privProtocol,
168
- encryptedPdu,
169
- msgPrivacyParameters,
170
- privPassword,
171
- authProtocol,
172
- engine
173
- );
174
-
175
- // Verify decryption works
176
- assert.deepStrictEqual(decryptedPdu.slice(0, testData.length), testData);
177
- });
178
-
179
- it('should support AES encryption protocol', function () {
180
- // Test AES encryption/decryption with SHA authentication
181
- const authProtocol = AuthProtocols.sha;
182
- const privProtocol = PrivProtocols.aes;
183
-
184
- // Encrypt the test data
185
- const { encryptedPdu, msgPrivacyParameters } = Encryption.encryptPdu(
186
- privProtocol,
187
- testData,
188
- privPassword,
189
- authProtocol,
190
- engine
191
- );
192
-
193
- // Verify encryption was done (output should be different from input)
194
- assert.notDeepStrictEqual(encryptedPdu, testData);
195
-
196
- // Decrypt the data
197
- const decryptedPdu = Encryption.decryptPdu(
198
- privProtocol,
199
- encryptedPdu,
200
- msgPrivacyParameters,
201
- privPassword,
202
- authProtocol,
203
- engine
204
- );
205
-
206
- // Verify decryption works
207
- assert.deepStrictEqual(decryptedPdu.slice(0, testData.length), testData);
208
- });
209
-
210
- it('should support AES-256b (Blumenthal) encryption protocol', function () {
211
- // Test AES-256 Blumenthal encryption/decryption with SHA authentication
212
- const authProtocol = AuthProtocols.sha;
213
- const privProtocol = PrivProtocols.aes256b;
214
-
215
- // Encrypt the test data
216
- const { encryptedPdu, msgPrivacyParameters } = Encryption.encryptPdu(
217
- privProtocol,
218
- testData,
219
- privPassword,
220
- authProtocol,
221
- engine
222
- );
223
-
224
- // Verify encryption was done (output should be different from input)
225
- assert.notDeepStrictEqual(encryptedPdu, testData);
226
-
227
- // Decrypt the data
228
- const decryptedPdu = Encryption.decryptPdu(
229
- privProtocol,
230
- encryptedPdu,
231
- msgPrivacyParameters,
232
- privPassword,
233
- authProtocol,
234
- engine
235
- );
236
-
237
- // Verify decryption works
238
- assert.deepStrictEqual(decryptedPdu.slice(0, testData.length), testData);
239
- });
240
-
241
- it('should support AES-256r (Reeder) encryption protocol', function () {
242
- // Test AES-256 Reeder encryption/decryption with SHA authentication
243
- const authProtocol = AuthProtocols.sha;
244
- const privProtocol = PrivProtocols.aes256r;
245
-
246
- // Encrypt the test data
247
- const { encryptedPdu, msgPrivacyParameters } = Encryption.encryptPdu(
248
- privProtocol,
249
- testData,
250
- privPassword,
251
- authProtocol,
252
- engine
253
- );
254
-
255
- // Verify encryption was done (output should be different from input)
256
- assert.notDeepStrictEqual(encryptedPdu, testData);
257
-
258
- // Decrypt the data
259
- const decryptedPdu = Encryption.decryptPdu(
260
- privProtocol,
261
- encryptedPdu,
262
- msgPrivacyParameters,
263
- privPassword,
264
- authProtocol,
265
- engine
266
- );
267
-
268
- // Verify decryption works
269
- assert.deepStrictEqual(decryptedPdu.slice(0, testData.length), testData);
270
- });
271
- });
272
-
273
- describe('SecurityLevel combinations', function () {
274
- it('should support authNoPriv security level', function () {
275
- // Create a user with authentication but no privacy
276
- const user = {
277
- name: 'authNoPrivUser',
278
- level: SecurityLevel.authNoPriv,
279
- authProtocol: AuthProtocols.sha,
280
- authKey: 'authPassword',
281
- };
282
-
283
- assert.strictEqual(user.level, SecurityLevel.authNoPriv);
284
- assert.strictEqual(user.authProtocol, AuthProtocols.sha);
285
- assert.strictEqual(user.authKey, 'authPassword');
286
- assert.strictEqual(user.privProtocol, undefined);
287
- assert.strictEqual(user.privKey, undefined);
288
- });
289
-
290
- it('should support authPriv security level', function () {
291
- // Create a user with authentication and privacy
292
- const user = {
293
- name: 'authPrivUser',
294
- level: SecurityLevel.authPriv,
295
- authProtocol: AuthProtocols.sha256,
296
- authKey: 'authPassword',
297
- privProtocol: PrivProtocols.aes,
298
- privKey: 'privPassword',
299
- };
300
-
301
- assert.strictEqual(user.level, SecurityLevel.authPriv);
302
- assert.strictEqual(user.authProtocol, AuthProtocols.sha256);
303
- assert.strictEqual(user.authKey, 'authPassword');
304
- assert.strictEqual(user.privProtocol, PrivProtocols.aes);
305
- assert.strictEqual(user.privKey, 'privPassword');
306
- });
307
-
308
- it('should validate all required parameters are provided for each security level', function () {
309
- // noAuthNoPriv only requires username and level
310
- const user1 = {
311
- name: 'user1',
312
- level: SecurityLevel.noAuthNoPriv,
313
- };
314
-
315
- // authNoPriv requires authentication parameters
316
- const user2 = {
317
- name: 'user2',
318
- level: SecurityLevel.authNoPriv,
319
- authProtocol: AuthProtocols.sha,
320
- authKey: 'authPassword',
321
- };
322
-
323
- // authPriv requires both authentication and privacy parameters
324
- const user3 = {
325
- name: 'user3',
326
- level: SecurityLevel.authPriv,
327
- authProtocol: AuthProtocols.sha,
328
- authKey: 'authPassword',
329
- privProtocol: PrivProtocols.aes,
330
- privKey: 'privPassword',
331
- };
332
-
333
- // This function would typically be part of parameter validation
334
- function validateUser(user) {
335
- if (user.level === SecurityLevel.authNoPriv || user.level === SecurityLevel.authPriv) {
336
- assert.ok(user.authProtocol, 'authProtocol required for this security level');
337
- assert.ok(user.authKey, 'authKey required for this security level');
338
- }
339
-
340
- if (user.level === SecurityLevel.authPriv) {
341
- assert.ok(user.privProtocol, 'privProtocol required for this security level');
342
- assert.ok(user.privKey, 'privKey required for this security level');
343
- }
344
-
345
- return true;
346
- }
347
-
348
- assert.strictEqual(validateUser(user1), true);
349
- assert.strictEqual(validateUser(user2), true);
350
- assert.strictEqual(validateUser(user3), true);
351
- });
352
- });
353
-
354
- describe('Custom engineID handling', function () {
355
- it('should correctly use engineID parameter', function () {
356
- // This test verifies the fix for issue #283
357
- // Create a session with default settings (no engineID)
358
- const defaultSession = new snmp.Session({
359
- host: 'example.org',
360
- version: snmp.Version3
361
- });
362
-
363
- // Default session should have an engineID of expected format (17 bytes)
364
- assert.strictEqual(defaultSession.engine.engineID.length, 17);
365
-
366
- // Convert to hex string for easier inspection
367
- const defaultEngineIDHex = defaultSession.engine.engineID.toString('hex');
368
- // First 5 bytes should match the standard format 8000B98380
369
- assert.strictEqual(defaultEngineIDHex.substring(0, 10), '8000b98380');
370
-
371
- // Create a second session - should generate a different random engineID
372
- const anotherDefaultSession = new snmp.Session({
373
- host: 'example.org',
374
- version: snmp.Version3
375
- });
376
-
377
- // The two sessions should have different engineIDs (random part differs)
378
- assert.notStrictEqual(
379
- defaultSession.engine.engineID.toString('hex'),
380
- anotherDefaultSession.engine.engineID.toString('hex')
381
- );
382
- });
383
- });
384
- });
@@ -1,71 +0,0 @@
1
- const assert = require('assert');
2
- const snmp = require('../');
3
-
4
- describe('Custom dgram module support', function () {
5
- it('should use custom dgram module in Session', function (done) {
6
- let createSocketCalled = false;
7
- const mockDgram = {
8
- createSocket: function (transport) {
9
- createSocketCalled = true;
10
- assert.equal(transport, 'udp4');
11
-
12
- // Return a mock socket
13
- return {
14
- unref: function () {},
15
- on: function () {},
16
- bind: function () {},
17
- close: function () {}
18
- };
19
- }
20
- };
21
-
22
- const session = snmp.createSession('127.0.0.1', 'public', {
23
- dgramModule: mockDgram
24
- });
25
-
26
- assert(createSocketCalled, 'Custom dgram module createSocket should have been called');
27
- session.close();
28
- done();
29
- });
30
-
31
- it('should use custom dgram module in Receiver', function (done) {
32
- let createSocketCalled = false;
33
- const mockDgram = {
34
- createSocket: function (transport) {
35
- createSocketCalled = true;
36
- assert.equal(transport, 'udp4');
37
-
38
- // Return a mock socket
39
- return {
40
- on: function () {},
41
- bind: function () {},
42
- close: function () {},
43
- address: function () {
44
- return { address: '127.0.0.1', family: 'IPv4', port: 162 };
45
- }
46
- };
47
- }
48
- };
49
-
50
- const receiver = snmp.createReceiver({
51
- dgramModule: mockDgram,
52
- port: 1162
53
- }, function () {});
54
-
55
- assert(createSocketCalled, 'Custom dgram module createSocket should have been called');
56
- receiver.close();
57
- done();
58
- });
59
-
60
- it('should fallback to default dgram when no custom module provided', function (done) {
61
- // This should not throw an error
62
- const session = snmp.createSession('127.0.0.1', 'public', {
63
- // No dgramModule specified
64
- });
65
-
66
- // Session should be created successfully
67
- assert(session);
68
- session.close();
69
- done();
70
- });
71
- });
package/test/mib.test.js DELETED
@@ -1,139 +0,0 @@
1
- const assert = require('assert');
2
- const snmp = require('..');
3
-
4
- let mibProviders;
5
- let mib;
6
-
7
- describe('MIB', function () {
8
-
9
- this.beforeAll(function () {
10
- const mibDir = './test/';
11
- const store = snmp.createModuleStore();
12
- store.loadFromFile(mibDir + 'TEST-MIB.mib');
13
- mibProviders = store.getProvidersForModule('TEST-MIB');
14
- });
15
-
16
- beforeEach(function () {
17
- mib = snmp.createMib();
18
- mib.registerProviders(mibProviders);
19
- });
20
-
21
- describe('setScalarValue()', function () {
22
- it('sets a scalar value', function () {
23
- mib.setScalarValue('testScalarInteger', 42);
24
- assert.equal(mib.getScalarValue('testScalarInteger'), 42);
25
- });
26
- });
27
-
28
- describe('getScalarValue()', function () {
29
- it('sets a scalar value', function () {
30
- mib.setScalarValue('testScalarInteger', 42);
31
- assert.equal(mib.getScalarValue('testScalarInteger'), 42);
32
- });
33
- });
34
-
35
- describe('addTableRow()', function () {
36
- it('adds a row to a table', function () {
37
- const row = [1, 'RowValue'];
38
- mib.addTableRow('testEntry1', row);
39
- const tableData = mib.getTableCells('testEntry1', true, true);
40
- assert.deepEqual(tableData, [[ [1], 'RowValue']]);
41
- });
42
- });
43
-
44
- describe('getTableColumnDefinitions()', function () {
45
- it('returns column definitions for a table', function () {
46
- const columns = mib.getTableColumnDefinitions('testEntry1');
47
- assert.equal(columns.length, 2);
48
- assert.equal(columns[0].name, 'testTable1Index');
49
- assert.equal(columns[1].name, 'testTable1Value');
50
- });
51
- });
52
-
53
- describe('getTableCells()', function () {
54
- it('retrieves table data by rows', function () {
55
- const row = [1, 'RowValue'];
56
- mib.addTableRow('testEntry1', row);
57
- const data = mib.getTableCells('testEntry1', true, true);
58
- assert.deepEqual(data, [[ [1], 'RowValue']]);
59
- });
60
-
61
- it('retrieves table data by columns', function () {
62
- const row = [1, 'RowValue'];
63
- mib.addTableRow('testEntry1', row);
64
- const data = mib.getTableCells('testEntry1', false, true);
65
- assert.deepEqual(data, [ [[1]], ['RowValue']]);
66
- });
67
- });
68
-
69
- describe('getTableColumnCells()', function () {
70
- it('retrieves a single column of table data', function () {
71
- const row = [1, 'RowValue'];
72
- mib.addTableRow('testEntry1', row);
73
- const columnData = mib.getTableColumnCells('testEntry1', 2);
74
- assert.deepEqual(columnData, ['RowValue']);
75
- });
76
- });
77
-
78
- describe('getTableRowCells()', function () {
79
- it('retrieves a single row of table data', function () {
80
- const row = [1, 'RowValue'];
81
- mib.addTableRow('testEntry1', row);
82
- const rowData = mib.getTableRowCells('testEntry1', [1]);
83
- assert.deepEqual(rowData, ['RowValue']);
84
- });
85
- });
86
-
87
- describe('getTableSingleCell()', function () {
88
- it('retrieves a single cell value from a table', function () {
89
- const row = [1, 'RowValue'];
90
- mib.addTableRow('testEntry1', row);
91
- const cellValue = mib.getTableSingleCell('testEntry1', 2, [1]);
92
- assert.equal(cellValue, 'RowValue');
93
- });
94
- });
95
-
96
- describe('setTableSingleCell()', function () {
97
- it('sets a single cell value in a table', function () {
98
- const row = [1, 'RowValue'];
99
- mib.addTableRow('testEntry1', row);
100
- mib.setTableSingleCell('testEntry1', 2, [1], 'NewValue');
101
- const cellValue = mib.getTableSingleCell('testEntry1', 2, [1]);
102
- assert.equal(cellValue, 'NewValue');
103
- });
104
- });
105
-
106
- describe('deleteTableRow()', function () {
107
- it('deletes a table row', function () {
108
- const row1 = [1, 'CellValue1'];
109
- const row2 = [2, 'CellValue2'];
110
- mib.addTableRow('testEntry1', row1);
111
- mib.addTableRow('testEntry1', row2);
112
- mib.deleteTableRow('testEntry1', [1]);
113
- const data = mib.getTableCells('testEntry1', true, true);
114
- assert.deepEqual(data, [[ [2], 'CellValue2']]);
115
- });
116
-
117
- it('deletes a table row with string index', function () {
118
- const row1 = ['ABC', 100];
119
- const row2 = ['XYZ', 200];
120
- mib.addTableRow('testEntry2', row1);
121
- mib.addTableRow('testEntry2', row2);
122
- mib.deleteTableRow('testEntry2', 'ABC');
123
- const data = mib.getTableCells('testEntry2', true, true);
124
- assert.deepEqual(data, [[ ['XYZ'], 200]]);
125
- });
126
- });
127
-
128
- describe('registerProvider() - scalar defVal', function () {
129
- it('adds a scalar value on registration', function () {
130
- const options = {
131
- addScalarDefaultsOnRegistration: true
132
- };
133
- mib = snmp.createMib(options);
134
- mib.registerProviders(mibProviders);
135
- assert.strictEqual(mib.getScalarValue('testScalarIntegerDefval'), 49);
136
- });
137
- });
138
-
139
- });