gen-15.py 17.3 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Test suite for lyskomd.
# Copyright (C) 2000  Lysator Academic Computer Association.
#
# This file is part of the LysKOM server.
# 
# LysKOM 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 1, or (at your option) 
# any later version.
# 
# LysKOM 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 LysKOM; see the file COPYING.  If not, write to
# Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
# or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
# MA 02139, USA.
#
# Please mail bug reports to bug-lyskom@lysator.liu.se. 

24
25
import string

26
27
28
29
30
31
# Set EXTENDED to 1 to emit tests for lots of misc-into combinations,
# which takes a very long time to test (approx. 5 minutes on a 600 MHz Athlon).
EXTENDED = 0

# Set DEBUG to 1 to emit extra "get-time" calls, which slows the test down
# approximately 5%, but makes it easier to spot errors.
Per Cederqvist's avatar
Per Cederqvist committed
32
DEBUG = 0
33

34
class factory:
35
36
    def __init__(self, bcc_trigg, bcc_visible, bcc_author_visible,
                 other_trigg, other_visible, rd):
37
38
        self.__bcc_trigg = bcc_trigg
        self.__bcc_visible = bcc_trigg + bcc_visible
39
40
        self.__bcc_author_visible = (bcc_trigg + bcc_visible
                                     + bcc_author_visible)
41
42
43
44
45
        self.__other_trigg = bcc_trigg + other_trigg
        self.__other_visible = bcc_trigg + other_trigg + other_visible
        for n in bcc_visible:
            if n not in self.__other_visible:
                self.__other_visible.append(n)
46
	self.__readable = bcc_trigg + other_trigg + rd
47

48
    def create(self, author, misc, conf, loc,
49
               rec_time = None, sent_by = None, sent_at = None):
50
51
52
        if misc == 15:
            trigg = self.__bcc_trigg
            visib = self.__bcc_visible
53
            author_visib = self.__bcc_author_visible
54
55
56
        else:
            trigg = self.__other_trigg
            visib = self.__other_visible
57
58
59
            author_visib = self.__other_visible
        return misc_group(misc, conf, loc, trigg, visib, author_visib,
                          self.__readable, rec_time, sent_by, sent_at, author)
60
61
62
63
64
65
66
67
68
69


def a_list_pattern(lst):
    if len(lst) == 0:
        return "0 \\\\\\*"
    else:
        return "%d { %s }" % (len(lst), string.join(lst, " "))

def a_list_rq(lst):
    return "%d { %s }" % (len(lst), string.join(lst, " "))
70

71
72
73
74
75
76
77
class sequence_allocator:
    def __init__(self, start):
        self.curr = start

    def next(self):
        self.curr = self.curr + 1
        return self.curr
78
79

class misc_group:
80
81
    def __init__(self, misc, conf, loc, trigg, visib, visib_by_author,
                 readable, rec_time, sent_by, sent_at, author):
82
83
84
85
86
        self.__misc = misc
        self.__conf = conf
        self.__loc = loc
        self.__trigg = trigg
        self.__visib = visib
87
        self.__visib_by_author = visib_by_author
88
	self.__readable = readable
89
90
91
        self.__rec_time = rec_time
        self.__sent_by = sent_by
        self.__sent_at = sent_at
92
93
94
95
96
97
        self.__author = author

    def visible(self, viewer):
        return (viewer in self.__visib
                or (viewer == self.__author
                    and viewer in self.__visib_by_author))
98

99
    def miscs(self, viewer):
100
        if not self.visible(viewer):
101
102
103
            return []
        res = ["%d %d" % (self.__misc, self.__conf),
               "6 %d" % self.__loc]
104
        if self.__rec_time != None:
105
            res.append("7 $any_time")
106
107
108
        if self.__sent_by != None:
            res.append("8 %d" % self.__sent_by)
        if self.__sent_at != None:
109
110
111
            res.append("9 $any_time")
        return res

112
113
    def async(self, viewer, text_no):
        if not self.visible(viewer):
114
            return None
