drsuapi.idl: fix source_dsa spelling
[samba4-gss.git] / python / samba / tests / dsdb_dns.py
blobc175adb691ec0db2a572ba862cd88760996b8971
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
21 def unix2nttime(t):
22 # here we reimplement unix_to_nt_time from lib/util/time.c
23 if t == -1:
24 return t
25 if t == (1 << 63) - 1:
26 return (1 << 63) - 1
27 if t == 0:
28 return 0
29 t += 11644473600
30 t *= 1e7
31 return int(t)
34 def unix2dns_timestamp(t):
35 nt = unix2nttime(t)
36 if nt < 0:
37 # because NTTIME is a uint64_t.
38 nt += 1 << 64
39 return nt // int(3.6e10)
42 def timestamp2nttime(ts):
43 nt = ts * int(3.6e10)
44 if nt >= 1 << 63:
45 raise OverflowError("nt time won't fit this")
46 return nt
49 class DsdbDnsTestCase(TestCase):
50 def test_unix_to_dns_timestamp(self):
51 unixtimes = [1616829393,
54 -1,
55 1 << 31 - 1]
57 for t in unixtimes:
58 expected = unix2dns_timestamp(t)
59 result = dsdb_dns.unix_to_dns_timestamp(t)
60 self.assertEqual(result, expected)
62 def test_dns_timestamp_to_nt_time(self):
63 timestamps = [16168393,
66 (1 << 32) - 1,
67 (1 << 63) - 1,
68 int((1 << 63) / 3.6e10),
69 int((1 << 63) / 3.6e10) + 1, # overflows
72 for t in timestamps:
73 overflows = False
74 try:
75 expected = timestamp2nttime(t)
76 except OverflowError:
77 overflows = True
78 try:
79 result = dsdb_dns.dns_timestamp_to_nt_time(t)
80 except ValueError:
81 self.assertTrue(overflows, f"timestamp {t} should not overflow")
82 continue
83 self.assertFalse(overflows, f"timestamp {t} should overflow")
85 self.assertEqual(result, expected)