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 |
} |