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 (+62 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.apache.commons.codec.digest.DigestUtils;
10
import java.util.Collection;
11
import java.util.LinkedList;
12
import java.util.List;
13
14
/**
15
 * 
16
 * Sha1 Encode Function Optional Upper case value and save to variable name
17
 * 
18
 * @author orim
19
 *
20
 */
21
public class Sha1Encode extends AbstractFunction {
22
23
	private static final List<String> desc = new LinkedList<String>();
24
	private static final String KEY = "__sha1";
25
26
	// Number of parameters expected - used to reject invalid calls
27
	private static final int MIN_PARAMETER_COUNT = 1;
28
	private static final int MAX_PARAMETER_COUNT = 3;
29
30
	static {
31
		desc.add(JMeterUtils.getResString("sha_string"));
32
		desc.add(JMeterUtils.getResString("upper_case"));
33
		desc.add(JMeterUtils.getResString("function_name_paropt"));
34
	}
35
36
	private CompoundVariable[] values;
37
38
	@Override
39
	public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
40
		String stringToEncode = values[0].execute();
41
		String encodedString = DigestUtils.sha1Hex(stringToEncode);
42
		encodedString = upperCase(encodedString, values, 1);
43
		addVariableValue(encodedString, values, 2);
44
		return encodedString;
45
	}
46
47
	@Override
48
	public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
49
		checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
50
		values = parameters.toArray(new CompoundVariable[parameters.size()]);
51
	}
52
53
	@Override
54
	public String getReferenceKey() {
55
		return KEY;
56
	}
57
58
	@Override
59
	public List<String> getArgumentDesc() {
60
		return desc;
61
	}
62
}
(-)src/functions/org/apache/jmeter/functions/Sha512Encode.java (+85 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
/**
21
 * 
22
 * Sha512 Encode Function Optional Upper case value and save to variable name
23
 * 
24
 * @author orim
25
 *
26
 */
27
public class Sha512Encode extends AbstractFunction {
28
29
	private static final Logger log = LoggerFactory.getLogger(Sha512Encode.class);
30
	private static final String UTF_8 = "UTF-8";
31
	private static final String SHA_512 = "SHA-512";
32
	private static final List<String> desc = new LinkedList<String>();
33
	private static final String KEY = "__sha512";
34
35
	// Number of parameters expected - used to reject invalid calls
36
	private static final int MIN_PARAMETER_COUNT = 2;
37
	private static final int MAX_PARAMETER_COUNT = 4;
38
39
	static {
40
		desc.add(JMeterUtils.getResString("sha_string"));
41
		desc.add(JMeterUtils.getResString("salt_string"));
42
		desc.add(JMeterUtils.getResString("upper_case"));
43
		desc.add(JMeterUtils.getResString("function_name_paropt"));
44
	}
45
46
	private CompoundVariable[] values;
47
48
	@Override
49
	public synchronized String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
50
		String encodedString = null;
51
		String stringToEncode = values[0].execute();
52
		String salt = values[1].execute();
53
		try {
54
			MessageDigest md = MessageDigest.getInstance(SHA_512);
55
			md.update(salt.getBytes(UTF_8));
56
			byte[] bytes = md.digest(stringToEncode.getBytes(UTF_8));
57
			StringBuilder sb = new StringBuilder();
58
			for (int i = 0; i < bytes.length; i++) {
59
				sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
60
			}
61
			encodedString = upperCase(sb.toString(), values, 2);
62
			addVariableValue(encodedString, values, 3);
63
64
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
65
			log.warn("Error executing SHA512", e);
66
		}
67
		return encodedString;
68
	}
69
70
	@Override
71
	public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
72
		checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
73
		values = parameters.toArray(new CompoundVariable[parameters.size()]);
74
	}
75
76
	@Override
77
	public String getReferenceKey() {
78
		return KEY;
79
	}
80
81
	@Override
82
	public List<String> getArgumentDesc() {
83
		return desc;
84
	}
