public static byte[] read(String fileName) throws IOException { try (FileInputStream f = new FileInputStream(fileName)) { return read(f); } }
public void addNewItems(TestItem... items) { newItems.addAll(Do.listOf(items)); }
static public PrivateKey privateKey(int index) throws EncryptionError { byte[] src = Do.decodeBase64(binaryKeys[index]); return new PrivateKey(src); }
return a.equals(b); if (a.getClass().isArray() || a instanceof Collection) { Collection<?> ca = Do.collection(a); Collection<?> cb = Do.collection(b); if (ca.size() != cb.size()) return false; Iterator<?> itb = cb.iterator(); while (ita.hasNext()) { if (!deepEqualityTest(ita.next(), itb.next())) return false; return false; for (Map.Entry<?, ?> e : ma.entrySet()) { if (!deepEqualityTest(e.getValue(), mb.get(e.getKey()))) return false;
@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); }
Session(NodeInfo remoteNodeInfo) { this.remoteNodeInfo = remoteNodeInfo; localNonce = Do.randomBytes(64); state = new AtomicInteger(STATE_HANDSHAKE); handshakeStep = new AtomicInteger(HANDSHAKE_STEP_INIT); handshakeExpiresAt = Instant.now().minusMillis(HANDSHAKE_TIMEOUT_MILLIS); }
@Test public void concurrentResyncTest() throws Exception { boolean doShutdown = true; PrivateKey issuerKey = new PrivateKey(Do.read("./src/test_contracts/keys/reconfig_key.private.unikey")); TestSpace testSpace = prepareTestSpace(issuerKey); testSpace.nodes.forEach(n -> n.config.setIsFreeRegistrationsAllowedFromYaml(true)); }); Contract c = contractsToJoin.remove(Do.randomInt(contractsToJoin.size())); Contract main = c.createRevision(issuerKey); main.getStateData().set("amount","400");
public void registerSlotContractInNewItemBadRevoke() throws Exception { final PrivateKey key = new PrivateKey(Do.read(ROOT_PATH + "_xer0yfe2nn1xthc.private.unikey")); Set<PrivateKey> slotIssuerPrivateKeys = new HashSet<>(); slotIssuerPrivateKeys.add(key); stepaPrivateKeys.add(new PrivateKey(Do.read(ROOT_PATH + "keys/stepan_mamontov.private.unikey"))); refilledSlotContract.setCreator(Do.list(slotIssuerPublicKeys));
@Test public void encode() { for(int i=0; i<100;i++) { byte [] src = Do.randomBytes(256+Do.randomInt(1024)); assertArrayEquals(src, Safe58.decode(Safe58.encode(src))); } }
public static <T> Collection<T> collection(Object x) { if (x instanceof Collection) return (Collection<T>) x; return list(x); }
public void addKeys(AbstractKey... newKeys) { keys.addAll(Do.collection(newKeys)); }
public static int randomIntInRange(int inclusiveMinimum, int inclusiveMaximum) { return randomInt(inclusiveMaximum - inclusiveMinimum + 1) + inclusiveMinimum; }
@Override public Map<String, Object> toHash() throws IllegalStateException { return Do.map("key", key); }
public static void assertDeepEquals(Object a, Object b) { if (!Do.deepEqualityTest(a, b)) { System.out.println("Test failed: Not same"); System.out.println("a: " + a.toString()); System.out.println("b: " + b.toString()); fail("deep equality check failed"); } }
Binder connect() { if (serverNonce == null) serverNonce = Do.randomBytes(48); return Binder.fromKeysValues( "server_nonce", serverNonce, "session_id", ""+sessionId ); }
/** * Create new revision to be changed, signed sealed and then ready to approve. Created "revision" contract is a copy * of this contract, with all fields and references correctly set. After this call one need to change mutable * fields, add signing keys, seal it and then apss to Universa network for approval. * * @param keys initially added and signer keys. Role "creator" is set to these keys for new revision * @param transactional is {@link Transactional} section to create new revision with * @return new revision of this contract, identical to this one, to be modified. */ public Contract createRevision(Transactional transactional, PrivateKey... keys) { return createRevision(Do.list(keys), transactional); }
KRing(AbstractKey... kk) { keys = Do.collection(kk); }
/** * Get a random item of the collection. Effectively process {@link RandomAccess} enablied {@link List} collections. * For other types of collections converts i to an array which might require further optimization. * * @param source collection * * @return random item. null if the collection is empty. */ public static <T> T sample(Collection source) { int size = source.size(); if (size <= 0) return null; int i = Do.randomInt(size); if (source instanceof List && source instanceof RandomAccess) return (T) ((List) source).get(i); else return (T) source.toArray()[i]; }
@Test(timeout = 100) public void send() throws Exception { StreamConnector sa = new StreamConnector(); JsonConnector jsc = new JsonConnector(sa.getInputStream(), sa.getOutputStream()); jsc.send(Do.map("hello", "мыльня")); Map<String, Object> res = jsc.receive(); assertEquals(1, res.size()); assertEquals("мыльня", res.get("hello")); }
public static String readToString(InputStream inputStream) throws IOException { return new String(read(inputStream), utf8); }