Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testing.py 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tools for automated testing of L{twisted.pair}-based applications.
  5. """
  6. import socket
  7. import struct
  8. from collections import deque
  9. from errno import EAGAIN, EBADF, EINTR, EINVAL, ENOBUFS, ENOSYS, EPERM, EWOULDBLOCK
  10. from functools import wraps
  11. from zope.interface import implementer
  12. from twisted.internet.protocol import DatagramProtocol
  13. from twisted.pair.ethernet import EthernetProtocol
  14. from twisted.pair.ip import IPProtocol
  15. from twisted.pair.rawudp import RawUDPProtocol
  16. from twisted.pair.tuntap import _IFNAMSIZ, _TUNSETIFF, TunnelFlags, _IInputOutputSystem
  17. from twisted.python.compat import nativeString
  18. # The number of bytes in the "protocol information" header that may be present
  19. # on datagrams read from a tunnel device. This is two bytes of flags followed
  20. # by two bytes of protocol identification. All this code does with this
  21. # information is use it to discard the header.
  22. _PI_SIZE = 4
  23. def _H(n):
  24. """
  25. Pack an integer into a network-order two-byte string.
  26. @param n: The integer to pack. Only values that fit into 16 bits are
  27. supported.
  28. @return: The packed representation of the integer.
  29. @rtype: L{bytes}
  30. """
  31. return struct.pack(">H", n)
  32. _IPv4 = 0x0800
  33. def _ethernet(src, dst, protocol, payload):
  34. """
  35. Construct an ethernet frame.
  36. @param src: The source ethernet address, encoded.
  37. @type src: L{bytes}
  38. @param dst: The destination ethernet address, encoded.
  39. @type dst: L{bytes}
  40. @param protocol: The protocol number of the payload of this datagram.
  41. @type protocol: L{int}
  42. @param payload: The content of the ethernet frame (such as an IP datagram).
  43. @type payload: L{bytes}
  44. @return: The full ethernet frame.
  45. @rtype: L{bytes}
  46. """
  47. return dst + src + _H(protocol) + payload
  48. def _ip(src, dst, payload):
  49. """
  50. Construct an IP datagram with the given source, destination, and
  51. application payload.
  52. @param src: The source IPv4 address as a dotted-quad string.
  53. @type src: L{bytes}
  54. @param dst: The destination IPv4 address as a dotted-quad string.
  55. @type dst: L{bytes}
  56. @param payload: The content of the IP datagram (such as a UDP datagram).
  57. @type payload: L{bytes}
  58. @return: An IP datagram header and payload.
  59. @rtype: L{bytes}
  60. """
  61. ipHeader = (
  62. # Version and header length, 4 bits each
  63. b"\x45"
  64. # Differentiated services field
  65. b"\x00"
  66. # Total length
  67. + _H(20 + len(payload))
  68. + b"\x00\x01\x00\x00\x40\x11"
  69. # Checksum
  70. + _H(0)
  71. # Source address
  72. + socket.inet_pton(socket.AF_INET, nativeString(src))
  73. # Destination address
  74. + socket.inet_pton(socket.AF_INET, nativeString(dst))
  75. )
  76. # Total all of the 16-bit integers in the header
  77. checksumStep1 = sum(struct.unpack("!10H", ipHeader))
  78. # Pull off the carry
  79. carry = checksumStep1 >> 16
  80. # And add it to what was left over
  81. checksumStep2 = (checksumStep1 & 0xFFFF) + carry
  82. # Compute the one's complement sum
  83. checksumStep3 = checksumStep2 ^ 0xFFFF
  84. # Reconstruct the IP header including the correct checksum so the platform
  85. # IP stack, if there is one involved in this test, doesn't drop it on the
  86. # floor as garbage.
  87. ipHeader = ipHeader[:10] + struct.pack("!H", checksumStep3) + ipHeader[12:]
  88. return ipHeader + payload
  89. def _udp(src, dst, payload):
  90. """
  91. Construct a UDP datagram with the given source, destination, and
  92. application payload.
  93. @param src: The source port number.
  94. @type src: L{int}
  95. @param dst: The destination port number.
  96. @type dst: L{int}
  97. @param payload: The content of the UDP datagram.
  98. @type payload: L{bytes}
  99. @return: A UDP datagram header and payload.
  100. @rtype: L{bytes}
  101. """
  102. udpHeader = (
  103. # Source port
  104. _H(src)
  105. # Destination port
  106. + _H(dst)
  107. # Length
  108. + _H(len(payload) + 8)
  109. # Checksum
  110. + _H(0)
  111. )
  112. return udpHeader + payload
  113. class Tunnel:
  114. """
  115. An in-memory implementation of a tun or tap device.
  116. @cvar _DEVICE_NAME: A string representing the conventional filesystem entry
  117. for the tunnel factory character special device.
  118. @type _DEVICE_NAME: C{bytes}
  119. """
  120. _DEVICE_NAME = b"/dev/net/tun"
  121. # Between POSIX and Python, there are 4 combinations. Here are two, at
  122. # least.
  123. EAGAIN_STYLE = IOError(EAGAIN, "Resource temporarily unavailable")
  124. EWOULDBLOCK_STYLE = OSError(EWOULDBLOCK, "Operation would block")
  125. # Oh yea, and then there's the case where maybe we would've read, but
  126. # someone sent us a signal instead.
  127. EINTR_STYLE = IOError(EINTR, "Interrupted function call")
  128. nonBlockingExceptionStyle = EAGAIN_STYLE
  129. SEND_BUFFER_SIZE = 1024
  130. def __init__(self, system, openFlags, fileMode):
  131. """
  132. @param system: An L{_IInputOutputSystem} provider to use to perform I/O.
  133. @param openFlags: Any flags to apply when opening the tunnel device.
  134. See C{os.O_*}.
  135. @type openFlags: L{int}
  136. @param fileMode: ignored
  137. """
  138. self.system = system
  139. # Drop fileMode on the floor - evidence and logic suggest it is
  140. # irrelevant with respect to /dev/net/tun
  141. self.openFlags = openFlags
  142. self.tunnelMode = None
  143. self.requestedName = None
  144. self.name = None
  145. self.readBuffer = deque()
  146. self.writeBuffer = deque()
  147. self.pendingSignals = deque()
  148. @property
  149. def blocking(self):
  150. """
  151. If the file descriptor for this tunnel is open in blocking mode,
  152. C{True}. C{False} otherwise.
  153. """
  154. return not (self.openFlags & self.system.O_NONBLOCK)
  155. @property
  156. def closeOnExec(self):
  157. """
  158. If the file descriptor for this tunnel is marked as close-on-exec,
  159. C{True}. C{False} otherwise.
  160. """
  161. return bool(self.openFlags & self.system.O_CLOEXEC)
  162. def addToReadBuffer(self, datagram):
  163. """
  164. Deliver a datagram to this tunnel's read buffer. This makes it
  165. available to be read later using the C{read} method.
  166. @param datagram: The IPv4 datagram to deliver. If the mode of this
  167. tunnel is TAP then ethernet framing will be added automatically.
  168. @type datagram: L{bytes}
  169. """
  170. # TAP devices also include ethernet framing.
  171. if self.tunnelMode & TunnelFlags.IFF_TAP.value:
  172. datagram = _ethernet(
  173. src=b"\x00" * 6, dst=b"\xff" * 6, protocol=_IPv4, payload=datagram
  174. )
  175. self.readBuffer.append(datagram)
  176. def read(self, limit):
  177. """
  178. Read a datagram out of this tunnel.
  179. @param limit: The maximum number of bytes from the datagram to return.
  180. If the next datagram is larger than this, extra bytes are dropped
  181. and lost forever.
  182. @type limit: L{int}
  183. @raise OSError: Any of the usual I/O problems can result in this
  184. exception being raised with some particular error number set.
  185. @raise IOError: Any of the usual I/O problems can result in this
  186. exception being raised with some particular error number set.
  187. @return: The datagram which was read from the tunnel. If the tunnel
  188. mode does not include L{TunnelFlags.IFF_NO_PI} then the datagram is
  189. prefixed with a 4 byte PI header.
  190. @rtype: L{bytes}
  191. """
  192. if self.readBuffer:
  193. if self.tunnelMode & TunnelFlags.IFF_NO_PI.value:
  194. header = b""
  195. else:
  196. # Synthesize a PI header to include in the result. Nothing in
  197. # twisted.pair uses the PI information yet so we can synthesize
  198. # something incredibly boring (ie 32 bits of 0).
  199. header = b"\x00" * _PI_SIZE
  200. limit -= 4
  201. return header + self.readBuffer.popleft()[:limit]
  202. elif self.blocking:
  203. raise NotImplementedError()
  204. else:
  205. raise self.nonBlockingExceptionStyle
  206. def write(self, datagram):
  207. """
  208. Write a datagram into this tunnel.
  209. @param datagram: The datagram to write.
  210. @type datagram: L{bytes}
  211. @raise IOError: Any of the usual I/O problems can result in this
  212. exception being raised with some particular error number set.
  213. @return: The number of bytes of the datagram which were written.
  214. @rtype: L{int}
  215. """
  216. if self.pendingSignals:
  217. self.pendingSignals.popleft()
  218. raise OSError(EINTR, "Interrupted system call")
  219. if len(datagram) > self.SEND_BUFFER_SIZE:
  220. raise OSError(ENOBUFS, "No buffer space available")
  221. self.writeBuffer.append(datagram)
  222. return len(datagram)
  223. def _privileged(original):
  224. """
  225. Wrap a L{MemoryIOSystem} method with permission-checking logic. The
  226. returned function will check C{self.permissions} and raise L{IOError} with
  227. L{errno.EPERM} if the function name is not listed as an available
  228. permission.
  229. @param original: The L{MemoryIOSystem} instance to wrap.
  230. @return: A wrapper around C{original} that applies permission checks.
  231. """
  232. @wraps(original)
  233. def permissionChecker(self, *args, **kwargs):
  234. if original.__name__ not in self.permissions:
  235. raise OSError(EPERM, "Operation not permitted")
  236. return original(self, *args, **kwargs)
  237. return permissionChecker
  238. @implementer(_IInputOutputSystem)
  239. class MemoryIOSystem:
  240. """
  241. An in-memory implementation of basic I/O primitives, useful in the context
  242. of unit testing as a drop-in replacement for parts of the C{os} module.
  243. @ivar _devices:
  244. @ivar _openFiles:
  245. @ivar permissions:
  246. @ivar _counter:
  247. """
  248. _counter = 8192
  249. O_RDWR = 1 << 0
  250. O_NONBLOCK = 1 << 1
  251. O_CLOEXEC = 1 << 2
  252. def __init__(self):
  253. self._devices = {}
  254. self._openFiles = {}
  255. self.permissions = {"open", "ioctl"}
  256. def getTunnel(self, port):
  257. """
  258. Get the L{Tunnel} object associated with the given L{TuntapPort}.
  259. @param port: A L{TuntapPort} previously initialized using this
  260. L{MemoryIOSystem}.
  261. @return: The tunnel object created by a prior use of C{open} on this
  262. object on the tunnel special device file.
  263. @rtype: L{Tunnel}
  264. """
  265. return self._openFiles[port.fileno()]
  266. def registerSpecialDevice(self, name, cls):
  267. """
  268. Specify a class which will be used to handle I/O to a device of a
  269. particular name.
  270. @param name: The filesystem path name of the device.
  271. @type name: L{bytes}
  272. @param cls: A class (like L{Tunnel}) to instantiated whenever this
  273. device is opened.
  274. """
  275. self._devices[name] = cls
  276. @_privileged
  277. def open(self, name, flags, mode=None):
  278. """
  279. A replacement for C{os.open}. This initializes state in this
  280. L{MemoryIOSystem} which will be reflected in the behavior of the other
  281. file descriptor-related methods (eg L{MemoryIOSystem.read},
  282. L{MemoryIOSystem.write}, etc).
  283. @param name: A string giving the name of the file to open.
  284. @type name: C{bytes}
  285. @param flags: The flags with which to open the file.
  286. @type flags: C{int}
  287. @param mode: The mode with which to open the file.
  288. @type mode: C{int}
  289. @raise OSError: With C{ENOSYS} if the file is not a recognized special
  290. device file.
  291. @return: A file descriptor associated with the newly opened file
  292. description.
  293. @rtype: L{int}
  294. """
  295. if name in self._devices:
  296. fd = self._counter
  297. self._counter += 1
  298. self._openFiles[fd] = self._devices[name](self, flags, mode)
  299. return fd
  300. raise OSError(ENOSYS, "Function not implemented")
  301. def read(self, fd, limit):
  302. """
  303. Try to read some bytes out of one of the in-memory buffers which may
  304. previously have been populated by C{write}.
  305. @see: L{os.read}
  306. """
  307. try:
  308. return self._openFiles[fd].read(limit)
  309. except KeyError:
  310. raise OSError(EBADF, "Bad file descriptor")
  311. def write(self, fd, data):
  312. """
  313. Try to add some bytes to one of the in-memory buffers to be accessed by
  314. a later C{read} call.
  315. @see: L{os.write}
  316. """
  317. try:
  318. return self._openFiles[fd].write(data)
  319. except KeyError:
  320. raise OSError(EBADF, "Bad file descriptor")
  321. def close(self, fd):
  322. """
  323. Discard the in-memory buffer and other in-memory state for the given
  324. file descriptor.
  325. @see: L{os.close}
  326. """
  327. try:
  328. del self._openFiles[fd]
  329. except KeyError:
  330. raise OSError(EBADF, "Bad file descriptor")
  331. @_privileged
  332. def ioctl(self, fd, request, args):
  333. """
  334. Perform some configuration change to the in-memory state for the given
  335. file descriptor.
  336. @see: L{fcntl.ioctl}
  337. """
  338. try:
  339. tunnel = self._openFiles[fd]
  340. except KeyError:
  341. raise OSError(EBADF, "Bad file descriptor")
  342. if request != _TUNSETIFF:
  343. raise OSError(EINVAL, "Request or args is not valid.")
  344. name, mode = struct.unpack("%dsH" % (_IFNAMSIZ,), args)
  345. tunnel.tunnelMode = mode
  346. tunnel.requestedName = name
  347. tunnel.name = name[: _IFNAMSIZ - 3] + b"123"
  348. return struct.pack("%dsH" % (_IFNAMSIZ,), tunnel.name, mode)
  349. def sendUDP(self, datagram, address):
  350. """
  351. Write an ethernet frame containing an ip datagram containing a udp
  352. datagram containing the given payload, addressed to the given address,
  353. to a tunnel device previously opened on this I/O system.
  354. @param datagram: A UDP datagram payload to send.
  355. @type datagram: L{bytes}
  356. @param address: The destination to which to send the datagram.
  357. @type address: L{tuple} of (L{bytes}, L{int})
  358. @return: A two-tuple giving the address from which gives the address
  359. from which the datagram was sent.
  360. @rtype: L{tuple} of (L{bytes}, L{int})
  361. """
  362. # Just make up some random thing
  363. srcIP = "10.1.2.3"
  364. srcPort = 21345
  365. serialized = _ip(
  366. src=srcIP,
  367. dst=address[0],
  368. payload=_udp(src=srcPort, dst=address[1], payload=datagram),
  369. )
  370. openFiles = list(self._openFiles.values())
  371. openFiles[0].addToReadBuffer(serialized)
  372. return (srcIP, srcPort)
  373. def receiveUDP(self, fileno, host, port):
  374. """
  375. Get a socket-like object which can be used to receive a datagram sent
  376. from the given address.
  377. @param fileno: A file descriptor representing a tunnel device which the
  378. datagram will be received via.
  379. @type fileno: L{int}
  380. @param host: The IPv4 address to which the datagram was sent.
  381. @type host: L{bytes}
  382. @param port: The UDP port number to which the datagram was sent.
  383. received.
  384. @type port: L{int}
  385. @return: A L{socket.socket}-like object which can be used to receive
  386. the specified datagram.
  387. """
  388. return _FakePort(self, fileno)
  389. class _FakePort:
  390. """
  391. A socket-like object which can be used to read UDP datagrams from
  392. tunnel-like file descriptors managed by a L{MemoryIOSystem}.
  393. """
  394. def __init__(self, system, fileno):
  395. self._system = system
  396. self._fileno = fileno
  397. def recv(self, nbytes):
  398. """
  399. Receive a datagram sent to this port using the L{MemoryIOSystem} which
  400. created this object.
  401. This behaves like L{socket.socket.recv} but the data being I{sent} and
  402. I{received} only passes through various memory buffers managed by this
  403. object and L{MemoryIOSystem}.
  404. @see: L{socket.socket.recv}
  405. """
  406. data = self._system._openFiles[self._fileno].writeBuffer.popleft()
  407. datagrams = []
  408. receiver = DatagramProtocol()
  409. def capture(datagram, address):
  410. datagrams.append(datagram)
  411. receiver.datagramReceived = capture
  412. udp = RawUDPProtocol()
  413. udp.addProto(12345, receiver)
  414. ip = IPProtocol()
  415. ip.addProto(17, udp)
  416. mode = self._system._openFiles[self._fileno].tunnelMode
  417. if mode & TunnelFlags.IFF_TAP.value:
  418. ether = EthernetProtocol()
  419. ether.addProto(0x800, ip)
  420. datagramReceived = ether.datagramReceived
  421. else:
  422. datagramReceived = lambda data: ip.datagramReceived(
  423. data, None, None, None, None
  424. )
  425. dataHasPI = not (mode & TunnelFlags.IFF_NO_PI.value)
  426. if dataHasPI:
  427. # datagramReceived can't handle the PI, get rid of it.
  428. data = data[_PI_SIZE:]
  429. datagramReceived(data)
  430. return datagrams[0][:nbytes]