115
        return "%d %d %d" % (text_no, self.__conf, self.__misc)
116

117
118
    def trigger(self, viewer):
        return viewer in self.__trigg
119
120
121

    def readable(self, viewer):
        return viewer in self.__readable
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
    def recipient(self):
        return self.__conf

    def recipient_type(self):
        return self.__misc


class text_stat:
    tno = sequence_allocator(0)

    def __init__(self, author):
        self.__author = author
        self.__misc_groups = []
        self.__text_no = self.tno.next()

    def add_misc_group(self, misc_grp):
        assert isinstance(misc_grp, misc_group)
        self.__misc_groups.append(misc_grp)

    def author(self):
        return self.__author

    def misc_groups(self):
        return self.__misc_groups[:]

    def text_no(self):
        return self.__text_no

    def remove_misc_group(self, removed):
        self.__misc_groups.remove(removed)

# (creat, rcpt): bcc-trigg, bcc-vis, bcc-author-vis, other-trigg, other-vis, rd
155
obj = { 
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
    (6,  6): factory([6], [], [7, 8, 9], [], [7, 8, 9], []),
    (6,  7): factory([], [6, 7], [8, 9], [], [8, 9], [7]),
    (6,  8): factory([8], [6], [7, 9], [], [7, 9], []),
    (6,  9): factory([9], [6], [7, 8], [], [7, 8], []),
    (6, 10): factory([7, 8], [6, 9], [], [], [], [6, 9]),
    (6, 11): factory([7, 8], [6], [9], [], [9], []),
    (6, 12): factory([7, 8], [6], [], [], [9], []),
    (6, 13): factory([7, 8], [6, 9], [], [], [], [6, 9]),
    (6, 14): factory([7, 8], [6, 9], [], [], [], [9]),
    (6, 15): factory([7, 8], [6, 9], [], [], [], [9]),

    (7,  6): factory([6], [7], [8, 9], [], [8, 9], []),
    (7,  7): factory([], [7], [6, 8, 9], [], [6, 8, 9], [7]),
    (7,  8): factory([8], [7], [6, 9], [], [6, 7, 9], []),
    (7,  9): factory([9], [7], [6, 8], [], [6, 7, 8], []),
    (7, 10): factory([7, 8], [6, 9], [], [], [], [6, 9]),
    (7, 11): factory([7, 8], [], [6, 9], [], [6, 9], []),
    (7, 12): factory([7, 8], [], [], [], [], []),
    (7, 13): factory([7, 8], [6, 9], [], [], [], [6, 9]),
    (7, 14): factory([7, 8], [9], [6], [], [6], [9]),
    (7, 15): factory([7, 8], [9], [], [], [], [9]),

    (8,  6): factory([6], [8], [7, 9], [], [7, 9], []),
    (8,  7): factory([], [7, 8], [6, 9], [], [6, 9], [7]),
    (8,  8): factory([8], [], [6, 7, 9], [], [6, 7, 9], []),
    (8,  9): factory([9], [8], [6, 7], [], [6, 7], []),
    (8, 10): factory([7, 8], [6, 9], [], [], [], [6, 9]),
    (8, 11): factory([7, 8], [], [6, 9], [], [6, 9], []),
    (8, 12): factory([7, 8], [], [], [], [], []),
    (8, 13): factory([7, 8], [6, 9], [], [], [], [6, 9]),
    (8, 14): factory([7, 8], [9], [6], [], [6], [9]),
    (8, 15): factory([7, 8], [9], [], [], [], [9]),

    (9,  6): factory([6], [9], [7, 8], [], [7, 8], []),
    (9,  7): factory([], [7, 9], [6, 8], [], [6, 8], [7]),
    (9,  8): factory([8], [9], [6, 7], [], [6, 7], []),
    (9,  9): factory([9], [], [6, 7, 8], [], [6, 7, 8], []),
    (9, 10): factory([7, 8], [6, 9], [], [], [], [6, 9]),
    (9, 11): factory([7, 8], [9], [6], [], [6], []),
    (9, 12): factory([7, 8], [9], [], [], [], []),
    (9, 13): factory([7, 8], [6, 9], [], [], [], [6, 9]),
    (9, 14): factory([7, 8], [9], [6], [], [6], [9]),
    (9, 15): factory([7, 8], [9], [], [], [], [9]),
199

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    }

