Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Sep 23, 2024
1 parent 2f04745 commit 3baa815
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 81 deletions.
30 changes: 15 additions & 15 deletions test/unit/facts.test.js → test/unit/collector/facts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const os = require('node:os')
const fs = require('node:fs')
const net = require('node:net')

const helper = require('../lib/agent_helper')
const { match } = require('../lib/custom-assertions')
const sysInfo = require('../../lib/system-info')
const utilTests = require('../lib/cross_agent_tests/utilization/utilization_json')
const bootIdTests = require('../lib/cross_agent_tests/utilization/boot_id')
const helper = require('../../lib/agent_helper')
const { match } = require('../../lib/custom-assertions')
const sysInfo = require('../../../lib/system-info')
const utilTests = require('../../lib/cross_agent_tests/utilization/utilization_json')
const bootIdTests = require('../../lib/cross_agent_tests/utilization/boot_id')

const APP_NAMES = ['a', 'c', 'b']
const DISABLE_ALL_DETECTIONS = {
Expand Down Expand Up @@ -63,7 +63,7 @@ test('fun facts about apps that New Relic is interested in including', async (t)
ctx.nr.logger = logger
ctx.nr.logs = logs

const facts = require('../../lib/collector/facts')
const facts = require('../../../lib/collector/facts')
ctx.nr.facts = function (agent, callback) {
return facts(agent, callback, { logger: ctx.nr.logger })
}
Expand Down Expand Up @@ -260,11 +260,11 @@ test('fun facts about apps that New Relic is interested in including', async (t)
})

test('utilization facts', async (t) => {
const awsInfo = require('../../lib/utilization/aws-info')
const azureInfo = require('../../lib/utilization/azure-info')
const gcpInfo = require('../../lib/utilization/gcp-info')
const kubernetesInfo = require('../../lib/utilization/kubernetes-info')
const common = require('../../lib/utilization/common')
const awsInfo = require('../../../lib/utilization/aws-info')
const azureInfo = require('../../../lib/utilization/azure-info')
const gcpInfo = require('../../../lib/utilization/gcp-info')
const kubernetesInfo = require('../../../lib/utilization/kubernetes-info')
const common = require('../../../lib/utilization/common')

t.beforeEach((ctx) => {
ctx.nr = {}
Expand All @@ -287,7 +287,7 @@ test('utilization facts', async (t) => {

ctx.nr.networkInterfaces = os.networkInterfaces

const facts = require('../../lib/collector/facts')
const facts = require('../../../lib/collector/facts')
ctx.nr.facts = function (agent, callback) {
return facts(agent, callback, { logger: ctx.nr.logger })
}
Expand Down Expand Up @@ -488,12 +488,12 @@ test('utilization facts', async (t) => {
})

test('boot id facts', async (t) => {
const common = require('../../lib/utilization/common')
const common = require('../../../lib/utilization/common')

t.beforeEach((ctx) => {
ctx.nr = {}

const facts = require('../../lib/collector/facts')
const facts = require('../../../lib/collector/facts')
ctx.nr.facts = function (agent, callback) {
return facts(agent, callback, { logger: ctx.nr.logger })
}
Expand Down Expand Up @@ -612,7 +612,7 @@ test('display_host facts', async (t) => {
t.beforeEach((ctx) => {
ctx.nr = {}

const facts = require('../../lib/collector/facts')
const facts = require('../../../lib/collector/facts')
ctx.nr.facts = function (agent, callback) {
return facts(agent, callback, { logger: ctx.nr.logger })
}
Expand Down
47 changes: 0 additions & 47 deletions test/unit/hashes.test.js

This file was deleted.

74 changes: 55 additions & 19 deletions test/unit/util/hashes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,69 @@
*/

'use strict'

const assert = require('node:assert')
const test = require('node:test')

const testData = require('../../lib/obfuscation-data')
const hashes = require('../../../lib/util/hashes')

test('hashes', async (t) => {
await t.test('#makeId always returns the correct length', () => {
for (let length = 4; length < 64; length++) {
for (let attempts = 0; attempts < 500; attempts++) {
const id = hashes.makeId(length)
assert.equal(id.length, length)
}
const major = process.version.slice(1).split('.').map(Number).shift()

test('#makeId always returns the correct length', () => {
for (let length = 4; length < 64; length++) {
for (let attempts = 0; attempts < 500; attempts++) {
const id = hashes.makeId(length)
assert.equal(id.length, length)
}
})
}
})

test('#makeId always unique', () => {
const ids = {}
for (let length = 16; length < 64; length++) {
for (let attempts = 0; attempts < 500; attempts++) {
const id = hashes.makeId(length)

await t.test('#makeId always unique', () => {
const ids = {}
for (let length = 16; length < 64; length++) {
for (let attempts = 0; attempts < 500; attempts++) {
const id = hashes.makeId(length)
// Should be unique
assert.equal(ids[id], undefined)
ids[id] = true

// Should be unique
assert.equal(ids[id], undefined)
ids[id] = true
// and the correct length
assert.equal(id.length, length)
}
}
})

// and the correct length
assert.equal(id.length, length)
}
test('obfuscation', async (t) => {
await t.test('should obfuscate strings correctly', () => {
for (const data of testData) {
assert.equal(hashes.obfuscateNameUsingKey(data.input, data.key), data.output)
}
})
})

test('deobfuscation', async (t) => {
await t.test('should deobfuscate strings correctly', () => {
for (const data of testData) {
assert.equal(hashes.deobfuscateNameUsingKey(data.output, data.key), data.input)
}
})
})

// TODO: remove this test when we drop support for node 18
test('getHash', { skip: major > 18 }, async (t) => {
/**
* TODO: crypto.DEFAULT_ENCODING has been deprecated.
* When fully disabled, this test can likely be removed.
* https://nodejs.org/api/deprecations.html#DEP0091
*/
/* eslint-disable node/no-deprecated-api */
await t.test('should not crash when changing the DEFAULT_ENCODING key on crypto', () => {
const crypto = require('node:crypto')
const oldEncoding = crypto.DEFAULT_ENCODING
crypto.DEFAULT_ENCODING = 'utf-8'
assert.doesNotThrow(hashes.getHash.bind(null, 'TEST_APP', 'TEST_TXN'))
crypto.DEFAULT_ENCODING = oldEncoding
})
})

0 comments on commit 3baa815

Please sign in to comment.