rims 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/ChangeLog +379 -0
  4. data/Gemfile +11 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +566 -0
  7. data/Rakefile +29 -0
  8. data/bin/rims +11 -0
  9. data/lib/rims.rb +45 -0
  10. data/lib/rims/auth.rb +133 -0
  11. data/lib/rims/cksum_kvs.rb +68 -0
  12. data/lib/rims/cmd.rb +809 -0
  13. data/lib/rims/daemon.rb +338 -0
  14. data/lib/rims/db.rb +793 -0
  15. data/lib/rims/error.rb +23 -0
  16. data/lib/rims/gdbm_kvs.rb +76 -0
  17. data/lib/rims/hash_kvs.rb +66 -0
  18. data/lib/rims/kvs.rb +101 -0
  19. data/lib/rims/lock.rb +151 -0
  20. data/lib/rims/mail_store.rb +663 -0
  21. data/lib/rims/passwd.rb +251 -0
  22. data/lib/rims/pool.rb +88 -0
  23. data/lib/rims/protocol.rb +71 -0
  24. data/lib/rims/protocol/decoder.rb +1469 -0
  25. data/lib/rims/protocol/parser.rb +1114 -0
  26. data/lib/rims/rfc822.rb +456 -0
  27. data/lib/rims/server.rb +567 -0
  28. data/lib/rims/test.rb +391 -0
  29. data/lib/rims/version.rb +11 -0
  30. data/load_test/Rakefile +93 -0
  31. data/rims.gemspec +38 -0
  32. data/test/test_auth.rb +174 -0
  33. data/test/test_cksum_kvs.rb +121 -0
  34. data/test/test_config.rb +533 -0
  35. data/test/test_daemon_status_file.rb +169 -0
  36. data/test/test_daemon_waitpid.rb +72 -0
  37. data/test/test_db.rb +602 -0
  38. data/test/test_db_recovery.rb +732 -0
  39. data/test/test_error.rb +97 -0
  40. data/test/test_gdbm_kvs.rb +32 -0
  41. data/test/test_hash_kvs.rb +116 -0
  42. data/test/test_lock.rb +161 -0
  43. data/test/test_mail_store.rb +750 -0
  44. data/test/test_passwd.rb +203 -0
  45. data/test/test_protocol.rb +91 -0
  46. data/test/test_protocol_auth.rb +121 -0
  47. data/test/test_protocol_decoder.rb +6490 -0
  48. data/test/test_protocol_fetch.rb +994 -0
  49. data/test/test_protocol_request.rb +332 -0
  50. data/test/test_protocol_search.rb +974 -0
  51. data/test/test_rfc822.rb +696 -0
  52. metadata +174 -0
