From d86680b07320a76ac81cc628783ed7e5bef9656d Mon Sep 17 00:00:00 2001 From: Enji Cooper Date: Sun, 24 Sep 2017 00:14:48 +0000 Subject: [PATCH] Convert some idioms over to py3k-compatible idioms - Import print_function from __future__ and use print(..) instead of `print ..`. - Use repr instead of backticks when the object needs to be dumped, unless print(..) can do it lazily. Use str instead of backticks as appropriate for simplification reasons. This doesn't fully convert these modules over py3k. It just gets over some of the trivial compatibility hurdles. --- tests/sys/opencrypto/cryptodev.py | 84 +++++++++++++++--------------- tests/sys/opencrypto/cryptotest.py | 27 +++++----- 2 files changed, 57 insertions(+), 54 deletions(-) diff --git a/tests/sys/opencrypto/cryptodev.py b/tests/sys/opencrypto/cryptodev.py index afe075d7de8..3e0ae0120b0 100644 --- a/tests/sys/opencrypto/cryptodev.py +++ b/tests/sys/opencrypto/cryptodev.py @@ -30,6 +30,7 @@ # $FreeBSD$ # +from __future__ import print_function import array import dpkt from fcntl import ioctl @@ -174,9 +175,9 @@ class Crypto: if not cipher and not mac: raise ValueError('one of cipher or mac MUST be specified.') ses.crid = crid - #print `ses` + #print(ses) s = array.array('B', ses.pack_hdr()) - #print `s` + #print(s) ioctl(_cryptodev, CIOCGSESSION2, s, 1) ses.unpack(s) @@ -206,7 +207,7 @@ class Crypto: ivbuf = array.array('B', iv) cop.iv = ivbuf.buffer_info()[0] - #print 'cop:', `cop` + #print('cop:', cop) ioctl(_cryptodev, CIOCCRYPT, str(cop)) s = s.tostring() @@ -234,7 +235,8 @@ class Crypto: if tag is None: tag = array.array('B', [0] * self._maclen) else: - assert len(tag) == self._maclen, `len(tag), self._maclen` + assert len(tag) == self._maclen, \ + '%d != %d' % (len(tag), self._maclen) tag = array.array('B', tag) caead.tag = tag.buffer_info()[0] @@ -288,8 +290,8 @@ class Crypto: signal.signal(signal.SIGALRM, oldalarm) - print 'time:', end - start - print 'perf MB/sec:', (reps * size) / (end - start) / 1024 / 1024 + print('time:', end - start) + print('perf MB/sec:', (reps * size) / (end - start) / 1024 / 1024) def encrypt(self, data, iv, aad=None): if aad is None: @@ -332,7 +334,7 @@ class KATParser: if i[0] == '[': yield i[1:].split(']', 1)[0], self.fielditer() else: - raise ValueError('unknown line: %s' % `i`) + raise ValueError('unknown line: %r' % repr(i)) def eatblanks(self): while True: @@ -362,12 +364,12 @@ class KATParser: if line == 'FAIL': f, v = 'FAIL', '' else: - print 'line:', `line` + print('line:', repr(line)) raise v = v.strip() if f in values: - raise ValueError('already present: %s' % `f`) + raise ValueError('already present: %r' % repr(f)) values[f] = v line = self.fp.readline().strip() if not line: @@ -377,7 +379,7 @@ class KATParser: remain = self.fields.copy() - set(values.keys()) # XXX - special case GCM decrypt if remain and not ('FAIL' in values and 'PT' in remain): - raise ValueError('not all fields found: %s' % `remain`) + raise ValueError('not all fields found: %r' % repr(remain)) yield values @@ -388,22 +390,22 @@ if __name__ == '__main__': if True: try: crid = Crypto.findcrid('aesni0') - print 'aesni:', crid + print('aesni:', crid) except IOError: - print 'aesni0 not found' + print('aesni0 not found') for i in xrange(10): try: name = Crypto.getcridname(i) - print '%2d: %s' % (i, `name`) + print('%2d: %r' % (i, repr(name))) except IOError: pass elif False: kp = KATParser('/usr/home/jmg/aesni.testing/format tweak value input - data unit seq no/XTSGenAES128.rsp', [ 'COUNT', 'DataUnitLen', 'Key', 'DataUnitSeqNumber', 'PT', 'CT' ]) for mode, ni in kp: - print `i`, `ni` + print(i, ni) for j in ni: - print `j` + print(j) elif False: key = _spdechex('c939cc13397c1d37de6ae0e1cb7c423c') iv = _spdechex('00000000000000000000000000000001') @@ -414,15 +416,15 @@ if __name__ == '__main__': c = Crypto(CRYPTO_AES_ICM, key) enc = c.encrypt(pt, iv) - print 'enc:', enc.encode('hex') - print ' ct:', ct.encode('hex') + print('enc:', enc.encode('hex')) + print(' ct:', ct.encode('hex')) assert ct == enc dec = c.decrypt(ct, iv) - print 'dec:', dec.encode('hex') - print ' pt:', pt.encode('hex') + print('dec:', dec.encode('hex')) + print(' pt:', pt.encode('hex')) assert pt == dec elif False: @@ -435,15 +437,15 @@ if __name__ == '__main__': c = Crypto(CRYPTO_AES_ICM, key) enc = c.encrypt(pt, iv) - print 'enc:', enc.encode('hex') - print ' ct:', ct.encode('hex') + print('enc:', enc.encode('hex')) + print(' ct:', ct.encode('hex')) assert ct == enc dec = c.decrypt(ct, iv) - print 'dec:', dec.encode('hex') - print ' pt:', pt.encode('hex') + print('dec:', dec.encode('hex')) + print(' pt:', pt.encode('hex')) assert pt == dec elif False: @@ -455,15 +457,15 @@ if __name__ == '__main__': enc = c.encrypt(pt, iv) - print 'enc:', enc.encode('hex') - print ' ct:', ct.encode('hex') + print('enc:', enc.encode('hex')) + print(' ct:', ct.encode('hex')) assert ct == enc dec = c.decrypt(ct, iv) - print 'dec:', dec.encode('hex') - print ' pt:', pt.encode('hex') + print('dec:', dec.encode('hex')) + print(' pt:', pt.encode('hex')) assert pt == dec elif False: @@ -481,26 +483,26 @@ if __name__ == '__main__': enc, enctag = c.encrypt(pt, iv, aad=aad) - print 'enc:', enc.encode('hex') - print ' ct:', ct.encode('hex') + print('enc:', enc.encode('hex')) + print(' ct:', ct.encode('hex')) assert enc == ct - print 'etg:', enctag.encode('hex') - print 'tag:', tag.encode('hex') + print('etg:', enctag.encode('hex')) + print('tag:', tag.encode('hex')) assert enctag == tag # Make sure we get EBADMSG #enctag = enctag[:-1] + 'a' dec, dectag = c.decrypt(ct, iv, aad=aad, tag=enctag) - print 'dec:', dec.encode('hex') - print ' pt:', pt.encode('hex') + print('dec:', dec.encode('hex')) + print(' pt:', pt.encode('hex')) assert dec == pt - print 'dtg:', dectag.encode('hex') - print 'tag:', tag.encode('hex') + print('dtg:', dectag.encode('hex')) + print('tag:', tag.encode('hex')) assert dectag == tag elif False: @@ -517,13 +519,13 @@ if __name__ == '__main__': enc, enctag = c.encrypt(pt, iv, aad=aad) - print 'enc:', enc.encode('hex') - print ' ct:', ct.encode('hex') + print('enc:', enc.encode('hex')) + print(' ct:', ct.encode('hex')) assert enc == ct - print 'etg:', enctag.encode('hex') - print 'tag:', tag.encode('hex') + print('etg:', enctag.encode('hex')) + print('tag:', tag.encode('hex')) assert enctag == tag elif False: for i in xrange(100000): @@ -550,9 +552,9 @@ if __name__ == '__main__': else: key = '1bbfeadf539daedcae33ced497343f3ca1f2474ad932b903997d44707db41382'.decode('hex') - print 'XTS %d testing:' % (len(key) * 8) + print('XTS %d testing:' % (len(key) * 8)) c = Crypto(CRYPTO_AES_XTS, key) for i in [ 8192, 192*1024]: - print 'block size: %d' % i + print('block size: %d' % i) c.perftest(COP_ENCRYPT, i) c.perftest(COP_DECRYPT, i) diff --git a/tests/sys/opencrypto/cryptotest.py b/tests/sys/opencrypto/cryptotest.py index e82c2c88764..5634a4d6eeb 100644 --- a/tests/sys/opencrypto/cryptotest.py +++ b/tests/sys/opencrypto/cryptotest.py @@ -29,6 +29,7 @@ # $FreeBSD$ # +from __future__ import print_function import cryptodev import itertools import os @@ -57,17 +58,17 @@ def GenTestCase(cname): ############### ##### AES ##### ############### - @unittest.skipIf(cname not in aesmodules, 'skipping AES on %s' % `cname`) + @unittest.skipIf(cname not in aesmodules, 'skipping AES on %s' % (cname)) def test_xts(self): for i in katg('XTSTestVectors/format tweak value input - data unit seq no', '*.rsp'): self.runXTS(i, cryptodev.CRYPTO_AES_XTS) - @unittest.skipIf(cname not in aesmodules, 'skipping AES on %s' % `cname`) + @unittest.skipIf(cname not in aesmodules, 'skipping AES on %s' % (cname)) def test_cbc(self): for i in katg('KAT_AES', 'CBC[GKV]*.rsp'): self.runCBC(i) - @unittest.skipIf(cname not in aesmodules, 'skipping AES on %s' % `cname`) + @unittest.skipIf(cname not in aesmodules, 'skipping AES on %s' % (cname)) def test_gcm(self): for i in katg('gcmtestvectors', 'gcmEncrypt*'): self.runGCM(i, 'ENCRYPT') @@ -88,7 +89,7 @@ def GenTestCase(cname): swapptct = True curfun = Crypto.decrypt else: - raise RuntimeError('unknown mode: %s' % `mode`) + raise RuntimeError('unknown mode: %r' % repr(mode)) for bogusmode, lines in cryptodev.KATParser(fname, [ 'Count', 'Key', 'IV', 'CT', 'AAD', 'Tag', 'PT', ]): @@ -116,8 +117,8 @@ def GenTestCase(cname): rtag = rtag[:len(tag)] data['rct'] = rct.encode('hex') data['rtag'] = rtag.encode('hex') - self.assertEqual(rct, ct, `data`) - self.assertEqual(rtag, tag, `data`) + self.assertEqual(rct, ct, repr(data)) + self.assertEqual(rtag, tag, repr(data)) else: if len(tag) != 16: continue @@ -130,7 +131,7 @@ def GenTestCase(cname): data['rpt'] = rpt.encode('hex') data['rtag'] = rtag.encode('hex') self.assertEqual(rpt, pt, - `data`) + repr(data)) def runCBC(self, fname): curfun = None @@ -143,7 +144,7 @@ def GenTestCase(cname): swapptct = True curfun = Crypto.decrypt else: - raise RuntimeError('unknown mode: %s' % `mode`) + raise RuntimeError('unknown mode: %r' % repr(mode)) for data in lines: curcnt = int(data['COUNT']) @@ -171,7 +172,7 @@ def GenTestCase(cname): swapptct = True curfun = Crypto.decrypt else: - raise RuntimeError('unknown mode: %s' % `mode`) + raise RuntimeError('unknown mode: %r' % repr(mode)) for data in lines: curcnt = int(data['COUNT']) @@ -194,7 +195,7 @@ def GenTestCase(cname): ############### ##### DES ##### ############### - @unittest.skipIf(cname not in desmodules, 'skipping DES on %s' % `cname`) + @unittest.skipIf(cname not in desmodules, 'skipping DES on %s' % (cname)) def test_tdes(self): for i in katg('KAT_TDES', 'TCBC[a-z]*.rsp'): self.runTDES(i) @@ -210,7 +211,7 @@ def GenTestCase(cname): swapptct = True curfun = Crypto.decrypt else: - raise RuntimeError('unknown mode: %s' % `mode`) + raise RuntimeError('unknown mode: %r' % repr(mode)) for data in lines: curcnt = int(data['COUNT']) @@ -230,14 +231,14 @@ def GenTestCase(cname): ############### ##### SHA ##### ############### - @unittest.skipIf(cname not in shamodules, 'skipping SHA on %s' % `cname`) + @unittest.skipIf(cname not in shamodules, 'skipping SHA on %s' % str(cname)) def test_sha(self): # SHA not available in software pass #for i in iglob('SHA1*'): # self.runSHA(i) - @unittest.skipIf(cname not in shamodules, 'skipping SHA on %s' % `cname`) + @unittest.skipIf(cname not in shamodules, 'skipping SHA on %s' % str(cname)) def test_sha1hmac(self): for i in katg('hmactestvectors', 'HMAC.rsp'): self.runSHA1HMAC(i)