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

45 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) 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 

20from typing import Union 

21 

22import dns.exception 

23 

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

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

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

27# 

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

29MAX_TTL = 2**32 - 1 

30 

31 

32class BadTTL(dns.exception.SyntaxError): 

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

34 

35 

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

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

38 

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

40 

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

42 

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

44 

45 Returns an ``int``. 

46 """ 

47 

48 if text.isdigit(): 

49 total = int(text) 

50 elif len(text) == 0: 

51 raise BadTTL 

52 else: 

53 total = 0 

54 current = 0 

55 need_digit = True 

56 for c in text: 

57 if c.isdigit(): 

58 current *= 10 

59 current += int(c) 

60 need_digit = False 

61 else: 

62 if need_digit: 

63 raise BadTTL 

64 c = c.lower() 

65 if c == "w": 

66 total += current * 604800 

67 elif c == "d": 

68 total += current * 86400 

69 elif c == "h": 

70 total += current * 3600 

71 elif c == "m": 

72 total += current * 60 

73 elif c == "s": 

74 total += current 

75 else: 

76 raise BadTTL("unknown unit '%s'" % c) 

77 current = 0 

78 need_digit = True 

79 if not current == 0: 

80 raise BadTTL("trailing integer") 

81 if total < 0 or total > MAX_TTL: 

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

83 return total 

84 

85 

86def make(value: Union[int, str]) -> int: 

87 if isinstance(value, int): 

88 return value 

89 elif isinstance(value, str): 

90 return dns.ttl.from_text(value) 

91 else: 

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