File: test_disonline.tcl

package info (click to toggle)
tkrat 1%3A2.2cvs20100105-true-dfsg-6.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,340 kB
  • ctags: 9,259
  • sloc: ansic: 96,057; tcl: 25,667; makefile: 1,638; sh: 282
file content (355 lines) | stat: -rw-r--r-- 9,086 bytes parent folder | download | duplicates (3)
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
puts "$HEAD Test disconnected folders in online mode"

namespace eval test_disonline {
    global start_uid

    variable uidmap {}
    variable uid_lastlocal 0
    variable uid_lastremote [expr $start_uid-1]
}

proc test_disonline::verify_map {mf map} {
    global tmp

    foreach e $map {
	set expected($e) 1
    }
    if {[catch {open $mf r} f]} {
	return 0
    }
    file copy -force $mf $tmp/map
    while {-1 != [gets $f line]} {
	if {[catch {unset expected($line)}]} {
	    close $f
	    return 0
	}
    }
    close $f
    if {0 != [array size expected]} {
	return 0
    }
    return 1
}

proc test_disonline::dis_verify {f map name {diff 0}} {
    variable uidmap

    set expected_map $uidmap
    set num [expr {[llength $uidmap]+$diff}]

    set i [lindex [$f info] 1]
    if {$num != $i} {
	ReportError "$name: Got $i expected $num"
    }
    if {![verify_map $map $expected_map]} {
	ReportError "$name: map verify failed"
    }
}

proc test_disonline::add_to_uidmap {} {
    variable uidmap
    variable uid_lastlocal
    variable uid_lastremote

    lappend uidmap [list [incr uid_lastremote] [incr uid_lastlocal]]
}

proc test_disonline::add_mixed_to_uidmap {} {
    variable uidmap
    variable uid_lastlocal
    variable uid_lastremote

    lappend uidmap [list [expr {$uid_lastremote+2}] [expr {$uid_lastlocal+1}]]
    lappend uidmap [list [expr {$uid_lastremote+1}] [expr {$uid_lastlocal+2}]]
    incr uid_lastlocal +2
    incr uid_lastremote +2
}

proc test_disonline::remove_from_uidmap {index} {
    variable uidmap

    set uidmap [lreplace $uidmap $index $index]
}

proc test_disonline::verify_flag {f index flag expected desc} {
    set real [$f getFlag $index $flag]
    if {$expected != $real} {
        ReportError "$desc: flag $flag was $real (expected $expected)"
    }
}

