Resource icon

Generating java hashcode equivalant in loadrunner (C Language)

  • perf-test.com need your contributions to build up a strong repository of performance engineering resources.

We recently encountered a situation where we were expected to simulate a forgot password retrieval functionality. Application used to send an email to registered user id and user was expected to click on link in the email to reset the password.

To avoid the hassle of retrieving email we went directly to developer to understand the logic of the link generation. The logic involved passing of a hashed value into the generated url. Hash was generated using the java hashcode upon username+password. Below is an example of java hashcode

Code:
import java.io.*;
public class HelloWorld{

     public static void main(String []args){
              String Str = new String("SPEForums.com");
      System.out.println("Hashcode for Str :" + Str.hashCode() );
     }
}

We could not find a way to include above code in http/html scripts, hence we had to find the equivalent code of hashcode in C language.

Following code was used for this in loadrunner
Code:
Action()
{
            //h is the hash value
            int h = 0;           
            //Left offset Value
            int off = 0;           
            //String to be hashed
            char val[] = "SPEForums.com";           
            //Length of the string
            int len = strlen(val);
           
            //For loop
            int i;
           
           
            //Algo equivalant to java hashcode
           for (i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
           // hash = h; Print hash code
            lr_output_message("hash is %d",h);

    return 0;
}

Screenshots:

upload_2016-10-29_0-3-29.png


Output::

upload_2016-10-29_0-4-31.png
Author
admin
Views
49
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from admin