Skip to main content
Glama
trusted-tool-verification-test.js•8.31 kB
/** * Test Trusted Tool Verification Implementation */ import { TrustedToolVerifier } from '../lib/security/TrustedToolVerifier.js'; import { writeFile, unlink, chmod } from 'fs/promises'; import { resolve } from 'path'; async function testTrustedToolVerifier() { console.log('šŸ” Testing Trusted Tool Verifier'); console.log('==============================='); // Test 1: Basic instantiation console.log('āœ“ Testing instantiation...'); const verifier = new TrustedToolVerifier({ enableChecksumVerification: true, enableSignatureVerification: true, allowSelfSigned: true }); console.log('āœ… TrustedToolVerifier instantiated successfully'); // Test 2: View trusted tools registry console.log('āœ“ Testing trusted tools registry...'); const trustedTools = verifier.getTrustedTools(); console.log('āœ… Built-in trusted tools loaded:', Object.keys(trustedTools).length); console.log(' - Sample tools:', Object.keys(trustedTools).slice(0, 5)); // Test 3: Verify system tool (echo) console.log('āœ“ Testing system tool verification...'); try { const echoResult = await verifier.verifyTool('/bin/echo'); console.log('āœ… Echo verification result:', echoResult.trusted, '-', echoResult.message); if (echoResult.trusted) { console.log(' - Verification methods:', echoResult.metadata.verification ? Object.keys(echoResult.metadata.verification) : 'none'); } } catch (error) { console.log('āš ļø Echo verification failed (may not exist on this system):', error.message); } // Test 4: Create test file and verify console.log('āœ“ Testing custom file verification...'); const testFilePath = resolve('./test-tool.sh'); const testContent = '#!/bin/bash\necho "test tool"'; try { await writeFile(testFilePath, testContent); await chmod(testFilePath, 0o755); // Add as trusted tool verifier.addTrustedTool('test-tool', { type: 'test', paths: [testFilePath], description: 'Test tool for verification', permissions: ['execute'] }); // First verification (learning mode) const firstResult = await verifier.verifyTool(testFilePath); console.log('āœ… First verification (learning):', firstResult.trusted, '-', firstResult.message); // Second verification (should use stored checksums) const secondResult = await verifier.verifyTool(testFilePath); console.log('āœ… Second verification (cached):', secondResult.trusted, '-', secondResult.message); // Modify file and verify (should fail) await writeFile(testFilePath, testContent + '\n# modified'); const modifiedResult = await verifier.verifyTool(testFilePath); console.log('āœ… Modified file verification:', modifiedResult.trusted, '-', modifiedResult.message); // Cleanup await unlink(testFilePath); console.log('āœ… Test file cleanup completed'); } catch (error) { console.log('āŒ Custom file test failed:', error.message); } // Test 5: Untrusted tool verification console.log('āœ“ Testing untrusted tool rejection...'); try { const untrustedResult = await verifier.verifyTool('/non/existent/tool'); console.log('āœ… Untrusted tool correctly rejected:', !untrustedResult.trusted); console.log(' - Rejection reason:', untrustedResult.metadata.reason); } catch (error) { console.log('āœ… Untrusted tool verification handled error correctly'); } // Test 6: Cache functionality console.log('āœ“ Testing cache functionality...'); const metrics1 = verifier.getVerificationMetrics(); // Verify same tool again (should hit cache) if (trustedTools.echo) { try { await verifier.verifyTool('/bin/echo'); const metrics2 = verifier.getVerificationMetrics(); const cacheHitIncrease = metrics2.cacheHits - metrics1.cacheHits; console.log('āœ… Cache hit increase:', cacheHitIncrease); } catch (error) { console.log('āš ļø Cache test skipped (echo not available)'); } } // Test 7: Statistics and metrics console.log('āœ“ Testing metrics collection...'); const finalMetrics = verifier.getVerificationMetrics(); console.log('āœ… Verification metrics:'); console.log(' - Total verifications:', finalMetrics.totalVerifications); console.log(' - Successful verifications:', finalMetrics.successfulVerifications); console.log(' - Failed verifications:', finalMetrics.failedVerifications); console.log(' - Cache hits:', finalMetrics.cacheHits); console.log(' - Cache misses:', finalMetrics.cacheMisses); console.log(' - Trusted tools count:', finalMetrics.trustedToolsCount); // Test 8: Configuration options console.log('āœ“ Testing configuration options...'); const strictVerifier = new TrustedToolVerifier({ enableChecksumVerification: true, enableSignatureVerification: true, allowSelfSigned: false, cacheTtlMs: 1000 }); console.log('āœ… Strict verifier configured successfully'); // Cleanup verifier.destroy(); strictVerifier.destroy(); console.log('āœ… TrustedToolVerifier cleanup completed'); } async function testTrustedToolTypes() { console.log('\nšŸ” Testing Tool Type Categories'); console.log('=============================='); const verifier = new TrustedToolVerifier(); const tools = verifier.getTrustedTools(); // Categorize tools by type const categories = {}; for (const [name, config] of Object.entries(tools)) { if (!categories[config.type]) { categories[config.type] = []; } categories[config.type].push(name); } console.log('āœ“ Tool categories:'); for (const [type, toolList] of Object.entries(categories)) { console.log(`āœ… ${type}: ${toolList.length} tools`); console.log(` - ${toolList.slice(0, 3).join(', ')}${toolList.length > 3 ? '...' : ''}`); } // Test consent-required tools const consentTools = Object.entries(tools) .filter(([_, config]) => config.requiresConsent) .map(([name, _]) => name); console.log('āœ… Consent-required tools:', consentTools.length); if (consentTools.length > 0) { console.log(' -', consentTools.join(', ')); } verifier.destroy(); } async function testVerificationPerformance() { console.log('\n⚔ Testing Verification Performance'); console.log('================================='); const verifier = new TrustedToolVerifier({ enableChecksumVerification: true, enableSignatureVerification: false, // Disable for performance test cacheTtlMs: 5000 }); const testTool = '/bin/echo'; // Use echo as it's commonly available const iterations = 10; try { console.log(`āœ“ Testing ${iterations} verifications...`); const startTime = Date.now(); for (let i = 0; i < iterations; i++) { await verifier.verifyTool(testTool); } const endTime = Date.now(); const totalTime = endTime - startTime; const avgTime = totalTime / iterations; console.log('āœ… Performance results:'); console.log(` - Total time: ${totalTime}ms`); console.log(` - Average per verification: ${avgTime.toFixed(2)}ms`); console.log(` - Verifications per second: ${(1000 / avgTime).toFixed(2)}`); const metrics = verifier.getVerificationMetrics(); console.log(` - Cache hit ratio: ${((metrics.cacheHits / metrics.totalVerifications) * 100).toFixed(1)}%`); } catch (error) { console.log('āš ļø Performance test skipped (echo not available):', error.message); } verifier.destroy(); } async function runAllTrustedToolTests() { try { await testTrustedToolVerifier(); await testTrustedToolTypes(); await testVerificationPerformance(); console.log('\nšŸ“Š Trusted Tool Verification Test Summary:'); console.log('āœ… All trusted tool verification tests completed successfully'); console.log('šŸŽ‰ Cryptographic tool verification system is fully functional!'); return true; } catch (error) { console.error('āŒ Trusted tool verification tests failed:', error); console.error(error.stack); return false; } } // Run tests if called directly if (import.meta.url === `file://${process.argv[1]}`) { runAllTrustedToolTests().then(success => { process.exit(success ? 0 : 1); }); } export { runAllTrustedToolTests };

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/moikas-code/moidvk'

If you have feedback or need assistance with the MCP directory API, please join our Discord server