Ejemplo n.º 1
0
        /*
        ****************************************************************************
        * testProxy()
        ****************************************************************************
        */
        /**
        *
        */
        public void testProxy()
        {
            URI oProxyURI = null;
            try
            {
            oProxyURI = new URI("http://xri.epok.net/proxy");
            }
            catch (URISyntaxException e)
            {
            fail("Unexpected initialization error");
            }

            Resolver oResolver = new TestResolver();
            oResolver.setProxyURI(oProxyURI);
            oResolver.setMaxHttpRedirects(0);

            // should not work - not enough descriptors
            try
            {
            XRD oDesc = oResolver.resolveAuthToXRD("xri://@!foo!bar!baz", new TrustType(), true);
            assertFalse("Should get a PartialResolutionException", oDesc.getStatusCode().Equals(Status.SUCCESS));
            }
            catch (Exception e)
            {
            if (e is PartialResolutionException) {
                String stat1 = null, stat2 = null, stat3 = null;
                PartialResolutionException pe = (PartialResolutionException)e;
                System.err.println(pe.getPartialXRDS().toString());

                assertTrue("Should contain 3 XRDs, got " + pe.getPartialXRDS().getNumChildren() + " instead",
                        pe.getPartialXRDS().getNumChildren() == 3);

                try {
                    stat1 = pe.getPartialXRDS().getDescriptorAt(0).getStatusCode();
                    stat2 = pe.getPartialXRDS().getDescriptorAt(1).getStatusCode();
                    stat3 = pe.getPartialXRDS().getDescriptorAt(2).getStatusCode();
                }
                catch (Exception e1) {
                    assertTrue("Got exception: " + e1.getMessage(), false);
                }

                assertTrue("First XRD should be successful", stat1.Equals(Status.SUCCESS));
                assertTrue("Second XRD should be successful", stat2.Equals(Status.SUCCESS));
                assertTrue("Third XRD should be a failure", stat3.Equals(Status.AUTH_RES_NOT_FOUND));
            }
            else {
                fail("Got exception while trying to resolve via proxy " + e);
                e.printStackTrace();
            }
            }

            // should not work - too many descriptors
            /*** [wil] test disabled - proxy resolution currently does not validate the number of XRDs returned.
            try
            {
            XRD oDesc = oResolver.resolveAuthToXRD("xri://@!foo", new TrustType(), true);
            assertFalse("Should get a failure code for too many XRDs", oDesc.getStatusCode().Equals(Status.SUCCESS));
            }
            catch (Exception oEx)
            {
            fail("Got exception while trying to resolve via proxy  " + oEx);
            }
            */

            // should work as (null response)
            try
            {
            XRD oDesc = oResolver.resolveAuthToXRD("xri://@!foo!nonexistent", new TrustType(), true);
            assertTrue(
                "Should not have obtained XRD from proxy",
                oDesc == null);
            }
            catch (Exception e)
            {
            if (e is PartialResolutionException) {
                PartialResolutionException pe = (PartialResolutionException)e;
                String stat = null;
                try {
                    stat = pe.getPartialXRDS().getDescriptorAt(0).getStatusCode();
                }
                catch (Exception e1) { assertTrue("got exception: " + e1.getMessage(), false); }

                assertTrue("First XRD should fail", stat.Equals(Status.AUTH_RES_NOT_FOUND));
            }
            else {
                fail("Got exception while trying to resolve via proxy " + e);
            }
            }

            // should work
            try
            {
            XRD oDesc = oResolver.resolveAuthToXRD("xri://@!foo!bar", new TrustType(), false);
            assertTrue(
                "Failed to obtain XRD from proxy", oDesc != null);
            }
            catch (Exception oEx)
            {
            fail("Got exception while trying to resolve via proxy " + oEx);
            }
        }