Search in sources :

Example 1 with SideEffect

use of dalvik.annotation.SideEffect in project robovm by robovm.

the class ContentHandlerFactoryTest method test_createContentHandler.

@SideEffect("This test affects tests that are run after this one." + " The reason are side effects due to caching in URLConnection." + " Maybe this test needs to be run in isolation.")
public void test_createContentHandler() throws IOException {
    TestContentHandlerFactory factory = new TestContentHandlerFactory();
    if (isTestable) {
        assertFalse(isCreateContentHandlerCalled);
        URL url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
        URLConnection.setContentHandlerFactory(factory);
        URLConnection con = url.openConnection();
        try {
            con.getContent();
            assertTrue(isCreateContentHandlerCalled);
            assertTrue(isGetContentCalled);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        isGetContentCalled = false;
        try {
            con.getContent(new Class[] {});
            assertTrue(isGetContentCalled);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        try {
            con.setContentHandlerFactory(factory);
            fail("java.lang.Error was not thrown.");
        } catch (java.lang.Error e) {
        //expected
        }
        try {
            con.setContentHandlerFactory(null);
            fail("java.lang.Error was not thrown.");
        } catch (java.lang.Error e) {
        //expected
        }
    } else {
        ContentHandler ch = factory.createContentHandler("text/plain");
        URL url;
        try {
            url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
            assertNotNull(ch.getContent(url.openConnection()));
        } catch (MalformedURLException e) {
            fail("MalformedURLException was thrown: " + e.getMessage());
        } catch (IOException e) {
            fail("IOException was thrown.");
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ContentHandler(java.net.ContentHandler) SideEffect(dalvik.annotation.SideEffect)

Example 2 with SideEffect

use of dalvik.annotation.SideEffect in project robovm by robovm.

the class OldURLClassLoaderTest method test_findResourceLjava_lang_String.

@SideEffect("Support_TestWebServer requires isolation.")
public void test_findResourceLjava_lang_String() throws Exception {
    File tmp = File.createTempFile("test", ".txt");
    Support_TestWebServer server = new Support_TestWebServer();
    try {
        int port = server.initServer(tmp.getAbsolutePath(), "text/html");
        URL[] urls = { new URL("http://localhost:" + port + "/") };
        ucl = new URLClassLoader(urls);
        URL res = ucl.findResource("test1");
        assertNotNull("Failed to locate resource", res);
        StringBuffer sb = getResContent(res);
        assertEquals("Returned incorrect resource", new String(Support_TestWebData.test1), sb.toString());
    } finally {
        server.close();
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) Support_TestWebServer(tests.support.Support_TestWebServer) File(java.io.File) URL(java.net.URL) SideEffect(dalvik.annotation.SideEffect)

Example 3 with SideEffect

use of dalvik.annotation.SideEffect in project robovm by robovm.

the class HttpURLConnectionTest method testProxyAuthorization.

@SideEffect("Suffers from side effect of other, currently unknown test")
public void testProxyAuthorization() throws Exception {
    // Set up test Authenticator
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "password".toCharArray());
        }
    });
    try {
        MockProxyServer proxy = new MockProxyServer("ProxyServer");
        URL url = new URL("http://remotehost:55555/requested.data");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxy.port())));
        connection.setConnectTimeout(1000);
        connection.setReadTimeout(1000);
        proxy.start();
        connection.connect();
        assertEquals("unexpected response code", 200, connection.getResponseCode());
        proxy.join();
        assertTrue("Connection did not send proxy authorization request", proxy.acceptedAuthorizedRequest);
    } finally {
        // remove previously set authenticator
        Authenticator.setDefault(null);
    }
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) InetSocketAddress(java.net.InetSocketAddress) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) SideEffect(dalvik.annotation.SideEffect)

Example 4 with SideEffect

use of dalvik.annotation.SideEffect in project robovm by robovm.

the class SoftReferenceTest method test_get_SoftReference.

