public void addNewItems(TestItem... items) { newItems.addAll(Do.listOf(items)); }
/** * Set "owner" role to given key or key record * @param keyOrRecord key or key record to set "creator" role to * @return owner role */ @NonNull public Role setOwnerKey(Object keyOrRecord) { return setRole("owner", Do.listOf(keyOrRecord)); }
@Test public void sample() throws Exception { HashSet<String> x = new HashSet<>(Do.listOf("aa", "bb", "cc", "cd")); // RandomAccess-ed lists have different algorithms, so test them separately HashSet<String> y = new HashSet<>(); ArrayList<String> z = new ArrayList<>(Do.listOf("aa", "bb", "cc", "cd")); HashSet<String> t = new HashSet<>(); int repetitions = 10000; for (int i = 0; i < repetitions; i++) { y.add(Do.sample(x)); t.add(Do.sample(z)); } assertEquals(y,x); }
@Override public void deliver(NodeInfo toNode, Notification notification) { if(!shutdown) { try { byte[] data = packNotifications(myInfo, Do.listOf(notification)); try { unpack(data); } catch (Exception e) { System.err.println("-- pack test failed -- " + e); e.printStackTrace(); System.exit(75); } adapter.send(toNode, data); } catch (InterruptedException e) { System.err.println("expected interrupt exception " + e.toString()); } catch (Exception e) { System.out.println("--------------> " + shutdown + " " + myInfo + " " + adapter + " " + consumer); e.printStackTrace(); } } }
@Override public void deliver(NodeInfo toNode, Notification notification) { try { byte[] data = packNotifications(myInfo, Do.listOf(notification)); logNotification(notification,toNode); if(adapter != null) { adapter.send(toNode, data); } else { report(getLabel(), "UDPAdapter is null"); } } catch (InterruptedException e) { report(getLabel(), "Expected interrupted exception"); } catch (Exception e) { report(getLabel(), "deliver exception: " + e.getMessage()); e.printStackTrace(); } }
@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 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 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 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 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 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 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 nestedLists() throws Exception { Binder a = Binder.fromKeysValues("hello", "world", "foo", "bar"); Binder b = Binder.fromKeysValues("hello", "world", "foo", "bar"); ArrayList<Object> r1 = Do.listOf(a, a, b, a ); ArrayList<Object> r2 = Do.listOf(a, b, b, b); assertNull(Delta.between(r1, r2)); b.put("foo", 117); ListDelta ld = Delta.between(r1, r2); assertNotNull(ld); assertFalse(ld.isEmpty()); assertEquals(2, ld.getChanges().size()); MapDelta md = (MapDelta) ld.getChange(1); assertNotNull(md); assertEquals(1,md.getChanges().size()); ChangedItem ci = (ChangedItem) md.getChange("foo"); assertEquals("bar", ci.oldValue()); assertEquals(117, ci.newValue()); assertEquals(2, ld.getChanges().size()); md = (MapDelta) ld.getChange(3); assertNotNull(md); assertEquals(1,md.getChanges().size()); ci = (ChangedItem) md.getChange("foo"); assertEquals("bar", ci.oldValue()); assertEquals(117, ci.newValue()); List nestedDelta = ld.getNestedDelta(); assertEquals(2, nestedDelta.size()); }
@Test public void itemResultWithErrors() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); ModifyDataPermission modifyDataPermission = new ModifyDataPermission(contract.getRole("owner"), new Binder()); modifyDataPermission.addField("value", Do.listOf("0", "1")); contract.addPermission(modifyDataPermission); contract.getStateData().set("value", "1"); contract.seal(); node.registerItem(contract); node.waitItem(contract.getId(), 5000); Contract contract2 = contract.createRevision(); contract2.addSignerKey(TestKeys.privateKey(0)); contract2.getStateData().set("value", "2"); contract2.seal(); node.registerItem(contract2); ItemResult itemResult = node.waitItem(contract2.getId(), 5000); System.out.println("itemResult: " + itemResult.state); System.out.println("errors: " + itemResult.errors); if (itemResult.errors != null) { for (ErrorRecord er : itemResult.errors) System.out.println(" error: " + er); } assertNotNull(itemResult.errors); assertEquals(true, itemResult.errors.size() > 0); }
@Test public void modifyExpiresAtWhiteList() throws Exception { ZonedDateTime now = ZonedDateTime.ofInstant(Instant.ofEpochSecond(ZonedDateTime.now().toEpochSecond()), ZoneId.systemDefault()); Contract contract = new Contract(TestKeys.privateKey(0)); ModifyDataPermission modifyDataPermission = new ModifyDataPermission(contract.getRole("owner"), new Binder()); modifyDataPermission.addField("/expires_at", Do.listOf(now.plusDays(1), now.plusDays(2))); contract.addPermission(modifyDataPermission); contract.seal(); Contract changed = contract.createRevision(); changed.addSignerKey(TestKeys.privateKey(0)); changed.setExpiresAt(now.plusDays(1)); changed.seal(); changed.check(); changed.traceErrors(); assertTrue(changed.isOk()); changed = contract.createRevision(); changed.addSignerKey(TestKeys.privateKey(0)); changed.setExpiresAt(now.plusDays(2)); changed.seal(); changed.check(); assertTrue(changed.isOk()); changed = contract.createRevision(); changed.addSignerKey(TestKeys.privateKey(0)); changed.setExpiresAt(now.plusDays(3)); changed.seal(); changed.check(); assertTrue(!changed.isOk()); }