View | Details | Raw Unified | Return to bug 61724
Collapse All | Expand All

(-)src/core/org/apache/jmeter/functions/AbstractFunction.java (+39 lines)
Lines 20-25 Link Here
20
20
21
import java.util.Collection;
21
import java.util.Collection;
22
22
23
import org.apache.commons.lang3.StringUtils;
23
import org.apache.jmeter.engine.util.CompoundVariable;
24
import org.apache.jmeter.engine.util.CompoundVariable;
24
import org.apache.jmeter.samplers.SampleResult;
25
import org.apache.jmeter.samplers.SampleResult;
25
import org.apache.jmeter.samplers.Sampler;
26
import org.apache.jmeter.samplers.Sampler;
Lines 31-36 Link Here
31
 * Provides common methods for all functions
32
 * Provides common methods for all functions
32
 */
33
 */
33
public abstract class AbstractFunction implements Function {
34
public abstract class AbstractFunction implements Function {
35
	
36
	private static final String SHOULD_DO_UPPERCASE = "true";
34
37
35
    /**
38
    /**
36
     * <p><b>
39
     * <p><b>
Lines 140-143 Link Here
140
                   );
143
                   );
141
        }
144
        }
142
    }
145
    }
146
    
147
    /**
148
     * Utility method to add variable value by variable name
149
     * 
150
     * @param variableName variable name to put
151
     * @param value variable value to put 
152
     */
153
	protected void addVariableValue(String value, CompoundVariable[] values, int index) {
154
		if (values.length > index) {
155
			String variableName = values[index].execute();
156
			if (StringUtils.isNotEmpty(variableName)) {
157
				JMeterVariables vars = getVariables();
158
				if (vars != null) {
159
					vars.put(variableName, value);
160
				}
161
			}
162
		}
163
	}
164
	
165
166
	/**
167
	 * Upper case value if optional parameter value is true 
168
	 * @param encodedString
169
	 * @param index
170
	 * @return
171
	 */
172
	protected String upperCase(String encodedString, CompoundVariable[] values, int index) {
173
		if (values.length > index) {
174
			String shouldUpperCase = values[index].execute();
175
			boolean shouldDoUpperCase = SHOULD_DO_UPPERCASE.equalsIgnoreCase(shouldUpperCase);
176
			if (shouldDoUpperCase) {
177
				encodedString = encodedString.toUpperCase();
178
			}
179
		}
180
		return encodedString;
181
	}
143
}
182
}
(-)src/core/org/apache/jmeter/resources/messages.properties (+3 lines)
Lines 933-938 Link Here
933
running_test=Running test
933
running_test=Running test
934
runtime_controller_title=Runtime Controller
934
runtime_controller_title=Runtime Controller
935
runtime_seconds=Runtime (seconds)
935
runtime_seconds=Runtime (seconds)
936
salt_string=Salt to be used for encoding
936
sample_result_save_configuration=Sample Result Save Configuration
937
sample_result_save_configuration=Sample Result Save Configuration
937
sample_scope=Apply to:
938
sample_scope=Apply to:
938
sample_scope_all=Main sample and sub-samples
939
sample_scope_all=Main sample and sub-samples
Lines 1023-1028 Link Here
1023
servername=Servername \:
1024
servername=Servername \:
1024
session_argument_name=Session Argument Name
1025
session_argument_name=Session Argument Name
1025
setup_thread_group_title=setUp Thread Group
1026
setup_thread_group_title=setUp Thread Group
1027
sha_string=String to be encoded
1026
should_save=You should save your test plan before running it.  \nIf you are using supporting data files (ie, for CSV Data Set or __StringFromFile), \nthen it is particularly important to first save your test script. \nDo you want to save your test plan first?
1028
should_save=You should save your test plan before running it.  \nIf you are using supporting data files (ie, for CSV Data Set or __StringFromFile), \nthen it is particularly important to first save your test script. \nDo you want to save your test plan first?
1027
shutdown=Shutdown
1029
shutdown=Shutdown
1028
simple_config_element=Simple Config Element
1030
simple_config_element=Simple Config Element
Lines 1224-1229 Link Here
1224
update_per_iter=Update Once Per Iteration
1226
update_per_iter=Update Once Per Iteration
1225
upload=File Upload
1227
upload=File Upload
1226
upper_bound=Upper Bound
1228
upper_bound=Upper Bound
1229
upper_case=Upper case result - default false (optional)
1227
url=URL
1230
url=URL
1228
url_config_get=GET
1231
url_config_get=GET
1229
url_config_http=HTTP
1232
url_config_http=HTTP
(-)src/functions/org/apache/jmeter/functions/Sha1Encode.java (+58 lines)
Line 0 Link Here
1
package org.apache.jmeter.functions;
2
3
import org.apache.jmeter.engine.util.CompoundVariable;
4
import org.apache.jmeter.functions.AbstractFunction;
5
import org.apache.jmeter.functions.InvalidVariableException;
6
import org.apache.jmeter.samplers.SampleResult;
7
import org.apache.jmeter.samplers.Sampler;
8
import org.apache.jmeter.threads.JMeterVariables;
9
import org.apache.jmeter.util.JMeterUtils;
10
import org.apache.commons.codec.digest.DigestUtils;
11
import org.apache.commons.lang3.StringUtils;
12
13
import java.util.Collection;
14
import java.util.LinkedList;
15
import java.util.List;
16
17
public class Sha1Encode extends AbstractFunction {
18
19
	private static final List<String> desc = new LinkedList<String>();
20
	private static final String KEY = "__sha1";
21
22
	// Number of parameters expected - used to reject invalid calls
23
	private static final int MIN_PARAMETER_COUNT = 1;
24
	private static final int MAX_PARAMETER_COUNT = 3;
25
26
	static {
27
		desc.add(JMeterUtils.getResString("sha_string"));
28
		desc.add(JMeterUtils.getResString("upper_case"));
29
		desc.add(JMeterUtils.getResString("function_name_paropt"));
30
	}
31
32
	private CompoundVariable[] values;
33
34
	@Override
35
	public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
36
		String stringToEncode = values[0].execute();
37
		String encodedString = DigestUtils.sha1Hex(stringToEncode);
38
		encodedString = upperCase(encodedString, values, 1);
39
		addVariableValue(encodedString, values , 2);
40
		return encodedString;
41
	}
