3232package com .google .auth .oauth2 ;
3333
3434import static org .junit .Assert .*;
35+ import static org .mockito .Mockito .when ;
3536
3637import com .google .auth .oauth2 .IdentityPoolCredentialSource .CertificateConfig ;
3738import com .google .gson .Gson ;
4041import java .io .ByteArrayInputStream ;
4142import java .io .File ;
4243import java .io .IOException ;
44+ import java .net .URISyntaxException ;
45+ import java .net .URL ;
4346import java .nio .charset .StandardCharsets ;
4447import java .nio .file .Files ;
48+ import java .nio .file .Paths ;
4549import java .security .cert .CertificateException ;
4650import java .security .cert .CertificateFactory ;
4751import java .security .cert .X509Certificate ;
4852import java .util .Base64 ;
4953import org .junit .Before ;
5054import org .junit .Rule ;
5155import org .junit .Test ;
52- import org .junit .rules .TemporaryFolder ;
5356import org .junit .runner .RunWith ;
5457import org .junit .runners .JUnit4 ;
5558import org .mockito .Mock ;
6164public class CertificateIdentityPoolSubjectTokenSupplierTest {
6265
6366 @ Rule public MockitoRule mockitoRule = MockitoJUnit .rule ();
64- @ Rule public TemporaryFolder tempFolder = new TemporaryFolder ();
6567
6668 @ Mock private IdentityPoolCredentialSource mockCredentialSource ;
6769 @ Mock private CertificateConfig mockCertificateConfig ;
@@ -70,40 +72,33 @@ public class CertificateIdentityPoolSubjectTokenSupplierTest {
7072 private CertificateIdentityPoolSubjectTokenSupplier supplier ;
7173 private static final Gson GSON = new Gson ();
7274
73- // Certificate data from X509ProviderTest
74- private static final String TEST_CERT_PEM =
75- "-----BEGIN CERTIFICATE-----\n "
76- + "MIICGzCCAYSgAwIBAgIIWrt6xtmHPs4wDQYJKoZIhvcNAQEFBQAwMzExMC8GA1UE\n "
77- + "AxMoMTAwOTEyMDcyNjg3OC5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbTAeFw0x\n "
78- + "MjEyMDExNjEwNDRaFw0yMjExMjkxNjEwNDRaMDMxMTAvBgNVBAMTKDEwMDkxMjA3\n "
79- + "MjY4NzguYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20wgZ8wDQYJKoZIhvcNAQEB\n "
80- + "BQADgY0AMIGJAoGBAL1SdY8jTUVU7O4/XrZLYTw0ON1lV6MQRGajFDFCqD2Fd9tQ\n "
81- + "GLW8Iftx9wfXe1zuaehJSgLcyCxazfyJoN3RiONBihBqWY6d3lQKqkgsRTNZkdFJ\n "
82- + "Wdzl/6CxhK9sojh2p0r3tydtv9iwq5fuuWIvtODtT98EgphhncQAqkKoF3zVAgMB\n "
83- + "AAGjODA2MAwGA1UdEwEB/wQCMAAwDgYDVR0PAQH/BAQDAgeAMBYGA1UdJQEB/wQM\n "
84- + "MAoGCCsGAQUFBwMCMA0GCSqGSIb3DQEBBQUAA4GBAD8XQEqzGePa9VrvtEGpf+R4\n "
85- + "fkxKbcYAzqYq202nKu0kfjhIYkYSBj6gi348YaxE64yu60TVl42l5HThmswUheW4\n "
86- + "uQIaq36JvwvsDP5Zoj5BgiNSnDAFQp+jJFBRUA5vooJKgKgMDf/r/DCOsbO6VJF1\n "
87- + "kWwa9n19NFiV0z3m6isj\n "
88- + "-----END CERTIFICATE-----\n " ;
89-
90- private static final byte [] TEST_CERT_BYTES = TEST_CERT_PEM .getBytes (StandardCharsets .UTF_8 );
9175 private static final byte [] INVALID_CERT_BYTES =
9276 "invalid certificate data" .getBytes (StandardCharsets .UTF_8 );
9377
78+ private byte [] testCertBytesFromFile ;
79+
9480 @ Before
95- public void setUp () throws IOException {
96- File testCertFile = tempFolder .newFile ("certificate.pem" );
97- Files .write (testCertFile .toPath (), TEST_CERT_BYTES );
98- mockCredentialSource .certificateConfig = mockCertificateConfig ;
99- mockCredentialSource .credentialLocation = testCertFile .getAbsolutePath ();
81+ public void setUp () throws IOException , URISyntaxException {
82+ ClassLoader classLoader = getClass ().getClassLoader ();
83+ URL leafCertUrl = classLoader .getResource ("x509_leaf_certificate.pem" );
84+ assertNotNull ("Test leaf certificate file not found!" , leafCertUrl );
85+ File testCertFile = new File (leafCertUrl .getFile ());
86+
87+ when (mockCertificateConfig .useDefaultCertificateConfig ()).thenReturn (false );
88+ when (mockCertificateConfig .getCertificateConfigLocation ())
89+ .thenReturn (testCertFile .getAbsolutePath ());
90+
91+ when (mockCredentialSource .getCertificateConfig ()).thenReturn (mockCertificateConfig );
92+ when (mockCredentialSource .getCredentialLocation ()).thenReturn (testCertFile .getAbsolutePath ());
93+
10094 supplier = new CertificateIdentityPoolSubjectTokenSupplier (mockCredentialSource );
95+ testCertBytesFromFile = Files .readAllBytes (Paths .get (leafCertUrl .toURI ()));
10196 }
10297
10398 @ Test
10499 public void parseCertificate_validData_returnsCertificate () throws Exception {
105100 X509Certificate cert =
106- CertificateIdentityPoolSubjectTokenSupplier .parseCertificate (TEST_CERT_BYTES );
101+ CertificateIdentityPoolSubjectTokenSupplier .parseCertificate (testCertBytesFromFile );
107102 assertNotNull (cert );
108103 }
109104
@@ -136,10 +131,10 @@ public void parseCertificate_invalidData_throwsCertificateException() {
136131
137132 @ Test
138133 public void getSubjectToken_success () throws Exception {
139- // Calculate expected result
134+ // Calculate expected result based on the file content.
140135 CertificateFactory cf = CertificateFactory .getInstance ("X.509" );
141136 X509Certificate expectedCert =
142- (X509Certificate ) cf .generateCertificate (new ByteArrayInputStream (TEST_CERT_BYTES ));
137+ (X509Certificate ) cf .generateCertificate (new ByteArrayInputStream (testCertBytesFromFile ));
143138 String expectedEncodedDer = Base64 .getEncoder ().encodeToString (expectedCert .getEncoded ());
144139 JsonArray expectedJsonArray = new JsonArray ();
145140 expectedJsonArray .add (new JsonPrimitive (expectedEncodedDer ));
0 commit comments