class loc_no_allocator:
    def __init__(self):
        self.__prev = {}

    def alloc(self, conf):
        res = self.__prev.get(conf, 0) + 1
        self.__prev[conf] = res
        return res

locno = loc_no_allocator()

def talk_to(person):
    global active_person

    if active_person != person:
        print "talk_to client %d" % (person - 6)
        active_person = person

active_person = None

ref = sequence_allocator(999)
223
        
224
def create(author, recipients):
225
226
    print
    txt = text_stat(author)
227
228
    crea = []
    for (m, r) in recipients:
229
230
        txt.add_misc_group(obj[(author, r)].create(author, m, r,
                                                   locno.alloc(r)))
231
232
233
        crea.append("%d %d" % (m, r))
    talk_to(author)
    cs = ref.next()
234
    print "# Creating text %d by %d" % (txt.text_no(), txt.author())
235
    print "send \"%d 86 [holl \"text %d\"] %s 0 { }\\n\"" % (
236
237
        cs, txt.text_no(), a_list_rq(crea))
    new_text(txt)
238
    talk_to(author)
239
240
241
242
243
244
245
246
247
248
249
250
    print "simple_expect \"=%d %d\"" % (cs, txt.text_no())
    verify_text_stat(txt)
    return txt

def add_misc(adder, txt, misc_type, rcpt):
    sent_by = None
    if adder != txt.author():
        sent_by = adder
    misc = obj[(adder, rcpt)].create(txt.author(), misc_type, rcpt,
                                     locno.alloc(rcpt),
                                     sent_at = 1, sent_by = sent_by)
    talk_to(adder)
251
    cs = ref.next()
252
253
254
255
    print "# Adding recipient to text %d; adder %d" % (txt.text_no(), adder)
    print "send \"%d 30 %d %d %d\\n\"" % (cs, txt.text_no(), rcpt, misc_type)
    new_recipient(adder, txt, misc)
    talk_to(adder)
256
    print "simple_expect \"=%d\"" % cs
257
    txt.add_misc_group(misc)
258

259
def verify_text_stat(txt):
260
261
    for p in PERSONS:
        talk_to(p)
262
263
        print "send \"%d 90 %d\\n\"" % (ref.next(), txt.text_no())
        visib = p == txt.author()
264
        pattern = []
265
        for m in txt.misc_groups():
266
267
268
269
270
            pattern = pattern + m.miscs(p)
            if m.readable(p):
                visib = 1
        if visib:
            print "simple_expect \"=%d $any_time " \
271
                  "%d 0 %d 0 %s 0 \\\\\\*\"" % (
272
                ref.curr, txt.author(), len("text %d" % txt.text_no()),
273
                a_list_pattern(pattern))
274
        else:
275
            print "simple_expect \"%%%d 14 %d\"" % (ref.curr, txt.text_no())
276
277
278

PERSONS = range(6, 10)

279
def new_text(txt):
280
281
282
    for viewer in PERSONS:
        sent = 0
        pattern = []
283
        for m in txt.misc_groups():
284
285
286
287
288
289
            if m.trigger(viewer):
                sent = 1
            pattern = pattern + m.miscs(viewer)
        if sent:
            talk_to(viewer)
            print "simple_expect \":18 15 %d $any_time " \
290
                  "%d 0 %d 0 %s 0 \\\\\\*\"" % (
291
                txt.text_no(), txt.author(), len("text %d" % txt.text_no()),
292
                a_list_pattern(pattern))
293
        elif txt.author() != viewer and DEBUG:
294
295
296
297
            talk_to(viewer)
            print "send \"%d 35\\n\"" % ref.next()
            print "simple_expect \"=%d $any_time\"" % ref.curr

