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); }
@Test public void shouldNotAllowToSetQuorumInConstructor() { try { new ListRole("roles", ListRole.Mode.QUORUM, Collections.emptyList()); 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 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.")); } }
result = new RoleLink(name); } else if(type.equalsIgnoreCase("list")) { result = new ListRole(name); } else { throw new IllegalArgumentException("Unknown role type: " + type);
@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 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 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 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 sign() throws Exception { PrivateKey key1 = new PrivateKey(2048); PrivateKey key2 = new PrivateKey(2048); Files.write(Paths.get(basePath + "signKey1.privateKey.unikey"),key1.pack(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); Files.write(Paths.get(basePath + "signKey2.privateKey.unikey"),key2.pack(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); HashSet<PrivateKey> issuers = new HashSet<>(); issuers.add(key1); HashSet<PublicKey> owners = new HashSet<>(); owners.add(key1.getPublicKey()); Contract contract = ContractsService.createTokenContract(issuers, owners, new BigDecimal("10000.50"), new BigDecimal("0.01")); SimpleRole issuer1 = new SimpleRole("issuer1",new KeyRecord(key1.getPublicKey())); SimpleRole issuer2 = new SimpleRole("issuer2",new KeyRecord(key2.getPublicKey())); ListRole issuer = new ListRole("issuer",ListRole.Mode.ALL,Do.listOf(issuer1,issuer2)); contract.registerRole(issuer); contract.seal(); contract.check(); assertFalse(contract.isOk()); Files.write(Paths.get(basePath + "sign.unicon"),contract.getPackedTransaction(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); assertFalse(CLIMain.loadContract(basePath + "sign.unicon").check()); callMain("--sign",basePath + "sign.unicon","--keys",basePath + "signKey2.privateKey.unikey"); callMain("--sign",basePath + "sign.unicon","--keys",basePath + "signKey2.privateKey.unikey","--output", basePath + "sign_2.unicon"); assertTrue(CLIMain.loadContract(basePath + "sign_2.unicon").check()); }
@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 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 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)); }
@Test public void shouldPerformRoleWithQuorumMode() { Contract c = new Contract(); 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(3).getPublicKey())); c.registerRole(s1); c.registerRole(s2); c.registerRole(s3); ListRole roleList = new ListRole("listQuorumMode", 2, Do.listOf(s1, s3, s2)); HashSet<AbstractKey> keys = new HashSet<>(Do.listOf( ListRoleTest.keys.get(0).getPublicKey(), ListRoleTest.keys.get(2).getPublicKey())); assertTrue(roleList.isAllowedForKeys(keys)); }
@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 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()) )); }
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 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")); }
private Contract createNetConfigContract(List<NodeInfo> netConfig,PrivateKey issuerKey) throws IOException { Contract contract = new Contract(); contract.setIssuerKeys(issuerKey.getPublicKey()); contract.registerRole(new RoleLink("creator", "issuer")); 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); RoleLink ownerLink = new RoleLink("ownerlink","owner"); ChangeOwnerPermission changeOwnerPermission = new ChangeOwnerPermission(ownerLink); HashMap<String,Object> fieldsMap = new HashMap<>(); fieldsMap.put("net_config",null); Binder modifyDataParams = Binder.of("fields",fieldsMap); ModifyDataPermission modifyDataPermission = new ModifyDataPermission(ownerLink,modifyDataParams); contract.addPermission(changeOwnerPermission); contract.addPermission(modifyDataPermission); contract.setExpiresAt(ZonedDateTime.now().plusYears(40)); contract.getStateData().set("net_config",netConfig); contract.addSignerKey(issuerKey); contract.seal(); return contract; }
ListRole followerAllRole = new ListRole("all", 0, new ArrayList<>()); List<Role> followerAllRoles = Do.listOf(followerAllRole);