Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dns/ttl.py: 13%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

45 statements  

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

2 

3# Copyright (C) 2003-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 

18"""DNS TTL conversion.""" 

19 

20import dns.exception 

21 

22# Technically TTLs are supposed to be between 0 and 2**31 - 1, with values 

23# greater than that interpreted as 0, but we do not impose this policy here 

24# as values > 2**31 - 1 occur in real world data. 

25# 

26# We leave it to applications to impose tighter bounds if desired. 

27MAX_TTL = 2**32 - 1 

28 

29 

30class BadTTL(dns.exception.SyntaxError): 

31 """DNS TTL value is not well-formed.""" 

32 

33 

34def from_text(text: str) -> int: 

35 """Convert the text form of a TTL to an integer. 

36 

37 The BIND 8 units syntax for TTLs (e.g. '1w6d4h3m10s') is supported. 

38 

39 *text*, a ``str``, the textual TTL. 

40 

41 Raises ``dns.ttl.BadTTL`` if the TTL is not well-formed. 

42 

43 Returns an ``int``. 

44 """ 

45 

46 if text.isdigit(): 

47 total = int(text) 

48 elif len(text) == 0: 

49 raise BadTTL 

50 else: 

51 total = 0 

52 current = 0 

53 need_digit = True 

54 for c in text: 

55 if c.isdigit(): 

56 current *= 10 

57 current += int(c) 

58 need_digit = False 

59 else: 

60 if need_digit: 

61 raise BadTTL 

62 c = c.lower() 

63 if c == "w": 

64 total += current * 604800 

65 elif c == "d": 

66 total += current * 86400 

67 elif c == "h": 

68 total += current * 3600 

69 elif c == "m": 

70 total += current * 60 

71 elif c == "s": 

72 total += current 

73 else: 

74 raise BadTTL(f"unknown unit '{c}'") 

75 current = 0 

76 need_digit = True 

77 if not current == 0: 

78 raise BadTTL("trailing integer") 

79 if total < 0 or total > MAX_TTL: 

80 raise BadTTL("TTL should be between 0 and 2**32 - 1 (inclusive)") 

81 return total 

82 

83 

84def make(value: int | str) -> int: 

85 if isinstance(value, int): 

86 return value 

87 elif isinstance(value, str): 

88 return from_text(value) 

89 else: 

90 raise ValueError("cannot convert value to TTL")