This file is indexed.

/usr/share/pyshared/zope/app/server/tests/test_mkzopeinstance.py is in python-zope.app.server 3.6.0-0ubuntu3.

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
##############################################################################
#
# Copyright (c) 2004 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Tests for the implementation of the mkzopeinstance script.
"""
import os
import shutil
import sys
import tempfile
import unittest
import zope.app.server

from StringIO import StringIO

from zope.app.server import mkzopeinstance


class TestBase(unittest.TestCase):

    def setUp(self):
        self.stdout = StringIO()
        self.stderr = StringIO()
        self.old_stdout = sys.stdout
        self.old_stderr = sys.stderr
        sys.stdout = self.stdout
        sys.stderr = self.stderr

    def tearDown(self):
        sys.stdout = self.old_stdout
        sys.stderr = self.old_stderr


class ArgumentParsingTestCase(TestBase):
    """Ensure the command line is properly converted to an options
    object.
    """

    def parse_args(self, args):
        argv = ["foo/bar.py"] + args
        options = mkzopeinstance.parse_args(argv)
        self.assertEqual(options.program, "bar.py")
        self.assert_(options.version)
        return options

    def test_no_arguments(self):
        options = self.parse_args([])

    def test_version_long(self):
        self.check_stdout_content(["--version"])

    def test_help_long(self):
        self.check_stdout_content(["--help"])

    def test_help_short(self):
        self.check_stdout_content(["-h"])

    def check_stdout_content(self, args):
        try:
            options = self.parse_args(args)
        except SystemExit, e:
            self.assertEqual(e.code, 0)
            self.assert_(self.stdout.getvalue())
            self.failIf(self.stderr.getvalue())
        else:
            self.fail("expected SystemExit")

    def test_without_destination(self):
        options = self.parse_args([])
        self.assertEqual(options.destination, None)

    def test_destination_long(self):
        options = self.parse_args(["--dir", "some/dir"])
        self.assertEqual(options.destination, "some/dir")

    def test_destination_short(self):
        options = self.parse_args(["-d", "some/dir"])
        self.assertEqual(options.destination, "some/dir")

    def test_without_skeleton(self):
        # make sure we get *some* skeleton directory by default
        # there's no claim that it exists
        options = self.parse_args([])
        self.assertNotEqual(options.skeleton, None)

    def test_with_skeleton_long(self):
        options = self.parse_args(["--skelsrc", "some/dir"])
        self.assertEqual(options.skeleton, "some/dir")
        self.failIf(options.add_package_includes)

    def test_with_skeleton_short(self):
        options = self.parse_args(["-s", "some/dir"])
        self.assertEqual(options.skeleton, "some/dir")
        self.failIf(options.add_package_includes)

    def test_without_username(self):
        options = self.parse_args([])
        self.assertEqual(options.username, None)
        self.assertEqual(options.password, None)

    def test_username_without_password_long(self):
        options = self.parse_args(["--user", "User"])
        self.assertEqual(options.username, "User")
        self.assertEqual(options.password, None)

    def test_username_without_password_short(self):
        options = self.parse_args(["-u", "User"])
        self.assertEqual(options.username, "User")
        self.assertEqual(options.password, None)

    def test_username_with_password_long(self):
        options = self.parse_args(["--user", "User:Pass"])
        self.assertEqual(options.username, "User")
        self.assertEqual(options.password, "Pass")

    def test_username_with_password_short(self):
        options = self.parse_args(["-u", "User:Pass"])
        self.assertEqual(options.username, "User")
        self.assertEqual(options.password, "Pass")

    def test_without_password_manager(self):
        options = self.parse_args([])
        self.assertEqual(options.password_manager, None)

    def test_password_manager_short(self):
        options = self.parse_args(["-m", "Manager"])
        self.assertEqual(options.password_manager, "Manager")

    def test_password_manager_long(self):
        options = self.parse_args(["--password-manager", "Manager"])
        self.assertEqual(options.password_manager, "Manager")

    def test_junk_positional_arg(self):
        try:
            self.parse_args(["junk"])
        except SystemExit, e:
            self.assert_(e.code)
        else:
            self.fail("expected SystemExit")


class InputCollectionTestCase(TestBase):

    def setUp(self):
        super(InputCollectionTestCase, self).setUp()
        self.tmpdir = tempfile.mkdtemp(prefix="test-mkzopeinstance-")
        self.skeleton = os.path.join(self.tmpdir, "skel")
        self.instance = os.path.join(self.tmpdir, "inst")
        os.mkdir(self.skeleton)

    def tearDown(self):
        shutil.rmtree(self.tmpdir)
        super(InputCollectionTestCase, self).tearDown()

    def createOptions(self):
        options = Options()
        options.skeleton = self.skeleton
        options.interactive = True
        return options

    def test_get_skeltarget(self):
        options = self.createOptions()
        input = ["  ", " foo "]
        app = ControlledInputApplication(options, input)
        skel = app.get_skeltarget()
        self.assertEqual(skel, "foo")
        self.assertEqual(input, [])
        self.assert_(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_process_creates_destination(self):
        options = self.createOptions()
        input = [self.instance]
        app = ControlledInputApplication(options, input)
        self.assertEqual(app.process(), 0)
        self.assert_(os.path.isdir(self.instance))
        self.assertEqual(input, [])
        self.failUnless(app.all_input_consumed())

    def test_zserver_support(self):
        # test the zserver option.  We should get zserver versions of
        # runzope, zopectl, debugzope and zope.conf.  Any of these
        # that we provide in put skeleton should be overritten.

        # privide a dummy runzope
        os.mkdir(os.path.join(self.skeleton, 'bin'))
        f = open(os.path.join(self.skeleton, 'bin', 'runzope.in'), 'w')
        f.write('runzope')
        f.close()

        options = self.createOptions()
        options.destination = self.instance
        options.interactive = False
        options.zserver = True
        app = ControlledInputApplication(options, [])
        self.assertEqual(app.process(), 0)
        self.assert_(
            'from zope.app.server.main import main' in
            open(os.path.join(self.instance, 'bin', 'runzope')).read()
            )
        self.assert_(
            'from zope.app.server.main import debug' in
            open(os.path.join(self.instance, 'bin', 'debugzope')).read()
            )
        self.assert_(os.path.exists(
            os.path.join(self.instance, 'etc', 'zope.conf')
            ))


    def test_process_aborts_on_file_destination(self):
        options = self.createOptions()
        options.destination = self.instance
        open(self.instance, "w").close()
        app = ControlledInputApplication(options, [])
        self.assertEqual(app.process(), 1)
        self.assert_(self.stderr.getvalue())

    def test_process_aborts_on_failed_destination_creation(self):
        options = self.createOptions()
        options.destination = os.path.join(self.instance, "foo")
        app = ControlledInputApplication(options, [])
        self.assertEqual(app.process(), 1)
        self.assert_(self.stderr.getvalue())

    def test_get_username(self):
        options = self.createOptions()
        app = ControlledInputApplication(options, ["myuser"])
        usr = app.get_username()
        self.assertEqual(usr, "myuser")
        self.failIf(self.stderr.getvalue())
        self.failUnless(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_get_username_strips_whitespace(self):
        options = self.createOptions()
        app = ControlledInputApplication(options, ["  myuser\t"])
        usr = app.get_username()
        self.assertEqual(usr, "myuser")
        self.failIf(self.stderr.getvalue())
        self.failUnless(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_get_username_ignores_empty_names(self):
        options = self.createOptions()
        app = ControlledInputApplication(options, ["", "  ", "\t", "myuser"])
        usr = app.get_username()
        self.assertEqual(usr, "myuser")
        self.failUnless(self.stderr.getvalue())
        self.failUnless(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_get_password_manager(self):
        options = self.createOptions()
        options.password_manager = None
        app = ControlledInputApplication(options, ["1"])
        name, pwm = app.get_password_manager()
        self.assertEqual(name, "Plain Text")
        self.assertEqual(pwm.encodePassword("foo"), "foo")
        self.failIf(self.stderr.getvalue())
        self.failUnless(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_get_wrong_password_manager(self):
        options = self.createOptions()
        options.password_manager = "Unknown"
        app = ControlledInputApplication(options, [])
        try:
            app.get_password_manager()
        except SystemExit, e:
            self.assertEqual(e.code, 1)
        else:
            self.fail("expected SystemExit")
        self.failUnless(self.stderr.getvalue())
        self.failIf(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_get_password(self):
        options = self.createOptions()
        app = ControlledInputApplication(options, ["foo", "foo"])
        pw = app.get_password()
        self.assertEqual(pw, "foo")
        self.failIf(self.stderr.getvalue())
        self.failUnless(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_get_password_not_verified(self):
        options = self.createOptions()
        app = ControlledInputApplication(options, ["foo", "bar"])
        try:
            app.get_password()
        except SystemExit, e:
            self.assertEqual(e.code, 1)
        else:
            self.fail("expected SystemExit")
        self.failUnless(self.stderr.getvalue())
        self.failUnless(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_get_password_empty(self):
        # Make sure the empty password is ignored.
        options = self.createOptions()
        app = ControlledInputApplication(options, ["", "foo", "foo"])
        pw = app.get_password()
        self.assertEqual(pw, "foo")
        self.failUnless(self.stderr.getvalue())
        self.failUnless(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_get_password_disallows_whitespace(self):
        # Any password that contains spaces is disallowed.
        options = self.createOptions()
        app = ControlledInputApplication(options, [" ", "\t", "a b",
                                                   " a", "b ", "foo", "foo"])
        pw = app.get_password()
        self.assertEqual(pw, "foo")
        self.failUnless(self.stderr.getvalue())
        self.failUnless(self.stdout.getvalue())
        self.failUnless(app.all_input_consumed())

    def test_can_rewrite_existing_instance(self):
        # Fill out the skeleton a little so we test more cases:
        os.mkdir(os.path.join(self.skeleton, "etc"))
        f = open(os.path.join(self.skeleton, "etc", "README.txt"), "w")
        f.write("Configuration goes here.\n")
        f.close()

        # Create an instance home:
        options = self.createOptions()
        options.destination = self.instance
        app = ControlledInputApplication(options, [])
        rc = app.process()
        self.assertEqual(rc, 0)
        self.failUnless(app.all_input_consumed())
        self.failUnless(os.path.exists(os.path.join(self.instance, "etc")))

        # Make sure we can do it again:
        options = self.createOptions()
        options.destination = self.instance
        app = ControlledInputApplication(options, [])
        rc = app.process()
        self.assertEqual(rc, 0)
        self.failUnless(app.all_input_consumed())
        self.failUnless(os.path.exists(os.path.join(self.instance, "etc")))

    def test_zope_namespace_package_doesnt_affect_software_home(self):
        # Make sure that a zope namespace package in a different
        # location won't affect SOFTWARE_HOME

        # let's mess with zope's __file__
        import zope
        old_path = zope.__file__
        zope.__file__ = os.path.join(
            *'and now for something completely different'.split())

        # place a test file into the skeleton dir that'll be expanded
        # to SOFTWARE_HOME by mkzopeinstance
        f = file(os.path.join(self.skeleton, 'test.in'), 'w')
        f.write('<<SOFTWARE_HOME>>')
        f.close()

        # run mkzopeinstance
        options = self.createOptions()
        options.destination = self.instance
        app = ControlledInputApplication(options, [])
        rc = app.process()

        # check for the expected output: mkzopeinstance should take
        # zope.app as an anchor for determining SOFTWARE_HOME
        import zope.app
        expected = os.path.dirname(os.path.dirname(
            os.path.dirname(zope.app.__file__)))
        self.assertEqual(file(os.path.join(self.instance, 'test')).read(),
                         expected)

        # cleanup the fake 'zope' module
        zope.__file__ = old_path

class ControlledInputApplication(mkzopeinstance.Application):

    def __init__(self, options, input_lines):
        mkzopeinstance.Application.__init__(self, options)
        self.__input = input_lines

    def read_input_line(self, prompt):
        return self.__input.pop(0)

    read_password = read_input_line

    def all_input_consumed(self):
        return not self.__input


class Options(object):

    username = "[test-username]"
    password_manager = "Plain Text"
    password = "[test-password]"
    destination = None
    version = "[test-version]"
    program = "[test-program]"
    add_package_includes = False
    zserver = False


def test_suite():
    suite = unittest.makeSuite(ArgumentParsingTestCase)
    suite.addTest(unittest.makeSuite(InputCollectionTestCase))
    return suite

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