public JSApiListRole(String name, String mode, JSApiRole... roles) { List<Role> listRoles = new ArrayList<>(); for (int i = 0; i < roles.length; ++i) listRoles.add(roles[i].extractRole(new JSApiAccessor())); listRole = new ListRole(name, modeStringToEnum(mode), listRoles); }
/** * Create new role combining other roles (sub-roles) * * @param name is role name * @param mode is mode of sub-roles combining: "and", "or" and "any N of" principle * @param roles is collection of sub-roles */ public ListRole(String name, Mode mode, @NonNull Collection<Role> roles) { super(name); setMode(mode); addAll(roles); }
@Override public void deserialize(Binder data, BiDeserializer deserializer) { super.deserialize(data, deserializer); this.quorumSize = data.getInt("quorumSize", 0); Object mode = data.getOrDefault("mode", null); if (mode != null) { this.mode = Mode.valueOf((String) mode); } List<Binder> roles = data.getList("roles", null); if (roles != null) { this.roles.clear(); roles.forEach(role -> addRole(deserializer.deserialize(role))); } }
@Test public void shouldNotAllowToSetQuorum() { ListRole listRole = new ListRole("roles"); try { listRole.setMode(ListRole.Mode.QUORUM); fail("Expected exception to be thrown."); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().equalsIgnoreCase("Only ANY or ALL of the modes should be set.")); } }
@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())))); }
ListRole listRole = new ListRole("list_role"); SimpleRole ownerRole = new SimpleRole("owner", stepaPrivateKeys); listRole.addRole(ownerRole); listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);
private Contract createNetConfigContract(Contract contract, List<NodeInfo> netConfig, Collection<PrivateKey> currentConfigKeys) throws IOException { contract = contract.createRevision(); ListRole listRole = new ListRole("owner"); for(NodeInfo ni: netConfig) { SimpleRole role = new SimpleRole(ni.getName()); contract.registerRole(role); role.addKeyRecord(new KeyRecord(ni.getPublicKey())); listRole.addRole(role); } listRole.setQuorum(netConfig.size()-1); contract.registerRole(listRole); contract.getStateData().set("net_config",netConfig); List<KeyRecord> creatorKeys = new ArrayList<>(); for(PrivateKey key : currentConfigKeys) { creatorKeys.add(new KeyRecord(key.getPublicKey())); contract.addSignerKey(key); } contract.setCreator(creatorKeys); contract.seal(); return contract; }
@Test public void shouldNotPerformRoleWithQuorumMode() { 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("listQuorumMode"); roleList.setQuorum(2); roleList.addAll(Do.listOf(s1, s2)); c.registerRole(roleList); HashSet<AbstractKey> keys = new HashSet<>(Do.listOf( ListRoleTest.keys.get(1).getPublicKey())); assertFalse(roleList.isAllowedForKeys(keys)); }
@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())))); }
ListRole listRole = new ListRole("list_role"); SimpleRole ownerRole = new SimpleRole("owner", stepaPrivateKeys); listRole.setMode(ListRole.Mode.ALL); listRole.addRole(ownerRole); listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);
@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 getAllAddresses() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); ListRole owner = new ListRole("owner"); owner.addRole(new SimpleRole("owner", Arrays.asList(TestKeys.publicKey(0), TestKeys.publicKey(1)))); owner.addRole(new SimpleRole("owner", Arrays.asList(TestKeys.publicKey(2).getLongAddress()))); owner.addRole(new SimpleRole("owner", Arrays.asList(TestKeys.publicKey(3).getShortAddress(), TestKeys.publicKey(4)))); contract.registerRole(owner); List<String> addresses = contract.getRole("owner").getAllAddresses(); System.out.println("owner: " + addresses); for (String addr : addresses) assertThat(addr, Matchers.anyOf( Matchers.equalTo(TestKeys.publicKey(0).getShortAddress().toString()), Matchers.equalTo(TestKeys.publicKey(1).getShortAddress().toString()), Matchers.equalTo(TestKeys.publicKey(2).getLongAddress().toString()), Matchers.equalTo(TestKeys.publicKey(3).getShortAddress().toString()), Matchers.equalTo(TestKeys.publicKey(4).getShortAddress().toString()) )); }
public boolean containReference(String name) { if(requiredAllReferences.contains(name) || requiredAnyReferences.contains(name)) return true; if(this instanceof RoleLink) { return ((RoleLink)this).getRole().containReference(name); } if(this instanceof ListRole) { return ((ListRole)this).getRoles().stream().anyMatch(r -> r.containReference(name)); } return false; }
public void setQuorum(int quorum) { listRole.setQuorum(quorum); }
@Override public boolean isAllowedForKeys(PublicKey... keys) { HashSet<PublicKey> keySet = new HashSet<>(Arrays.asList(keys)); return listRole.isAllowedForKeys(keySet); }
/** * Create new role combining other roles (sub-roles) in the "any N of" principle ({@link Mode#QUORUM}). * * @param name is role name * @param quorumSize is N in "any N of" principle * @param roles is collection of sub-roles */ public ListRole(String name, int quorumSize, @NonNull Collection<Role> roles) { super(name); this.mode = Mode.QUORUM; this.quorumSize = quorumSize; addAll(roles); }
/** * Get role as string. * * @return string with data of role */ @Override public String toString() { return String.format("ListRole<%s:%s:%s:%s>", System.identityHashCode(this), getName(), this.mode == null ? "" : this.mode == Mode.QUORUM ? this.mode.name().toLowerCase() + "_" + this.quorumSize : this.mode.name().toLowerCase(), this.roles); }
@Test public void shouldPerformRoleWithAllMode() { 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"); roleList.setMode(ListRole.Mode.ALL); roleList.addRole(s1) .addRole(s2); HashSet<AbstractKey> keys = new HashSet<>(Do.listOf( ListRoleTest.keys.get(0).getPublicKey(), ListRoleTest.keys.get(1).getPublicKey())); assertTrue(roleList.isAllowedForKeys(keys)); }
ListRole listRole = new ListRole("list_role"); SimpleRole ownerRole = new SimpleRole("owner", stepaPrivateKeys); listRole.addRole(ownerRole); listRole.addRequiredReference("certification_contract", Role.RequiredMode.ALL_OF);