42
43
	@Override
44
	public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
45
		checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
46
		values = parameters.toArray(new CompoundVariable[parameters.size()]);
47
	}
48
49
	@Override
50
	public String getReferenceKey() {
51
		return KEY;
52
	}
53
54
	@Override
55
	public List<String> getArgumentDesc() {
56
		return desc;
57
	}
58
}
(-)src/functions/org/apache/jmeter/functions/Sha512Encode.java (+78 lines)
Line 0 Link Here
1
package org.apache.jmeter.functions;
2
3
import org.apache.jmeter.engine.util.CompoundVariable;
4
import org.apache.jmeter.functions.AbstractFunction;
5
import org.apache.jmeter.functions.InvalidVariableException;
6
import org.apache.jmeter.samplers.SampleResult;
7
import org.apache.jmeter.samplers.Sampler;
8
import org.apache.jmeter.util.JMeterUtils;
9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
11
12
import java.io.UnsupportedEncodingException;
13
import java.security.MessageDigest;
14
import java.security.NoSuchAlgorithmException;
15
16
import java.util.Collection;
17
import java.util.LinkedList;
18
import java.util.List;
19
20
public class Sha512Encode extends AbstractFunction {
21
22
	private static final Logger log = LoggerFactory.getLogger(Sha512Encode.class);
23
	private static final String UTF_8 = "UTF-8";
24
	private static final String SHA_512 = "SHA-512";
25
	private static final List<String> desc = new LinkedList<String>();
26
	private static final String KEY = "__sha512";
27
28
	// Number of parameters expected - used to reject invalid calls
29
	private static final int MIN_PARAMETER_COUNT = 2;
30
	private static final int MAX_PARAMETER_COUNT = 4;
31
32
	static {
33
		desc.add(JMeterUtils.getResString("sha_string"));
34
		desc.add(JMeterUtils.getResString("salt_string"));
35
		desc.add(JMeterUtils.getResString("upper_case"));
36
		desc.add(JMeterUtils.getResString("function_name_paropt"));
37
	}
38
39
	private CompoundVariable[] values;
40
41
	@Override
42
	public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
43
		String encodedString = null;
44
		String stringToEncode = values[0].execute();
45
		String salt = values[1].execute();
46
		try {
47
			MessageDigest md = MessageDigest.getInstance(SHA_512);
48
			md.update(salt.getBytes(UTF_8));
49
			byte[] bytes = md.digest(stringToEncode.getBytes(UTF_8));
50
			StringBuilder sb = new StringBuilder();
51
			for (int i = 0; i < bytes.length; i++) {
52
				sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
53
			}
54
			encodedString = upperCase(sb.toString(), values, 2);
55
			addVariableValue(encodedString, values, 3);
56
57
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
58
			log.warn("Error executing SHA512", e);
59
		}
60
		return encodedString;
61
	}
