Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(9)

Side by Side Diff: Lib/test/test_socket.py

Issue 27744: Add AF_ALG (Linux Kernel crypto) to socket module
Patch Set: Created 3 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 import unittest 1 import unittest
2 from test import support 2 from test import support
3 3
4 import errno 4 import errno
5 import io 5 import io
6 import itertools 6 import itertools
7 import socket 7 import socket
8 import select 8 import select
9 import tempfile 9 import tempfile
10 import time 10 import time
11 import traceback 11 import traceback
12 import queue 12 import queue
13 import sys 13 import sys
14 import os 14 import os
15 import array 15 import array
16 import contextlib 16 import contextlib
17 from weakref import proxy 17 from weakref import proxy
18 import signal 18 import signal
19 import math 19 import math
20 import pickle 20 import pickle
21 import struct 21 import struct
22 import random 22 import random
23 import string 23 import string
24 from binascii import hexlify, unhexlify
24 try: 25 try:
25 import multiprocessing 26 import multiprocessing
26 except ImportError: 27 except ImportError:
27 multiprocessing = False 28 multiprocessing = False
28 try: 29 try:
29 import fcntl 30 import fcntl
30 except ImportError: 31 except ImportError:
31 fcntl = None 32 fcntl = None
32 33
33 HOST = support.HOST 34 HOST = support.HOST
(...skipping 5284 matching lines...) Expand 10 before | Expand all | Expand 10 after
5318 @unittest.skipUnless(thread, 'Threading required for this test.') 5319 @unittest.skipUnless(thread, 'Threading required for this test.')
5319 @unittest.skipUnless(hasattr(os, "sendfile"), 5320 @unittest.skipUnless(hasattr(os, "sendfile"),
5320 'os.sendfile() required for this test.') 5321 'os.sendfile() required for this test.')
5321 class SendfileUsingSendfileTest(SendfileUsingSendTest): 5322 class SendfileUsingSendfileTest(SendfileUsingSendTest):
5322 """ 5323 """
5323 Test the sendfile() implementation of socket.sendfile(). 5324 Test the sendfile() implementation of socket.sendfile().
5324 """ 5325 """
5325 def meth_from_sock(self, sock): 5326 def meth_from_sock(self, sock):
5326 return getattr(sock, "_sendfile_use_sendfile") 5327 return getattr(sock, "_sendfile_use_sendfile")
5327 5328
5329 @unittest.skipUnless(hasattr(socket, "AF_ALG"), 'AF_ALG required')
5330 class LinuxKernelCryptoAPI(unittest.TestCase):
5331 # tests for AF_ALG
5332 def create_alg(self, typ, name):
5333 sock = socket.socket(socket.AF_ALG, socket.SOCK_SEQPACKET, 0)
5334 sock.bind((typ, name))
5335 return sock
5336
5337 def test_sha256(self):
5338 expected = b"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20 015ad"
5339 with self.create_alg('hash', 'sha256') as algo:
5340 op, _ = algo.accept()
5341 try:
5342 op.send(b"abc")
haypo 2016/08/17 13:20:25 You may use sendall() to be consistent with the te
5343 self.assertEqual(hexlify(op.recv(512)), expected)
5344 finally:
5345 op.close()
haypo 2016/08/17 13:20:25 Use "with op:"
5346
5347 op, _ = algo.accept()
5348 try:
5349 op.send(b'a', socket.MSG_MORE)
5350 op.send(b'b', socket.MSG_MORE)
5351 op.send(b'c', socket.MSG_MORE)
5352 op.send(b'')
5353 self.assertEqual(hexlify(op.recv(512)), expected)
5354 finally:
5355 op.close()
5356
5357 def test_hmac_sha1(self):
5358 expected = b"effcdf6ae5eb2fa2d27416d5f184df9c259a7c79"
5359 with self.create_alg('hash', 'hmac(sha1)') as algo:
5360 algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, b"Jefe")
5361 op, _ = algo.accept()
5362 try:
5363 op.sendall(b"what do ya want for nothing?")
5364 self.assertEqual(hexlify(op.recv(512)), expected)
5365 finally:
5366 op.close()
5367
5368 def test_aes_cbc(self):
5369 key = unhexlify('06a9214036b8a15b512e03d534120006')
5370 iv = unhexlify('3dafba429d9eb430b422da802c9fac41')
5371 msg = b"Single block msg"
5372 ciphertext = unhexlify('e353779c1079aeb82708942dbe77181a')
5373 with self.create_alg('skcipher', 'cbc(aes)') as algo:
5374 algo.setsockopt(socket.SOL_ALG, socket.ALG_SET_KEY, key)
5375 op, _ = algo.accept()
5376 try:
5377 op.algset(op=socket.ALG_OP_ENCRYPT, iv=iv)
5378 op.sendall(msg)
5379 self.assertEqual(op.recv(16), ciphertext)
5380 finally:
5381 op.close()
5328 5382
5329 def test_main(): 5383 def test_main():
5330 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, 5384 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
5331 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeo utTest ] 5385 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeo utTest ]
5332 5386
5333 tests.extend([ 5387 tests.extend([
5334 NonBlockingTCPTests, 5388 NonBlockingTCPTests,
5335 FileObjectClassTestCase, 5389 FileObjectClassTestCase,
5336 UnbufferedFileObjectClassTestCase, 5390 UnbufferedFileObjectClassTestCase,
5337 LineBufferedFileObjectClassTestCase, 5391 LineBufferedFileObjectClassTestCase,
5338 SmallBufferedFileObjectClassTestCase, 5392 SmallBufferedFileObjectClassTestCase,
5339 UnicodeReadFileObjectClassTestCase, 5393 UnicodeReadFileObjectClassTestCase,
5340 UnicodeWriteFileObjectClassTestCase, 5394 UnicodeWriteFileObjectClassTestCase,
5341 UnicodeReadWriteFileObjectClassTestCase, 5395 UnicodeReadWriteFileObjectClassTestCase,
5342 NetworkConnectionNoServer, 5396 NetworkConnectionNoServer,
5343 NetworkConnectionAttributesTest, 5397 NetworkConnectionAttributesTest,
5344 NetworkConnectionBehaviourTest, 5398 NetworkConnectionBehaviourTest,
5345 ContextManagersTest, 5399 ContextManagersTest,
5346 InheritanceTest, 5400 InheritanceTest,
5347 NonblockConstantTest 5401 NonblockConstantTest
5348 ]) 5402 ])
5349 tests.append(BasicSocketPairTest) 5403 tests.append(BasicSocketPairTest)
5350 tests.append(TestUnixDomain) 5404 tests.append(TestUnixDomain)
5351 tests.append(TestLinuxAbstractNamespace) 5405 tests.append(TestLinuxAbstractNamespace)
5352 tests.extend([TIPCTest, TIPCThreadableTest]) 5406 tests.extend([TIPCTest, TIPCThreadableTest])
5353 tests.extend([BasicCANTest, CANTest]) 5407 tests.extend([BasicCANTest, CANTest])
5354 tests.extend([BasicRDSTest, RDSTest]) 5408 tests.extend([BasicRDSTest, RDSTest])
5409 tests.append(LinuxKernelCryptoAPI)
5355 tests.extend([ 5410 tests.extend([
5356 CmsgMacroTests, 5411 CmsgMacroTests,
5357 SendmsgUDPTest, 5412 SendmsgUDPTest,
5358 RecvmsgUDPTest, 5413 RecvmsgUDPTest,
5359 RecvmsgIntoUDPTest, 5414 RecvmsgIntoUDPTest,
5360 SendmsgUDP6Test, 5415 SendmsgUDP6Test,
5361 RecvmsgUDP6Test, 5416 RecvmsgUDP6Test,
5362 RecvmsgRFC3542AncillaryUDP6Test, 5417 RecvmsgRFC3542AncillaryUDP6Test,
5363 RecvmsgIntoRFC3542AncillaryUDP6Test, 5418 RecvmsgIntoRFC3542AncillaryUDP6Test,
5364 RecvmsgIntoUDP6Test, 5419 RecvmsgIntoUDP6Test,
5365 SendmsgTCPTest, 5420 SendmsgTCPTest,
5366 RecvmsgTCPTest, 5421 RecvmsgTCPTest,
5367 RecvmsgIntoTCPTest, 5422 RecvmsgIntoTCPTest,
5368 SendmsgSCTPStreamTest, 5423 SendmsgSCTPStreamTest,
5369 RecvmsgSCTPStreamTest, 5424 RecvmsgSCTPStreamTest,
5370 RecvmsgIntoSCTPStreamTest, 5425 RecvmsgIntoSCTPStreamTest,
5371 SendmsgUnixStreamTest, 5426 SendmsgUnixStreamTest,
5372 RecvmsgUnixStreamTest, 5427 RecvmsgUnixStreamTest,
5373 RecvmsgIntoUnixStreamTest, 5428 RecvmsgIntoUnixStreamTest,
5374 RecvmsgSCMRightsStreamTest, 5429 RecvmsgSCMRightsStreamTest,
5375 RecvmsgIntoSCMRightsStreamTest, 5430 RecvmsgIntoSCMRightsStreamTest,
5376 # These are slow when setitimer() is not available 5431 # These are slow when setitimer() is not available
5377 InterruptedRecvTimeoutTest, 5432 InterruptedRecvTimeoutTest,
5378 InterruptedSendTimeoutTest, 5433 InterruptedSendTimeoutTest,
5379 TestSocketSharing, 5434 TestSocketSharing,
5380 SendfileUsingSendTest, 5435 SendfileUsingSendTest,
5381 SendfileUsingSendfileTest, 5436 SendfileUsingSendfileTest,
5382 ]) 5437 ])
5383 5438
5439 tests = [LinuxKernelCryptoAPI]
haypo 2016/08/17 13:20:25 Don't do that :-) (You skipped all other tests, no
5384 thread_info = support.threading_setup() 5440 thread_info = support.threading_setup()
5385 support.run_unittest(*tests) 5441 support.run_unittest(*tests)
5386 support.threading_cleanup(*thread_info) 5442 support.threading_cleanup(*thread_info)
5387 5443
5388 if __name__ == "__main__": 5444 if __name__ == "__main__":
5389 test_main() 5445 test_main()
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+