Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 24 Sep 2017 00:07:18 +0000 (UTC)
From:      Ngie Cooper <ngie@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r323959 - projects/runtime-coverage/tests/sys/opencrypto
Message-ID:  <201709240007.v8O07I5r023458@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ngie
Date: Sun Sep 24 00:07:18 2017
New Revision: 323959
URL: https://svnweb.freebsd.org/changeset/base/323959

Log:
  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.

Modified:
  projects/runtime-coverage/tests/sys/opencrypto/cryptodev.py
  projects/runtime-coverage/tests/sys/opencrypto/cryptotest.py

Modified: projects/runtime-coverage/tests/sys/opencrypto/cryptodev.py
==============================================================================
--- projects/runtime-coverage/tests/sys/opencrypto/cryptodev.py	Sat Sep 23 19:49:12 2017	(r323958)
+++ projects/runtime-coverage/tests/sys/opencrypto/cryptodev.py	Sun Sep 24 00:07:18 2017	(r323959)
@@ -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)

Modified: projects/runtime-coverage/tests/sys/opencrypto/cryptotest.py
==============================================================================
--- projects/runtime-coverage/tests/sys/opencrypto/cryptotest.py	Sat Sep 23 19:49:12 2017	(r323958)
+++ projects/runtime-coverage/tests/sys/opencrypto/cryptotest.py	Sun Sep 24 00:07:18 2017	(r323959)
@@ -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)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201709240007.v8O07I5r023458>