This file is indexed.

/usr/lib/python2.7/dist-packages/txdav/caldav/datastore/test/test_util.py is in calendarserver 5.2+dfsg-1.

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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
##
# Copyright (c) 2010-2014 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
from txdav.caldav.datastore.test.util import buildCalendarStore

"""
Tests for txdav.caldav.datastore.util.
"""

import textwrap

from twisted.trial.unittest import TestCase as BaseTestCase
from twext.web2.http_headers import MimeType

from twisted.internet.defer import inlineCallbacks

from twistedcaldav.ical import Component
from twistedcaldav.test.util import TestCase

from txdav.common.datastore.test.util import populateCalendarsFrom, CommonCommonTests

from txdav.caldav.datastore.util import dropboxIDFromCalendarObject, \
    StorageTransportBase, migrateHome

from txdav.common.icommondatastore import HomeChildNameAlreadyExistsError

class DropboxIDTests(TestCase):
    """
    Test dropbox ID extraction from calendar data.
    """

    class FakeCalendarResource(object):
        """
        Fake object resource to work with tests.
        """

        def __init__(self, data):

            self.ical = Component.fromString(data)

        def component(self):
            return self.ical

        def uid(self):
            return self.ical.resourceUID()


    @inlineCallbacks
    def test_noAttachOrXdash(self):
        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals(
            (yield dropboxIDFromCalendarObject(resource)),
            "12345-67890.dropbox"
        )


    @inlineCallbacks
    def test_okXdash(self):

        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
X-APPLE-DROPBOX:http://example.com/calendars/__uids__/1234/dropbox/12345-67890X.dropbox
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals(
            (yield dropboxIDFromCalendarObject(resource)),
            "12345-67890X.dropbox"
        )


    @inlineCallbacks
    def test_emptyXdash(self):
        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
X-APPLE-DROPBOX:
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals((yield dropboxIDFromCalendarObject(resource)), "12345-67890.dropbox")


    @inlineCallbacks
    def test_okAttach(self):

        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
ATTACH;VALUE=URI:http://example.com/calendars/__uids__/1234/dropbox/12345-67890Y.dropbox/text.txt
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals(
            (yield dropboxIDFromCalendarObject(resource)),
            "12345-67890Y.dropbox"
        )


    @inlineCallbacks
    def test_badAttach(self):

        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
ATTACH;VALUE=URI:tag:bogus
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals(
            (yield dropboxIDFromCalendarObject(resource)),
            "12345-67890.dropbox"
        )


    @inlineCallbacks
    def test_inlineAttach(self):

        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
ATTACH:bmFzZTY0
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals(
            (yield dropboxIDFromCalendarObject(resource)),
            "12345-67890.dropbox"
        )


    @inlineCallbacks
    def test_multipleAttach(self):

        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
ATTACH;VALUE=URI:tag:bogus
ATTACH:bmFzZTY0
ATTACH;VALUE=URI:http://example.com/calendars/__uids__/1234/dropbox/12345-67890Z.dropbox/text.txt
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals(
            (yield dropboxIDFromCalendarObject(resource)),
            "12345-67890Z.dropbox"
        )


    @inlineCallbacks
    def test_okAttachRecurring(self):

        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
RRULE:FREQ=YEARLY
END:VEVENT
BEGIN:VEVENT
UID:12345-67890
RECURRENCE-ID:20081114T000000Z
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
ATTACH;VALUE=URI:http://example.com/calendars/__uids__/1234/dropbox/12345-67890Y.dropbox/text.txt
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals(
            (yield dropboxIDFromCalendarObject(resource)),
            "12345-67890Y.dropbox"
        )


    @inlineCallbacks
    def test_okAttachAlarm(self):

        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:12345-67890
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
BEGIN:VALARM
ACTION:AUDIO
ATTACH;VALUE=URI:Ping
TRIGGER:-PT15M
X-WR-ALARMUID:5548D654-8FDA-49DB-8983-8FCAD1F322B1
END:VALARM
END:VEVENT
END:VCALENDAR
""")

        self.assertEquals(
            (yield dropboxIDFromCalendarObject(resource)),
            "12345-67890.dropbox"
        )


    @inlineCallbacks
    def test_UIDbadPath(self):
        test_UIDs = (
            ("12345/67890", "12345-67890"),
            ("http://12345,67890", "12345,67890"),
            ("https://12345,67890", "12345,67890"),
            ("12345:67890", "1234567890"),
            ("12345.67890", "1234567890"),
            ("12345/6:7.890", "12345-67890"),
        )

        for uid, result in test_UIDs:
            resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
UID:%s
DTSTART:20071114T000000Z
ATTENDEE:mailto:user1@example.com
ATTENDEE:mailto:user2@example.com
END:VEVENT
END:VCALENDAR
""" % (uid,))

            self.assertEquals(
                (yield dropboxIDFromCalendarObject(resource)),
                "%s.dropbox" % (result,),
            )



