use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.
the class WebSessionSelfTest method testSingleRequest.
/**
* Tests single request to a server. Checks modification attribute in cache.
*
* @param cfg Configuration.
* @throws Exception If failed.
*/
private void testSingleRequest(String cfg) throws Exception {
Server srv = null;
try {
srv = startServer(TEST_JETTY_PORT, cfg, null, new SessionCreateServlet(keepBinary()));
URLConnection conn = new URL("http://localhost:" + TEST_JETTY_PORT + "/ignitetest/test").openConnection();
conn.connect();
try (BufferedReader rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
String sesId = rdr.readLine();
if (!keepBinary()) {
IgniteCache<String, HttpSession> cache = G.ignite().cache(getCacheName());
assertNotNull(cache);
HttpSession ses = cache.get(sesId);
assertNotNull(ses);
assertEquals("val1", ses.getAttribute("key1"));
} else {
final IgniteCache<String, WebSessionEntity> cache = G.ignite().cache(getCacheName());
assertNotNull(cache);
final WebSessionEntity entity = cache.get(sesId);
assertNotNull(entity);
final byte[] data = entity.attributes().get("key1");
assertNotNull(data);
final Marshaller marshaller = G.ignite().configuration().getMarshaller();
final String val = marshaller.unmarshal(data, getClass().getClassLoader());
assertEquals("val1", val);
}
}
} finally {
stopServer(srv);
}
}
use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.
the class WebSessionSelfTest method testChangeSessionId.
/**
* Tests session id change.
*
* @throws Exception Exception If failed.
*/
public void testChangeSessionId() throws Exception {
String newWebSesId;
Server srv = null;
try {
srv = startServer(TEST_JETTY_PORT, "/modules/core/src/test/config/websession/example-cache.xml", null, new SessionIdChangeServlet());
Ignite ignite = G.ignite();
URLConnection conn = new URL("http://localhost:" + TEST_JETTY_PORT + "/ignitetest/chngsesid").openConnection();
conn.connect();
try (BufferedReader rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
// checks if the old session object is invalidated.
String oldId = rdr.readLine();
assertNotNull(oldId);
// id from genuine session
String newGenSesId = rdr.readLine();
assertNotNull(newGenSesId);
assertFalse(newGenSesId.equals(oldId));
// id from replicated session
newWebSesId = rdr.readLine();
assertNotNull(newWebSesId);
assertTrue(newGenSesId.equals(newWebSesId));
if (!keepBinary()) {
IgniteCache<String, HttpSession> cache = ignite.cache(getCacheName());
assertNotNull(cache);
Thread.sleep(1000);
HttpSession ses = cache.get(newWebSesId);
assertNotNull(ses);
assertEquals("val1", ses.getAttribute("key1"));
} else {
IgniteCache<String, WebSessionEntity> cache = ignite.cache(getCacheName());
assertNotNull(cache);
Thread.sleep(1000);
WebSessionEntity ses = cache.get(newWebSesId);
assertNotNull(ses);
final Marshaller marshaller = ignite.configuration().getMarshaller();
assertEquals("val1", marshaller.<String>unmarshal(ses.attributes().get("key1"), getClass().getClassLoader()));
}
}
conn = new URL("http://localhost:" + TEST_JETTY_PORT + "/ignitetest/simple").openConnection();
conn.addRequestProperty("Cookie", "JSESSIONID=" + newWebSesId);
conn.connect();
try (BufferedReader rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
// checks if it can be handled with the subsequent request.
String sesId = rdr.readLine();
assertTrue(newWebSesId.equals(sesId));
String attr = rdr.readLine();
assertEquals("val1", attr);
String reqSesValid = rdr.readLine();
assertEquals("true", reqSesValid);
assertEquals("invalidated", rdr.readLine());
}
} finally {
stopServer(srv);
}
}
use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.
the class AbstractDiscoverySelfTest method writeObject.
/**
* @param node Grid node.
* @throws IOException If write failed.
*/
private void writeObject(ClusterNode node) throws Exception {
Marshaller marshaller = getTestResources().getMarshaller();
OutputStream out = new NullOutputStream();
try {
marshaller.marshal(node, out);
} finally {
U.close(out, null);
}
}
use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.
the class SocketStreamerUnmarshalVulnerabilityTest method testExploit.
/**
* @param positive Positive.
*/
private void testExploit(boolean positive) throws Exception {
try {
Ignite ignite = startGrid();
SocketStreamer<Exploit, Integer, String> sockStmr = null;
try (IgniteDataStreamer<Integer, String> stmr = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
stmr.allowOverwrite(true);
stmr.autoFlushFrequency(10);
sockStmr = new SocketStreamer<>();
sockStmr.setIgnite(ignite);
sockStmr.setStreamer(stmr);
sockStmr.setPort(port);
sockStmr.setSingleTupleExtractor(new StreamSingleTupleExtractor<Exploit, Integer, String>() {
@Override
public Map.Entry<Integer, String> extract(Exploit msg) {
return new IgniteBiTuple<>(1, "val");
}
});
sockStmr.start();
try (Socket sock = new Socket(InetAddress.getLocalHost(), port);
OutputStream os = new BufferedOutputStream(sock.getOutputStream())) {
Marshaller marsh = new JdkMarshaller();
byte[] msg = marsh.marshal(new Exploit());
os.write(msg.length >>> 24);
os.write(msg.length >>> 16);
os.write(msg.length >>> 8);
os.write(msg.length);
os.write(msg);
} catch (IOException | IgniteCheckedException e) {
throw new IgniteException(e);
}
boolean res = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return SHARED.get();
}
}, 3000L);
if (positive)
assertTrue(res);
else
assertFalse(res);
} finally {
if (sockStmr != null)
sockStmr.stop();
}
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.marshaller.Marshaller in project ignite by apache.
the class IgniteTestResources method getMarshaller.
/**
* @return Marshaller.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
public static synchronized Marshaller getMarshaller() throws IgniteCheckedException {
String marshallerName = System.getProperty(MARSH_CLASS_NAME, GridTestProperties.getProperty(GridTestProperties.MARSH_CLASS_NAME));
Marshaller marsh;
if (marshallerName == null)
marsh = new BinaryMarshaller();
else {
try {
Class<? extends Marshaller> cls = (Class<? extends Marshaller>) Class.forName(marshallerName);
marsh = cls.newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new IgniteCheckedException("Failed to create test marshaller [marshaller=" + marshallerName + ']', e);
}
}
marsh.setContext(new MarshallerContextTestImpl());
if (marsh instanceof BinaryMarshaller) {
BinaryContext ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), new NullLogger());
IgniteUtils.invoke(BinaryMarshaller.class, marsh, "setBinaryContext", ctx, new IgniteConfiguration());
}
return marsh;
}
Aggregations