298
def new_recipient(author, txt, misc):
299
300
    for viewer in PERSONS:
        sent = 0
301
        for m in txt.misc_groups() + [misc]:
302
303
            if m.trigger(viewer):
                sent = 1
304
        async = misc.async(viewer, txt.text_no())
305
306
307
308
309
310
311
312
        if sent and async != None:
            talk_to(viewer)
            print "simple_expect \":3 16 %s\"" % async
        elif author != viewer and DEBUG:
            talk_to(viewer)
            print "send \"%d 35\\n\"" % ref.next()
            print "simple_expect \"=%d $any_time\"" % ref.curr

313
def delete(deleter, txt):
314
    nr = ref.next()
315
316
317
318
319
    talk_to(deleter)
    print "# Deleting text %d; deleter %d" % (txt.text_no(), deleter)
    print "send \"%d 29 %d\\n\"" % (nr, txt.text_no())
    deleted_text(deleter, txt)
    talk_to(deleter)
320
321
    print "simple_expect \"=%d\"" % nr

322
def deleted_text(deleter, txt):
323
324
325
    for viewer in PERSONS:
        sent = 0
        pattern = []
326
        for m in txt.misc_groups():
327
328
329
330
331
332
333
            if m.trigger(viewer):
                sent = 1
            pattern = pattern + m.miscs(viewer)
        if sent:
            talk_to(viewer)
            print "simple_expect \":18 14 %d $any_time " \
                  "%d 0 %d 0 %s 0 \\\\\\*\"" % (
334
                txt.text_no(), txt.author(), len("text %d" % txt.text_no()),
335
                a_list_pattern(pattern))
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
        elif deleter != viewer and DEBUG:
            talk_to(viewer)
            print "send \"%d 35\\n\"" % ref.next()
            print "simple_expect \"=%d $any_time\"" % ref.curr


def remove_misc(remover, txt, recip):
    for removed in txt.misc_groups():
        if removed.recipient() == recip:
            break
    else:
        raise 'no-such-recipient'
    nr = ref.next()
    talk_to(remover)
    print "# Removing recipient from text %d; remover %d" % (
        txt.text_no(), remover)
    print "send \"%d 31 %d %d\\n\"" % (nr, txt.text_no(), recip)
    sub_recipient(remover, txt, recip, removed.recipient_type())
    talk_to(remover)
    print "simple_expect \"=%d\"" % nr
    txt.remove_misc_group(removed)

def sub_recipient(remover, txt, recip, misc_type):
    for viewer in PERSONS:
        sent = 0
        for m in txt.misc_groups():
            if m.trigger(viewer):
                sent = 1
        if sent:
            talk_to(viewer)
            print "simple_expect \":3 17 %d %d %s\"" % (
                txt.text_no(), recip, misc_type)
        elif remover != viewer and DEBUG:
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
            talk_to(viewer)
            print "send \"%d 35\\n\"" % ref.next()
            print "simple_expect \"=%d $any_time\"" % ref.curr

