Skip to content

Commit fa7838e

Browse files
committed
CYBS-737: Added few new test cases
1 parent d0f27d2 commit fa7838e

3 files changed

Lines changed: 45 additions & 25 deletions

File tree

java/src/main/java/com/cybersource/ws/client/SecurityUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ private static void loadJavaKeystore(MerchantConfig merchantConfig, Logger logge
420420

421421
}
422422

423-
private static String getServerAlias() {
423+
protected static String getServerAlias() {
424424
String serverAlias = SERVER_ALIAS;
425425
if(!identities.containsKey(serverAlias)) {
426426
if(identities.containsKey(serverAlias.toLowerCase())) {
Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
11
package com.cybersource.ws.client;
22

3-
import org.junit.Before;
43
import org.junit.Test;
54
import org.mockito.Mockito;
65

76
import static org.junit.Assert.*;
87

98
import java.io.File;
9+
import java.io.IOException;
1010
import java.io.InputStream;
1111
import java.security.Principal;
1212
import java.security.PrivateKey;
1313
import java.security.cert.X509Certificate;
1414
import java.util.*;
1515

16-
public class IdentityTest{
17-
Properties merchantProperties;
18-
private MerchantConfig config;
19-
20-
@Before
21-
public void setUp() throws Exception {
22-
//Loading the properties file from src/test/resources
23-
Properties merchantProperties = new Properties();
24-
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("test_cybs.properties");
25-
if (in == null) {
26-
throw new RuntimeException("Unable to load test_cybs.properties file");
27-
}
28-
merchantProperties.load(in);
29-
config = new MerchantConfig(merchantProperties, merchantProperties.getProperty("merchantID"));
30-
}
16+
public class IdentityTest {
3117

3218
@Test
3319
public void testSetUpMerchant() throws SignException, ConfigException{
@@ -52,17 +38,45 @@ public void testSetUpMerchant() throws SignException, ConfigException{
5238
}
5339

5440
@Test
55-
public void testsetUpServer() throws InstantiationException, IllegalAccessException, SignException{
41+
public void testSetUpServerForP12Certs() throws SignException, IOException, ConfigException {
42+
Properties merchantProps = getConfigProps();
43+
merchantProps.setProperty("enableCacert", "false");
44+
MerchantConfig customConfig = new MerchantConfig(merchantProps, merchantProps.getProperty("merchantID"));
5645
String keyAlias = "CN=CyberSource_SJC_US,SERIALNUMBER=400000009910179089277";
5746
X509Certificate x509Cert = Mockito.mock(X509Certificate.class);
5847
Principal principal = Mockito.mock(Principal.class);
59-
Logger logger = Mockito.mock(Logger.class);
6048
Mockito.when(x509Cert.getSubjectDN()).thenReturn(principal);
6149
Mockito.when(principal.getName()).thenReturn(keyAlias);
62-
Identity identity = new Identity(config,x509Cert);
50+
Identity identity = new Identity(customConfig, x509Cert);
6351
assertEquals(identity.getName(), "CyberSource_SJC_US");
6452
assertEquals(identity.getSerialNumber(), "400000009910179089277");
6553
assertNull(identity.getPrivateKey());
6654
}
6755

56+
@Test
57+
public void testSetUpServerForCaCerts() throws SignException, IOException, ConfigException {
58+
Properties merchantProps = getConfigProps();
59+
merchantProps.setProperty("enableCacert", "true");
60+
MerchantConfig customConfig = new MerchantConfig(merchantProps, merchantProps.getProperty("merchantID"));
61+
String keyAlias = "SERIALNUMBER=400000009910179089277,CN=CyberSource_SJC_US";
62+
X509Certificate x509Cert = Mockito.mock(X509Certificate.class);
63+
Principal principal = Mockito.mock(Principal.class);
64+
Mockito.when(x509Cert.getSubjectDN()).thenReturn(principal);
65+
Mockito.when(principal.getName()).thenReturn(keyAlias);
66+
Identity identity = new Identity(customConfig, x509Cert);
67+
assertEquals(identity.getName(), "CyberSource_SJC_US");
68+
assertEquals(identity.getSerialNumber(), "400000009910179089277");
69+
assertNull(identity.getPrivateKey());
70+
}
71+
72+
private Properties getConfigProps() throws IOException {
73+
Properties merchantProperties = new Properties();
74+
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("test_cybs.properties");
75+
if (in == null) {
76+
throw new RuntimeException("Unable to load test_cybs.properties file");
77+
}
78+
merchantProperties.load(in);
79+
return merchantProperties;
80+
}
81+
6882
}

java/src/test/java/com/cybersource/ws/client/SecurityUtilSystemTest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.mockito.Mockito.times;
2525
import static org.mockito.Mockito.verify;
2626

27+
import org.junit.Assert;
2728
import org.junit.Before;
2829
import org.junit.Test;
2930
import org.mockito.Mockito;
@@ -87,7 +88,7 @@ public void setup() throws Exception{
8788
requestMap.put("purchaseTotals_currency", "USD");
8889
requestMap.put("item_0_unitPrice", "12.34");
8990
requestMap.put("item_1_unitPrice", "56.78");
90-
requestMap.put("merchant_id", "cybs_test_ashish");
91+
requestMap.put("merchant_id", "smccfep");
9192

9293
//Loading the properties file from src/test/resources
9394
merchantProperties = new Properties();
@@ -154,12 +155,11 @@ public void testServerIdentityToKeyStore() throws Exception{
154155
public void testMerchantIdentityToKeyStore() throws Exception{
155156
Identity identity = Mockito.mock(Identity.class);
156157
X509Certificate x509Cert = Mockito.mock(X509Certificate.class);
157-
PrivateKey pkey = Mockito.mock(PrivateKey.class);
158158
Logger logger = Mockito.mock(Logger.class);
159159
KeyStore myKeystore = KeyStore.getInstance("jks");
160160
myKeystore.load(null, null);
161161

162-
PrivateKey newPkay= instPrivateKey(pkey);
162+
PrivateKey newPkay= instPrivateKey();
163163

164164
Mockito.when(identity.getPrivateKey()).thenReturn(newPkay);
165165
Mockito.when(identity.getX509Cert()).thenReturn(x509Cert);
@@ -203,9 +203,15 @@ public void testCertificateCacheEnabled() throws Exception{
203203
SecurityUtil.loadMerchantP12File(certificateCacheDisabledSpy, logger);
204204

205205
verify(certificateCacheDisabledSpy, times(4)).getKeyFile();
206-
}
206+
}
207+
208+
@Test
209+
// change expected result as per the P12 cert you are using for testing
210+
public void testServerAlias() {
211+
Assert.assertEquals("CyberSource_SJC_US".toLowerCase(), SecurityUtil.getServerAlias());
212+
}
207213

208-
private static PrivateKey instPrivateKey(PrivateKey pkey) throws Exception{
214+
private static PrivateKey instPrivateKey() throws Exception{
209215
byte[] pkByts = {(byte) 0x30, (byte) 0x82, (byte) 0x02, (byte) 0x75, (byte) 0x02, (byte) 0x01, (byte) 0x00, (byte) 0x30, (byte) 0x0d, (byte) 0x06, (byte) 0x09, (byte) 0x2a, (byte) 0x86, (byte) 0x48, (byte) 0x86, (byte) 0xf7, (byte) 0x0d, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x05, (byte) 0x00, (byte) 0x04, (byte) 0x82, (byte) 0x02, (byte) 0x5f, (byte) 0x30, (byte) 0x82, (byte) 0x02, (byte) 0x5b, (byte) 0x02, (byte) 0x01, (byte) 0x00, (byte) 0x02, (byte) 0x81, (byte) 0x81, (byte) 0x00, (byte) 0xb9, (byte) 0x53, (byte) 0x1a, (byte) 0x98, (byte) 0xaa, (byte) 0x44, (byte) 0xcf, (byte) 0xe2, (byte) 0x9a, (byte) 0x42, (byte) 0x4d, (byte) 0x59, (byte) 0xba, (byte) 0x35, (byte) 0x18, (byte) 0x9a, (byte) 0xce, (byte) 0xb1, (byte) 0x3b, (byte) 0xcf, (byte) 0x60, (byte) 0x22, (byte) 0xc9, (byte) 0x7f, (byte) 0x86, (byte) 0x6e, (byte) 0x61, (byte) 0x31, (byte) 0xfe, (byte) 0xf9, (byte) 0x19, (byte) 0xce, (byte) 0x46, (byte) 0xb0, (byte) 0xe4, (byte) 0x86, (byte) 0x60, (byte) 0x3d, (byte) 0x54, (byte) 0x23, (byte) 0x7e, (byte) 0x0d, (byte) 0x65, (byte) 0xfa, (byte) 0xc1, (byte) 0x5b, (byte) 0xd9, (byte) 0x92, (byte) 0xa3, (byte) 0x24, (byte) 0x8f, (byte) 0x81, (byte) 0x76, (byte) 0x01, (byte) 0x1f, (byte) 0x4a, (byte) 0x6a, (byte) 0xf3, (byte) 0x65, (byte) 0xfa, (byte) 0xa5, (byte) 0xae, (byte) 0xd6, (byte) 0x84, (byte) 0xa5, (byte) 0x59, (byte) 0x1f, (byte) 0x32, (byte) 0xbf, (byte) 0xd0, (byte) 0x00, (byte) 0x3b, (byte) 0x86, (byte) 0x76, (byte) 0xca, (byte) 0x6e, (byte) 0xbb, (byte) 0x21, (byte) 0xca, (byte) 0xf1, (byte) 0xdf, (byte) 0xbf, (byte) 0x69, (byte) 0x4d, (byte) 0x8e, (byte) 0x91, (byte) 0xea, (byte) 0x44, (byte) 0x09, (byte) 0xb3, (byte) 0xc1, (byte) 0xf0, (byte) 0x3d, (byte) 0x81, (byte) 0xcf, (byte) 0x49, (byte) 0xdd, (byte) 0xca, (byte) 0xd3, (byte) 0x0d, (byte) 0x73, (byte) 0xea, (byte) 0xfa, (byte) 0xdb, (byte) 0x10, (byte) 0x26, (byte) 0x34, (byte) 0x69, (byte) 0x3e, (byte) 0x12, (byte) 0xdd, (byte) 0x33, (byte) 0x7f, (byte) 0x8f, (byte) 0x6b, (byte) 0x0e, (byte) 0x80, (byte) 0xc4, (byte) 0x5b, (byte) 0x42, (byte) 0x79, (byte) 0x7e, (byte) 0x28, (byte) 0x5f, (byte) 0xde, (byte) 0x7d, (byte) 0x99, (byte) 0x4f, (byte) 0x02, (byte) 0x03, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x81, (byte) 0x80, (byte) 0x69, (byte) 0xc8, (byte) 0xdf, (byte) 0x18, (byte) 0x9f, (byte) 0xb0, (byte) 0x91, (byte) 0xbd, (byte) 0x76, (byte) 0x72, (byte) 0x3c, (byte) 0x36, (byte) 0xe8, (byte) 0x8c, (byte) 0x60, (byte) 0x54, (byte) 0x15, (byte) 0x81, (byte) 0xa3, (byte) 0x73, (byte) 0x57, (byte) 0x1b, (byte) 0xe4, (byte) 0x4a, (byte) 0xcf, (byte) 0xd0, (byte) 0x77, (byte) 0xd8, (byte) 0x93, (byte) 0x03, (byte) 0x5b, (byte) 0xd0, (byte) 0x9c, (byte) 0x17, (byte) 0x63, (byte) 0x0a, (byte) 0xb5, (byte) 0x2a, (byte) 0xac, (byte) 0xb9, (byte) 0x69, (byte) 0xbd, (byte) 0x7a, (byte) 0x25, (byte) 0xad, (byte) 0x73, (byte) 0xa1, (byte) 0x79, (byte) 0x0b, (byte) 0x78, (byte) 0xd6, (byte) 0x15, (byte) 0x7e, (byte) 0xe7, (byte) 0x5b, (byte) 0x16, (byte) 0x1e, (byte) 0x80, (byte) 0x7b, (byte) 0x08, (byte) 0x9c, (byte) 0xc4, (byte) 0x75, (byte) 0x1b, (byte) 0xdd, (byte) 0xad, (byte) 0x70, (byte) 0x00, (byte) 0x38, (byte) 0x0d, (byte) 0xe0, (byte) 0x72, (byte) 0x69, (byte) 0x07, (byte) 0xe4, (byte) 0x31, (byte) 0x7a, (byte) 0x06, (byte) 0x58, (byte) 0x94, (byte) 0x9c, (byte) 0x41, (byte) 0xc6, (byte) 0x21, (byte) 0x73, (byte) 0x0b, (byte) 0x7e, (byte) 0x9a, (byte) 0x6b, (byte) 0x21, (byte) 0xc0, (byte) 0x8d, (byte) 0xd7, (byte) 0x57, (byte) 0x98, (byte) 0x70, (byte) 0x24, (byte) 0x69, (byte) 0x85, (byte) 0x9f, (byte) 0x77, (byte) 0x6e, (byte) 0x10, (byte) 0xf4, (byte) 0xdf, (byte) 0x4f, (byte) 0xbf, (byte) 0x38, (byte) 0xe7, (byte) 0xc3, (byte) 0x35, (byte) 0x55, (byte) 0xbe, (byte) 0x18, (byte) 0xbf, (byte) 0x33, (byte) 0x87, (byte) 0xba, (byte) 0x08, (byte) 0xe8, (byte) 0x22, (byte) 0xbe, (byte) 0x7f, (byte) 0x8e, (byte) 0xdc, (byte) 0x1a, (byte) 0x04, (byte) 0x21, (byte) 0x02, (byte) 0x41, (byte) 0x00, (byte) 0xf6, (byte) 0xd8, (byte) 0x18, (byte) 0x84, (byte) 0x52, (byte) 0x5f, (byte) 0xdf, (byte) 0x8f, (byte) 0xc9, (byte) 0xd4, (byte) 0x7d, (byte) 0x94, (byte) 0x70, (byte) 0x1d, (byte) 0x18, (byte) 0xab, (byte) 0xd3, (byte) 0x18, (byte) 0x7e, (byte) 0xfd, (byte) 0x32, (byte) 0xb5, (byte) 0xca, (byte) 0x5e, (byte) 0x97, (byte) 0x36, (byte) 0xc5, (byte) 0x66, (byte) 0xdc, (byte) 0x09, (byte) 0x5f, (byte) 0x5a, (byte) 0x9e, (byte) 0x82, (byte) 0x22, (byte) 0x05, (byte) 0x5c, (byte) 0x91, (byte) 0x8a, (byte) 0x66, (byte) 0xb1, (byte) 0x1d, (byte) 0xf1, (byte) 0x8c, (byte) 0x60, (byte) 0xd6, (byte) 0xe2, (byte) 0x98, (byte) 0xdc, (byte) 0x9c, (byte) 0x08, (byte) 0x02, (byte) 0x33, (byte) 0xa4, (byte) 0x79, (byte) 0x73, (byte) 0xef, (byte) 0x22, (byte) 0x8e, (byte) 0xe9, (byte) 0xc4, (byte) 0xe4, (byte) 0x13, (byte) 0x3f, (byte) 0x02, (byte) 0x41, (byte) 0x00, (byte) 0xc0, (byte) 0x32, (byte) 0xd9, (byte) 0xca, (byte) 0xb5, (byte) 0xda, (byte) 0x54, (byte) 0xf0, (byte) 0x13, (byte) 0xcd, (byte) 0x31, (byte) 0x3b, (byte) 0x11, (byte) 0xb1, (byte) 0x86, (byte) 0xa4, (byte) 0x27, (byte) 0x02, (byte) 0x79, (byte) 0xc2, (byte) 0xd4, (byte) 0xf7, (byte) 0x34, (byte) 0xb1, (byte) 0x8e, (byte) 0x76, (byte) 0x5d, (byte) 0x44, (byte) 0xe3, (byte) 0x1b, (byte) 0x3f, (byte) 0x3f, (byte) 0x18, (byte) 0x75, (byte) 0x1f, (byte) 0xe9, (byte) 0xc9, (byte) 0xf1, (byte) 0x03, (byte) 0xdf, (byte) 0x6e, (byte) 0xb1, (byte) 0x39, (byte) 0x8b, (byte) 0x6a, (byte) 0x69, (byte) 0x05, (byte) 0x4b, (byte) 0x27, (byte) 0xe2, (byte) 0x82, (byte) 0x65, (byte) 0x2b, (byte) 0x23, (byte) 0x11, (byte) 0x12, (byte) 0x76, (byte) 0x5b, (byte) 0x80, (byte) 0x67, (byte) 0xd9, (byte) 0x08, (byte) 0xc5, (byte) 0xf1, (byte) 0x02, (byte) 0x40, (byte) 0x70, (byte) 0xf1, (byte) 0x1a, (byte) 0xf6, (byte) 0xa0, (byte) 0x42, (byte) 0x21, (byte) 0xa6, (byte) 0x46, (byte) 0xb0, (byte) 0xa4, (byte) 0xec, (byte) 0xe0, (byte) 0x07, (byte) 0x50, (byte) 0x1c, (byte) 0x7e, (byte) 0x2f, (byte) 0xbd, (byte) 0x1a, (byte) 0xd8, (byte) 0xb2, (byte) 0xf8, (byte) 0xef, (byte) 0x22, (byte) 0xbc, (byte) 0xfa, (byte) 0xc1, (byte) 0x3f, (byte) 0x78, (byte) 0x42, (byte) 0x5a, (byte) 0xd2, (byte) 0x1f, (byte) 0xb4, (byte) 0xb5, (byte) 0x43, (byte) 0x4f, (byte) 0x8c, (byte) 0x45, (byte) 0xc4, (byte) 0x50, (byte) 0x71, (byte) 0x0e, (byte) 0xcb, (byte) 0xd8, (byte) 0x46, (byte) 0x41, (byte) 0xae, (byte) 0xde, (byte) 0xed, (byte) 0x83, (byte) 0x24, (byte) 0x61, (byte) 0xe2, (byte) 0xf8, (byte) 0x3a, (byte) 0xb8, (byte) 0x53, (byte) 0x2f, (byte) 0x7e, (byte) 0xd8, (byte) 0xe4, (byte) 0x3d, (byte) 0x02, (byte) 0x40, (byte) 0x1b, (byte) 0x03, (byte) 0x4a, (byte) 0x9e, (byte) 0xf5, (byte) 0xfe, (byte) 0x30, (byte) 0xaf, (byte) 0xe9, (byte) 0x68, (byte) 0x8e, (byte) 0x81, (byte) 0xc9, (byte) 0xd3, (byte) 0xd4, (byte) 0xa3, (byte) 0x9f, (byte) 0xa3, (byte) 0xf6, (byte) 0x6f, (byte) 0x0e, (byte) 0xb5, (byte) 0x8b, (byte) 0xdf, (byte) 0x64, (byte) 0xb1, (byte) 0x78, (byte) 0x1c, (byte) 0x65, (byte) 0x7a, (byte) 0xff, (byte) 0xe1, (byte) 0xa3, (byte) 0x53, (byte) 0x5a, (byte) 0xdf, (byte) 0xe5, (byte) 0xf5, (byte) 0x0c, (byte) 0xe1, (byte) 0x4b, (byte) 0x52, (byte) 0x77, (byte) 0x4f, (byte) 0x03, (byte) 0xee, (byte) 0xac, (byte) 0xc2, (byte) 0xca, (byte) 0x61, (byte) 0x48, (byte) 0x88, (byte) 0x65, (byte) 0x8e, (byte) 0xb1, (byte) 0x28, (byte) 0x92, (byte) 0x1f, (byte) 0xfc, (byte) 0x25, (byte) 0x1c, (byte) 0x58, (byte) 0xe2, (byte) 0x51, (byte) 0x02, (byte) 0x40, (byte) 0x48, (byte) 0x2e, (byte) 0x48, (byte) 0xa5, (byte) 0x92, (byte) 0x9e, (byte) 0xc8, (byte) 0x08, (byte) 0xca, (byte) 0x0e, (byte) 0x89, (byte) 0x80, (byte) 0x16, (byte) 0xff, (byte) 0x4c, (byte) 0x56, (byte) 0xa7, (byte) 0x4c, (byte) 0xc2, (byte) 0xc1, (byte) 0x2c, (byte) 0xb5, (byte) 0x80, (byte) 0x7d, (byte) 0xe4, (byte) 0xdd, (byte) 0xef, (byte) 0x25, (byte) 0x7c, (byte) 0xaa, (byte) 0xd1, (byte) 0x6d, (byte) 0x8d, (byte) 0xf1, (byte) 0x94, (byte) 0x3d, (byte) 0xf9, (byte) 0xb8, (byte) 0x13, (byte) 0x24, (byte) 0xad, (byte) 0x6e, (byte) 0x84, (byte) 0x6f, (byte) 0xfe, (byte) 0xa5, (byte) 0x2a, (byte) 0xa8, (byte) 0xfb, (byte) 0x87, (byte) 0xcd, (byte) 0x9e, (byte) 0x80, (byte) 0xc8, (byte) 0xc4, (byte) 0x53, (byte) 0x0d, (byte) 0xe2, (byte) 0x63, (byte) 0x67, (byte) 0x04, (byte) 0x5f, (byte) 0xa3, (byte) 0x09};
210216
KeyFactory pvtcf = KeyFactory.getInstance("RSA");
211217
KeySpec keySpec = new PKCS8EncodedKeySpec(pkByts);

0 commit comments

Comments
 (0)