This file is indexed.

/usr/lib/python2.7/dist-packages/ubuntu-sso-client/ubuntu_sso/utils/webclient/tests/test_timestamp.py is in python-ubuntu-sso-client.tests 13.10-0ubuntu11.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
#
# Copyright 2011-2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the
# OpenSSL library under certain conditions as described in each
# individual source file, and distribute linked combinations
# including the two.
# You must obey the GNU General Public License in all respects
# for all of the code used other than OpenSSL.  If you modify
# file(s) with this exception, you may extend this exception to your
# version of the file(s), but you are not obligated to do so.  If you
# do not wish to do so, delete this exception statement from your
# version.  If you delete this exception statement from all source
# files in the program, then also delete it here.
"""Tests for the timestamp sync classes."""

from twisted.internet import defer
from twisted.trial.unittest import TestCase
from twisted.web import resource

from ubuntuone.devtools.testing.txwebserver import HTTPWebServer

from ubuntu_sso.utils.webclient import timestamp, webclient_module


class FakedError(Exception):
    """Stub to replace Request.error."""


class RootResource(resource.Resource):
    """A root resource that logs the number of calls."""

    isLeaf = True

    def __init__(self, *args, **kwargs):
        """Initialize this fake instance."""
        resource.Resource.__init__(self, *args, **kwargs)
        self.count = 0
        self.request_headers = []

    # pylint: disable=C0103
    def render_HEAD(self, request):
        """Increase the counter on each render."""
        self.request_headers.append(request.requestHeaders)
        self.count += 1
        return ""


class MockWebServer(HTTPWebServer):
    """A mock webserver for testing."""

    def __init__(self):
        """Create a new server."""
        super(MockWebServer, self).__init__(RootResource())


class TimestampCheckerTestCase(TestCase):
    """Tests for the timestamp checker."""

    timeout = 5

    @defer.inlineCallbacks
    def setUp(self):
        yield super(TimestampCheckerTestCase, self).setUp()
        self.ws = MockWebServer()
        self.ws.start()
        self.addCleanup(self.ws.stop)
        self.webclient_class = webclient_module().WebClient
        self.patch(timestamp.TimestampChecker, "SERVER_IRI", self.ws.get_iri())

    @defer.inlineCallbacks
    def test_returned_value_is_int(self):
        """The returned value is an integer."""
        checker = timestamp.TimestampChecker(self.webclient_class)
        result = yield checker.get_faithful_time()
        self.assertEqual(type(result), int)

    @defer.inlineCallbacks
    def test_first_call_does_head(self):
        """The first call gets the clock from our web."""
        checker = timestamp.TimestampChecker(self.webclient_class)
        yield checker.get_faithful_time()
        self.assertEqual(self.ws.root.count, 1)

    @defer.inlineCallbacks
    def test_second_call_is_cached(self):
        """For the second call, the time is cached."""
        checker = timestamp.TimestampChecker(self.webclient_class)
        yield checker.get_faithful_time()
        yield checker.get_faithful_time()
        self.assertEqual(self.ws.root.count, 1)

    @defer.inlineCallbacks
    def test_after_timeout_cache_expires(self):
        """After some time, the cache expires."""
        fake_timestamp = 1
        self.patch(timestamp.time, "time", lambda: fake_timestamp)
        checker = timestamp.TimestampChecker(self.webclient_class)
        yield checker.get_faithful_time()
        fake_timestamp += timestamp.TimestampChecker.CHECKING_INTERVAL
        yield checker.get_faithful_time()
        self.assertEqual(self.ws.root.count, 2)

    @defer.inlineCallbacks
    def test_server_error_means_skew_not_updated(self):
        """When server can't be reached, the skew is not updated."""
        fake_timestamp = 1
        self.patch(timestamp.time, "time", lambda: fake_timestamp)
        checker = timestamp.TimestampChecker(self.webclient_class)
        failing_get_server_time = lambda _: defer.fail(FakedError())
        self.patch(checker, "get_server_time", failing_get_server_time)
        yield checker.get_faithful_time()
        self.assertEqual(checker.skew, 0)
        self.assertEqual(checker.next_check,
                    fake_timestamp + timestamp.TimestampChecker.ERROR_INTERVAL)

    @defer.inlineCallbacks
    def test_server_date_sends_nocache_headers(self):
        """Getting the server date sends the no-cache headers."""
        checker = timestamp.TimestampChecker(self.webclient_class)
        yield checker.get_server_date_header(self.ws.get_iri())
        self.assertEqual(len(self.ws.root.request_headers), 1)
        headers = self.ws.root.request_headers[0]
        result = headers.getRawHeaders("Cache-Control")
        self.assertEqual(result, ["no-cache"])