### Question:
DefaultX509Source implements X509Source { public static DefaultX509Source newSource() throws SocketEndpointAddressException, X509SourceException { val x509SourceOptions = X509SourceOptions.builder().initTimeout(DEFAULT_TIMEOUT).build(); return newSource(x509SourceOptions); } private  DefaultX509Source(final Function<List<X509Svid>, X509Svid> svidPicker, final WorkloadApiClient workloadApiClient); static DefaultX509Source newSource(); static DefaultX509Source newSource(@NonNull final X509SourceOptions options); @Override X509Svid getX509Svid(); @Override X509Bundle getBundleForTrustDomain(@NonNull final TrustDomain trustDomain); @SneakyThrows @Override void close();  }### Answer:
@Test void newSource_success() { val options = DefaultX509Source.X509SourceOptions .builder() .workloadApiClient(workloadApiClient) .svidPicker((list) -> list.get(0)) .initTimeout(Duration.ofSeconds(0)) .build(); try { DefaultX509Source jwtSource = DefaultX509Source.newSource(options); assertNotNull(jwtSource); } catch (SocketEndpointAddressException | X509SourceException e) { fail(e); } }
@Test void newSource_nullParam() { try { DefaultX509Source.newSource(null); fail(); } catch (NullPointerException e) { assertEquals("options is marked non-null but is null", e.getMessage()); } catch (SocketEndpointAddressException | X509SourceException e) { fail(); } }
@Test void newSource_timeout() throws Exception { try { val options = DefaultX509Source.X509SourceOptions .builder() .initTimeout(Duration.ofSeconds(1)) .spiffeSocketPath("unix:/tmp/test") .build(); DefaultX509Source.newSource(options); fail(); } catch (X509SourceException e) { assertEquals("Error creating X.509 source", e.getMessage()); } catch (SocketEndpointAddressException e) { fail(); } }
@Test void newSource_errorFetchingJwtBundles() { val options = DefaultX509Source.X509SourceOptions .builder() .workloadApiClient(workloadApiClientErrorStub) .spiffeSocketPath("unix:/tmp/test") .build(); try { DefaultX509Source.newSource(options); fail(); } catch (X509SourceException e) { assertEquals("Error creating X.509 source", e.getMessage()); assertEquals("Error in X509Context watcher", e.getCause().getMessage()); } catch (Exception e) { fail(); } }
@Test void newSource_noSocketAddress() throws Exception { try { TestUtils.setEnvironmentVariable(Address.SOCKET_ENV_VARIABLE, ""); DefaultX509Source.newSource(); fail(); } catch (X509SourceException | SocketEndpointAddressException e) { fail(); } catch (IllegalStateException e) { assertEquals("Endpoint Socket Address Environment Variable is not set: SPIFFE_ENDPOINT_SOCKET", e.getMessage()); } }