@@ -0,0 +1,169 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'fileutils'
4
+ require 'pp' if $DEBUG
5
+ require 'rims'
6
+ require 'test/unit'
7
+
8
+ module RIMS::Test
9
+ class DaemonStatusFileTest < Test::Unit::TestCase
10
+ def setup
11
+ @stat_path = "status.#{$$}"
12
+ @stat_file = RIMS::Daemon.new_status_file(@stat_path, exclusive: true)
13
+ @read_file = RIMS::Daemon.new_status_file(@stat_path, exclusive: false)
14
+ @ready_spin_lock = "ready_spin_lock.#{$$}"
15
+ @final_spin_lock = "final_spin_lock.#{$$}"
16
+ end
17
+
18
+ def teardown
19
+ FileUtils.rm_f(@stat_path)
20
+ FileUtils.rm_f(@ready_spin_lock)
21
+ FileUtils.rm_f(@final_spin_lock)
22
+ end
23
+
24
+ def test_lock_status
25
+ @stat_file.open{
26
+ assert_equal(false, @stat_file.locked?)
27
+ @stat_file.synchronize{
28
+ assert_equal(true, @stat_file.locked?)
29
+ }
30
+ assert_equal(false, @stat_file.locked?)
31
+ }
32
+ end
33
+
34
+ def test_should_be_locked
35
+ assert_raise(RuntimeError) {
36
+ @stat_file.should_be_locked
37
+ }
38
+
39
+ @stat_file.open{
40
+ @stat_file.synchronize{
41
+ @stat_file.should_be_locked
42
+ }
43
+ }
44
+ end
45
+
46
+ def test_should_not_be_locked
47
+ @stat_file.should_not_be_locked
48
+
49
+ @stat_file.open{
50
+ @stat_file.synchronize{
51
+ assert_raise(RuntimeError) {
52
+ @stat_file.should_not_be_locked
53
+ }
54
+ }
55
+ }
56
+ end
57
+
58
+ def test_lock_guard
59
+ @stat_file.open{
60
+ @stat_file.synchronize{
61
+ assert_raise(RuntimeError) {
62
+ @stat_file.lock
63
+ }
64
+ }
65
+ }
66
+ end
67
+
68
+ def test_unlock_guard
69
+ @stat_file.open{
70
+ assert_raise(RuntimeError) {
71
+ @stat_file.unlock
72
+ }
73
+ }
74
+ end
75
+
76
+ def another_process_exclusive_lock(write_text: nil)
77
+ FileUtils.touch(@ready_spin_lock)
78
+ FileUtils.touch(@final_spin_lock)
79
+
80
+ pid = Process.fork{
81
+ lock_file = RIMS::Daemon.new_status_file(@stat_path, exclusive: true)
82
+ lock_file.open{
83
+ lock_file.synchronize{
84
+ lock_file.write(write_text) if write_text
85
+ FileUtils.rm_f(@ready_spin_lock)
86
+ while (File.exist? @final_spin_lock)
87
+ # nothing to do.
88
+ end
89
+ }
90
+ }
91
+ exit!
92
+ }
93
+
94
+ while (File.exist? @ready_spin_lock)
95
+ # nothing to do.
96
+ end
97
+
98
+ begin
99
+ yield
100
+ ensure
101
+ FileUtils.rm_f(@final_spin_lock)
102
+ Process.waitpid(pid)
103
+ end
104
+ end
105
+ private :another_process_exclusive_lock
106
+
107
+ def test_exclusive_lock
108
+ another_process_exclusive_lock{
109
+ @stat_file.open{
110
+ assert_raise(RuntimeError) {
111
+ @stat_file.synchronize{
112
+ flunk("don't reach here.")
113
+ }
114
+ }
115
+ }
116
+ }
117
+ end
118
+
119
+ def test_readable_lock_status
120
+ assert_raise(Errno::ENOENT) {
121
+ @read_file.open
122
+ }
123
+
124
+ another_process_exclusive_lock{
125
+ @read_file.open{
126
+ assert_equal(true, @read_file.locked?)
127
+ }
128
+ }
129
+
130
+ @read_file.open{
131
+ assert_equal(false, @read_file.locked?)
132
+ }
133
+ end
134
+
135
+ def test_readable_should_be_locked
136
+ another_process_exclusive_lock{
137
+ @read_file.open{
138
+ @read_file.should_be_locked
139
+ }
140
+ }
141
+
142
+ @read_file.open{
143
+ assert_raise(RuntimeError) {
144
+ @read_file.should_be_locked
145
+ }
146
+ }
147
+ end
148
+
149
+ def test_write_read
150
+ another_process_exclusive_lock(write_text: "pid: #{$$}") {
151
+ @read_file.open{
152
+ assert_equal("pid: #{$$}", @read_file.read)
153
+ assert_equal("pid: #{$$}", @read_file.read, 'rewind')
154
+ }
155
+ }
156
+
157
+ @read_file.open{
158
+ assert_raise(RuntimeError) {
159
+ @read_file.read
160
+ }
161
+ }
162
+ end
163
+ end
164
+ end
165
+
166
+ # Local Variables:
167
+ # mode: Ruby
168
+ # indent-tabs-mode: nil
169
+ # End:
@@ -0,0 +1,72 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'pp' if $DEBUG
4
+ require 'test/unit'
5
+
6
+ module RIMS::Test
7
+ class DaemonWaitpidTest < Test::Unit::TestCase
8
+ def setup
9
+ @child_pid_list = []
10
+ end
11
+
12
+ def teardown
13
+ for pid in @child_pid_list
14
+ begin
15
+ unless (Process.waitpid(pid, Process::WNOHANG)) then
16
+ Process.kill('KILL', pid)
17
+ Process.wait
18
+ end
19
+ rescue SystemCallError
20
+ next
21
+ end
22
+ end
23
+ end
24
+
25
+ def fork_child_process
26
+ latch_in, latch_out = IO.pipe
27
+
28
+ pid = fork{
29
+ latch_out.close
30
+ latch_in.gets
31
+ yield
32
+ exit!
33
+ }
34
+ @child_pid_list << pid
35
+ latch_in.close
36
+
37
+ return latch_out, pid
38
+ end
39
+ private :fork_child_process
40
+
41
+ def until_child_process_exit
42
+ until (pid = yield)
43
+ # nothing to do.
44
+ end
45
+
46
+ pid
47
+ end
48
+ private :until_child_process_exit
49
+
50
+ def test_waitpid
51
+ latch_out1, pid1 = fork_child_process{ exit!(0) }
52
+ latch_out2, pid2 = fork_child_process{ exit!(1) }
53
+ latch_out3, pid3 = fork_child_process{ exit!(2) }
54
+
55
+ assert_nil(Process.waitpid(-1, Process::WNOHANG))
56
+
57
+ latch_out3.puts
58
+ assert_equal(pid3, until_child_process_exit{ Process.waitpid(-1) })
59
+
60
+ latch_out1.puts
61
+ assert_equal(pid1, until_child_process_exit{ Process.waitpid(-1) })
62
+
63
+ latch_out2.puts
64
+ assert_equal(pid2, until_child_process_exit{ Process.waitpid(-1) })
65
+ end
66
+ end
67
+ end
68
+
69
+ # Local Variables:
70
+ # mode: Ruby
71
+ # indent-tabs-mode: nil
72
+ # End:
@@ -0,0 +1,602 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'pp' if $DEBUG
4
+ require 'rims'
5
+ require 'set'
6
+ require 'test/unit'
7
+
8
+ module RIMS::Test
9
+ class DBMetaTest < Test::Unit::TestCase
10
+ def setup
11
+ @kvs = {}
12
+ @db = RIMS::DB::Meta.new(RIMS::Hash_KeyValueStore.new(@kvs))
13
+ end
14
+
15
+ def teardown
16
+ pp @kvs if $DEBUG
17
+ end
18
+
19
+ def test_dirty
20
+ assert_equal(false, @db.dirty?)
21
+
22
+ @db.dirty = true
23
+ assert_equal(true, @db.dirty?)
24
+
25
+ @db.dirty = false
26
+ assert_equal(false, @db.dirty?)
27
+ end
28
+
29
+ def test_cnum
30
+ assert_equal(0, @db.cnum)
31
+ assert_equal(0, @db.cnum_succ!)
32
+ assert_equal(1, @db.cnum)
33
+ end
34
+
35
+ def test_msg_id
36
+ assert_equal(0, @db.msg_id)
37
+ assert_equal(0, @db.msg_id_succ!)
38
+ assert_equal(1, @db.msg_id)
39
+ end
40
+
41
+ def test_uidvalidity
42
+ assert_equal(1, @db.uidvalidity)
43
+ assert_equal(1, @db.uidvalidity_succ!)
44
+ assert_equal(2, @db.uidvalidity)
45
+ end
46
+
47
+ def test_mbox
48
+ assert_equal(1, @db.uidvalidity)
49
+ assert_equal([], @db.each_mbox_id.to_a)
50
+ assert_nil(@db.mbox_name(1))
51
+ assert_nil(@db.mbox_id('foo'))
52
+
53
+ id = @db.add_mbox('foo')
54
+ assert_equal(1, id)
55
+
56
+ assert_equal(2, @db.uidvalidity)
57
+ assert_equal([ 1 ], @db.each_mbox_id.to_a)
58
+ assert_equal('foo', @db.mbox_name(1))
59
+ assert_equal(1, @db.mbox_id('foo'))
60
+
61
+ assert_nil(@db.rename_mbox(1, 'foo'))
62
+
63
+ assert_equal(2, @db.uidvalidity)
64
+ assert_equal([ 1 ], @db.each_mbox_id.to_a)
65
+ assert_equal('foo', @db.mbox_name(1))
66
+ assert_equal(1, @db.mbox_id('foo'))
67
+
68
+ assert_not_nil(@db.rename_mbox(1, 'bar'))
69
+
70
+ assert_equal(2, @db.uidvalidity)
71
+ assert_equal([ 1 ], @db.each_mbox_id.to_a)
72
+ assert_equal('bar', @db.mbox_name(1))
73
+ assert_nil(@db.mbox_id('foo'))
74
+ assert_equal(1, @db.mbox_id('bar'))
75
+
76
+ assert_nil(@db.del_mbox(2))
77
+
78
+ assert_equal(2, @db.uidvalidity)
79
+ assert_equal([ 1 ], @db.each_mbox_id.to_a)
80
+ assert_equal('bar', @db.mbox_name(1))
81
+ assert_nil(@db.mbox_id('foo'))
82
+ assert_equal(1, @db.mbox_id('bar'))
83
+
84
+ assert_not_nil(@db.del_mbox(1))
85
+
86
+ assert_equal(2, @db.uidvalidity)
87
+ assert_equal([], @db.each_mbox_id.to_a)
88
+ assert_nil(@db.mbox_name(1))
89
+ assert_nil(@db.mbox_id('foo'))
90
+ assert_nil(@db.mbox_id('bar'))
91
+
92
+ id = @db.add_mbox('baz')
93
+ assert_equal(2, id)
94
+
95
+ assert_equal(3, @db.uidvalidity)
96
+ assert_equal([ 2 ], @db.each_mbox_id.to_a)
97
+ assert_nil(@db.mbox_name(1))
98
+ assert_equal('baz', @db.mbox_name(2))
99
+ assert_nil(@db.mbox_id('foo'))
100
+ assert_nil(@db.mbox_id('bar'))
101
+ assert_equal(2, @db.mbox_id('baz'))
102
+
103
+ id = @db.add_mbox('foo', mbox_id: 1)
104
+ assert_equal(1, id)
105
+
106
+ assert_equal(3, @db.uidvalidity)
107
+ assert_equal([ 1, 2 ], @db.each_mbox_id.sort)
108
+ assert_equal('foo', @db.mbox_name(1))
109
+ assert_equal('baz', @db.mbox_name(2))
110
+ assert_equal(1, @db.mbox_id('foo'))
111
+ assert_nil(@db.mbox_id('bar'))
112
+ assert_equal(2, @db.mbox_id('baz'))
113
+
114
+ id = @db.add_mbox('bar', mbox_id: 5)
115
+ assert_equal(5, id)
116
+
117
+ assert_equal(6, @db.uidvalidity)
118
+ assert_equal([ 1, 2, 5 ], @db.each_mbox_id.sort)
119
+ assert_equal('foo', @db.mbox_name(1))
120
+ assert_equal('baz', @db.mbox_name(2))
121
+ assert_equal('bar', @db.mbox_name(5))
122
+ assert_equal(1, @db.mbox_id('foo'))
123
+ assert_equal(5, @db.mbox_id('bar'))
124
+ assert_equal(2, @db.mbox_id('baz'))
125
+ end
126
+
127
+ def test_mbox_uid
128
+ id = @db.add_mbox('foo')
129
+ assert_equal(1, @db.mbox_uid(id))
130
+ assert_equal(1, @db.mbox_uid_succ!(id))
131
+ assert_equal(2, @db.mbox_uid(id))
132
+ end
133
+
134
+ def test_mbox_msg_num
135
+ id = @db.add_mbox('foo')
136
+ assert_equal(0, @db.mbox_msg_num(id))
137
+ @db.mbox_msg_num_increment(id)
138
+ assert_equal(1, @db.mbox_msg_num(id))
139
+ @db.mbox_msg_num_decrement(id)
140
+ assert_equal(0, @db.mbox_msg_num(id))
141
+ end
142
+
143
+ def test_mbox_flags
144
+ id = @db.add_mbox('foo')
145
+ assert_equal(0, @db.mbox_flag_num(id, 'flagged'))
146
+ @db.mbox_flag_num_increment(id, 'flagged')
147
+ assert_equal(1, @db.mbox_flag_num(id, 'flagged'))
148
+ @db.mbox_flag_num_decrement(id, 'flagged')
149
+ assert_equal(0, @db.mbox_flag_num(id, 'flagged'))
150
+ @db.mbox_flag_num_increment(id, 'flagged')
151
+ @db.mbox_flag_num_increment(id, 'flagged')
152
+ assert_equal(2, @db.mbox_flag_num(id, 'flagged'))
153
+ assert_not_nil(@db.clear_mbox_flag_num(id, 'flagged'))
154
+ assert_equal(0, @db.mbox_flag_num(id, 'flagged'))
155
+ assert_nil(@db.clear_mbox_flag_num(id, 'flagged'))
156
+ assert_equal(0, @db.mbox_flag_num(id, 'flagged'))
157
+ end
158
+
159
+ def test_msg_date
160
+ t = Time.mktime(2014, 3, 7, 18, 15, 56)
161
+ @db.set_msg_date(0, t)
162
+ assert_equal(t, @db.msg_date(0))
163
+ assert_not_nil(@db.clear_msg_date(0))
164
+ assert_nil(@db.clear_msg_date(0))
165
+ end
166
+
167
+ def test_msg_flag
168
+ assert_equal(false, @db.msg_flag(0, 'recent'))
169
+ assert_equal(false, @db.msg_flag(0, 'seen'))
170
+
171
+ @db.set_msg_flag(0, 'recent', true)
172
+ assert_equal(true, @db.msg_flag(0, 'recent'))
173
+ assert_equal(false, @db.msg_flag(0, 'seen'))
174
+
175
+ @db.set_msg_flag(0, 'seen', true)
176
+ assert_equal(true, @db.msg_flag(0, 'recent'))
177
+ assert_equal(true, @db.msg_flag(0, 'seen'))
178
+
179
+ @db.set_msg_flag(0, 'recent', false)
180
+ assert_equal(false, @db.msg_flag(0, 'recent'))
181
+ assert_equal(true, @db.msg_flag(0, 'seen'))
182
+
183
+ assert_not_nil(@db.clear_msg_flag(0))
184
+ assert_equal(false, @db.msg_flag(0, 'recent'))
185
+ assert_equal(false, @db.msg_flag(0, 'seen'))
186
+
187
+ assert_nil(@db.clear_msg_flag(0))
188
+ assert_equal(false, @db.msg_flag(0, 'recent'))
189
+ assert_equal(false, @db.msg_flag(0, 'seen'))
190
+ end
191
+
192
+ def test_msg_mbox_uid_mapping
193
+ assert_equal(1, @db.add_mbox('INBOX'))
194
+ assert_equal(2, @db.add_mbox('foo'))
195
+
196
+ assert_equal({}, @db.msg_mbox_uid_mapping(0))
197
+
198
+ assert_equal(1, @db.add_msg_mbox_uid(0, 1))
199
+ assert_equal({ 1 => [ 1 ].to_set
200
+ }, @db.msg_mbox_uid_mapping(0))
201
+
202
+ assert_equal(2, @db.add_msg_mbox_uid(0, 1))
203
+ assert_equal({ 1 => [ 1, 2 ].to_set
204
+ }, @db.msg_mbox_uid_mapping(0))
205
+
206
+ assert_equal(1, @db.add_msg_mbox_uid(0, 2))
207
+ assert_equal({ 1 => [ 1, 2 ].to_set,
208
+ 2 => [ 1 ].to_set
209
+ }, @db.msg_mbox_uid_mapping(0))
210
+
211
+ assert_nil(@db.del_msg_mbox_uid(0, 2, 2))
212
+ assert_equal({ 1 => [ 1, 2 ].to_set,
213
+ 2 => [ 1 ].to_set
214
+ }, @db.msg_mbox_uid_mapping(0))
215
+
216
+ assert_equal({ 1 => [ 1, 2 ].to_set
217
+ }, @db.del_msg_mbox_uid(0, 2, 1))
218
+ assert_equal({ 1 => [ 1, 2 ].to_set
219
+ }, @db.msg_mbox_uid_mapping(0))
220
+
221
+ assert_nil(@db.del_msg_mbox_uid(0, 2, 1))
222
+ assert_equal({ 1 => [ 1, 2 ].to_set
223
+ }, @db.msg_mbox_uid_mapping(0))
224
+
225
+ assert_equal({ 1 => [ 2 ].to_set
226
+ }, @db.del_msg_mbox_uid(0, 1, 1))
227
+ assert_equal({ 1 => [ 2 ].to_set
228
+ }, @db.msg_mbox_uid_mapping(0))
229
+
230
+ assert_equal({}, @db.del_msg_mbox_uid(0, 1, 2))
231
+ assert_equal({}, @db.msg_mbox_uid_mapping(0))
232
+
233
+ assert_not_nil(@db.clear_msg_mbox_uid_mapping(0))
234
+ assert_equal({}, @db.msg_mbox_uid_mapping(0))
235
+
236
+ assert_nil(@db.clear_msg_mbox_uid_mapping(0))
237
+ assert_equal({}, @db.msg_mbox_uid_mapping(0))
238
+ end
239
+
240
+ def test_mbox_msg_num_auto_increment_decrement
241
+ inbox_id = @db.add_mbox('INBOX')
242
+ foo_id = @db.add_mbox('foo')
243
+
244
+ msg_a = 0
245
+ msg_b = 1
246
+
247
+ assert_equal(0, @db.mbox_msg_num(inbox_id))
248
+ assert_equal(0, @db.mbox_msg_num(foo_id))
249
+
250
+ assert_equal(1, @db.add_msg_mbox_uid(msg_a, inbox_id))
251
+
252
+ assert_equal(1, @db.mbox_msg_num(inbox_id))
253
+ assert_equal(0, @db.mbox_msg_num(foo_id))
254
+
255
+ assert_equal(2, @db.add_msg_mbox_uid(msg_b, inbox_id))
256
+
257
+ assert_equal(2, @db.mbox_msg_num(inbox_id))
258
+ assert_equal(0, @db.mbox_msg_num(foo_id))
259
+
260
+ assert_equal(3, @db.add_msg_mbox_uid(msg_a, inbox_id))
261
+
262
+ assert_equal(3, @db.mbox_msg_num(inbox_id))
263
+ assert_equal(0, @db.mbox_msg_num(foo_id))
264
+
265
+ assert_equal(1, @db.add_msg_mbox_uid(msg_a, foo_id))
266
+
267
+ assert_equal(3, @db.mbox_msg_num(inbox_id))
268
+ assert_equal(1, @db.mbox_msg_num(foo_id))
269
+
270
+ assert_equal({ inbox_id => [ 1, 3 ].to_set,
271
+ foo_id => [ 1 ].to_set
272
+ }, @db.msg_mbox_uid_mapping(msg_a))
273
+ assert_equal({ inbox_id => [ 2 ].to_set,
274
+ }, @db.msg_mbox_uid_mapping(msg_b))
275
+
276
+ @db.del_msg_mbox_uid(msg_a, inbox_id, 1)
277
+
278
+ assert_equal(2, @db.mbox_msg_num(inbox_id))
279
+ assert_equal(1, @db.mbox_msg_num(foo_id))
280
+
281
+ @db.del_msg_mbox_uid(msg_a, foo_id, 1)
282
+
283
+ assert_equal(2, @db.mbox_msg_num(inbox_id))
284
+ assert_equal(0, @db.mbox_msg_num(foo_id))
285
+
286
+ @db.del_msg_mbox_uid(msg_b, inbox_id, 2)
287
+
288
+ assert_equal(1, @db.mbox_msg_num(inbox_id))
289
+ assert_equal(0, @db.mbox_msg_num(foo_id))
290
+
291
+ assert_equal({ inbox_id => [ 3 ].to_set,
292
+ }, @db.msg_mbox_uid_mapping(msg_a))
293
+ assert_equal({}, @db.msg_mbox_uid_mapping(msg_b))
294
+ end
295
+
296
+ def test_mbox_flag_num_auto_increment_decrement
297
+ inbox_id = @db.add_mbox('INBOX')
298
+ foo_id = @db.add_mbox('foo')
299
+
300
+ msg_a = 0
301
+ msg_b = 1
302
+
303
+ assert_equal({}, @db.msg_mbox_uid_mapping(msg_a))
304
+ assert_equal({}, @db.msg_mbox_uid_mapping(msg_b))
305
+
306
+ assert_equal([ 0, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
307
+ assert_equal([ 0, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
308
+
309
+ assert_equal(1, @db.add_msg_mbox_uid(msg_a, inbox_id))
310
+ assert_equal({ inbox_id => [ 1 ].to_set
311
+ }, @db.msg_mbox_uid_mapping(msg_a))
312
+
313
+ assert_equal([ 0, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
314
+ assert_equal([ 0, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
315
+
316
+ @db.set_msg_flag(msg_a, 'recent', true)
317
+
318
+ assert_equal([ 1, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
319
+ assert_equal([ 0, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
320
+
321
+ assert_equal(1, @db.add_msg_mbox_uid(msg_a, foo_id))
322
+ assert_equal({ inbox_id => [ 1 ].to_set,
323
+ foo_id => [ 1 ].to_set
324
+ }, @db.msg_mbox_uid_mapping(msg_a))
325
+
326
+ assert_equal([ 1, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
327
+ assert_equal([ 1, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
328
+
329
+ @db.set_msg_flag(msg_a, 'seen', true)
330
+
331
+ assert_equal([ 1, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
332
+ assert_equal([ 1, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
333
+
334
+ @db.set_msg_flag(msg_b, 'recent', true)
335
+
336
+ assert_equal([ 1, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
337
+ assert_equal([ 1, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
338
+
339
+ assert_equal(2, @db.add_msg_mbox_uid(msg_b, inbox_id))
340
+ assert_equal({ inbox_id => [ 2 ].to_set
341
+ }, @db.msg_mbox_uid_mapping(msg_b))
342
+
343
+ assert_equal([ 2, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
344
+ assert_equal([ 1, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
345
+
346
+ assert_equal(3, @db.add_msg_mbox_uid(msg_b, inbox_id))
347
+ assert_equal({ inbox_id => [ 2, 3 ].to_set
348
+ }, @db.msg_mbox_uid_mapping(msg_b))
349
+
350
+ assert_equal([ 3, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
351
+ assert_equal([ 1, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
352
+
353
+ @db.set_msg_flag(msg_b, 'seen', true)
354
+
355
+ assert_equal([ 3, 3 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
356
+ assert_equal([ 1, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
357
+
358
+ @db.set_msg_flag(msg_a, 'recent', false)
359
+
360
+ assert_equal([ 2, 3 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
361
+ assert_equal([ 0, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
362
+
363
+ @db.set_msg_flag(msg_b, 'recent', false)
364
+
365
+ assert_equal([ 0, 3 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
366
+ assert_equal([ 0, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
367
+
368
+ @db.del_msg_mbox_uid(msg_a, inbox_id, 1)
369
+ assert_equal({ foo_id => [ 1 ].to_set
370
+ }, @db.msg_mbox_uid_mapping(msg_a))
371
+
372
+ assert_equal([ 0, 2 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
373
+ assert_equal([ 0, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
374
+
375
+ @db.del_msg_mbox_uid(msg_b, inbox_id, 2)
376
+ assert_equal({ inbox_id => [ 3 ].to_set
377
+ }, @db.msg_mbox_uid_mapping(msg_b))
378
+
379
+ assert_equal([ 0, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
380
+ assert_equal([ 0, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
381
+
382
+ @db.del_msg_mbox_uid(msg_a, foo_id, 1)
383
+ assert_equal({}, @db.msg_mbox_uid_mapping(msg_a))
384
+
385
+ assert_equal([ 0, 1 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
386
+ assert_equal([ 0, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
387
+
388
+ @db.del_msg_mbox_uid(msg_b, inbox_id, 3)
389
+ assert_equal({}, @db.msg_mbox_uid_mapping(msg_b))
390
+
391
+ assert_equal([ 0, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(inbox_id, name) })
392
+ assert_equal([ 0, 0 ], %w[ recent seen ].map{|name| @db.mbox_flag_num(foo_id, name) })
393
+ end
394
+ end
395
+
396
+ class DBMessageTest < Test::Unit::TestCase
397
+ def setup
398
+ @kvs = {}
399
+ @db = RIMS::DB::Message.new(RIMS::Hash_KeyValueStore.new(@kvs))
400
+ end
401
+
402
+ def teardown
403
+ pp @kvs if $DEBUG
404
+ end
405
+
406
+ def test_msg
407
+ assert_equal([], @db.each_msg_id.to_a)
408
+ assert_nil(@db.msg_text(0))
409
+ assert_nil(@db.msg_text(1))
410
+ assert_nil(@db.msg_text(2))
411
+ assert_equal(false, (@db.msg_exist? 0))
412
+ assert_equal(false, (@db.msg_exist? 1))
413
+ assert_equal(false, (@db.msg_exist? 2))
414
+
415
+ @db.add_msg(0, 'foo')
416
+ assert_equal([ 0 ], @db.each_msg_id.to_a)
417
+ assert_equal('foo', @db.msg_text(0))
418
+ assert_nil(@db.msg_text(1))
419
+ assert_nil(@db.msg_text(2))
420
+ assert_equal(true, (@db.msg_exist? 0))
421
+ assert_equal(false, (@db.msg_exist? 1))
422
+ assert_equal(false, (@db.msg_exist? 2))
423
+
424
+ @db.add_msg(1, 'bar')
425
+ assert_equal([ 0, 1 ], @db.each_msg_id.to_a)
426
+ assert_equal('foo', @db.msg_text(0))
427
+ assert_equal('bar', @db.msg_text(1))
428
+ assert_nil(@db.msg_text(2))
429
+ assert_equal(true, (@db.msg_exist? 0))
430
+ assert_equal(true, (@db.msg_exist? 1))
431
+ assert_equal(false, (@db.msg_exist? 2))
432
+
433
+ @db.add_msg(2, 'baz')
434
+ assert_equal([ 0, 1, 2 ], @db.each_msg_id.to_a)
435
+ assert_equal('foo', @db.msg_text(0))
436
+ assert_equal('bar', @db.msg_text(1))
437
+ assert_equal('baz', @db.msg_text(2))
438
+ assert_equal(true, (@db.msg_exist? 0))
439
+ assert_equal(true, (@db.msg_exist? 1))
440
+ assert_equal(true, (@db.msg_exist? 2))
441
+
442
+ @db.del_msg(1)
443
+ assert_equal([ 0, 2 ], @db.each_msg_id.to_a)
444
+ assert_equal('foo', @db.msg_text(0))
445
+ assert_nil(@db.msg_text(1))
446
+ assert_equal('baz', @db.msg_text(2))
447
+ assert_equal(true, (@db.msg_exist? 0))
448
+ assert_equal(false, (@db.msg_exist? 1))
449
+ assert_equal(true, (@db.msg_exist? 2))
450
+
451
+ assert_raise(RuntimeError) { @db.del_msg(1) }
452
+ end
453
+ end
454
+
455
+ class DBMailboxTest < Test::Unit::TestCase
456
+ def setup
457
+ @kvs = {}
458
+ @db = RIMS::DB::Mailbox.new(RIMS::Hash_KeyValueStore.new(@kvs))
459
+ end
460
+
461
+ def teardown
462
+ pp @kvs if $DEBUG
463
+ end
464
+
465
+ def test_msg
466
+ assert_equal([], @db.each_msg_uid.to_a)
467
+ [ [ 1, false, nil, nil ],
468
+ [ 2, false, nil, nil ],
469
+ [ 3, false, nil, nil ]
470
+ ].each do |uid, exist, msg_id, deleted|
471
+ assert_equal(exist, (@db.msg_exist? uid))
472
+ assert_equal(msg_id, @db.msg_id(uid))
473
+ assert_equal(deleted, @db.msg_flag_deleted(uid))
474
+ end
475
+
476
+ @db.add_msg(1, 0)
477
+
478
+ assert_equal([ 1 ], @db.each_msg_uid.to_a)
479
+ [ [ 1, true, 0, false ],
480
+ [ 2, false, nil, nil ],
481
+ [ 3, false, nil, nil ]
482
+ ].each do |uid, exist, msg_id, deleted|
483
+ assert_equal(exist, (@db.msg_exist? uid))
484
+ assert_equal(msg_id, @db.msg_id(uid))
485
+ assert_equal(deleted, @db.msg_flag_deleted(uid))
486
+ end
487
+
488
+ @db.add_msg(2, 1)
489
+
490
+ assert_equal([ 1, 2 ], @db.each_msg_uid.to_a)
491
+ [ [ 1, true, 0, false ],
492
+ [ 2, true, 1, false ],
493
+ [ 3, false, nil, nil ]
494
+ ].each do |uid, exist, msg_id, deleted|
495
+ assert_equal(exist, (@db.msg_exist? uid))
496
+ assert_equal(msg_id, @db.msg_id(uid))
497
+ assert_equal(deleted, @db.msg_flag_deleted(uid))
498
+ end
499
+
500
+ @db.add_msg(3, 0)
501
+
502
+ assert_equal([ 1, 2, 3 ], @db.each_msg_uid.to_a)
503
+ [ [ 1, true, 0, false ],
504
+ [ 2, true, 1, false ],
505
+ [ 3, true, 0, false ]
506
+ ].each do |uid, exist, msg_id, deleted|
507
+ assert_equal(exist, (@db.msg_exist? uid))
508
+ assert_equal(msg_id, @db.msg_id(uid))
509
+ assert_equal(deleted, @db.msg_flag_deleted(uid))
510
+ end
511
+
512
+ @db.set_msg_flag_deleted(1, true)
513
+
514
+ [ [ 1, true, 0, true ],
515
+ [ 2, true, 1, false ],
516
+ [ 3, true, 0, false ]
517
+ ].each do |uid, exist, msg_id, deleted|
518
+ assert_equal(exist, (@db.msg_exist? uid))
519
+ assert_equal(msg_id, @db.msg_id(uid))
520
+ assert_equal(deleted, @db.msg_flag_deleted(uid))
521
+ end
522
+
523
+ @db.expunge_msg(1)
524
+
525
+ [ [ 1, false, nil, nil ],
526
+ [ 2, true, 1, false ],
527
+ [ 3, true, 0, false ]
528
+ ].each do |uid, exist, msg_id, deleted|
529
+ assert_equal(exist, (@db.msg_exist? uid))
530
+ assert_equal(msg_id, @db.msg_id(uid))
531
+ assert_equal(deleted, @db.msg_flag_deleted(uid))
532
+ end
533
+
534
+ assert_raise(RuntimeError) { @db.expunge_msg(1) }
535
+ assert_raise(RuntimeError) { @db.expunge_msg(2) }
536
+ end
537
+ end
538
+
539
+ class DBCoreTestReadAllTest < Test::Unit::TestCase
540
+ def setup
541
+ @kvs = {}
542
+ @builder = RIMS::KeyValueStore::FactoryBuilder.new
543
+ @builder.open{|name| RIMS::Hash_KeyValueStore.new(@kvs) }
544
+ @builder.use(RIMS::Checksum_KeyValueStore)
545
+ @cksum_kvs = @builder.factory.call('test')
546
+ @db = RIMS::DB::Core.new(@cksum_kvs)
547
+ end
548
+
549
+ def teardown
550
+ pp @kvs if $DEBUG
551
+ end
552
+
553
+ def test_test_read_all_empty
554
+ @db.test_read_all{|read_error|
555
+ flunk('no error.')
556
+ }
557
+ end
558
+
559
+ def test_test_read_all_good
560
+ @cksum_kvs['foo'] = 'apple'
561
+ @cksum_kvs['bar'] = 'banana'
562
+ @cksum_kvs['baz'] = 'orange'
563
+
564
+ @db.test_read_all{|read_error|
565
+ flunk('no error.')
566
+ }
567
+ end
568
+
569
+ def test_test_read_all_bad
570
+ @cksum_kvs['foo'] = 'apple'
571
+ @cksum_kvs['bar'] = 'banana'; @kvs['bar'] = 'banana'
572
+ @cksum_kvs['baz'] = 'orange'
573
+
574
+ count = 0
575
+ assert_raise(RuntimeError) {
576
+ @db.test_read_all{|read_error|
577
+ assert_kind_of(RuntimeError, read_error)
578
+ count += 1
579
+ }
580
+ }
581
+ assert_equal(1, count)
582
+
583
+ @cksum_kvs['foo'] = 'apple'; @kvs['foo'] = 'apple'
584
+ @cksum_kvs['bar'] = 'banana'
585
+ @cksum_kvs['baz'] = 'orange'; @kvs['baz'] = 'orange'
586
+
587
+ count = 0
588
+ assert_raise(RuntimeError) {
589
+ @db.test_read_all{|read_error|
590
+ assert_kind_of(RuntimeError, read_error)
591
+ count += 1
592
+ }
593
+ }
594
+ assert_equal(2, count)
595
+ end
596
+ end
597
+ end
598
+
599
+ # Local Variables:
600
+ # mode: Ruby
601
+ # indent-tabs-mode: nil
602
+ # End: