/** * Quantize given permission (add cost for that permission). * Use for permissions that will be applicated, but before checking. * @param permission that will be quantized * @throws Quantiser.QuantiserException if processing cost limit is got */ public void checkApplicablePermissionQuantized(Permission permission) throws Quantiser.QuantiserException { // Add check an applicable permission quanta quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_APPLICABLE_PERM); // Add check a splitjoin permission in addition to the permission check quanta if(permission instanceof SplitJoinPermission) { quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_SPLITJOIN_PERM); } }
/** * Verify signature, but before quantize this operation. * @param key that will be quantized * @throws Quantiser.QuantiserException if processing cost limit is got */ protected void verifySignatureQuantized(PublicKey key) throws Quantiser.QuantiserException { // Add check signature quanta if(key.getBitStrength() == 2048) { quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_2048_SIG); } else { quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_4096_SIG); } }
@Test public void limit() throws Exception { byte[] hashBytes = new byte[128]; new Random().nextBytes(hashBytes); try { Quantiser q = new Quantiser(); q.reset(1000); for (int i = 0; i < 1000000; ++i) q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION); fail(); } catch(Quantiser.QuantiserException e) { return; } }
@Test public void noLimit() throws Exception { try { byte[] hashBytes = new byte[128]; new Random().nextBytes(hashBytes); Quantiser q = new Quantiser(); q.resetNoLimit(); for (int i = 0; i < 1000000; ++i) q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION); } catch(Quantiser.QuantiserException e) { fail(); } }
@Test public void fullSum() throws Exception { int wantedSum = Quantiser.QuantiserProcesses.PRICE_APPLICABLE_PERM.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_2048_SIG.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_4096_SIG.getCost() + Quantiser.QuantiserProcesses.PRICE_CHECK_REFERENCED_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_REVOKE_VERSION.getCost() + Quantiser.QuantiserProcesses.PRICE_SPLITJOIN_PERM.getCost(); try { byte[] hashBytes = new byte[128]; new Random().nextBytes(hashBytes); HashId hashId = new HashId(hashBytes); Quantiser q = new Quantiser(); q.reset(wantedSum); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_APPLICABLE_PERM); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_2048_SIG); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_4096_SIG); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_REFERENCED_VERSION); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REVOKE_VERSION); q.addWorkCost(Quantiser.QuantiserProcesses.PRICE_SPLITJOIN_PERM); assertEquals(wantedSum, q.getQuantaSum()); } catch (Quantiser.QuantiserException e) { fail(); } }
quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REGISTER_VERSION); quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_REVOKE_VERSION); quantiser.addWorkCost(Quantiser.QuantiserProcesses.PRICE_CHECK_REFERENCED_VERSION);