This file is indexed.

/usr/lib/python2.7/dist-packages/launchpadlib/tests/test_credential_store.py is in python-launchpadlib 1.10.2+ds-2.

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright 2010-2011 Canonical Ltd.

# This file is part of launchpadlib.
#
# launchpadlib is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, version 3 of the License.
#
# launchpadlib 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 Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with launchpadlib. If not, see <http://www.gnu.org/licenses/>.

"""Tests for the credential store classes."""

import os
import tempfile
try:
    import unittest2 as unittest
except ImportError:
    import unittest

from base64 import b64decode

from launchpadlib.testing.helpers import (
    fake_keyring,
    InMemoryKeyring,
)

from launchpadlib.credentials import (
    AccessToken,
    Credentials,
    KeyringCredentialStore,
    UnencryptedFileCredentialStore,
)


class CredentialStoreTestCase(unittest.TestCase):

    def make_credential(self, consumer_key):
        """Helper method to make a fake credential."""
        return Credentials(
            "app name", consumer_secret='consumer_secret:42',
            access_token=AccessToken(consumer_key, 'access_secret:168'))


class TestUnencryptedFileCredentialStore(CredentialStoreTestCase):
    """Tests for the UnencryptedFileCredentialStore class."""

    def setUp(self):
        ignore, self.filename = tempfile.mkstemp()
        self.store = UnencryptedFileCredentialStore(self.filename)

    def tearDown(self):
        if os.path.exists(self.filename):
            os.remove(self.filename)

    def test_save_and_load(self):
        # Make sure you can save and load credentials to a file.
        credential = self.make_credential("consumer key")
        self.store.save(credential, "unique key")
        credential2 = self.store.load("unique key")
        self.assertEquals(credential.consumer.key, credential2.consumer.key)

    def test_unique_id_doesnt_matter(self):
        # If a file contains a credential, that credential will be
        # accessed no matter what unique ID you specify.
        credential = self.make_credential("consumer key")
        self.store.save(credential, "some key")
        credential2 = self.store.load("some other key")
        self.assertEquals(credential.consumer.key, credential2.consumer.key)

    def test_file_only_contains_one_credential(self):
        # A credential file may contain only one credential. If you
        # write two credentials with different unique IDs to the same
        # file, the first credential will be overwritten with the
        # second.
        credential1 = self.make_credential("consumer key")
        credential2 = self.make_credential("consumer key2")
        self.store.save(credential1, "unique key 1")
        self.store.save(credential1, "unique key 2")
        loaded = self.store.load("unique key 1")
        self.assertEquals(loaded.consumer.key, credential2.consumer.key)


class TestKeyringCredentialStore(CredentialStoreTestCase):
    """Tests for the KeyringCredentialStore class."""

    def setUp(self):
        self.keyring = InMemoryKeyring()
        self.store = KeyringCredentialStore()

    def test_save_and_load(self):
        # Make sure you can save and load credentials to a keyring.
        with fake_keyring(self.keyring):
            credential = self.make_credential("consumer key")
            self.store.save(credential, "unique key")
            credential2 = self.store.load("unique key")
            self.assertEquals(
                credential.consumer.key, credential2.consumer.key)

    def test_lookup_by_unique_key(self):
        # Credentials in the keyring are looked up by the unique ID
        # under which they were stored.
        with fake_keyring(self.keyring):
            credential1 = self.make_credential("consumer key1")
            self.store.save(credential1, "key 1")

            credential2 = self.make_credential("consumer key2")
            self.store.save(credential2, "key 2")

            loaded1 = self.store.load("key 1")
            self.assertEquals(
                credential1.consumer.key, loaded1.consumer.key)

            loaded2 = self.store.load("key 2")
            self.assertEquals(
                credential2.consumer.key, loaded2.consumer.key)

    def test_reused_unique_id_overwrites_old_credential(self):
        # Writing a credential to the keyring with a given unique ID
        # will overwrite any credential stored under that ID.

        with fake_keyring(self.keyring):
            credential1 = self.make_credential("consumer key1")
            self.store.save(credential1, "the only key")

            credential2 = self.make_credential("consumer key2")
            self.store.save(credential2, "the only key")

            loaded = self.store.load("the only key")
            self.assertEquals(
                credential2.consumer.key, loaded.consumer.key)

    def test_bad_unique_id_returns_none(self):
        # Trying to load a credential without providing a good unique
        # ID will get you None.
        with fake_keyring(self.keyring):
            self.assertEquals(None, self.store.load("no such key"))

    def test_keyring_returns_unicode(self):
        # Kwallet is reported to sometimes return Unicode, which broke the
        # credentials parsing.  This test ensures a Unicode password is
        # handled correctly.  (See bug lp:877374)
        class UnicodeInMemoryKeyring(InMemoryKeyring):
            def get_password(self, service, username):
                return unicode(
                    super(UnicodeInMemoryKeyring, self).get_password(
                        service, username))

        self.keyring = UnicodeInMemoryKeyring()
        with fake_keyring(self.keyring):
            credential = self.make_credential("consumer key")
            self.store.save(credential, "unique key")
            credential2 = self.store.load("unique key")
            self.assertEquals(
                credential.consumer.key, credential2.consumer.key)
            self.assertEquals(
                credential.consumer.secret, credential2.consumer.secret)

    def test_nonencoded_key_handled(self):
        # For backwards compatibility with keys that are not base 64 encoded.

        class UnencodedInMemoryKeyring(InMemoryKeyring):
            def get_password(self, service, username):
                pw = super(UnencodedInMemoryKeyring, self).get_password(
                    service, username)
                return b64decode(pw[5:])

        self.keyring = UnencodedInMemoryKeyring()
        with fake_keyring(self.keyring):
            credential = self.make_credential("consumer key")
            self.store.save(credential, "unique key")
            credential2 = self.store.load("unique key")
            self.assertEquals(
                credential.consumer.key, credential2.consumer.key)
            self.assertEquals(
                credential.consumer.secret, credential2.consumer.secret)

    def test_corrupted_key_handled(self):
        # A corrupted password results in None being returned.

        class CorruptedInMemoryKeyring(InMemoryKeyring):
            def get_password(self, service, username):
                return "bad"

        self.keyring = CorruptedInMemoryKeyring()
        with fake_keyring(self.keyring):
            credential = self.make_credential("consumer key")
            self.store.save(credential, "unique key")
            credential2 = self.store.load("unique key")
            self.assertIsNone(credential2)