0
votes

Every time I need to achieve something in Jmeter, I ask a question in SO... To make a story short: my goal is to read Base64 encoded values from csv file line by line and save them into separate variables. According to this page (in order to decode them), there are functions ${__base64Encode} and ${__base64Decode}. I installed Custom Jmeter Functions Plugin and restarted the Jmeter. Another referral to this answer, there is an advisory to use JSR223 PostProcessor / Sampler, verbatim:

it is recommended to use Groovy for any form of scripting to consider migrating to JSR223 PostProcessor

so I decided to give it a try. Below is my script to read csv files:

def csvFileLocation = new File("C://JohnDoe//MyWork//sensitive_data.csv")
def lines = csvFileLocation.readLines()
lines.eachWithIndex {line, idx ->
                     vars.put("Id_value_" + idx, line)
}

${__base64Decode(${Id_value_0}, first_variable)}
${__base64Decode(${Id_value_1}, second_variable)}
${__base64Decode(${Id_value_2}, third_variable)}

Then I saw something interesting:

2018-01-01 12:30:60,767 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script10.groovy: 8: unexpected token: ! @ line 8, column 1. !ßïj[žÿ

I simply decided to test it on https://www.base64encode.org/ with a string this is the test of base64 encoding

enter image description here


However, Jmeter _base64Encode function produces completely different result:

vars.put("jmeter_variable", "this is the test of base64 encoding");

${__base64Encode(${jmeter_variable}, my_variable)}
log.info(${my_variable});

OUTPUT:

JHtqbWV0ZXJfdmFyaWFibGV9

enter image description here

My question: what type of encoding algorithm Jmeter uses? Should I be able to save already encoded values into the csv file and retrieve them to the original value with the help of __base64Decode function? Thanks for the help...

1
this really is the same question as linked "possible duplicate". Anything ${} is evaluated before script runs. So if you want to base64Encode variables you just created in the script, you cannot do it using ${} notation. But you don't need to either: you are in Java land, and all you need there to do the same thing that base64Encode is doing, is call Base64.getEncoder().encodeToString(...Kiril S.

1 Answers

0
votes

You need to replace these lines:

${__base64Decode(${Id_value_0}, first_variable)}
${__base64Decode(${Id_value_1}, second_variable)}
${__base64Decode(${Id_value_2}, third_variable)}

With these ones:

vars.put('first_variable', vars.get('Id_value_0').decodeBase64() as String())
vars.put('second_variable', vars.get('Id_value_2').decodeBase64() as String())
vars.put('third_variable', vars.get('Id_value_2').decodeBase64() as String())

vars is a shorthand for JMeterVariables class instance, it provides read/write access to JMeter Variables in current test element's scope.

As per JSR223 Sampler documentation you should avoid inlining JMeter Functions and/or variables into Groovy scripts as they may resolve into something which can cause your script compilation failure (like in your case) or unexpected behaviour. Moreover it does not align with Groovy's compilation caching feature therefore performance of your Groovy script even if everything will be fine from syntax perspective will be a big question mark.

So the most "natural" way from Groovy perspective will be using String.decodeBase64() function like demonstrated above. See The Groovy Templates Cheat Sheet for JMeter article to learn what else could be done with Groovy and how.