proc test_disonline::test_disonline {} {
    global dir errors mailServer dis_def imap_map imap_def \
	    msg1 msg2 msg3 msg4 msg5 msg6 msg7 msg8 msg9 msg10 \
	    msg11 msg12 msg13 msg14 msg15 msg16 msg17 msg18 msg19 msg20

    # Setup
    InitTestmsgs
    RatLibSetOnlineMode 1
    set tmpfn [pwd]/folder.[pid]-tmp
    set tmpdef [list Test file {} $tmpfn]

    init_imap_folder $imap_def
    insert_imap $imap_def $msg1
    add_to_uidmap

    StartTest "opening"
    set f [RatOpenFolder $dis_def]
    dis_verify $f $imap_map "Initial"

    StartTest "update after open"
    $f update update
    dis_verify $f $imap_map "After first update"

    StartTest "new mail arrival"
    insert_imap $imap_def $msg2
    add_to_uidmap
    $f update sync
    dis_verify $f $imap_map "After 1 new message"
    $f close

    StartTest "opening again"
    set f [RatOpenFolder $dis_def]
    dis_verify $f $imap_map "Second open"

    StartTest "new mail arrival again"
    insert_imap $imap_def $msg18
    add_to_uidmap
    $f update sync
    dis_verify $f $imap_map "After another new message"

    StartTest "multiple new messages"
    insert_imap $imap_def $msg3 $msg4
    add_to_uidmap
    add_to_uidmap
    after 300
    $f update update
    dis_verify $f $imap_map "After 2 new messages"

    StartTest "deleting message"
    $f setFlag 1 deleted 1
    $f update sync
    remove_from_uidmap 1
    dis_verify $f $imap_map "After deleting"

    StartTest "new message and one deleted"
    $f setFlag 1 deleted 1
    remove_from_uidmap 1
    insert_imap $imap_def $msg5
    $f update sync
    add_to_uidmap
    dis_verify $f $imap_map "After new & deleted"

    StartTest "inserting one message"
    set fh [open $tmpfn w]
    puts $fh $msg6
    close $fh
    set f2 [RatOpenFolder $tmpdef]
    $f2 list "%s"
    set m [$f2 get 0]
    $f insert $m
    $f2 close
    file delete $tmpfn
    after 300
    $f update sync
    add_to_uidmap
    dis_verify $f $imap_map "After inserting"

    StartTest "inserting one message two times (different)"
    set fh [open $tmpfn w]
    puts $fh $msg7
    puts $fh $msg8
    close $fh
    set f2 [RatOpenFolder $tmpdef]
    $f2 list "%s"
    $f insert [$f2 get 0]
    $f insert [$f2 get 1]
    $f2 close
    add_to_uidmap
    add_to_uidmap
    file delete $tmpfn
    $f update sync
    dis_verify $f $imap_map "After inserting one two times"

    StartTest "inserting two messages"
    set fh [open $tmpfn w]
    puts $fh $msg9
    puts $fh $msg10
    close $fh
    set f2 [RatOpenFolder $tmpdef]
    $f2 list "%s"
    $f insert [$f2 get 0] [$f2 get 1]
    $f2 close
    add_to_uidmap
    add_to_uidmap
    file delete $tmpfn
    $f update sync
    dis_verify $f $imap_map "After inserting two"

    StartTest "inserting two messages sequentially"
    set fh [open $tmpfn w]
    puts $fh $msg9
    puts $fh $msg10
    close $fh
    set f2 [RatOpenFolder $tmpdef]
    $f2 list "%s"
    $f insert [$f2 get 0]
    $f insert [$f2 get 1]
    $f2 close
    add_to_uidmap
    add_to_uidmap
    file delete $tmpfn
    $f close
    set f [RatOpenFolder $dis_def]
    $f update sync
    dis_verify $f $imap_map "After inserting two sequentially"

    StartTest "deleting inserted directly"
    set n [lindex [$f info] 1]
    set fh [open $tmpfn w]
    puts $fh $msg11
    close $fh
    set f2 [RatOpenFolder $tmpdef]
    $f2 list "%s"
    $f insert [$f2 get 0]
    $f2 close
    file delete $tmpfn
    $f setFlag $n deleted 1
    $f update sync
    add_to_uidmap
    remove_from_uidmap $n
    dis_verify $f $imap_map "After immediately deleted inserted"

    StartTest "insert, delete and new messages"
    set fh [open $tmpfn w]
    puts $fh $msg12
    close $fh
    set f2 [RatOpenFolder $tmpdef]
    $f2 list "%s"
    $f insert [$f2 get 0]
    add_to_uidmap
    $f2 close
    file delete $tmpfn
    $f setFlag 1 deleted 1
    remove_from_uidmap 1
    insert_imap $imap_def $msg13
    add_to_uidmap
    $f update sync
    dis_verify $f $imap_map "After insert, deleted and new"

    StartTest "resetting folder"
    for {set i 0} {$i <9} {incr i} {
	$f setFlag $i deleted 1
	remove_from_uidmap 0
    }
    insert_imap $imap_def $msg14
    add_to_uidmap
    $f update sync
    dis_verify $f $imap_map "After reset"

    StartTest "offline-->online transition"
    # Go offline
    RatLibSetOnlineMode 0
    $f update sync
    dis_verify $f $imap_map "When offline"
    RatLibSetOnlineMode 1
    dis_verify $f $imap_map "When online again"
    $f update sync
    dis_verify $f $imap_map "After sync"
 
    StartTest "offline-->online transition (with flag changes)"
    RatLibSetOnlineMode 0
    $f setFlag 1 seen 0
    $f setFlag 1 seen 1
    $f setFlag 1 flagged 1
    $f setFlag 1 flagged 0
    dis_verify $f $imap_map "When offline"
    verify_flag $f 1 seen 1 "When offline"
    verify_flag $f 1 flagged 0 "When offline"
    # Go online
    RatLibSetOnlineMode 1
    dis_verify $f $imap_map "When online"
    verify_flag $f 1 seen 1 "When online"
    verify_flag $f 1 flagged 0 "When online"
    $f update sync
    dis_verify $f $imap_map "After sync"
    verify_flag $f 1 seen 1 "After sync"
    verify_flag $f 1 flagged 0 "After sync"

    StartTest "offline-->online transition (with new remote messages)"
    RatLibSetOnlineMode 0
    # Insert two new in remote
    insert_imap $imap_def $msg15 $msg16
    $f update sync
    dis_verify $f $imap_map "When offline"
    # Go online
    add_to_uidmap
    add_to_uidmap
    RatLibSetOnlineMode 1
    dis_verify $f $imap_map "When online"

    StartTest "offline-->online transition (with new local messages)"
    RatLibSetOnlineMode 0
    # Insert one new in local
    set fh [open $tmpfn w]
    puts $fh $msg17
    puts $fh $msg18
    close $fh
    set f2 [RatOpenFolder $tmpdef]
    $f2 list "%s"
    $f insert [$f2 get 0] [$f2 get 1]
    $f2 close
    file delete $tmpfn
    dis_verify $f $imap_map "When offline" 2
    add_to_uidmap
    add_to_uidmap
    # Go online
    RatLibSetOnlineMode 1
    dis_verify $f $imap_map "When online"

    StartTest "offline-->online transition (with new messages in both)"
    RatLibSetOnlineMode 0
    # Insert one new in local
    set fh [open $tmpfn w]
    puts $fh $msg19
    close $fh
    set f2 [RatOpenFolder $tmpdef]
    $f2 list "%s"
    $f insert [$f2 get 0]
    $f2 close
    file delete $tmpfn
    # Insert one new in remote
    insert_imap $imap_def $msg20
    $f update sync
    dis_verify $f $imap_map "When offline" 1
    add_mixed_to_uidmap
    # Go online
    RatLibSetOnlineMode 1
    dis_verify $f $imap_map "When online"

    StartTest "offline-->online transition (with local delete)"
    RatLibSetOnlineMode 0
    # Set delete flag in lcoal
    # The seen flag manipulation here is to simulate what happens when one
    # reads a folder. And it did trigger a bug once.
    $f setFlag 1 seen 0
    $f setFlag 1 deleted 1
    $f setFlag 1 seen 1
    dis_verify $f $imap_map "When offline"
    # Go online
    RatLibSetOnlineMode 1
    dis_verify $f $imap_map "When online"
    $f update sync
    remove_from_uidmap 1
    dis_verify $f $imap_map "After sync"

    # Cleanup
    $f close
    cleanup_imap_folder $imap_def
    file delete -force $dir/disconnected

    # Restore environment
    RatLibSetOnlineMode 0
}

test_disonline::test_disonline