use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.
the class ContractTest method testRoleFailures.
// @Test
// public void shouldFindWithValidReference() throws Exception {
// Contract c = Contract.fromDslFile(rootPath + "references/subscriptionReference.yml");
// Contract c2 = Contract.fromDslFile(rootPath + "references/subscriptionRoot.yml");
//
// List<Contract> contracts = c2.extractByValidReference(Arrays.asList(c));
// assertNotNull(contracts);
// assertEquals(1, contracts.size());
// }
//
// @Test
// public void shouldFindWithValidReferenceSeal() throws Exception {
// Contract c = Contract.fromDslFile(rootPath + "references/subscriptionReference.yml");
// Contract c2 = Contract.fromDslFile(rootPath + "references/subscriptionRoot.yml");
//
// c = new Contract(c.seal());
// c2 = new Contract(c2.seal());
//
// List<Contract> contracts = c2.extractByValidReference(Arrays.asList(c));
// assertNotNull(contracts);
// assertEquals(1, contracts.size());
// }
@Test
public void testRoleFailures() throws Exception {
final PrivateKey key = new PrivateKey(2048);
final PublicKey publicKey = key.getPublicKey();
{
// Missing issuer
final Contract c = new Contract(key);
c.getRoles().remove("issuer");
c.seal();
c.check();
assertFalse(c.isOk());
assertNull(c.getIssuer());
assertFalse(c.getOwner().isValid());
assertFalse(c.getCreator().isValid());
}
{
// Missing creator
final Contract c = new Contract(key);
c.getRoles().remove("creator");
c.seal();
c.check();
assertFalse(c.isOk());
assertTrue(c.getIssuer().isValid());
assertTrue(c.getOwner().isValid());
assertNull(c.getCreator());
}
{
// Missing owner
final Contract c = new Contract(key);
c.getRoles().remove("owner");
c.seal();
c.check();
assertFalse(c.isOk());
assertTrue(c.getIssuer().isValid());
assertNull(c.getOwner());
assertTrue(c.getCreator().isValid());
}
{
// Test chain of links (good), then test breaking it (bad).
// issuer:key, creator->owner, owner->issuer
final Contract c = new Contract(key);
c.getRoles().remove("creator");
c.registerRole(new RoleLink("creator", "owner"));
c.seal();
c.check();
assertTrue(c.isOk());
assertTrue(c.getIssuer().isValid());
assertTrue(c.getOwner().isValid());
assertTrue(c.getCreator().isValid());
// Let's break the link in the middle
c.getRoles().remove("owner");
// We haven't called `check` once again, so it's still ok
assertTrue(c.isOk());
// and now it is no more ok.
c.check();
assertFalse(c.isOk());
assertTrue(c.getIssuer().isValid());
assertNull(c.getOwner());
assertFalse(c.getCreator().isValid());
assertFalse(c.getErrors().isEmpty());
}
{
// Test loop of links (bad).
// issuer->creator, creator->owner, owner->issuer
final Contract c = new Contract(key);
c.getRoles().clear();
c.registerRole(new RoleLink("issuer", "creator"));
c.registerRole(new RoleLink("creator", "owner"));
c.registerRole(new RoleLink("owner", "issuer"));
c.seal();
// and now it is no more ok.
c.check();
assertFalse(c.isOk());
assertFalse(c.getIssuer().isValid());
assertFalse(c.getOwner().isValid());
assertFalse(c.getCreator().isValid());
}
}
use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.
the class MainTest method startNode.
@Test
public void startNode() throws Exception {
String path = new File("src/test_node_config_v2/node1").getAbsolutePath();
System.out.println(path);
String[] args = new String[] { "--test", "--config", path, "--nolog" };
Main main = new Main(args);
main.waitReady();
BufferedLogger l = main.logger;
Client client = new Client("http://localhost:8080", TestKeys.privateKey(3), main.getNodePublicKey(), null);
Binder data = client.command("status");
data.getStringOrThrow("status");
// assertThat(data.getListOrThrow("log").size(), greaterThan(3));
BasicHttpClient.Answer a = client.request("ping");
assertEquals("200: {ping=pong}", a.toString());
Contract c = new Contract();
c.setIssuerKeys(TestKeys.publicKey(3));
c.addSignerKey(TestKeys.privateKey(3));
c.registerRole(new RoleLink("owner", "issuer"));
c.registerRole(new RoleLink("creator", "issuer"));
c.setExpiresAt(ZonedDateTime.now().plusDays(2));
byte[] sealed = c.seal();
// Bytes.dump(sealed);
Contract c1 = new Contract(sealed);
assertArrayEquals(c.getLastSealedBinary(), c1.getLastSealedBinary());
main.cache.put(c);
assertNotNull(main.cache.get(c.getId()));
URL url = new URL("http://localhost:8080/contracts/" + c.getId().toBase64String());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
assertEquals(200, con.getResponseCode());
byte[] data2 = Do.read(con.getInputStream());
assertArrayEquals(c.getPackedTransaction(), data2);
url = new URL("http://localhost:8080/network");
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
assertEquals(200, con.getResponseCode());
Binder bres = Boss.unpack((Do.read(con.getInputStream()))).getBinderOrThrow("response");
List<Binder> ni = bres.getBinders("nodes");
String pubUrls = ni.stream().map(x -> x.getStringOrThrow("url")).collect(Collectors.toList()).toString();
assertEquals("[http://localhost:8080, http://localhost:6002, http://localhost:6004, http://localhost:6006]", pubUrls);
main.shutdown();
main.logger.stopInterceptingStdOut();
;
main.logger.getCopy().forEach(x -> System.out.println(x));
}
Aggregations