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

(-)src/org/apache/jmeter/functions/TestDigestFunction.java (+124 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 Digest function
18
 * 
19
 * Add Tests with Sha1 and Sha512
20
 * 
21
 * @author orim
22
 *
23
 */
24
public class TestDigestFunction extends JMeterTestCase {
25
	protected AbstractFunction digest;	
26
27
    private SampleResult result;
28
29
    private Collection<CompoundVariable> params;
30
31
    private JMeterVariables vars;
32
33
    private JMeterContext jmctx;
34
35
    @Before
36
    public void setUp() {
37
    	digest = new DigestEncode();
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(digest, 2, 5);
52
    }   
53
    
54
    @Test
55
    public void testSha512() throws Exception {
56
    	params.add(new CompoundVariable("SHA-512"));
57
    	params.add(new CompoundVariable("nofile"));
58
    	params.add(new CompoundVariable("nofile"));
59
    	digest.setParameters(params);
60
    	String returnValue = digest.execute(result, null);
61
    	assertEquals("0a76f7cd4f3cd9bec27146c32a8cad3de865a48d07ff6a2a40f18f2b3307fef4d0de695d14c90234bd453b375aa2974ba17743564bc782ad1d6cf8b3f4db72a4", returnValue);
62
    }      
63
    
64
    @Test
65
    public void testSha1() throws Exception {
66
    	params.add(new CompoundVariable("SHA-1"));    	
67
    	params.add(new CompoundVariable("nofile"));    	
68
    	digest.setParameters(params);
69
    	String returnValue = digest.execute(result, null);
70
    	assertEquals("4ea2ced10057872be25371cfe638d3b096c58f2f", returnValue);
71
    }
72
    
73
    @Test
74
    public void testSha1Variable() throws Exception {
75
    	params.add(new CompoundVariable("SHA-1"));    	
76
    	params.add(new CompoundVariable("nofile"));  
77
    	params.add(new CompoundVariable(""));  
78
    	params.add(new CompoundVariable("true"));
79
    	params.add(new CompoundVariable("newVar"));  	
80
    	digest.setParameters(params);
81
    	String returnValue = digest.execute(result, null);
82
    	assertEquals("4EA2CED10057872BE25371CFE638D3B096C58F2F", returnValue);
83
    }
84
85
    @Test
86
    public void testSha512Variable() throws Exception {    	
87
    	params.add(new CompoundVariable("SHA-512"));
88
    	params.add(new CompoundVariable("nofile"));  
89
    	params.add(new CompoundVariable(""));  
90
    	params.add(new CompoundVariable("true"));
91
    	params.add(new CompoundVariable("newVar"));
92
        digest.setParameters(params);
93
        String returnValue = digest.execute(result, null);
94
        assertEquals("58DA94D45A97B35B31D7F76D2EBAC184BC4BDA512B966CDBE43FDE1CAE1CFAF89617082CA89928FB5DC1C75D60B93ADB5631F518F970CA6DCC196E1AFC678B8C", returnValue);
95
    }
96
    
97
    @Test
98
    public void testSha512Error() throws Exception {
99
    	params.add(new CompoundVariable("nofile"));		
100
    	boolean isFailed = false;
101
    	try {
102
    		digest.setParameters(params);
103
    		digest.execute(result, null);
104
    	} catch (InvalidVariableException e) {
105
    		// expect failure
106
    		isFailed = true;
107
    	}
108
    	assertEquals(isFailed, true);
109
    }
110
    
111
    @Test
112
	public void testSha1Error() throws Exception {
113
		boolean isFailed = false;
114
		try {
115
	    	params.add(new CompoundVariable("SHA-1"));    	
116
			digest.setParameters(params);
117
			digest.execute(result, null);
118
		} catch (InvalidVariableException e) {
119
			// expect failure
120
			isFailed = true;
121
		}
122
		assertEquals(isFailed, true);
123
	}
124
}

Return to bug 61724