1 18 19 28 package org.apache.jmeter.assertions; 29 30 import java.io.Serializable ; 31 import java.security.MessageDigest ; 32 import java.security.NoSuchAlgorithmException ; 33 import java.text.MessageFormat ; 34 35 import junit.framework.TestCase; 36 37 import org.apache.jmeter.samplers.SampleResult; 38 import org.apache.jmeter.testelement.AbstractTestElement; 39 import org.apache.jmeter.testelement.property.StringProperty; 40 import org.apache.jmeter.util.JMeterUtils; 41 import org.apache.jorphan.logging.LoggingManager; 42 import org.apache.log.Logger; 43 44 public class MD5HexAssertion 45 extends AbstractTestElement 46 implements Serializable , Assertion { 47 48 private static final Logger log = LoggingManager.getLoggerForClass(); 49 50 51 private static final String MD5HEX_KEY = "MD5HexAssertion.size"; 52 53 57 public AssertionResult getResult(SampleResult response) { 58 59 AssertionResult result = new AssertionResult(); 60 result.setFailure(false); 61 byte[] resultData = response.getResponseData(); 62 63 if (resultData == null) { 64 result.setError(false); 65 result.setFailure(true); 66 result.setFailureMessage("Response was null"); 67 return result; 68 } 69 70 if (getAllowedMD5Hex().equals("")) { 72 result.setError(false); 73 result.setFailure(true); 74 result.setFailureMessage("MD5Hex to test against is empty"); 75 return result; 76 } 77 78 String md5Result=baMD5Hex(resultData); 79 80 82 if (!md5Result.equalsIgnoreCase(getAllowedMD5Hex())) { 83 result.setFailure(true); 84 85 Object [] arguments = { md5Result, getAllowedMD5Hex()}; 86 String message = 87 MessageFormat.format( 88 JMeterUtils.getResString("md5hex_assertion_failure"), 89 arguments); 90 result.setFailureMessage(message); 91 92 } 93 94 return result; 95 } 96 97 public void setAllowedMD5Hex(String hex) { 98 setProperty(new StringProperty(MD5HexAssertion.MD5HEX_KEY, hex)); 99 } 100 101 public String getAllowedMD5Hex() { 102 return getPropertyAsString(MD5HexAssertion.MD5HEX_KEY); 103 } 104 105 private static String baToHex(byte ba []) 106 { 107 StringBuffer sb = new StringBuffer (32); 108 for (int i = 0; i<ba.length; i++ ) 109 { 110 int j = ba[i]&0xff; 111 if (j < 16) sb.append("0"); 112 sb.append(Integer.toHexString(j)); 113 } 114 return sb.toString(); 115 } 116 117 private static String baMD5Hex(byte ba[]) 118 { 119 byte [] md5Result={}; 120 121 try 122 { 123 MessageDigest md; 124 md = MessageDigest.getInstance("MD5"); 125 md5Result = md.digest(ba); 126 } 127 catch (NoSuchAlgorithmException e) 128 { 129 log.error("",e); 130 } 131 return baToHex(md5Result); 132 } 133 134 public static class Test extends TestCase 135 { 136 public void testHex() throws Exception 137 { 138 assertEquals("00010203",baToHex(new byte[]{0,1,2,3})); 139 assertEquals("03020100",baToHex(new byte[]{3,2,1,0})); 140 assertEquals("0f807fff",baToHex(new byte[]{0xF,-128,127,-1})); 141 } 142 public void testMD5() throws Exception 143 { 144 assertEquals("D41D8CD98F00B204E9800998ECF8427E",baMD5Hex(new byte[]{}).toUpperCase()); 145 } 146 } 147 } 148 | Popular Tags |