|
| 1 | +// Flags: --permission --allow-fs-read=* --allow-fs-write=* --allow-natives-syntax --expose-internals --no-warnings |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const assert = require('assert'); |
| 6 | +const { internalBinding } = require('internal/test/binding'); |
| 7 | +const path = require('path'); |
| 8 | + |
| 9 | +// Test that process.permission.has() uses the V8 fast API path. |
| 10 | + |
| 11 | +// Test with scope only (no resource argument). |
| 12 | +function testHasScope() { |
| 13 | + assert.strictEqual(process.permission.has('fs.read'), true); |
| 14 | + assert.strictEqual(process.permission.has('fs.write'), true); |
| 15 | + assert.strictEqual(process.permission.has('child'), false); |
| 16 | + assert.strictEqual(process.permission.has('worker'), false); |
| 17 | +} |
| 18 | + |
| 19 | +// Test with scope and resource argument. |
| 20 | +function testHasResource() { |
| 21 | + assert.strictEqual(process.permission.has('fs.read', __filename), true); |
| 22 | + assert.strictEqual(process.permission.has('fs.write', __dirname), true); |
| 23 | + assert.strictEqual( |
| 24 | + process.permission.has('fs.read', path.resolve('/nonexistent')), |
| 25 | + true |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +// Test with invalid scope (should return false via fast path). |
| 30 | +function testHasInvalid() { |
| 31 | + assert.strictEqual(process.permission.has('invalid-key'), false); |
| 32 | +} |
| 33 | + |
| 34 | +// Warm up and optimize for the fast API path. |
| 35 | +eval('%PrepareFunctionForOptimization(testHasScope)'); |
| 36 | +testHasScope(); |
| 37 | +eval('%OptimizeFunctionOnNextCall(testHasScope)'); |
| 38 | +testHasScope(); |
| 39 | + |
| 40 | +eval('%PrepareFunctionForOptimization(testHasResource)'); |
| 41 | +testHasResource(); |
| 42 | +eval('%OptimizeFunctionOnNextCall(testHasResource)'); |
| 43 | +testHasResource(); |
| 44 | + |
| 45 | +eval('%PrepareFunctionForOptimization(testHasInvalid)'); |
| 46 | +testHasInvalid(); |
| 47 | +eval('%OptimizeFunctionOnNextCall(testHasInvalid)'); |
| 48 | +testHasInvalid(); |
| 49 | + |
| 50 | +if (common.isDebug) { |
| 51 | + const { getV8FastApiCallCount } = internalBinding('debug'); |
| 52 | + // After optimization: testHasScope = 4, testHasResource = 3, testHasInvalid = 1 |
| 53 | + assert.strictEqual(getV8FastApiCallCount('permission.has'), 8); |
| 54 | +} |
0 commit comments