Skip to content

Commit c6b2ca3

Browse files
committed
Add Test for KeyStore support
1 parent 6491262 commit c6b2ca3

3 files changed

Lines changed: 76 additions & 5 deletions

File tree

core/src/test/java/com/onelogin/saml2/test/settings/SettingBuilderTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
import static org.junit.Assert.assertNull;
99
import static org.junit.Assert.assertTrue;
1010

11+
import java.io.FileInputStream;
1112
import java.io.IOException;
1213
import java.net.URISyntaxException;
1314
import java.net.URL;
15+
import java.security.Key;
16+
import java.security.KeyStore;
17+
import java.security.KeyStoreException;
18+
import java.security.NoSuchAlgorithmException;
1419
import java.security.cert.CertificateException;
20+
import java.security.cert.X509Certificate;
1521
import java.util.ArrayList;
1622
import java.util.Arrays;
1723
import java.util.LinkedHashMap;
@@ -58,6 +64,36 @@ public void testLoadFromFileNotExist() throws IOException, SettingsException, Er
5864

5965
new SettingsBuilder().fromFile("config/config.notfound.properties").build();
6066
}
67+
68+
/**
69+
* Tests SettingsBuilder fromFile method
70+
* Case: Config file with KeyStore
71+
*
72+
* @throws IOException
73+
* @throws CertificateException
74+
* @throws URISyntaxException
75+
* @throws SettingsException
76+
* @throws Error
77+
* @throws KeyStoreException
78+
* @throws NoSuchAlgorithmException
79+
*
80+
* @see {@link com.onelogin.saml2.settings.SettingsBuilder#fromFile(String, java.security.KeyStore, String, String)}
81+
*/
82+
@Test
83+
public void testLoadFromFileAndKeyStore() throws IOException, CertificateException, URISyntaxException, SettingsException, Error, KeyStoreException, NoSuchAlgorithmException {
84+
85+
String password = "changeit";
86+
String keyStoreFile = "src/test/resources/keystore/oneloginTestKeystore.jks";
87+
String alias = "onelogintest";
88+
89+
KeyStore ks = KeyStore.getInstance("JKS");
90+
ks.load(new FileInputStream(keyStoreFile), password.toCharArray());
91+
92+
Saml2Settings setting = new SettingsBuilder().fromFile("config/config.empty.properties", ks, alias, password).build();
93+
assertNotNull(setting.getSPcert() instanceof X509Certificate);
94+
assertNotNull(setting.getSPkey() instanceof Key);
95+
}
96+
6197

6298
/**
6399
* Tests SettingsBuilder fromFile method
1.93 KB
Binary file not shown.

toolkit/src/test/java/com/onelogin/saml2/test/AuthTest.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,12 @@ public void testConstructorWithFilenameAndKeyStore() throws IOException, Setting
149149

150150
Auth auth = new Auth("config/config.min.properties", ks, alias, password);
151151
assertTrue(auth.getSettings() != null);
152-
153152
assertTrue(auth.getSettings().getSPcert() != null);
154153
assertTrue(auth.getSettings().getSPkey() != null);
155154

156-
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
157-
assertEquals(settings.getIdpEntityId(), auth.getSettings().getIdpEntityId());
158-
assertEquals(settings.getSpEntityId(), auth.getSettings().getSpEntityId());
155+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties", ks, alias, password).build();
156+
assertEquals(settings.getSPcert(), auth.getSettings().getSPcert());
157+
assertEquals(settings.getSPkey(), auth.getSettings().getSPkey());
159158
}
160159

161160
/**
@@ -183,7 +182,43 @@ public void testConstructorWithReqRes() throws IOException, SettingsException, U
183182
assertEquals(settings.getIdpEntityId(), auth.getSettings().getIdpEntityId());
184183
assertEquals(settings.getSpEntityId(), auth.getSettings().getSpEntityId());
185184
}
186-
185+
186+
/**
187+
* Tests the constructor of Auth
188+
* Case: KeyStore and HttpServletRequest and HttpServletResponse provided
189+
*
190+
* @throws SettingsException
191+
* @throws IOException
192+
* @throws URISyntaxException
193+
* @throws Error
194+
* @throws KeyStoreException
195+
* @throws CertificateException
196+
* @throws NoSuchAlgorithmException
197+
*
198+
* @see com.onelogin.saml2.Auth
199+
*/
200+
@Test
201+
public void testConstructorWithReqResAndKeyStore() throws IOException, SettingsException, URISyntaxException, Error, KeyStoreException, NoSuchAlgorithmException, CertificateException {
202+
HttpServletRequest request = mock(HttpServletRequest.class);
203+
HttpServletResponse response = mock(HttpServletResponse.class);
204+
205+
String password = "changeit";
206+
String keyStoreFile = "src/test/resources/keystore/oneloginTestKeystore.jks";
207+
String alias = "onelogintest";
208+
209+
KeyStore ks = KeyStore.getInstance("JKS");
210+
ks.load(new FileInputStream(keyStoreFile), password.toCharArray());
211+
212+
Auth auth = new Auth(ks, alias, password, request, response);
213+
assertTrue(auth.getSettings() != null);
214+
assertTrue(auth.getSettings().getSPcert() != null);
215+
assertTrue(auth.getSettings().getSPkey() != null);
216+
217+
Saml2Settings settings = new SettingsBuilder().fromFile("onelogin.saml.properties", ks, alias, password).build();
218+
assertEquals(settings.getSPkey(), auth.getSettings().getSPkey());
219+
assertEquals(settings.getSPcert(), auth.getSettings().getSPcert());
220+
}
221+
187222
/**
188223
* Tests the constructor of Auth
189224
* Case: filename, HttpServletRequest and HttpServletResponse provided

0 commit comments

Comments
 (0)