001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /**
020: * MD5HexAssertion class creates an MD5 checksum from the response <br/>
021: * and matches it with the MD5 hex provided.
022: * The assertion will fail when the expected hex is different from the <br/>
023: * one calculated from the response OR when the expected hex is left empty.
024: *
025: * @author <a href="mailto:jh@domek.be">Jorg Heymans</a>
026: */package org.apache.jmeter.assertions;
027:
028: import java.io.Serializable;
029: import java.security.MessageDigest;
030: import java.security.NoSuchAlgorithmException;
031: import java.text.MessageFormat;
032:
033: import org.apache.jmeter.samplers.SampleResult;
034: import org.apache.jmeter.testelement.AbstractTestElement;
035: import org.apache.jmeter.testelement.property.StringProperty;
036: import org.apache.jmeter.util.JMeterUtils;
037: import org.apache.jorphan.logging.LoggingManager;
038: import org.apache.log.Logger;
039:
040: public class MD5HexAssertion extends AbstractTestElement implements
041: Serializable, Assertion {
042:
043: private static final Logger log = LoggingManager
044: .getLoggerForClass();
045:
046: /** Key for storing assertion-informations in the jmx-file. */
047: private static final String MD5HEX_KEY = "MD5HexAssertion.size";
048:
049: /*
050: * @param response @return
051: */
052: public AssertionResult getResult(SampleResult response) {
053:
054: AssertionResult result = new AssertionResult(getName());
055: result.setFailure(false);
056: byte[] resultData = response.getResponseData();
057:
058: if (resultData.length == 0) {
059: result.setError(false);
060: result.setFailure(true);
061: result.setFailureMessage("Response was null");
062: return result;
063: }
064:
065: // no point in checking if we don't have anything to compare against
066: if (getAllowedMD5Hex().equals("")) {
067: result.setError(false);
068: result.setFailure(true);
069: result.setFailureMessage("MD5Hex to test against is empty");
070: return result;
071: }
072:
073: String md5Result = baMD5Hex(resultData);
074:
075: // String md5Result = DigestUtils.md5Hex(resultData);
076:
077: if (!md5Result.equalsIgnoreCase(getAllowedMD5Hex())) {
078: result.setFailure(true);
079:
080: Object[] arguments = { md5Result, getAllowedMD5Hex() };
081: String message = MessageFormat.format(JMeterUtils
082: .getResString("md5hex_assertion_failure"),
083: arguments); // $NON-NLS-1$
084: result.setFailureMessage(message);
085:
086: }
087:
088: return result;
089: }
090:
091: public void setAllowedMD5Hex(String hex) {
092: setProperty(new StringProperty(MD5HexAssertion.MD5HEX_KEY, hex));
093: }
094:
095: public String getAllowedMD5Hex() {
096: return getPropertyAsString(MD5HexAssertion.MD5HEX_KEY);
097: }
098:
099: // package protected so can be accessed by test class
100: static String baToHex(byte ba[]) {
101: StringBuffer sb = new StringBuffer(32);
102: for (int i = 0; i < ba.length; i++) {
103: int j = ba[i] & 0xff;
104: if (j < 16)
105: sb.append("0");
106: sb.append(Integer.toHexString(j));
107: }
108: return sb.toString();
109: }
110:
111: // package protected so can be accessed by test class
112: static String baMD5Hex(byte ba[]) {
113: byte[] md5Result = {};
114:
115: try {
116: MessageDigest md;
117: md = MessageDigest.getInstance("MD5");
118: md5Result = md.digest(ba);
119: } catch (NoSuchAlgorithmException e) {
120: log.error("", e);
121: }
122: return baToHex(md5Result);
123: }
124: }
|