Search in sources :

Example 1 with MyReceiver

use of org.jgroups.util.MyReceiver in project JGroups by belaban.

the class EncryptTest method testMessageReceptionByRogue.

/**
 * Tests that the non-member does NOT receive messages from cluster {A,B,C}. The de-serialization of a message's
 * payload (encrypted with the secret key of the rogue non-member) will fail, so the message is never passed up
 * to the application.
 */
// @Test(groups=Global.FUNCTIONAL,singleThreaded=true)
public void testMessageReceptionByRogue() throws Exception {
    rogue.setReceiver(r_rogue = new MyReceiver().rawMsgs(true));
    a.setReceiver(null);
    b.setReceiver(null);
    c.setReceiver(null);
    a.send(null, "Hello from A");
    b.send(null, "Hello from B");
    c.send(null, "Hello from C");
    for (int i = 0; i < 10; i++) {
        // 3 messages!
        if (r_rogue.size() > 0)
            break;
        Util.sleep(500);
    }
    // cause a deserialization exception, but it will not be able to read their contents:
    if (r_rogue.size() > 0) {
        System.out.printf("Rogue non-member received %d message(s), but it should not be able to read deserialize " + "the contents (this should throw exceptions below):\n", r_rogue.size());
        r_rogue.list().forEach(msg -> {
            try {
                String payload = msg.getObject();
                assert !payload.startsWith("Hello from");
            } catch (Exception t) {
                System.out.printf("caught exception trying to de-serialize garbage payload into a string: %s\n", t);
            }
        });
    }
}
Also used : MyReceiver(org.jgroups.util.MyReceiver)

Example 2 with MyReceiver

use of org.jgroups.util.MyReceiver in project JGroups by belaban.

the class ForkChannelTest method testNullForkStack.

/**
 * Tests the case where we don't add any fork-stack specific protocols
 */
public void testNullForkStack() throws Exception {
    fc1 = new ForkChannel(a, "stack", "fc1");
    fc2 = new ForkChannel(a, "stack", "fc2");
    MyReceiver<Integer> r1 = new MyReceiver<>(), r2 = new MyReceiver<>();
    fc1.setReceiver(r1);
    fc2.setReceiver(r2);
    a.connect(CLUSTER);
    fc1.connect("foo");
    fc2.connect("bar");
    for (int i = 1; i <= 5; i++) {
        fc1.send(null, i);
        fc2.send(null, i + 5);
    }
    List<Integer> l1 = r1.list(), l2 = r2.list();
    for (int i = 0; i < 20; i++) {
        if (l1.size() == 5 && l2.size() == 5)
            break;
        Util.sleep(500);
    }
    System.out.println("r1: " + r1.list() + ", r2: " + r2.list());
    assert r1.size() == 5 && r2.size() == 5;
    for (int i = 1; i <= 5; i++) assert r1.list().contains(i) && r2.list().contains(i + 5);
}
Also used : ForkChannel(org.jgroups.fork.ForkChannel) MyReceiver(org.jgroups.util.MyReceiver)

Example 3 with MyReceiver

use of org.jgroups.util.MyReceiver in project JGroups by belaban.

the class EncryptTest method testCapturingOfMessageByNonMemberAndResending.

/**
 * Tests the scenario where the non-member R captures a message from some cluster member in {A,B,C}, then
 * increments the NAKACK2 seqno and resends that message. The message must not be received by {A,B,C};
 * it should be discarded.
 */
// @Test(groups=Global.FUNCTIONAL,singleThreaded=true)
public void testCapturingOfMessageByNonMemberAndResending() throws Exception {
    rogue.setReceiver(msg -> {
        System.out.printf("rogue: modifying and resending msg %s, hdrs: %s\n", msg, msg.printHeaders());
        // to prevent recursive cycle
        rogue.setReceiver(null);
        try {
            short prot_id = ClassConfigurator.getProtocolId(NAKACK2.class);
            NakAckHeader2 hdr = msg.getHeader(prot_id);
            if (hdr != null) {
                long seqno = hdr.getSeqno();
                Util.setField(Util.getField(NakAckHeader2.class, "seqno"), hdr, seqno + 1);
            } else {
                System.out.printf("Rogue was not able to get the %s header, fabricating one with seqno=50\n", NAKACK2.class.getSimpleName());
                NakAckHeader2 hdr2 = NakAckHeader2.createMessageHeader(50);
                msg.putHeader(prot_id, hdr2);
            }
            rogue.send(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    a.send(null, "Hello world from A");
    // everybody in {A,B,C} should receive this message, but NOT the rogue's resent message
    for (int i = 0; i < 10; i++) {
        if (ra.size() > 1 || rb.size() > 1 || rc.size() > 1)
            // this should NOT happen
            break;
        Util.sleep(500);
    }
    Stream.of(ra, rb, rc).map(MyReceiver::list).map(l -> l.stream().map(msg -> (String) msg.getObject()).collect(ArrayList::new, ArrayList::add, (x, y) -> {
    })).forEach(System.out::println);
    assert ra.size() == 1 : String.format("received msgs from non-member: '%s'; this should not be the case", print(ra.list()));
    assert rb.size() == 1 : String.format("received msgs from non-member: '%s'; this should not be the case", print(rb.list()));
    assert rc.size() == 1 : String.format("received msgs from non-member: '%s'; this should not be the case", print(rc.list()));
}
Also used : Arrays(java.util.Arrays) Util(org.jgroups.util.Util) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Test(org.testng.annotations.Test) GMS(org.jgroups.protocols.pbcast.GMS) NakAckHeader2(org.jgroups.protocols.pbcast.NakAckHeader2) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) MyReceiver(org.jgroups.util.MyReceiver) List(java.util.List) Stream(java.util.stream.Stream) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) Buffer(org.jgroups.util.Buffer) ByteArrayDataOutputStream(org.jgroups.util.ByteArrayDataOutputStream) org.jgroups(org.jgroups) ClassConfigurator(org.jgroups.conf.ClassConfigurator) SecretKey(javax.crypto.SecretKey) KeyStoreGenerator(org.jgroups.demos.KeyStoreGenerator) NAKACK2(org.jgroups.protocols.pbcast.NAKACK2) NakAckHeader2(org.jgroups.protocols.pbcast.NakAckHeader2) ArrayList(java.util.ArrayList) MyReceiver(org.jgroups.util.MyReceiver)

Aggregations

MyReceiver (org.jgroups.util.MyReceiver)3 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 SecretKey (javax.crypto.SecretKey)1 org.jgroups (org.jgroups)1 ClassConfigurator (org.jgroups.conf.ClassConfigurator)1 KeyStoreGenerator (org.jgroups.demos.KeyStoreGenerator)1 ForkChannel (org.jgroups.fork.ForkChannel)1 GMS (org.jgroups.protocols.pbcast.GMS)1 NAKACK2 (org.jgroups.protocols.pbcast.NAKACK2)1 NakAckHeader2 (org.jgroups.protocols.pbcast.NakAckHeader2)1 Buffer (org.jgroups.util.Buffer)1 ByteArrayDataOutputStream (org.jgroups.util.ByteArrayDataOutputStream)1 Util (org.jgroups.util.Util)1 Test (org.testng.annotations.Test)1