85
}
(-)test/src/org/apache/jmeter/functions/TestShaFunction.java (+122 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
 * Test Sha1 and Sha512 functions
18
 * 
19
 * @author orim
20
 *
21
 */
22
public class TestShaFunction extends JMeterTestCase {
23
	protected AbstractFunction sha512;
24
	protected AbstractFunction sha1;
25
26
    private SampleResult result;
27
28
    private Collection<CompoundVariable> params;
29
30
    private JMeterVariables vars;
31
32
    private JMeterContext jmctx;
33
34
    @Before
35
    public void setUp() {
36
    	sha512 = new Sha512Encode();
37
        sha1 = new Sha1Encode();
38
        result = new SampleResult();
39
        jmctx = JMeterContextService.getContext();
40
        String data = "dummy data";
41
        result.setResponseData(data, null);
42
        vars = new JMeterVariables();
43
        jmctx.setVariables(vars);
44
        jmctx.setPreviousResult(result);
45
        params = new LinkedList<>();
46
    }
47
    
48
    
49
    @Test
50
    public void testParameterCount512() throws Exception {
51
    	checkInvalidParameterCounts(sha512, 2, 4);
52
    }
53
54
    @Test
55
    public void testParameterCount1() throws Exception {
56
        checkInvalidParameterCounts(sha1, 1, 2);
57
    }
58
    
59
    @Test
60
    public void testSha512() throws Exception {
61
    	params.add(new CompoundVariable("nofile"));
62
    	params.add(new CompoundVariable("nofile"));
63
    	sha512.setParameters(params);
64
    	String returnValue = sha512.execute(result, null);
65
    	assertEquals("0a76f7cd4f3cd9bec27146c32a8cad3de865a48d07ff6a2a40f18f2b3307fef4d0de695d14c90234bd453b375aa2974ba17743564bc782ad1d6cf8b3f4db72a4", returnValue);
66
    }      
67
    
68
    @Test
69
    public void testSha1() throws Exception {
70
    	params.add(new CompoundVariable("nofile"));    	
71
    	sha1.setParameters(params);
72
    	String returnValue = sha1.execute(result, null);
73
    	assertEquals("4ea2ced10057872be25371cfe638d3b096c58f2f", returnValue);
74
    }
75
    @Test
76
    public void testSha1Variable() throws Exception {
77
    	params.add(new CompoundVariable("nofile"));  
78
    	params.add(new CompoundVariable("true"));
79
    	params.add(new CompoundVariable("newVar"));  	
80
    	sha1.setParameters(params);
81
    	String returnValue = sha1.execute(result, null);
82
    	assertEquals("4EA2CED10057872BE25371CFE638D3B096C58F2F", returnValue);
83
    }
84
85
    @Test
86
    public void testSha512Variable() throws Exception {
87
    	params.add(new CompoundVariable("nofile"));
88
    	params.add(new CompoundVariable("nofile"));
89
    	params.add(new CompoundVariable("true"));
90
    	params.add(new CompoundVariable("newVar"));
91
        sha512.setParameters(params);
92
        String returnValue = sha512.execute(result, null);
93
        assertEquals("0A76F7CD4F3CD9BEC27146C32A8CAD3DE865A48D07FF6A2A40F18F2B3307FEF4D0DE695D14C90234BD453B375AA2974BA17743564BC782AD1D6CF8B3F4DB72A4", returnValue);
94
    }
95
    
96
    @Test
97
    public void testSha512Error() throws Exception {
98
    	params.add(new CompoundVariable("nofile"));		
99
    	boolean isFailed = false;
100
    	try {
101
    		sha512.setParameters(params);
102
    		sha512.execute(result, null);
103
    	} catch (InvalidVariableException e) {
104
    		// expect failure
105
    		isFailed = true;
106
    	}
107
    	assertEquals(isFailed, true);
108
    }
109
    
110
    @Test
111
	public void testSha1Error() throws Exception {
112
		boolean isFailed = false;
113
		try {
114
			sha1.setParameters(params);
115
			sha1.execute(result, null);
116
		} catch (InvalidVariableException e) {
117
			// expect failure
118
			isFailed = true;
119
		}
120
		assertEquals(isFailed, true);
121
	}
122
}

Return to bug 61724