tests/krb5: Use Python bindings for LZ77+Huffman compression
[Samba.git] / python / samba / tests / dsdb_dns.py
blob8c7fc343278285d8abc2fdd8763048a1f7b481d2
1 # Unix SMB/CIFS implementation. Tests for dsdb_dns module
2 # Copyright © Catalyst IT 2021
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from samba.tests import TestCase
18 from samba import dsdb_dns
19 import time
22 def unix2nttime(t):
23 # here we reimplement unix_to_nt_time from lib/util/time.c
24 if t == -1:
25 return t
26 if t == (1 << 63) - 1:
27 return (1 << 63) - 1
28 if t == 0:
29 return 0
30 t += 11644473600
31 t *= 1e7
32 return int(t)
35 def unix2dns_timestamp(t):
36 nt = unix2nttime(t)
37 if nt < 0:
38 # because NTTIME is a uint64_t.
39 nt += 1 << 64
40 return nt // int(3.6e10)
43 def timestamp2nttime(ts):
44 nt = ts * int(3.6e10)
45 if nt >= 1 << 63:
46 raise OverflowError("nt time won't fit this")
47 return nt
50 class DsdbDnsTestCase(TestCase):
51 def test_unix_to_dns_timestamp(self):
52 unixtimes = [1616829393,
55 -1,
56 1 << 31 - 1]
58 for t in unixtimes:
59 expected = unix2dns_timestamp(t)
60 result = dsdb_dns.unix_to_dns_timestamp(t)
61 self.assertEqual(result, expected)
63 def test_dns_timestamp_to_nt_time(self):
64 timestamps = [16168393,
67 (1 << 32) - 1,
68 (1 << 63) - 1,
69 int((1 << 63) / 3.6e10),
70 int((1 << 63) / 3.6e10) + 1, # overflows
73 for t in timestamps:
74 overflows = False
75 try:
76 expected = timestamp2nttime(t)
77 except OverflowError:
78 overflows = True
79 try:
80 result = dsdb_dns.dns_timestamp_to_nt_time(t)
81 except ValueError:
82 self.assertTrue(overflows, f"timestamp {t} should not overflow")
83 continue
84 self.assertFalse(overflows, f"timestamp {t} should overflow")
86 self.assertEqual(result, expected)