class StorageTransportTests(TestCase):

    def test_MissingContentType(self):

        test_files = (
            ("plain.txt", MimeType.fromString("text/plain"),),
            ("word.doc", MimeType.fromString("application/msword"),),
            ("markup.html", MimeType.fromString("text/html"),),
            ("octet", MimeType.fromString("application/octet-stream"),),
            ("bogus.bog", MimeType.fromString("application/octet-stream"),),
        )

        class FakeAttachment(object):

            def __init__(self, name):
                self._name = name

            def name(self):
                return self._name

        for filename, result in test_files:
            item = StorageTransportBase(FakeAttachment(filename), None, None)
            self.assertEquals(item._contentType, result)
            self.assertEquals(item._dispositionName, None)
            item = StorageTransportBase(FakeAttachment(filename), result, filename)
            self.assertEquals(item._contentType, result)
            self.assertEquals(item._dispositionName, filename)



class HomeMigrationTests(CommonCommonTests, BaseTestCase):
    """
    Tests for L{migrateHome}.
    """

    @inlineCallbacks
    def setUp(self):
        yield super(HomeMigrationTests, self).setUp()
        self.theStore = yield buildCalendarStore(self, self.notifierFactory, homes=("conflict1", "conflict2",))


    def storeUnderTest(self):
        return self.theStore


    @inlineCallbacks
    def test_migrateEmptyHome(self):
        """
        Migrating an empty home into an existing home should destroy all the
        existing home's calendars.
        """
        yield populateCalendarsFrom({
            "empty_home": {
                # Some of the upgrade logic will ensure that sufficient default
                # calendars exist for basic usage, so this home is actually only
                # *mostly* empty; the important thing is that the default
                # calendar is removed.
                "other-default-calendar": {}
            },
            "non_empty_home": {
                "calendar": {},
                "inbox": {},
                # XXX: implementation is configuration-sensitive regarding the
                # 'tasks' calendar and it shouldn't be.
                "tasks": {}
            }
        }, self.storeUnderTest())
        txn = self.transactionUnderTest()
        emptyHome = yield txn.calendarHomeWithUID("empty_home")
        self.assertIdentical((yield emptyHome.calendarWithName("calendar")), None)
        nonEmpty = yield txn.calendarHomeWithUID("non_empty_home")
        yield migrateHome(emptyHome, nonEmpty)
        yield self.commit()
        txn = self.transactionUnderTest()
        emptyHome = yield txn.calendarHomeWithUID("empty_home")
        nonEmpty = yield txn.calendarHomeWithUID("non_empty_home")

        self.assertIdentical((yield nonEmpty.calendarWithName("calendar")), None)
        self.assertNotIdentical((yield nonEmpty.calendarWithName("inbox")), None)
        self.assertNotIdentical((yield nonEmpty.calendarWithName("other-default-calendar")), None)


    @staticmethod
    def sampleEvent(uid, summary=None):
        """
        Create the iCalendar text for a sample event that has no organizer nor
        any attendees.
        """
        if summary is None:
            summary = "event " + uid
        return textwrap.dedent(
            """\
            BEGIN:VCALENDAR
            VERSION:2.0
            CALSCALE:GREGORIAN
            PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
            BEGIN:VEVENT
            UID:{uid}
            DTSTART;VALUE=DATE:20060201
            DURATION:P1D
            CREATED:20060101T210000Z
            DTSTAMP:20051222T210146Z
            LAST-MODIFIED:20051222T210203Z
            SEQUENCE:1
            SUMMARY:{summary}
            TRANSP:TRANSPARENT
            END:VEVENT
            END:VCALENDAR
            """.replace("\n", "\r\n").format(uid=uid, summary=summary)
        ), {}


    @inlineCallbacks
    def createConflicted(self, c1=None, c2=None):
        """
        Create two calendar homes with calendars with the same names within
        them.  Parameters are both a mapping of calendar object names to
        2-tuples of (iCalendar data, metadata).

        @param c1: the calendar data for conflict1/conflicted/*

        @param c2: the calendar data for conflict2/conflicted/*
        """
        if c1 is None:
            c1 = {"1.ics": self.sampleEvent("uid1")}
        if c2 is None:
            c2 = {"2.ics": self.sampleEvent("uid2")}
        defaults = {"calendar": {}, "inbox": {}, "tasks": {}}
        def conflicted(caldata):
            d = defaults.copy()
            d.update(conflicted=caldata)
            return d
        yield populateCalendarsFrom({
            "conflict1": conflicted(c1),
            "conflict2": conflicted(c2),
        }, self.storeUnderTest())


    @inlineCallbacks
    def test_migrateConflict(self):
        """
        Migrating a home with conflicting (non-default) calendars will cause an
        error.
        """
        yield self.createConflicted()
        txn = self.transactionUnderTest()
        conflict1 = yield txn.calendarHomeWithUID("conflict1")
        conflict2 = yield txn.calendarHomeWithUID("conflict2")

        try:
            yield migrateHome(conflict1, conflict2)
        except HomeChildNameAlreadyExistsError:
            pass
        else:
            self.fail("No exception raised.")


    @inlineCallbacks
    def test_migrateMergeCalendars(self):
        """
        Migrating a home with a conflicting (non-default) calendar in merge
        mode will cause the properties on the conflicting calendar to be
        overridden by the new calendar of the same name, and calendar objects
        to be copied over.
        """
        yield self.createConflicted()
        from txdav.base.propertystore.base import PropertyName
        from txdav.xml import element as davxml
        class StubConflictingElement(davxml.WebDAVTextElement):
            namespace = "http://example.com/ns/stub-conflict"
            name = "conflict"
        beforeProp = StubConflictingElement.fromString("before")
        afterProp = StubConflictingElement.fromString("after")
        conflictPropName = PropertyName.fromElement(beforeProp)
        txn = self.transactionUnderTest()
        conflict1 = yield txn.calendarHomeWithUID("conflict1")
        conflict2 = yield txn.calendarHomeWithUID("conflict2")
        cal1 = yield conflict1.calendarWithName("conflicted")
        cal2 = yield conflict2.calendarWithName("conflicted")
        p1 = cal1.properties()
        p2 = cal2.properties()
        p1[conflictPropName] = afterProp
        p2[conflictPropName] = beforeProp
        yield migrateHome(conflict1, conflict2, merge=True)
        self.assertEquals(p2[conflictPropName].children[0].data, "after")
        obj1 = yield cal2.calendarObjectWithName("1.ics")
        obj2 = yield cal2.calendarObjectWithName("2.ics")
        # just a really cursory check to make sure they're really there.
        self.assertEquals(obj1.uid(), "uid1")
        self.assertEquals(obj2.uid(), "uid2")


    @inlineCallbacks
    def test_migrateMergeConflictingObjects(self):
        """
        When merging two homes together, calendar objects may conflict in the
        following ways:

        First, an object may have the same name and the same UID as an object
        in the target calendar.  We assume the target object is always be newer
        than the source object, so this type of conflict will leave the source
        object unmodified.  This type of conflict is expected, and may happen
        as a result of an implicitly scheduled event where the principal owning
        the merged calendars is an attendee of the conflicting object, and
        received a re-invitation.

        Second, an object may have a different name, but the same UID as an
        object in the target calendar.  While this type of conflict is not
        expected -- most clients will choose names for objects that correspond
        to the iCalendar UIDs of their main component -- it is treated the same
        way as the first conflict.

        Third, an object may have the same UID as an object on a different
        calendar in the target home.  This may also happen if a scheduled event
        was previously on a different (most likely non-default) calendar.
        Technically this is actually valid, and it is possible to have the same
        object in multiple calendars as long as the object is not scheduled;
        however, that type of conflict is extremely unlikely as the client
        would have to generate the same event twice.

        Basically, in all expected cases, conflicts will only occur because an
        update to a scheduled event was sent out and the target home accepted
        it.  Therefore, conflicts are always resolved in favor of ignoring the
        source data and trusting that the target data is more reliable.
        """
        # Note: these tests are all performed with un-scheduled data because it
        # is simpler.  Although the expected conflicts will involve scheduled
        # data the behavior will be exactly the same.
        yield self.createConflicted(
            {
                "same-name": self.sampleEvent("same-name", "source"),
                "other-name": self.sampleEvent("other-uid", "source other"),
                "other-calendar": self.sampleEvent("oc", "source calendar"),
                "no-conflict": self.sampleEvent("no-conflict", "okay"),
            },
            {
                "same-name": self.sampleEvent("same-name", "target"),
                "different-name": self.sampleEvent("other-uid", "tgt other"),
            },
        )

        txn = self.transactionUnderTest()
        c2 = yield txn.calendarHomeWithUID("conflict2")
        otherCal = yield c2.createCalendarWithName("othercal")
        yield otherCal.createCalendarObjectWithName(
            "some-name", Component.fromString(
                self.sampleEvent("oc", "target calendar")[0]
            )
        )
        yield self.commit()

        txn = self.transactionUnderTest()
        c1 = yield txn.calendarHomeWithUID("conflict1")
        c2 = yield txn.calendarHomeWithUID("conflict2")
        yield migrateHome(c1, c2, merge=True)
        yield self.commit()

        txn = self.transactionUnderTest()
        c2 = yield txn.calendarHomeWithUID("conflict2")
        targetCal = yield c2.calendarWithName("conflicted")
        yield self.checkSummary("same-name", "target", targetCal)
        yield self.checkSummary("different-name", "tgt other", targetCal)
        yield self.checkSummary("other-calendar", None, targetCal)
        yield self.checkSummary("other-name", None, targetCal)
        yield self.checkSummary("no-conflict", "okay", targetCal)
        yield self.checkSummary("oc", "target calendar", otherCal)


    @inlineCallbacks
    def checkSummary(self, name, summary, cal):
        """
        Verify that the summary of the calendar object for the given name in
        the given calendar matches.
        """
        obj = yield cal.calendarObjectWithName(name)
        if summary is None:
            self.assertIdentical(obj, None,
                                 name + " existed but shouldn't have")
        else:
            txt = ((yield obj.component()).mainComponent()
                   .getProperty("SUMMARY").value())
            self.assertEquals(txt, summary)


    @inlineCallbacks
    def test_migrateMergeDontDeleteDefault(self):
        """
        If we're doing a merge migration, it's quite possible that the user has
        scheduled events onto their default calendar already.  In fact the
        whole point of a merge migration is to preserve data that might have
        been created there.  So, let's make sure that we I{don't} delete any
        data from the default calendars in the case that we're merging.
        """
        yield populateCalendarsFrom({
            "empty_home": {
                # see test_migrateEmptyHome above.
                "other-default-calendar": {}
            },
            "non_empty_home": {
                "calendar": {
                    "some-name": self.sampleEvent("some-uid", "some summary"),
                }, "inbox": {}, "tasks": {}
            }
        }, self.storeUnderTest())
        txn = self.transactionUnderTest()
        emptyHome = yield txn.calendarHomeWithUID("empty_home")
        self.assertIdentical((yield emptyHome.calendarWithName("calendar")),
                             None)
        nonEmpty = yield txn.calendarHomeWithUID("non_empty_home")
        yield migrateHome(emptyHome, nonEmpty, merge=True)
        yield self.commit()
        txn = self.transactionUnderTest()
        emptyHome = yield txn.calendarHomeWithUID("empty_home")
        nonEmpty = yield txn.calendarHomeWithUID("non_empty_home")
        self.assertNotIdentical(
            (yield nonEmpty.calendarWithName("inbox")), None
        )
        defaultCal = (yield nonEmpty.calendarWithName("calendar"))
        self.assertNotIdentical(
            (yield defaultCal.calendarObjectWithName("some-name")), None
        )