TrustAnchorRetrievalService { public byte[] fetchTrustAnchorCertificate(URI trustAnchorCertificateURI, ValidationResult validationResult) { try { switch (trustAnchorCertificateURI.getScheme()) { case "rsync": return fetchRsyncTrustAnchorCertificate(trustAnchorCertificateURI, validationResult); case "https": return fetchHttpsTrustAnchorCertificate(trustAnchorCertificateURI, validationResult); case "file": return fetchFileTrustAnchorCertificate(trustAnchorCertificateURI, validationResult); default: validationResult.warn(ErrorCodes.TRUST_ANCHOR_FETCH, trustAnchorCertificateURI.toASCIIString(), "Unsupported URI"); return null; } } catch (IOException e) { validationResult.warn(ErrorCodes.TRUST_ANCHOR_FETCH, trustAnchorCertificateURI.toASCIIString(), e.getMessage()); return null; } }  byte[] fetchTrustAnchorCertificate(URI trustAnchorCertificateURI, ValidationResult validationResult);  }
@Test public void testFetchTrustAnchorCertificate_reject_http() throws Exception { given(trustAnchorRetrievalService.fetchTrustAnchorCertificate(any(), any())).willCallRealMethod(); final URI uri = URI.create("http: trustAnchorRetrievalService.fetchTrustAnchorCertificate(uri, validationResult); final List<ValidationCheck> validationChecks = validationResult.getAllValidationChecksForCurrentLocation(); assertThat(validationChecks).hasSize(1); assertThat(validationChecks.get(0).getKey()).isEqualTo(ErrorCodes.TRUST_ANCHOR_FETCH); }
@Test public void testFetchTrustAnchorCertificate_rejects_file_url() throws Exception { given(trustAnchorRetrievalService.fetchTrustAnchorCertificate(any(), any())).willCallRealMethod(); final URI uri = new File("/tmp/does-not-exist.cer").toURI(); trustAnchorRetrievalService.fetchTrustAnchorCertificate(uri, validationResult); final List<ValidationCheck> validationChecks = validationResult.getAllValidationChecksForCurrentLocation(); assertThat(validationChecks).hasSize(1); assertThat(validationChecks.get(0).getKey()).isEqualTo(ErrorCodes.TRUST_ANCHOR_FETCH); }