62
63
	@Override
64
	public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
65
		checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
66
		values = parameters.toArray(new CompoundVariable[parameters.size()]);
67
	}
68
69
	@Override
70
	public String getReferenceKey() {
71
		return KEY;
72
	}
73
74
	@Override
75
	public List<String> getArgumentDesc() {
76
		return desc;
77
	}
78
}
(-)test/src/org/apache/jmeter/functions/TestShaFunction.java (+91 lines)
Line 0 Link Here
1
package org.apache.jmeter.functions;
2
3
import static org.junit.Assert.assertEquals;
4
5
import java.util.Collection;
6
import java.util.LinkedList;
7
8
import org.apache.jmeter.engine.util.CompoundVariable;
9
import org.apache.jmeter.junit.JMeterTestCase;
10
import org.apache.jmeter.samplers.SampleResult;
11
import org.apache.jmeter.threads.JMeterContext;
12
import org.apache.jmeter.threads.JMeterContextService;
13
import org.apache.jmeter.threads.JMeterVariables;
14
import org.junit.Before;
15
import org.junit.Test;
16
17
public class TestShaFunction extends JMeterTestCase {
18
	protected AbstractFunction sha512;
19
	protected AbstractFunction sha1;
20
21
    private SampleResult result;
22
23
    private Collection<CompoundVariable> params;
24
25
    private JMeterVariables vars;
26
27
    private JMeterContext jmctx;
28
29
    @Before
30
    public void setUp() {
31
    	sha512 = new Sha512Encode();
32
        sha1 = new Sha1Encode();
33
        result = new SampleResult();
34
        jmctx = JMeterContextService.getContext();
35
        String data = "The quick brown fox";
36
        result.setResponseData(data, null);
37
        vars = new JMeterVariables();
38
        jmctx.setVariables(vars);
39
        jmctx.setPreviousResult(result);
40
        params = new LinkedList<>();
41
    }
42
    
43
    
44
    @Test
45
    public void testParameterCount512() throws Exception {
46
    	checkInvalidParameterCounts(sha512, 2, 4);
47
    }
48
49
    @Test
50
    public void testParameterCount1() throws Exception {
51
        checkInvalidParameterCounts(sha1, 1, 2);
52
    }
53
    
54
    @Test
55
    public void testSha512() throws Exception {
56
    	params.add(new CompoundVariable("nofile"));
57
    	params.add(new CompoundVariable("nofile"));
58
    	sha512.setParameters(params);
59
    	String returnValue = sha512.execute(result, null);
60
    	assertEquals("0a76f7cd4f3cd9bec27146c32a8cad3de865a48d07ff6a2a40f18f2b3307fef4d0de695d14c90234bd453b375aa2974ba17743564bc782ad1d6cf8b3f4db72a4", returnValue);
61
    }
62
    
63
    @Test
64
    public void testSha1() throws Exception {
65
    	params.add(new CompoundVariable("nofile"));    	
66
    	sha1.setParameters(params);
67
    	String returnValue = sha1.execute(result, null);
68
    	assertEquals("4ea2ced10057872be25371cfe638d3b096c58f2f", returnValue);
69
    }
70
    @Test
71
    public void testSha1Variable() throws Exception {
72
    	params.add(new CompoundVariable("nofile"));  
73
    	params.add(new CompoundVariable("true"));
74
    	params.add(new CompoundVariable("newVar"));  	
75
    	sha1.setParameters(params);
76
    	String returnValue = sha1.execute(result, null);
77
    	assertEquals("4EA2CED10057872BE25371CFE638D3B096C58F2F", returnValue);
78
    }
79
80
    @Test
81
    public void testSha512Variable() throws Exception {
82
    	params.add(new CompoundVariable("nofile"));
83
    	params.add(new CompoundVariable("nofile"));
84
    	params.add(new CompoundVariable("true"));
85
    	params.add(new CompoundVariable("newVar"));
86
        sha512.setParameters(params);
87
        String returnValue = sha512.execute(result, null);
88
        assertEquals("0A76F7CD4F3CD9BEC27146C32A8CAD3DE865A48D07FF6A2A40F18F2B3307FEF4D0DE695D14C90234BD453B375AA2974BA17743564BC782AD1D6CF8B3F4DB72A4", returnValue);
89
    }
90
    
91
}

Return to bug 61724