File: updater_test.py

package info (click to toggle)
nsscache 0.49-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,664 kB
  • sloc: python: 8,661; xml: 584; sh: 304; makefile: 19
file content (121 lines) | stat: -rw-r--r-- 4,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""Unit tests for nss_cache/update/base.py."""

__author__ = ("vasilios@google.com (V Hoffman)", "jaq@google.com (Jamie Wilkinson)")

import os
import shutil
import tempfile
import time
import unittest
from unittest import mock

from nss_cache import config
from nss_cache.update import updater


class TestUpdater(unittest.TestCase):
    """Unit tests for the Updater class."""

    def setUp(self):
        super(TestUpdater, self).setUp()
        self.workdir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.workdir)
        super(TestUpdater, self).tearDown()

    def testTimestampDir(self):
        """We read and write timestamps to the specified directory."""
        update_obj = updater.Updater(config.MAP_PASSWORD, self.workdir, {})
        self.updater = updater
        update_time = 1199149400
        modify_time = 1199149200

        update_obj.WriteUpdateTimestamp(update_time)
        update_obj.WriteModifyTimestamp(modify_time)

        update_stamp = update_obj.GetUpdateTimestamp()
        modify_stamp = update_obj.GetModifyTimestamp()

        self.assertEqual(
            update_time,
            update_stamp,
            msg=(
                "retrieved a different update time than we stored: "
                "Expected: %r, observed: %r" % (update_time, update_stamp)
            ),
        )
        self.assertEqual(
            modify_time,
            modify_stamp,
            msg=(
                "retrieved a different modify time than we stored: "
                "Expected %r, observed: %r" % (modify_time, modify_stamp)
            ),
        )

    def testWriteWhenTimestampIsNone(self):
        update_obj = updater.Updater(config.MAP_PASSWORD, self.workdir, {})
        self.assertEqual(True, update_obj.WriteUpdateTimestamp(None))
        self.assertEqual(True, update_obj.WriteModifyTimestamp(None))

    def testTimestampDefaultsToNone(self):
        """Missing or unreadable timestamps return None."""
        update_obj = updater.Updater(config.MAP_PASSWORD, self.workdir, {})
        self.updater = update_obj
        update_stamp = update_obj.GetUpdateTimestamp()
        modify_stamp = update_obj.GetModifyTimestamp()

        self.assertEqual(None, update_stamp, msg="update time did not default to None")
        self.assertEqual(None, modify_stamp, msg="modify time did not default to None")

        # touch a file, make it unreadable
        update_file = open(update_obj.update_file, "w")
        modify_file = open(update_obj.modify_file, "w")
        update_file.close()
        modify_file.close()
        os.chmod(update_obj.update_file, 0000)
        os.chmod(update_obj.modify_file, 0000)

        update_stamp = update_obj.GetUpdateTimestamp()
        modify_stamp = update_obj.GetModifyTimestamp()

        self.assertEqual(
            None, update_stamp, msg="unreadable update time did not default to None"
        )
        self.assertEqual(
            None, modify_stamp, msg="unreadable modify time did not default to None"
        )

    def testTimestampInTheFuture(self):
        """Timestamps in the future are turned into now."""
        update_obj = updater.Updater(config.MAP_PASSWORD, self.workdir, {})
        expected_time = 1
        update_time = 3601
        update_file = open(update_obj.update_file, "w")
        update_obj.WriteUpdateTimestamp(update_time)
        update_file.close()

        with mock.patch.object(
            update_obj, "_GetCurrentTime", return_value=expected_time
        ) as ct:
            self.assertEqual(expected_time, update_obj.GetUpdateTimestamp())


if __name__ == "__main__":
    unittest.main()