@SideEffect("Causes OutOfMemoryError to test finalization")
public void test_get_SoftReference() {
    class TestObject {

        public boolean finalized;

        public TestObject() {
            finalized = false;
        }

        protected void finalize() {
            finalized = true;
        }
    }
    final ReferenceQueue rq = new ReferenceQueue();
    class TestThread extends Thread {

        public void run() {
            Object testObj = new TestObject();
            r = new SoftReference(testObj, rq);
        }
    }
    Reference ref;
    try {
        TestThread t = new TestThread();
        t.start();
        t.join();
        Vector<StringBuffer> v = new Vector<StringBuffer>();
        try {
            while (true) {
                v.add(new StringBuffer(10000));
            }
        } catch (OutOfMemoryError ofme) {
            v = null;
        }
    } catch (InterruptedException e) {
        fail("InterruptedException : " + e.getMessage());
    }
    assertNull("get() should return null " + "if OutOfMemoryError is thrown.", r.get());
    try {
        TestThread t = new TestThread();
        t.start();
        t.join();
        FinalizationTester.induceFinalization();
        ref = rq.poll();
        assertNotNull("Object not garbage collected.", ref);
        assertNull("Object is not null.", ref.get());
        assertNotNull("Object could not be reclaimed.", r.get());
    } catch (Exception e) {
        fail("Exception : " + e.getMessage());
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) SoftReference(java.lang.ref.SoftReference) Vector(java.util.Vector) SideEffect(dalvik.annotation.SideEffect)

Example 5 with SideEffect

use of dalvik.annotation.SideEffect in project robovm by robovm.

the class X509CertificateTest method testVerifyPublicKey.

/**
     * @throws SignatureException
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws CertificateException
     * {@link Certificate#verify(PublicKey)}
     */
@SideEffect("Destroys MD5 provider, hurts succeeding tests")
public void testVerifyPublicKey() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, CertificateException {
    // Preconditions
    assertNotNull(javaxCert.getPublicKey());
    assertNotNull(javaxSSCert.getPublicKey());
    // here not self signed:
    try {
        javaxCert.verify(javaxCert.getPublicKey());
    } catch (SignatureException e) {
    // ok
    }
    PublicKey k = javaxCert.getPublicKey();
    MyModifiablePublicKey changedEncoding = new MyModifiablePublicKey(k);
    changedEncoding.setEncoding(new byte[javaxCert.getEncoded().length - 1]);
    try {
        javaxCert.verify(tbt_cert.getPublicKey());
    } catch (InvalidKeyException e) {
    // ok
    }
    try {
        javaxCert.verify(null);
    } catch (Exception e) {
    // ok
    }
    try {
        javaxCert.verify(changedEncoding);
        fail("Exception expected");
    } catch (Exception e) {
    // ok
    }
// following test doesn't work because the algorithm is derived from
// somewhere else.
// MyModifiablePublicKey changedAlgo = new MyModifiablePublicKey(k);
// changedAlgo.setAlgorithm("MD5withBla");
// try {
//     javaxCert.verify(changedAlgo);
//     fail("Exception expected");
// } catch (SignatureException e) {
//     // ok
// }
// Security.removeProvider(mySSProvider.getName());
// try {
//     javaxSSCert.verify(javaxSSCert.getPublicKey());
// } catch (NoSuchProviderException e) {
//     // ok
// }
// Security.addProvider(mySSProvider);
// must always evaluate true for self signed
// javaxSSCert.verify(javaxSSCert.getPublicKey());
}
Also used : PublicKey(java.security.PublicKey) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateExpiredException(javax.security.cert.CertificateExpiredException) CertificateNotYetValidException(javax.security.cert.CertificateNotYetValidException) CertificateEncodingException(javax.security.cert.CertificateEncodingException) CertificateException(javax.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) SideEffect(dalvik.annotation.SideEffect)

Aggregations

SideEffect (dalvik.annotation.SideEffect)7 URL (java.net.URL)3 File (java.io.File)2 IOException (java.io.IOException)2 URLClassLoader (java.net.URLClassLoader)2 Vector (java.util.Vector)2 Support_TestWebServer (tests.support.Support_TestWebServer)2 InputStream (java.io.InputStream)1 Reference (java.lang.ref.Reference)1 ReferenceQueue (java.lang.ref.ReferenceQueue)1 SoftReference (java.lang.ref.SoftReference)1 Authenticator (java.net.Authenticator)1 ContentHandler (java.net.ContentHandler)1 HttpURLConnection (java.net.HttpURLConnection)1 InetSocketAddress (java.net.InetSocketAddress)1 MalformedURLException (java.net.MalformedURLException)1 PasswordAuthentication (java.net.PasswordAuthentication)1 Proxy (java.net.Proxy)1 URLConnection (java.net.URLConnection)1 InvalidKeyException (java.security.InvalidKeyException)1