def setup():
    print "lyskomd_start"
    for p in PERSONS:
        print "client_start %d" % (p - 6)
        talk_to(p)
        print "send \"A3Hfoo\\n\""
        print "simple_expect \"LysKOM\" \"connected %d\"" % p
        print "send \"%d 80 4 { 14 15 16 17 }\\n\"" % ref.next()
        print "simple_expect \"=%d\"" % ref.curr
        print "send \"%d 89 [holl \"Person %d\"] [holl \"foo\"] " \
              "00000000 0 { }\\n\"" % (ref.next(), p)
        print "simple_expect \"=%d %d\"" % (ref.curr, p)
        print "send \"%d 62 %d [holl \"foo\"] 0\\n\"" % (ref.next(), p)
        print "simple_expect \"=%d\"" % ref.curr 
    for (creator, conf, conf_type) in [
        (7, 10, "0000"),
        (7, 11, "1000"),
        (7, 12, "1010"),
        (9, 13, "0000"),
        (9, 14, "1000"),
        (9, 15, "1010"),
        ]:

        talk_to(creator)
        print "send \"%d 88 [holl \"conf %d\"] %s 0 { }\\n\"" % (
            ref.next(), conf, conf_type)
        print "simple_expect \"=%d %d\"" % (ref.curr, conf)
    talk_to(7)
    for conf in [10, 11, 12]:
        for reader in [7, 8]:
            print "send \"%d 100 %d %d 100 3 00000000\\n\"" % (
                ref.next(), conf, reader)
            print "simple_expect \"=%d\"" % ref.curr
    talk_to(9)
    for conf in [13, 14, 15]:
        for reader in [7, 8]:
            print "send \"%d 100 %d %d 100 6 00000000\\n\"" % (
                ref.next(), conf, reader)
            print "simple_expect \"=%d\"" % ref.curr
    talk_to(7)
    print "send \"%d 15 7 7\\n\"" % ref.next()
    print "simple_expect \"=%d\"" % ref.curr

def disco():
    for p in PERSONS[:-1]:
        talk_to(p)
        print "send \"%d 55 0\\n\"" % ref.next()
        print "simple_expect \"=%d\"" % ref.curr
        print "client_death %d" % (p - 6)
    talk_to(PERSONS[-1])
    print "send \"%d 62 5 [holl \"gazonk\"] 1\\n\"" % ref.next()
    print "simple_expect \"=%d\"" % ref.curr
    print "send \"%d 42 255\\n\"" % ref.next()
    print "simple_expect \"=%d\"" % ref.curr
    print "send \"%d 44 0\\n\"" % ref.next()
    print "simple_expect \"=%d\"" % ref.curr
    print "client_death %d" % (PERSONS[-1] - 6)
    print "lyskomd_death"

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
def simple_create_delete():
    print "send_user \"testing simple create+delete\\n\""
    for author in [6, 7, 8, 9]:
        for misc_type in [0, 1, 15]:
            for rcpt in [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]:
                if author in [6, 9] and rcpt in [12, 15]:
                    continue
                txt = create(author, [(misc_type, rcpt)])
                delete(author, txt)
                if not EXTENDED:
                    continue
                for second_misc_type in [0, 1, 15]:
                    for second_rcpt in [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]:
                        if rcpt == second_rcpt:
                            continue
                        if author in [6, 9] and second_rcpt in [12, 15]:
                            continue
                        txt = create(author, [
                            (misc_type, rcpt),
                            (second_misc_type, second_rcpt)])
                        delete(author, txt)

def simple_create_add_delete():
    print "send_user \"testing simple create+add+delete\\n\""

    for author in [6, 7, 8, 9]:
        for misc_type in [0, 1, 15]:
            for rcpt in [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]:
                if author in [6, 9] and rcpt in [12, 15]:
                    continue
                txt = create(author, []) 
                add_misc(author, txt, misc_type, rcpt)
                verify_text_stat(txt)
                delete(author, txt)
                if not EXTENDED:
                    continue
                for second_misc_type in [0, 1, 15]:
                    for second_rcpt in [6, 7, 8, 9, 10, 11, 12, 13, 14, 15]:
                        if rcpt == second_rcpt:
                            continue
                        if author in [6, 9] and second_rcpt in [12, 15]:
                            continue
                        txt = create(author, [])
                        add_misc(author, txt, misc_type, rcpt)
                        verify_text_stat(txt)
                        add_misc(author, txt, second_misc_type, second_rcpt)
                        verify_text_stat(txt)
                        delete(author, txt)

def special_cases():
    print "send_user \"testing some special cases\\n\""

    txt = create(7, [(0, 10)])
    add_misc(6, txt, 15, 8)
    verify_text_stat(txt)
    remove_misc(7, txt, 10)
    verify_text_stat(txt)
    delete(7, txt)

def generate_test():
    setup()
    simple_create_delete()
    simple_create_add_delete()
    special_cases()
    disco()

generate_test()