Skip to content

Commit

Permalink
added wasm test and some test cases to unit engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptroger committed Sep 4, 2024
1 parent 575e9c3 commit 02fb396
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,73 @@ describe('OpenPolicyAgentEngine', () => {

const response = await e.evaluate(evaluation)

expect(response.decision).toEqual(Decision.FORBID)
})
it('forbids a transfer of 9223372036854775808 wei', async () => {
const e = await new OpenPolicyAgentEngine({
policies,
entities,
resourcePath: await getConfig('resourcePath')
}).load()

const request: Request = {
action: Action.SIGN_TRANSACTION,
nonce: 'test-nonce',
transactionRequest: {
from: '0x0301e2724a40E934Cce3345928b88956901aA127',
to: '0x76d1b7f9b3F69C435eeF76a98A415332084A856F',
value: toHex(9223372036854775808n),
chainId: 1
},
resourceId: 'eip155:eoa:0x0301e2724a40e934cce3345928b88956901aa127'
}

const evaluation: EvaluationRequest = {
authentication: await getJwt({
privateKey: FIXTURE.UNSAFE_PRIVATE_KEY.Bob,
sub: FIXTURE.USER.Bob.id,
request
}),
request
}

const response = await e.evaluate(evaluation)

expect(response.decision).toEqual(Decision.FORBID)
})
it('forbids a transfer of 18446744073709551617 wei', async () => {
const e = await new OpenPolicyAgentEngine({
policies,
entities,
resourcePath: await getConfig('resourcePath')
}).load()

const request: Request = {
action: Action.SIGN_TRANSACTION,
nonce: 'test-nonce',
transactionRequest: {
from: '0x0301e2724a40E934Cce3345928b88956901aA127',
to: '0x76d1b7f9b3F69C435eeF76a98A415332084A856F',
value: toHex(18446744073709552102n),
chainId: 1
},
resourceId: 'eip155:eoa:0x0301e2724a40e934cce3345928b88956901aa127'
}

const evaluation: EvaluationRequest = {
authentication: await getJwt({
privateKey: FIXTURE.UNSAFE_PRIVATE_KEY.Bob,
sub: FIXTURE.USER.Bob.id,
request
}),
request
}

const response = await e.evaluate(evaluation)

expect(response.decision).toEqual(Decision.FORBID)
})


// 10k eth
it('forbids a transfer of 10000000000000000000000 wei', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { build, getRegoCorePath } from '../../util/wasm-build.util';
import { getRegoRuleTemplatePath } from '../../util/rego-transpiler.util';
import { LoadedPolicy, loadPolicy } from '@open-policy-agent/opa-wasm';
import { POLICY_ENTRYPOINT } from '../../../open-policy-agent.constant';
import { ConfigModule, ConfigService } from '@narval/config-module'
import {
FIXTURE,
ValueOperators,
} from '@narval/policy-engine-shared'
import { Path, PathValue } from '@nestjs/config'
import { Test, TestingModule } from '@nestjs/testing'
import { Config, load } from '../../../../policy-engine.config'


const getConfig = async <P extends Path<Config>>(propertyPath: P): Promise<PathValue<Config, P>> => {
const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule.forRoot({ load: [load] })]
}).compile()

const service = module.get<ConfigService<Config>>(ConfigService)

return service.get(propertyPath)
}

describe('to_number in WebAssembly', () => {
let wasm;
let opa: LoadedPolicy;

beforeAll(async () => {
const resourcePath = await getConfig('resourcePath')
wasm = await build({
policies: [{
id: 'can transfer 1 wei',
description: 'Permit to transfer up to 1 wei',
when: [
{
criterion: 'checkIntentAmount',
args: {
value: '10000000',
operator: 'gte' as ValueOperators
}
}
],
then: 'permit'
}],
path: `/tmp/armory-policy-bundle-${'someId'}`,
regoCorePath: getRegoCorePath(resourcePath),
regoRuleTemplatePath: getRegoRuleTemplatePath(resourcePath)
})

opa = await loadPolicy(wasm, undefined, {
'time.now_ns': () => new Date().getTime() * 1000000,
'time.format': () => new Date().toISOString().split('T')[0],
'time.parse_ns': () => {
const now = new Date()
const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate())
return startOfDay.getTime() * 1000000
},
})
opa.setData(FIXTURE.ENTITIES)
})
const testCases = [
{ input: "9223372036854775807", expected: [{"result": {"default": false, "permit": true, "reasons": ['can transfer 1 wei']}}], description: "2^63 - 1" },
{ input: "9223372036854775808", expected: [{"result": {"default": false, "permit": true, "reasons": ['can transfer 1 wei']}}], description: "2^63" },
{ input: "9223372036854776000", expected: [{"result": {"default": false, "permit": true, "reasons": ['can transfer 1 wei']}}], description: "Slightly above 2^63" },
{ input: "18446744073709551615", expected: [{"result": {"default": false, "permit": true, "reasons": ['can transfer 1 wei']}}], description: "2^64 - 1" },
{ input: "18446744073709551616", expected: [{"result": {"default": false, "permit": true, "reasons": ['can transfer 1 wei']}}], description: "2^64" },
{ input: "18446744073709551617", expected: [{"result": {"default": false, "permit": true, "reasons": ['can transfer 1 wei']}}], description: "2^64 + 1" },
];

test.each(testCases)('handles $description correctly', async ({ input, expected }) => {
const result = await opa.evaluate(input, POLICY_ENTRYPOINT)
expect(result).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class OpenPolicyAgentEngine implements Engine<OpenPolicyAgentEngine> {
const now = new Date()
const startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate())
return startOfDay.getTime() * 1000000
}
},
})

this.opa.setData(toData(this.getEntities()))
Expand Down

0 comments on commit 02fb396

Please sign in to comment.