Squish/Calculating Checksums: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Calculating checksums=
[[Category:Tools::Squish]]
 
= Calculating checksums =


When you are testing e.g. a file transfer client, and you want to make sure the file transferred is the same on both the sender and the receiver side, one easy way of doing this is using checksums. And luckily for us, Python comes with a hashlib module, with support for exactly that! Problem? The Python code in your test script is being executed in the context of the squishrunner, which is a problem if your application (and hence your files) are being executed on another machine, e.g. an embedded system.
When you are testing e.g. a file transfer client, and you want to make sure the file transferred is the same on both the sender and the receiver side, one easy way of doing this is using checksums. And luckily for us, Python comes with a hashlib module, with support for exactly that! Problem? The Python code in your test script is being executed in the context of the squishrunner, which is a problem if your application (and hence your files) are being executed on another machine, e.g. an embedded system.


But as mentioned in the article on [[:Category:Tools::Squish::Generating-test-data|generating test data]], Squish gives us access to most of the Qt <span class="caps">API</span> using the built-in Qt wrapper library. And whatever Qt code we put into our test script will be executed in the context of the <span class="caps">AUT</span>, even when the <span class="caps">AUT</span> is being executed on an embedded system. So combining the fact that the Qt code is being executed on the <span class="caps">AUT</span> side along with Python’s hashlib module, we can generate checksums!
But as mentioned in the article on [[:Category:Tools::Squish::Generating test data | generating test data]], Squish gives us access to most of the Qt API using the built-in Qt wrapper library. And whatever Qt code we put into our test script will be executed in the context of the AUT, even when the AUT is being executed on an embedded system. So combining the fact that the Qt code is being executed on the AUT side along with Python's hashlib module, we can generate checksums!
 
Usage:<br />
 
Another option would be using Squish’s testData.get() function, copying the test data from the remote system to our local system, and using Python’s hashlib module without combining it with the Qt <span class="caps">API</span>.


===Categories:===
<code>import hashlib<br />def calculateMd5(fileName):<br /> m = hashlib.md5()<br /> file = QFile&amp;amp;#40;&quot;s/%s&amp;quot; (currentApplicationContext(&amp;#41;.cwd, fileName))<br /> if file.open(QIODevice.ReadOnly):<br /> mb = 1024*1024<br /> while not file.atEnd():<br /> data = file.read(mb)<br /> m.update(str(data))<br /> file.close()<br /> return m.hexdigest()</code>


* [[:Category:Tools|Tools]]
Usage:<br /><code>sum = calculateMd5(&quot;test.txt&amp;quot;)<code>
** [[:Category:Tools::Squish|Squish]]

Revision as of 10:25, 24 February 2015


Calculating checksums

When you are testing e.g. a file transfer client, and you want to make sure the file transferred is the same on both the sender and the receiver side, one easy way of doing this is using checksums. And luckily for us, Python comes with a hashlib module, with support for exactly that! Problem? The Python code in your test script is being executed in the context of the squishrunner, which is a problem if your application (and hence your files) are being executed on another machine, e.g. an embedded system.

But as mentioned in the article on generating test data, Squish gives us access to most of the Qt API using the built-in Qt wrapper library. And whatever Qt code we put into our test script will be executed in the context of the AUT, even when the AUT is being executed on an embedded system. So combining the fact that the Qt code is being executed on the AUT side along with Python's hashlib module, we can generate checksums!

import hashlib<br />def calculateMd5(fileName):<br /> m = hashlib.md5()<br /> file = QFile&amp;amp;#40;&quot;s/%s&amp;quot; (currentApplicationContext(&amp;#41;.cwd, fileName))<br /> if file.open(QIODevice.ReadOnly):<br /> mb = 1024*1024<br /> while not file.atEnd():<br /> data = file.read(mb)<br /> m.update(str(data))<br /> file.close()<br /> return m.hexdigest()

Usage:
sum = calculateMd5("test.txt&quot;)