Skip to content

Commit

Permalink
Add tests for stable hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
madirey committed Jul 17, 2020
1 parent a2fc28a commit 8e2cd14
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { listMock } from '../../../../../lists/server/mocks';
import { getFoundExceptionListItemSchemaMock } from '../../../../../lists/common/schemas/response/found_exception_list_item_schema.mock';
import { getExceptionListItemSchemaMock } from '../../../../../lists/common/schemas/response/exception_list_item_schema.mock';
import { EntriesArray, EntryList } from '../../../../../lists/common/schemas/types/entries';
import { getFullEndpointExceptionList } from './lists';
import { buildArtifact, getFullEndpointExceptionList } from './lists';
import { TranslatedEntry, TranslatedExceptionListItem } from '../../schemas/artifacts';

describe('buildEventTypeSignal', () => {
let mockExceptionClient: ExceptionListClient;
Expand Down Expand Up @@ -340,4 +341,95 @@ describe('buildEventTypeSignal', () => {
const resp = await getFullEndpointExceptionList(mockExceptionClient, 'linux', 'v1');
expect(resp.entries.length).toEqual(0);
});

test('it should return a stable hash regardless of order of entries', async () => {
const translatedEntries: TranslatedEntry[] = [
{
entries: [
{
field: 'some.nested.field',
operator: 'included',
type: 'exact_cased',
value: 'some value',
},
],
field: 'some.parentField',
type: 'nested',
},
{
field: 'nested.field',
operator: 'included',
type: 'exact_cased',
value: 'some value',
},
];
const translatedEntriesReversed = translatedEntries.reverse();

const translatedExceptionList = {
entries: [
{
type: 'simple',
entries: translatedEntries,
},
],
};

const translatedExceptionListReversed = {
entries: [
{
type: 'simple',
entries: translatedEntriesReversed,
},
],
};

const artifact1 = await buildArtifact(translatedExceptionList, 'linux', 'v1');
const artifact2 = await buildArtifact(translatedExceptionListReversed, 'linux', 'v1');
expect(artifact1.decodedSha256).toEqual(artifact2.decodedSha256);
});

test('it should return a stable hash regardless of order of items', async () => {
const translatedItems: TranslatedExceptionListItem[] = [
{
type: 'simple',
entries: [
{
entries: [
{
field: 'some.nested.field',
operator: 'included',
type: 'exact_cased',
value: 'some value',
},
],
field: 'some.parentField',
type: 'nested',
},
],
},
{
type: 'simple',
entries: [
{
field: 'nested.field',
operator: 'included',
type: 'exact_cased',
value: 'some value',
},
],
},
];

const translatedExceptionList = {
entries: translatedItems,
};

const translatedExceptionListReversed = {
entries: translatedItems.reverse(),
};

const artifact1 = await buildArtifact(translatedExceptionList, 'linux', 'v1');
const artifact2 = await buildArtifact(translatedExceptionListReversed, 'linux', 'v1');
expect(artifact1.decodedSha256).toEqual(artifact2.decodedSha256);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export function translateToEndpointExceptions(
if (schemaVersion === 'v1') {
exc.data.forEach((entry) => {
const translatedItem = translateItem(schemaVersion, entry);
// TODO: is JSON.stringify deterministic?
const entryHash = createHash('sha256').update(JSON.stringify(translatedItem)).digest('hex');
if (!entrySet.has(entryHash)) {
entriesFiltered.push(translatedItem);
Expand Down

0 comments on commit 8e2cd14

Please sign in to comment.