tsung <if> statement or how to get current page in tsung? - erlang

I have site with multiple redirects leading to login page. The number of redirects could vary and I solved it with :
<transaction name="get_login_page">
<request>
<dyn_variable name="redirect" re="Location: ((http|https)://.*)\r"/>
<http url='/' version='1.1' method='GET'>
</http>
</request>
<repeat name="redirect_loop" max_repeat="5">
<request subst="true">
<dyn_variable name="redirect" re="Location: ((http|https)://.*)\r"/>
<http url="%%_redirect%%" method="GET"></http>
</request>
<until var="redirect" eq=""/>
</repeat>
</transaction>
And from this point I have a problem, because on login page I have to submit data to current URL, but after last iteration variable "redirect" becomes empty. I've tried to create additional variable with condition in cycle to save last non-empty value :
<transaction name="get_login_page">
<request>
<dyn_variable name="redirect" re="Location: ((http|https)://.*)\r"/>
<http url='/' version='1.1' method='GET'>
</http>
</request>
<repeat name="redirect_loop" max_repeat="5">
<request subst="true">
<dyn_variable name="redirect" re="Location: ((http|https)://.*)\r"/>
<if var="redirect" eq=''>
<dyn_variable name="login_url" re="Location: ((http|https)://.*)\r"/>
</if>
<http url="%%_redirect%%" method="GET"></http>
</request>
<until var="redirect" eq=""/>
</repeat>
</transaction>
But now I've got an error on tsung run :
Starting Tsung
"Log directory is: /home/***/login_portal/logs/20141103-0945"
594- fatal: {failed_validation,element_unauthorize_in_choice}
Config Error, aborting ! {{badmatch,
{<<"<arrivalphase phase=\"1\" duration=\"5\" unit=\"second\">\n <users maxnumber=\"1\" arrivalrate=\"1\" unit=\"second\"></users>\n</arrivalphase>\n">>,
{xmerl_sax_parser_state,undefined,
#Fun<xmerl_sax_parser.1.53952608>,
{file_descriptor,prim_file,
{#Port<0.1047>,13}},
#Fun<xmerl_sax_parser.default_continuation_cb.1>,
utf8,1,
[{"xml",
"http://www.w3.org/XML/1998/namespace"}],
[],[],true,69656,no,normal,
"/home/***/login_portal/.",
"tsung_config_load.xml",false}}},
[{xmerl_sax_parser_utf8,handle_external_entity,2,
[{file,"xmerl_sax_parser_utf8.erl"},
{line,1692}]},
{xmerl_sax_parser_utf8,parse_external_entity,3,
[{file,"xmerl_sax_parser_utf8.erl"},
{line,1670}]},
{xmerl_sax_parser_utf8,parse_content,4,
[{file,"xmerl_sax_parser_utf8.erl"},
{line,1157}]},
{xmerl_sax_parser_utf8,parse_document,2,
[{file,"xmerl_sax_parser_utf8.erl"},
{line,179}]},
{xmerl_sax_parser_utf8,parse,2,
[{file,"xmerl_sax_parser_utf8.erl"},
{line,115}]},
{xmerl_sax_parser,file,2,
[{file,"xmerl_sax_parser.erl"},{line,77}]},
{ts_config,read,2,
[{file,"src/tsung_controller/ts_config.erl"},
{line,77}]},
{ts_config_server,handle_call,3,
[{file,
"src/tsung_controller/ts_config_server.erl"},
{line,206}]}]}
Is there is a way to get last accessed URL or to get it from DOM model using built-in functions? Or how I should make condition to let it work properly?

Related

Dynamic variable on Tsung XML config file

I've set up TSUNG (v1.7) to test my application, but I'm facing some problem using a dynamic variable on my http request. To be more precise I need to retrieve some data from a CSV file and inserting it in my request.
Reading the documentation it seems that I don't really need to write any sort of functions, since I'm using a version above the 1.3, so to achieve that I just need to specify the file path on the 'option' tag and use the 'setdynvars', but unfortunately it doesn't seems works (the web server response says that the content is empty). Any idea why?
<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/home/Desktop/tsung-1.7.0/tsung-1.0.dtd">
<tsung loglevel="warning">
<clients>
<client host="localhost" use_controller_vm="true"/>
</clients>
<servers>
<server host="127.0.0.1" port="8000" type="tcp"/>
</servers>
<load>
<arrivalphase phase="1" duration="2" unit="minute">
<users interarrival="45" unit="second"/>
</arrivalphase>
</load>
<options>
<option name="file_server" id="transactions" value="/home/Desktop/transactions.csv"/>
</options>
<sessions>
<session name="dummy" weight="1" type="ts_http">
<setdynvars sourcetype="file" fileid="transactions" delimiter=";" order="iter">
<var name="number_transaction"/>
</setdynvars>
<request>
<http url="...path..." method="GET" version="1.1"></http>
</request>
<request subst="true">
<http url='...path...' version='1.1' contents='transaction_id=%%_number_transaction%%' content_type='application/x-www-fomr-urlencoded' method='POST'></http>
</request>
</session>
</sessions>
</tsung>
After some attempt I've figure out that by simply removing the attribute content_type from the request it will make the whole configuration works!

Specify separate data to each user in Tsung

I am using Tsung for load testing. Here is the config file for Tsung.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd" []>
<tsung loglevel="warning">
<clients>
<client host="t1" cpu="2" maxusers="30000000"/>
<client host="t2" cpu="2" maxusers="30000000"/>
</clients>
<servers>
<server host="localhost" port="9200" type="tcp"/>
</servers>
<load>
<arrivalphase phase="1" duration="1" unit="minute">
<users arrivalrate="5" unit="second"/>
</arrivalphase>
</load>
</tsung>
But, I want the following:
Only one user per client everytime
Specific data to be read from file for each user. As in, I want to read data from a user1.json for user1 (on client 1) and from user2.json for user2 (on client2).
Is this possible in Tsung? I went through the docs, but didn't find any option to do so. Can someone help me out with this?
Not exactly what you're asking for. But something similar is possible, with one input file.
<options>
<option name="file_server" id="inputUsers" value="/tmp/users.txt"/>
</options>
<sessions>
<session probability="100" name="test" type="ts_http" >
<setdynvars sourcetype="file" fileid="inputUsers" delimiter=";" order="iter">
<var name="userId"/>
<var name="deviceMac"/>
<var name="tKey"/>
</setdynvars>
<request subst="true">
<http url="/abc/%%_userId%%/%%_deviceMac%%?arg=%%_tKey%%" version="1.1"></http>
</request>
<request subst="true">
<http url="/123/%%_userId%%" version="1.1"></http>
</request>
</session>
</sessions>
Where /tmp/users.txt contains colon separated user specific values - something like this (userId;deviceMac;tKey):
97099;05d4e99de98a;4xrwgyyze54kefnwsd74kj4ghvn5f1
Considering the setdynvars order value is "iter", it will iterate through each line, and use that input data as request parameters.
In the above example case, it would make these two requests:
/abc/97099/05d4e99de98a?arg=4xrwgyyze54kefnwsd74kj4ghvn5f1
/123/97099
You can achieve "user specific" load test scenario this way.

is it possible dynamically post files using tsung?

i am planning to post dynamic files,
<request subst="true">
<http url="/test.php" contents_from_file="file.csv" method="POST"></http>
</request>
is there any option to post dynamic files instead of hard coded one ?
The contents_from_file arg not support dynamic.
please see the ts_config_http.erl file line 58.
You can also refer to this way:
<if var="Args" eq="1">
<request subst="true">
<http url="/test.php" contents_from_file="1.csv" method="POST"></http>
</request>
</if>
<if var="Args" eq="0">
<request subst="true">
<http url="/test.php" contents_from_file="2.csv" method="POST"></http>
</request>
</if>

spring-security: where did the Principal go?

I've been playing with spring-security for a while and for some reason I'm not able to access the principal in the JSPs even when authentication and authorization are working right.
This is my index.jsp:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%# page import="org.springframework.security.core.context.SecurityContextHolder" %>
for authenticated<br/>
for admins
<sec:authorize access="! isAuthenticated()">
not logged in
</sec:authorize>
<sec:authorize access=" isAuthenticated()">
logged in
</sec:authorize>
Your principal object is....: <sec:authentication property="principal.username" /><br/>
Authentication = <%=SecurityContextHolder.getContext().getAuthentication() %>
<p>Logout</p>
And this is the *-security-xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http pattern="/loggedout.jsp" security="none"/>
<http pattern="/index.jsp" security="none"/>
<http pattern="/login.jsp" security="none"/>
<http auto-config="true">
<intercept-url pattern="/admin/*" access="ROLE_ADMIN,ROLE_SUPERUSER" />
<intercept-url pattern="/forAuthenticated.jsp" access="ROLE_USER" />
<intercept-url pattern="/logoutSuccess*" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page="/login.jsp"
authentication-failure-url = "/login.jsp?login_error=1"/>
<logout logout-success-url="/loggedout.jsp" delete-cookies="JSESSIONID"/>
<session-management invalid-session-url="/sessionTimeout.htm" />
</http>
<global-method-security pre-post-annotations="enabled"/>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service id="userDetailsService">
<user name="username" password="password" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="test" password="test" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
When I try to access the forAuthenticated.jsp I'm prompted for login. After entering test/test I access the forAuthenticated page, but not the admin/adminUsers.jsp. This is ok, but the problem is that I can't access the principal in the index.jsp. This is what index.jsp prints when logged in as test/test. Note that I can't see the not logged in nor logged in traces:
for authenticated
for admins
Your principal object is....:
null
Logout
What am I missing? what should I double-ckeck()?
As Karthikeyan suggests in his comment, the problem is the following line:
<http pattern="/index.jsp" security="none"/>
which effectively causes requests matching the given pattern to be dispatched straight to the handler method skipping the Spring Security filter chain. Not even the SecurityContext gets initialized in this case, so the authentication object won't be available for the <sec:authorize> tag based on which it should make decision whether to reveal the wrapped content (and it obviously won't by default).
The reference doc states it clearly as well:
A request pattern can be mapped to an empty filter chain, by setting this attribute (security) to none. No security will be applied and none of Spring Security's features will be available.
Instead of mapping an empty filter chain, you should simply allow anonymous access:
<sec:intercept-url pattern="/index.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/>

Parsing error for spring-security.xml file

I know very little or nothing about xml and I have to write a spring-security.xml file . The problem i guess has something to do with my xml not following xsd. Here is the xml.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:s="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<s:http auto-config="true">
<s:intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<s:intercept-url pattern="/index.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<s:intercept-url pattern="/**" access="ROLE_USER" />
<s:intercept-url pattern="/" access="ROLE_USER" />
<s:form-login login-page="/login" default-target-url="/getemp"/>
<s:logout logout-success-url="/logout" />
</s:http>
<s:authentication-manager>
<s:authentication-provider>
<s:ldap-authentication-provider user-dn-pattern="uid={0},ou=people"/>
</s:authentication-provider>
</s:authentication-manager>
<s:ldap-server id="ldapServer" url="ldap://test.com:389" />
</beans>
When I try to run the web application I am getting an error.
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 's:ldap-authentication-provider'. One of '{"http://www.springframework.org/schema/security":any-user-service, "http://www.springframework.org/schema/security":password-encoder}' is expected.
Here is the xsd
spring security xsd
The xsd says that <s:authentication-manager> accepts as children an authentication-provider OR an ldap-authentication-provider. So, remove the <s:authentication-provider> that is wrapping your <s:ldap-authentication-provider> and that should get you past this problem. Your final code should look like:
<s:authentication-manager>
<s:ldap-authentication-provider user-dn-pattern="uid={0},ou=people"/>
</s:authentication-manager>

Resources