0

I am testing a security service(running on remote linux server) which returns me an access token which is valid for 1 day.

I am writing a JMeter script to test this scenario. Here are the steps that i want to follow:

  1. Make a http token request to the service.
  2. Once the access token is received, validate if it is a valid token by resending it to the security service.
  3. Change system date and increment it by 1 day so that the token becomes invalid.
  4. Once invalid, validate it again by sending it to the security service.

I am not sure how can i execute step 3 and if it is possible via JMeter in the first place.

Stuck. Please suggest.

Thanks.

silver_noodles
  • 361
  • 2
  • 4
  • 12

2 Answers2

1

I'd suggest doing a Java ProcessBuilder.start() of the Linux date command.

One issue is that you'll need the appropriate privilege to change the system date.

It's worth noting that Linux date has the built-in ability to increment date values:

How to increment a date in a bash script

DATE=2013-05-25

for i in {0..8} do
  NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day") 
  echo $NEXT_DATE 
done

produces:

05-25-2013 
05-26-2013 
....
05-31-2013 
06-01-2013 
06-02-2013
Community
  • 1
  • 1
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
0

Given that you need to deal with remote linux system you'll need to change date somehow via SSH channel. So I would recommend doing it as follows:

  1. Download JSch.jar file (Java library which allows SSH and SCP protocols manipulations from Java code) and drop it to /lib folder of your JMeter installation. JMeter restart will be required to pick the .jar up.
  2. Add a Beanshell Sampler to your test plan where you need to change remote linux system time
  3. Put the following code into the Beanshell Sampler's "Script" area:

    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;
    
    JSch jSch = new JSch();
    Session session = jSch.getSession("root", "your.host", 22);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setPassword("your password");
    session.connect();
    
    Channel channel = session.openChannel("exec");
    
    String command = "date -s \"$(date --date \"+1 day\")\"";
    ((ChannelExec) channel).setCommand(command);
    channel.setInputStream(null);
    ((ChannelExec) channel).setErrStream(System.err);
    InputStream in = channel.getInputStream();
    
    channel.connect();
    StringBuilder rv = new StringBuilder();
    rv.append("New system date: ");
    byte[] tmp = new byte[1024];
    while (true) {
      while (in.available() > 0) {
          int i = in.read(tmp, 0, 1024);
          if (i < 0) break;
          rv.append(new String(tmp, 0, i));
      }
      if (channel.isClosed()) {
          break;
      }
      try {
          Thread.sleep(100);
      } catch (Exception ee) {
          ee.printStackTrace();
      }
    }
    in.close();
    channel.disconnect();
    session.disconnect();        
    SampleResult.setResponseData(rv.toString().getBytes());
    
  4. Beanshell Sampler response data will look something like:

    New system date: Mon Apr 13 10:16:48 BST 2015
    

See How to use BeanShell: JMeter's favorite built-in component to learn how to enhance your test with scripting where JMeter doesn't offer required test element.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133