Jmeter MD5 plug-in and jmetermd5 plug-in

Source: Internet
Author: User
Tags time in milliseconds

Jmeter MD5 plug-in and jmetermd5 plug-in

In actual business, an MD5 verification field is required to be added to the HTTP protocol to prevent malicious tampering of request parameters. This is a simple requirement for developers. However, it makes the automated test more difficult. Jmeter native does not support this function. At the requirements of testers, a simple small plug-in is developed.

I. Function Analysis

1. MD5 value generation rules:

Step 1: add the new ct parameter to the HTTP request, with the value of the current time in milliseconds;

Step 2: Get all parameters of the HTTP request and put them in the list;

Step 3: add the new parameter key to the List according to the agreement between the parties (not transmitted in the http request );

Step 4: sort the List by parameter name, splice the sorted parameter values, and perform MD5 operations on the splicing result;

Step 5: Add a new parameter code for the Http request. The value is the value calculated by step 4.

Note: different services may have slightly different rules. implement these rules based on actual conditions.

2. the MD5 value must be added before the HTTP request (which belongs to the Jmeter Preprocessor range ).

 

Ii. Problems to be Solved

1. Get and add parameters in the HTTP request of Jmeter

2. Implement the specific MD5 Logic

3. append the generated MD5 check value to the HTTP request parameters at the appropriate time.

4. integrate it into the Jmeter menu for ease of use.

 

3. Solution steps

1. Because you have no Jmeter plug-in development experience before, you need to learn how to develop it first. There is no clear solution for collecting information on the Internet. However, Jmeter has many plug-ins. Let's start from here and analyze the implementation methods of the existing plug-ins, and then use the same method to complete the plug-ins.

2. Download The Jmeter source code (version 2.11 I use) from the Apache website and locate the plug-in Code Location Based on the keywords displayed on the Jmeter interface.

3. The Http Sampler plug-in is developed. Search for the Http Sampler keyword to find HttpSampler. java. Read the code and obtain the answer to question 1.

4. Question 2 is not described here.

5. Problem 3: We need to call the plug-in before sending the HTTP request. We can see that this plug-in should be within the scope of the Preprocessor. This problem can be solved by analyzing the pre-processor code. In the Jmeter plug-in, the first PreProcessor is BeanShell PreProcessor. You can use the BeanShell and PreProcessor keywords to find the source code and find the BeanShellPreProcessor class. java; read the code of this class and its parent class to get answers to question 3 and question 4.

 

4. Specific Code

1. MD5PreProcessor. java

1 public class MD5PreProcessor extends acttestelement implements TestStateListener, PreProcessor, Serializable {2 3 private static final long serialVersionUID =-1L; 4 private static final Logger LOGGER = LoggingManager. getLoggerForClass (); 5 6/** 7 * Default constructor. 8 */9 public MD5PreProcessor () {10} 11 12 @ Override 13 public void testStarted () {14} 15 16 @ Override 17 public void TestStarted (String host) {18} 19 20 @ Override 21 public void testEnded () {22} 23 24 @ Override 25 public void testEnded (String host) {26} 27 28/* 29 * ---------------------------------------------------------------------------- 30 * Methods implemented from interface org. apache. jmeter. config. modifier 31 * -------------------------------------------------------------------------- 32 */33 34 @ Over Ride 35 public void process () {36 Sampler sam = getThreadContext (). getCurrentSampler (); 37 HTTPSamplerBase sampler = null; 38 if (! (Sam instanceof HTTPSamplerBase) {39 return; 40} else {41 sampler = (HTTPSamplerBase) sam; 42} 43 44 // sorting parameters and header (appversion, OS) 45 java. util. list <KeyValue> list = new ArrayList <KeyValue> (); 46 47 48 sampler. addArgument ("ct", System. currentTimeMillis () + ""); 49 50 // Add parameter list 51 Arguments arguments = sampler. getArguments (); 52 PropertyIterator iter = arguments. iterator (); 53 while (iter. hasNext () {54 Argument arg = getFromJMeterProperty (iter. next (); 55 KeyValue kv = new KeyValue (arg. getName (), arg. getValue (); 56 list. add (kv); 57} 58 59 // add header: OS appVersion 60 HeaderManager headerManager = sampler. getHeaderManager (); 61 for (int I = 0; I View Code

 

2. Md5PreProcessorGui. java

public class Md5PreProcessorGui extends AbstractPreProcessorGui {    private static final long serialVersionUID = 100L;    private static final Logger LOGGER = LoggingManager.getLoggerForClass();    public Md5PreProcessorGui() {        createGui();    }    public String getStaticLabel() {        return "MD5 Encode";    }    public String getLabelResource() {        return getClass().getCanonicalName();    }    public void configure(TestElement element) {        super.configure(element);    }    public TestElement createTestElement() {        MD5PreProcessor sampler = new MD5PreProcessor();        modifyTestElement(sampler);        return sampler;    }    public void modifyTestElement(TestElement element) {        element.clear();        configureTestElement(element);    }    public void clearGui() {        super.clearGui();    }    private void createGui() {        setLayout(new BorderLayout(0, 5));        setBorder(makeBorder());        this.setLayout(new BorderLayout(0, 5));        this.setBorder(this.makeBorder());        this.add(this.makeTitlePanel(), "North");        VerticalPanel mainPanel = new VerticalPanel();        add(mainPanel, "Center");    }}
View Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.