public JSApiSimpleRole(String name, String... addresses) throws KeyAddress.IllegalAddressException { List<KeyAddress> keyAddresses = new ArrayList<>(); for (int i = 0; i < addresses.length; ++i) { keyAddresses.add(new KeyAddress(addresses[i])); } simpleRole = new SimpleRole(name, keyAddresses); }
/** * Set role with given name to given keys * @param name role name * @param keys keys to set role to * @return registened role */ @NonNull private Role setRole(String name, Collection keys) { return registerRole(new SimpleRole(name, keys)); }
@Test public void testAddressRole() throws Exception { Set<KeyAddress> keyAddresses = new HashSet<>(); keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true)); SimpleRole sr = new SimpleRole("tr1", keyAddresses); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); Set<PublicKey> pubKeys = new HashSet<>(); pubKeys.add(keys.get(0).getPublicKey()); assertTrue(sr.isAllowedForKeys(pubKeys)); assertTrue(r1.isAllowedForKeys(pubKeys)); assertEquals(sr, r1); }
@Test public void testCloneAsAddressRole() throws Exception { Set<KeyAddress> keyAddresses = new HashSet<>(); keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true)); SimpleRole sr = new SimpleRole("tr1", keyAddresses); SimpleRole r1 = sr.cloneAs("tr2"); SimpleRole r2 = r1.cloneAs("tr1"); assertEquals(sr, r2); } }
@Test public void serializeNone() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(r1.getReferences(Role.RequiredMode.ANY_OF).isEmpty()); assertTrue(r1.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); }
@Test public void serializeMany() throws Exception { SimpleRole sr = new SimpleRole("tr1"); keys.forEach(k-> sr.addKeyRecord(new KeyRecord(k.getPublicKey()))); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); Set<PublicKey> kk = ((SimpleRole)r1).getSimpleKeys(); keys.forEach(k->assertTrue(kk.contains(k.getPublicKey()))); }
@Test public void serializeQuorum() throws Exception { SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); SimpleRole s3 = new SimpleRole("owner3"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); s3.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); ListRole roleList = new ListRole("listAnyMode", 2, Do.listOf(s1, s2, s3)); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
@Test public void serializeAny() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(sr.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).size(),1); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).iterator().next(),"ref"); }
@Test public void serializeAll() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref", Role.RequiredMode.ALL_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(sr.getReferences(Role.RequiredMode.ANY_OF).isEmpty()); assertEquals(sr.getReferences(Role.RequiredMode.ALL_OF).size(),1); assertEquals(sr.getReferences(Role.RequiredMode.ALL_OF).iterator().next(),"ref"); }
@Test public void serializeAny() throws Exception { SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); ListRole roleList = new ListRole("listAnyMode", ListRole.Mode.ANY, Do.listOf(s1, s2)); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
@Test public void shouldPerformRoleWithAnyMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAnyMode", ListRole.Mode.ANY, Do.listOf(s1, s2)); assertTrue(roleList.isAllowedForKeys(new HashSet<>(Do.listOf(keys.get(1).getPublicKey())))); }
@Test public void shouldNotPerformRoleWithAllMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAllMode", ListRole.Mode.ALL, Do.listOf(s1, s2)); HashSet<AbstractKey> keys = new HashSet<>(Do.listOf( ListRoleTest.keys.get(1).getPublicKey())); assertFalse(roleList.isAllowedForKeys(keys)); }
@Test public void serializeBoth() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref1", Role.RequiredMode.ALL_OF); sr.addRequiredReference("ref2", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertEquals(r1.getReferences(Role.RequiredMode.ALL_OF).size(),1); assertEquals(r1.getReferences(Role.RequiredMode.ANY_OF).size(),1); assertEquals(r1.getReferences(Role.RequiredMode.ALL_OF).iterator().next(),"ref1"); assertEquals(r1.getReferences(Role.RequiredMode.ANY_OF).iterator().next(),"ref2"); }
@Test public void shouldNotPerformRoleWithAnyMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAnyMode"); roleList.setMode(ListRole.Mode.ANY); roleList.addRole(s1) .addRole(s2); assertFalse(roleList.isAllowedForKeys(new HashSet<>(Do.listOf(keys.get(1).getPublicKey())))); }
@Test public void serializeWithMoreRoles() { SimpleRole s1 = new SimpleRole("s1"); SimpleRole s2 = new SimpleRole("s2"); ListRole lr1 = new ListRole("lr1", ListRole.Mode.ALL, Do.listOf(s1, s2)); ListRole lr2 = new ListRole("lr2", ListRole.Mode.ANY, Do.listOf(s1, s2)); assertEquals(lr1.getRoles(), lr2.getRoles()); Binder blr = BossBiMapper.serialize(lr1); ListRole slr1 = DefaultBiMapper.deserialize(blr); blr = BossBiMapper.serialize(lr1); ListRole slr2 = DefaultBiMapper.deserialize(blr); assertEquals(slr1.getRoles(), slr2.getRoles()); }
@Test public void serializeOne() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.setComment("<New role comment>"); sr.addRequiredReference("ref1", Role.RequiredMode.ALL_OF); sr.addRequiredReference("ref2", Role.RequiredMode.ALL_OF); sr.addRequiredReference("ref3", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertEquals(r1.getComment(), "<New role comment>"); }
@Test public void serializeAll() throws Exception { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); s1.addRequiredReference("refName", Role.RequiredMode.ALL_OF); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); ListRole roleList = new ListRole("listAllMode", ListRole.Mode.ALL, Do.listOf(s1)); c.registerRole(s1); c.registerRole(roleList); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
@Test public void serializeContractWithListRole() throws Exception { Contract c = Contract.fromDslFile("./src/test_contracts/simple_root_contract.yml"); SimpleRole s1 = new SimpleRole("role"); ListRole listRole = new ListRole("owner", 1, Do.listOf(s1)); c.registerRole(listRole); Binder b = BossBiMapper.serialize(c); Contract c1 = DefaultBiMapper.deserialize(b); assertEquals(c.getRole("owner"), c1.getRole("owner")); }
@Test public void resolve() throws Exception { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); c.registerRole(s1); RoleLink r1 = new RoleLink("lover", "owner"); c.registerRole(r1); RoleLink r2 = r1.linkAs("mucker"); assertSame(s1, s1.resolve()); assertSame(s1, r1.resolve()); assertSame(s1, r2.resolve()); }
@Test(timeout = 90000) public void checkPayment_wrongIssuer() throws Exception { Contract payment = checkPayment_preparePaymentContract(checkPayment_preparePrivateKeys()); PrivateKey manufactureFakePrivateKey = new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")); SimpleRole issuerRole = new SimpleRole("issuer"); KeyRecord kr = new KeyRecord(manufactureFakePrivateKey.getPublicKey()); issuerRole.addKeyRecord(kr); payment.registerRole(issuerRole); boolean res = payment.paymentCheck(config.getUIssuerKeys()); payment.traceErrors(); assertFalse(res); }