Search in sources :

Example 6 with RoleLink

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());
    }
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) PrivateKey(com.icodici.crypto.PrivateKey) PublicKey(com.icodici.crypto.PublicKey) Test(org.junit.Test)

Example 7 with RoleLink

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));
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) BufferedLogger(net.sergeych.tools.BufferedLogger) Binder(net.sergeych.tools.Binder) File(java.io.File) Test(org.junit.Test)

Aggregations

RoleLink (com.icodici.universa.contract.roles.RoleLink)7 PrivateKey (com.icodici.crypto.PrivateKey)4 PublicKey (com.icodici.crypto.PublicKey)4 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)4 ListRole (com.icodici.universa.contract.roles.ListRole)4 Test (org.junit.Test)4 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)3 ModifyDataPermission (com.icodici.universa.contract.permissions.ModifyDataPermission)2 Role (com.icodici.universa.contract.roles.Role)2 Binder (net.sergeych.tools.Binder)2 Permission (com.icodici.universa.contract.permissions.Permission)1 File (java.io.File)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BufferedLogger (net.sergeych.tools.BufferedLogger)1 Before (org.junit.Before)1