Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Squish/Calculating Checksums: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
m (AutoSpider moved page Category:Tools::Squish::Calculating-checksums to Squish/Calculating Checksums: Title name format)
m (style)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
[[Category:Tools::Squish]]
[[Category:Squish]]


= Calculating checksums =
= Calculating checksums =
Line 7: Line 7:
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!
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!


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


Usage:
Usage:
<code>sum = calculateMd5("test.txt")<code>
<code>sum = calculateMd5("test.txt")</code>

Latest revision as of 17:28, 12 August 2020


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
def calculateMd5(fileName):
  m = hashlib.md5()
  file = QFile("%s/%s" % (currentApplicationContext().cwd, fileName))
  if file.open(QIODevice.ReadOnly):
    mb = 1024*1024
    while not file.atEnd():
      data = file.read(mb)
      m.update(str(data))
    file.close()
  return m.hexdigest()

Usage:

sum = calculateMd5("test.txt")