-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathplugin-registry.test.ts
More file actions
127 lines (111 loc) · 3.76 KB
/
Copy pathplugin-registry.test.ts
File metadata and controls
127 lines (111 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { IRulePlugin, PluginMetadata } from '@veridion/scanner-types';
import { FindingSeverity } from '@veridion/shared';
import { beforeEach, describe, expect, it } from 'vitest';
import { createDefaultRegistry, PluginRegistry } from './plugin-registry';
import { Scanner } from './scanner';
function createMockPlugin(
id: string,
chains: string[] = ['ethereum'],
languages: string[] = ['solidity'],
): IRulePlugin {
const metadata: PluginMetadata = {
id,
name: `Test Plugin ${id}`,
version: '1.0.0',
description: 'Mock plugin for testing',
severity: FindingSeverity.MEDIUM,
category: 'CUSTOM',
chains,
languages,
tags: ['test'],
};
return {
metadata,
initialize: async () => {
// noop
},
// eslint-disable-next-line @typescript-eslint/require-await
analyze: async () => [],
getFixRecommendation: () => 'No fix needed',
supportsContext: (ctx) => chains.includes(ctx.chain) && languages.includes(ctx.language),
};
}
describe('PluginRegistry', () => {
let registry: PluginRegistry;
beforeEach(() => {
registry = new PluginRegistry();
});
it('should register a plugin', () => {
const plugin = createMockPlugin('test-plugin');
registry.register(plugin);
expect(registry.size).toBe(1);
});
it('should retrieve a registered plugin', () => {
const plugin = createMockPlugin('test-plugin');
registry.register(plugin);
expect(registry.get('test-plugin')).toBe(plugin);
});
it('should get plugins by chain', () => {
const ethPlugin = createMockPlugin('eth', ['ethereum']);
const polyPlugin = createMockPlugin('poly', ['polygon']);
registry.registerAll([ethPlugin, polyPlugin]);
const matching = registry.getMatchingPlugins({
contractName: 'Test',
sourceCode: 'contract Test {}',
chain: 'ethereum',
language: 'solidity',
compilerVersion: null,
metadata: {},
});
expect(matching).toHaveLength(1);
expect(matching[0]?.metadata.id).toBe('eth');
});
it('should unregister a plugin', () => {
const plugin = createMockPlugin('removable');
registry.register(plugin);
expect(registry.size).toBe(1);
registry.unregister('removable');
expect(registry.size).toBe(0);
});
it('should return all metadata', () => {
registry.registerAll([createMockPlugin('a'), createMockPlugin('b')]);
const allMeta = registry.getAllMetadata();
expect(allMeta).toHaveLength(2);
});
});
describe('createDefaultRegistry', () => {
it('should register unchecked-return plugin by default', () => {
const registry = createDefaultRegistry();
expect(registry.size).toBe(1);
const plugin = registry.get('unchecked-return');
expect(plugin).toBeDefined();
expect(plugin?.metadata.id).toBe('unchecked-return');
expect(plugin?.metadata.category).toBe('UNCHECKED_RETURN');
expect(plugin?.metadata.severity).toBe(FindingSeverity.HIGH);
});
it('should list unchecked-return in all metadata', () => {
const registry = createDefaultRegistry();
const ids = registry.getAllMetadata().map((m) => m.id);
expect(ids).toContain('unchecked-return');
});
it('should execute end-to-end scan through Scanner', async () => {
const registry = createDefaultRegistry();
const scanner = new Scanner(registry);
const result = await scanner.scan({
contractName: 'TestVault',
sourceCode: `
contract TestVault {
function withdraw(address payable recipient) public {
recipient.call("");
}
}`,
chain: 'ethereum',
language: 'solidity',
compilerVersion: '0.8.20',
metadata: {},
});
expect(result.findings).toHaveLength(1);
expect(result.findings[0]?.pluginId).toBe('unchecked-return');
expect(result.findings[0]?.lineStart).toBe(4);
});
});