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);
}
});
}
}
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);
}
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()));
}
Aggregations