Skip to content

Commit

Permalink
Add test cases for SOAP 1.2 client with security
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuvindu committed Oct 4, 2023
1 parent ddbb1a9 commit b1e8339
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions ballerina/modules/soap12/tests/soap12_client_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import soap.wssec;

import ballerina/mime;
import ballerina/test;
import ballerina/crypto;

const string KEY_ALIAS = "wss40";
const string KEY_PASSWORD = "security";
const string KEY_STORE_PATH = "modules/wssec/tests/resources/wss40.p12";
const string X509_KEY_STORE_PATH = "modules/wssec/tests/resources/x509_certificate.p12";
const string X509_KEY_STORE_PATH_2 = "modules/wssec/tests/resources/x509_certificate_2.p12";
const wssec:TransportBindingConfig TRANSPORT_BINDING = "TransportBinding";
const wssec:NoPolicy NO_POLICY = "NoPolicy";

Expand Down Expand Up @@ -193,3 +199,164 @@ function testSendReceiveError() returns error? {
test:assertTrue(response is Error);
test:assertEquals((<Error>response).message(), SOAP_RESPONSE_ERROR);
}


@test:Config {
groups: ["soap12", "send_receive"]
}
function testSendReceiveWithTimestampTokenSecurity() returns error? {
Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL",
{
inboundSecurity: [
{
timeToLive: 600
}
]
}
);
xml body = xml `<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(body, "http://tempuri.org/Add");
xml expected = xml `<soap:Body xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Fault><soap:Code><soap:Value>soap:MustUnderstand</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapHeaderException: SOAP header Security was not understood.
at System.Web.Services.Protocols.SoapHeaderHandling.SetHeaderMembers(SoapHeaderCollection headers, Object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, Boolean client)
at System.Web.Services.Protocols.SoapServerProtocol.CreateServerInstance()
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</soap:Text></soap:Reason></soap:Fault></soap:Body>`;

test:assertEquals(response.toString(), expected.toString());
}

@test:Config {
groups: ["soap12", "send_receive"]
}
function testSendReceiveWithUsernameTokenSecurity() returns error? {
Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL",
{
inboundSecurity: {
username: "user",
password: "password",
passwordType: wssec:TEXT
},
outboundSecurity: {}
}
);
xml body = xml `<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(body, "http://tempuri.org/Add");
xml expected = xml `<soap:Body xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Fault><soap:Code><soap:Value>soap:MustUnderstand</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapHeaderException: SOAP header Security was not understood.
at System.Web.Services.Protocols.SoapHeaderHandling.SetHeaderMembers(SoapHeaderCollection headers, Object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, Boolean client)
at System.Web.Services.Protocols.SoapServerProtocol.CreateServerInstance()
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</soap:Text></soap:Reason></soap:Fault></soap:Body>`;

test:assertEquals(response.toString(), expected.toString());
}

@test:Config {
groups: ["soap12", "send_receive"]
}
function testSendReceiveWithAsymmetricBindingSecurity() returns error? {
crypto:KeyStore serverKeyStore = {
path: X509_KEY_STORE_PATH,
password: KEY_PASSWORD
};

crypto:PublicKey serverPublicKey = check crypto:decodeRsaPublicKeyFromTrustStore(serverKeyStore, KEY_ALIAS);

crypto:KeyStore clientKeyStore = {
path: X509_KEY_STORE_PATH_2,
password: KEY_PASSWORD
};
crypto:PrivateKey clientPrivateKey = check crypto:decodeRsaPrivateKeyFromKeyStore(clientKeyStore, KEY_ALIAS, KEY_PASSWORD);

Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL",
{
inboundSecurity: {
signatureAlgorithm: wssec:RSA_SHA256,
encryptionAlgorithm: wssec:RSA_ECB,
signatureKey: clientPrivateKey,
encryptionKey: serverPublicKey
}
}
);
xml body = xml `<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(body, "http://tempuri.org/Add");
xml expected = xml `<soap:Body xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Fault><soap:Code><soap:Value>soap:MustUnderstand</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapHeaderException: SOAP header Security was not understood.
at System.Web.Services.Protocols.SoapHeaderHandling.SetHeaderMembers(SoapHeaderCollection headers, Object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, Boolean client)
at System.Web.Services.Protocols.SoapServerProtocol.CreateServerInstance()
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</soap:Text></soap:Reason></soap:Fault></soap:Body>`;

test:assertEquals(response.toString(), expected.toString());
}

@test:Config {
groups: ["soap12", "send_receive"]
}
function testSendReceiveWithSymmetricBindingSecurity() returns error? {
crypto:KeyStore serverKeyStore = {
path: X509_KEY_STORE_PATH,
password: KEY_PASSWORD
};
crypto:PublicKey serverPublicKey = check crypto:decodeRsaPublicKeyFromTrustStore(serverKeyStore, KEY_ALIAS);

crypto:KeyStore keyStore = {
path: KEY_STORE_PATH,
password: KEY_PASSWORD
};
crypto:PrivateKey symmetricKey = check crypto:decodeRsaPrivateKeyFromKeyStore(keyStore, KEY_ALIAS, KEY_PASSWORD);

Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL",
{
inboundSecurity: {
signatureAlgorithm: wssec:RSA_SHA256,
encryptionAlgorithm: wssec:RSA_ECB,
symmetricKey: symmetricKey,
servicePublicKey: serverPublicKey
}
}
);
xml body = xml `<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;
xml|mime:Entity[] response = check soapClient->sendReceive(body, "http://tempuri.org/Add");
xml expected = xml `<soap:Body xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Fault><soap:Code><soap:Value>soap:MustUnderstand</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapHeaderException: SOAP header Security was not understood.
at System.Web.Services.Protocols.SoapHeaderHandling.SetHeaderMembers(SoapHeaderCollection headers, Object target, SoapHeaderMapping[] mappings, SoapHeaderDirection direction, Boolean client)
at System.Web.Services.Protocols.SoapServerProtocol.CreateServerInstance()
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()</soap:Text></soap:Reason></soap:Fault></soap:Body>`;

test:assertEquals(response.toString(), expected.toString());
}

0 comments on commit b1e8339

Please sign in to comment.