Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/dns/rdtypes/ANY/OPT.py: 62%

34 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-02 06:07 +0000

1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license 

2 

3# Copyright (C) 2001-2017 Nominum, Inc. 

4# 

5# Permission to use, copy, modify, and distribute this software and its 

6# documentation for any purpose with or without fee is hereby granted, 

7# provided that the above copyright notice and this permission notice 

8# appear in all copies. 

9# 

10# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 

11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 

12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 

13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 

14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 

15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 

16# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

17 

18import struct 

19 

20import dns.edns 

21import dns.exception 

22import dns.immutable 

23import dns.rdata 

24 

25# We don't implement from_text, and that's ok. 

26# pylint: disable=abstract-method 

27 

28 

29@dns.immutable.immutable 

30class OPT(dns.rdata.Rdata): 

31 

32 """OPT record""" 

33 

34 __slots__ = ["options"] 

35 

36 def __init__(self, rdclass, rdtype, options): 

37 """Initialize an OPT rdata. 

38 

39 *rdclass*, an ``int`` is the rdataclass of the Rdata, 

40 which is also the payload size. 

41 

42 *rdtype*, an ``int`` is the rdatatype of the Rdata. 

43 

44 *options*, a tuple of ``bytes`` 

45 """ 

46 

47 super().__init__(rdclass, rdtype) 

48 

49 def as_option(option): 

50 if not isinstance(option, dns.edns.Option): 

51 raise ValueError("option is not a dns.edns.option") 

52 return option 

53 

54 self.options = self._as_tuple(options, as_option) 

55 

56 def _to_wire(self, file, compress=None, origin=None, canonicalize=False): 

57 for opt in self.options: 

58 owire = opt.to_wire() 

59 file.write(struct.pack("!HH", opt.otype, len(owire))) 

60 file.write(owire) 

61 

62 def to_text(self, origin=None, relativize=True, **kw): 

63 return " ".join(opt.to_text() for opt in self.options) 

64 

65 @classmethod 

66 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): 

67 options = [] 

68 while parser.remaining() > 0: 

69 (otype, olen) = parser.get_struct("!HH") 

70 with parser.restrict_to(olen): 

71 opt = dns.edns.option_from_wire_parser(otype, parser) 

72 options.append(opt) 

73 return cls(rdclass, rdtype, options) 

74 

75 @property 

76 def payload(self): 

77 "payload size" 

78 return self.rdclass