bc3 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bc3/time.rb ADDED
@@ -0,0 +1 @@
1
+ =begin rdoc
2
  bignum too big to convert into `unsigned long' (RangeError)
1
3
  AD_EPOCH = 116_444_736_000_000_000
2
4
  AD_MULTIPLIER = 10_000_000
3
5
  # convert a Time object to AD's epoch
4
6
  def time2ad()
5
7
  (self.to_i * AD_MULTIPLIER) + AD_EPOCH
6
8
  end
7
9
  # convert from AD's time string to a Time object
8
10
  def self.ad2time(time)
9
11
  Time.at((time.to_i - AD_EPOCH) / AD_MULTIPLIER)
10
12
  end
@@ -0,0 +1,66 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+
4
+ $:.unshift('../lib')
5
+ require 'bc3'
6
+
7
+ class Test_helper < Test::Unit::TestCase
8
+ include BC3::Helper
9
+ def test_fixnum2int64()
10
+ assert_equal("\x05\x00\x00\x00\x00\x00\x00\x00", fixnum2int64(5))
11
+ assert_equal("\xff\x00\x00\x00\x00\x00\x00\x00", fixnum2int64(255))
12
+ assert_equal("\x00\x01\x00\x00\x00\x00\x00\x00", fixnum2int64(256))
13
+ assert_equal("\x01\x01\x00\x00\x00\x00\x00\x00", fixnum2int64(257))
14
+ assert_equal("\x00\x04\x00\x00\x00\x00\x00\x00", fixnum2int64(1024))
15
+ end
16
+ def test_fixnum2int32()
17
+ assert_equal("\x05\x00\x00\x00", fixnum2int32(5))
18
+ assert_equal("\xff\x00\x00\x00", fixnum2int32(255))
19
+ assert_equal("\x00\x01\x00\x00", fixnum2int32(256))
20
+ assert_equal("\x01\x01\x00\x00", fixnum2int32(257))
21
+ assert_equal("\x00\x04\x00\x00", fixnum2int32(1024))
22
+ end
23
+ def test_crc32()
24
+ assert_equal('CBF43926', '%X' % BC3::Helper.crc32('123456789'))
25
+ assert_equal('D87F7E0C', '%X' % BC3::Helper.crc32('test'))
26
+ end
27
+ end
28
+
29
+
30
+ class Test_time < Test::Unit::TestCase
31
+ #Nonoseconds are ignored (missing feature in Time-class?)
32
+ def test_ad2time()
33
+ assert_equal('2006-08-17 09:19:04 000000000',
34
+ Time.ad2time(128002727440808261).strftime("%Y-%m-%d %H:%M:%S %9N")
35
+ )
36
+ assert_equal('2006-08-17 09:19:04 000000000',
37
+ Time.ad2time(128002727440000000).strftime("%Y-%m-%d %H:%M:%S %9N")
38
+ )
39
+ end
40
+ def test_time2ad()
41
+ assert_equal(128002464000000000, Time.utc(2006,8,17).time2ad())
42
+ assert_equal(128002727440000000, Time.local(2006,8,17,9,19,4).time2ad())
43
+
44
+ #Test does not work. Split in two areas (high/Low) makes calculating unpossible.
45
+ #~ p 60 * 60 * 1_000_000_000 #one hour in nanoseconds
46
+ #~ assert_equal( 3_600_000_000_000, #one hour in nanoseconds
47
+ #~ Time.utc(2006,8,17,12).time2ad() - Time.utc(2006,8,17,11).time2ad()
48
+ #~ )
49
+
50
+ #~ assert_equal( 3_600_000_000_000, #one hour in nanoseconds
51
+ #~ Time.utc(2006,8,17).time2ad() - Time.local(2006,8,17).time2ad()
52
+ #~ )
53
+ end
54
+ def test_conversions()
55
+ testtime = Time.utc(2006,8,17)
56
+ assert_equal(testtime, Time.ad2time(testtime.time2ad))
57
+
58
+ testtime = Time.local(2006,8,17)
59
+ assert_equal(testtime, Time.ad2time(testtime.time2ad))
60
+
61
+ #Test with now does not work. Changed nanoseconds?
62
+ #~ testtime = Time.now
63
+ #~ assert_equal(testtime, Time.ad2time(testtime.time2ad))
64
+ end
65
+ end
66
+
@@ -0,0 +1,35 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+
5
+ $:.unshift('../lib')
6
+ require 'bc3'
7
+ $log.level = Log4r::FATAL
8
+
9
+ #<just a timestamp for testing
10
+ $timestamp= Time.local(2010,1,1)
11
+
12
+ class Test_file < Test::Unit::TestCase
13
+ def self.startup
14
+ $log.level = Log4r::FATAL
15
+ end
16
+ def test_interface()
17
+ #Test all keys
18
+ assert_nothing_raised{ BC3::File.new( filename: 'f', filesize: 5, crc: 4, timestamp: Time.now )}
19
+ #Test all needed keys
20
+ assert_nothing_raised{ BC3::File.new( filename: 'f', filesize: 5 )}
21
+ #Test additional key (returns warning in log)
22
+ assert_nothing_raised{ BC3::File.new( filename: 'f', filesize: 5, xxx: nil )}
23
+ #Must be a hash
24
+ assert_raise(ArgumentError){ BC3::File.new( 'f' )}
25
+ assert_raise(ArgumentError){ BC3::File.new( 1 )}
26
+ end
27
+ def test_hash()
28
+ assert_equal( { filename: 'f', filesize: 5, timestamp: $timestamp, attributes: BC3::Attrib::Archive, crc: nil},
29
+ BC3::File.new( filename: 'f', filesize: 5, timestamp: $timestamp ).to_hash
30
+ )
31
+ assert_equal( { filename: 'f', filesize: 5, crc: 27, timestamp: $timestamp, attributes: 127 },
32
+ BC3::File.new( filename: 'f', filesize: 5, crc: 27, timestamp: $timestamp, attributes: 127 ).to_hash
33
+ )
34
+ end
35
+ end
@@ -0,0 +1,179 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+
5
+ $:.unshift('../lib')
6
+ require 'bc3'
7
+ $log.level = Log4r::FATAL
8
+
9
+ #<just a timestamp for testing
10
+ $timestamp= Time.local(2010,1,1)
11
+
12
+ class Test_folder < Test::Unit::TestCase
13
+ def test_add_content()
14
+ folder = BC3::Folder.new('x')
15
+ assert_nothing_raised{ folder << BC3::Folder.new('dir1')}
16
+ assert_nothing_raised{ folder << BC3::File.new( filename: 'file1', filesize: 1) }
17
+ assert_raise(ArgumentError){ folder << 1 }
18
+
19
+ assert_equal(2, folder.each.size)
20
+ end
21
+ def test_add_duplicate_filename()
22
+ folder = BC3::Folder.new('x')
23
+ assert_nothing_raised{ folder << BC3::Folder.new('name')}
24
+ #Overwrite content (Warning)
25
+ assert_nothing_raised{ folder << BC3::File.new( filename: 'name', filesize: 1) }
26
+ assert_equal(1, folder.each.size)
27
+ assert_kind_of(BC3::File, folder.each.first.last)
28
+
29
+ end
30
+ def test_hash()
31
+ folder = BC3::Folder.new('dir', $timestamp)
32
+ folder << BC3::File.new( filename: 'f', filesize: 5, timestamp: $timestamp )
33
+ assert_equal(
34
+ {
35
+ dirname: 'dir',
36
+ timestamp: $timestamp,
37
+ attributes: BC3::Attrib::Directory,
38
+ content: [
39
+ { filename: 'f', filesize: 5, timestamp: $timestamp, crc: nil, attributes: BC3::Attrib::Archive,},
40
+ ]
41
+ },
42
+ folder.to_hash
43
+ )
44
+ end
45
+ def test_newh_interface()
46
+ #Test no-hash
47
+ assert_raise(ArgumentError){ BC3::Folder.newh('x')}
48
+ assert_raise(ArgumentError){ BC3::Folder.newh('1')}
49
+ #Hash must contain dirname (and content?)
50
+ assert_raise(ArgumentError){ BC3::Folder.newh({})}
51
+ #~ assert_raise(ArgumentError){ BC3::Folder.newh({dirname: 'x' })}
52
+ #~ assert_raise(ArgumentError){ BC3::Folder.newh({dirname: 'x' })}
53
+ #~ assert_raise(ArgumentError){ BC3::Folder.newh({dirname: 'x', timestamp: $timestamp })}
54
+
55
+ assert_nothing_raised{ bc3 = BC3::Folder.newh({ dirname: 'x', content: [] })}
56
+ #Content must be an array
57
+ assert_raise(ArgumentError){ bc3 = BC3::Folder.newh({ dirname: 'x', content: :x })}
58
+ end
59
+
60
+ def test_newh()
61
+ hash = {
62
+ dirname: 'dir', timestamp: $timestamp, attributes: BC3::Attrib::Directory,
63
+ content: [
64
+ filename: 'file1', filesize: 5, timestamp: $timestamp, crc: nil, attributes: BC3::Attrib::Archive,
65
+ ]
66
+ }
67
+ assert_equal(hash, BC3::Folder.newh( hash ).to_hash)
68
+
69
+ hash = {
70
+ dirname: 'dir', timestamp: $timestamp, attributes: BC3::Attrib::Directory,
71
+ content: []
72
+ }
73
+ assert_equal(hash, BC3::Folder.newh( hash ).to_hash)
74
+ #Test w/o content
75
+ assert_equal(hash, BC3::Folder.newh( {dirname: 'dir', timestamp: $timestamp} ).to_hash)
76
+ end
77
+ def test_newh_yaml()
78
+ hash = YAML.load(%{
79
+ :dirname: dir
80
+ :timestamp: 2011-01-21 11:57:22 +01:00
81
+ :attributes: 16
82
+ :content:
83
+ - :filename: file1
84
+ :filesize: 5
85
+ :crc:
86
+ :attributes: 32
87
+ :timestamp: 2011-01-21 11:57:22 +01:00
88
+ })
89
+ assert_equal(hash, BC3::Folder.newh( hash ).to_hash)
90
+
91
+ hash = {
92
+ dirname: 'dir', timestamp: $timestamp, attributes: BC3::Attrib::Directory,
93
+ content: []
94
+ }
95
+ assert_equal(hash, BC3::Folder.newh( hash ).to_hash)
96
+ #Test w/o content
97
+ assert_equal(hash, BC3::Folder.newh( {dirname: 'dir', timestamp: $timestamp} ).to_hash)
98
+
99
+ end
100
+
101
+ def test_each_file()
102
+ hash = {
103
+ dirname: 'dir', timestamp: $timestamp, attributes: BC3::Attrib::Directory,
104
+ content: [
105
+ filename: 'file1', filesize: 5, timestamp: $timestamp, crc: nil, attributes: BC3::Attrib::Archive,
106
+ ]
107
+ }
108
+ folder = BC3::Folder.newh( hash )
109
+ #each without block
110
+ assert_instance_of(Hash, folder.each)
111
+
112
+ folder.each{|key, object|
113
+ assert_equal('file1', key)
114
+ assert_instance_of(BC3::File, object)
115
+ }
116
+ end
117
+
118
+ def test_each()
119
+ folder = BC3::Folder.newh( YAML.load(%{
120
+ :dirname: dir
121
+ :content:
122
+ - :dirname: subdir
123
+ :content:
124
+ - :filename: subfile
125
+ :filesize: 10
126
+ - :filename: file1
127
+ :filesize: 5
128
+ - :filename: file2
129
+ :filesize: 15
130
+ }))
131
+ #each without block
132
+ assert_instance_of(Hash, folder.each)
133
+ assert_equal(3, folder.each.size)
134
+ assert_equal(2, folder.each(:files).size)
135
+ assert_equal(1, folder.each(:folders).size)
136
+
137
+ assert_equal(%w{subdir file1 file2}, folder.each.keys)
138
+ assert_equal(%w{file1 file2}, folder.each(:files).keys)
139
+ assert_equal(%w{subdir}, folder.each(:folders).keys)
140
+
141
+ assert_equal(1, folder.each(:folders, :recursive).size)
142
+ assert_equal(3, folder.each(:files, :recursive).size)
143
+ assert_equal(4, folder.each(:folders,:files, :recursive).size)
144
+
145
+
146
+ assert_equal(%w{subdir}, folder.each(:folders, :recursive).keys)
147
+ assert_equal(%w{subdir/subfile file1 file2}, folder.each(:files, :recursive).keys)
148
+ assert_equal(%w{subdir subdir/subfile file1 file2}, folder.each(:folders,:files, :recursive).keys)
149
+
150
+ end
151
+
152
+ def test_each_2()
153
+ folder = BC3::Folder.newh( YAML.load(%{
154
+ :dirname: dir
155
+ :content:
156
+ - :dirname: subdir
157
+ :content:
158
+ - :dirname: subsubdir
159
+ :content:
160
+ - :filename: subsubfile
161
+ :filesize: 10
162
+ - :filename: subfile
163
+ :filesize: 10
164
+ - :filename: file
165
+ :filesize: 5
166
+ }))
167
+ #each without block
168
+
169
+ assert_equal(2, folder.each(:folders, :recursive).size)
170
+ assert_equal(3, folder.each(:files, :recursive).size)
171
+ assert_equal(5, folder.each(:folders,:files, :recursive).size)
172
+
173
+ assert_equal(%w{subdir subdir/subsubdir}, folder.each(:folders, :recursive).keys)
174
+ assert_equal(%w{subdir/subsubdir/subsubfile subdir/subfile file}, folder.each(:files, :recursive).keys)
175
+ assert_equal(%w{subdir subdir/subsubdir subdir/subsubdir/subsubfile subdir/subfile file}, folder.each(:folders,:files, :recursive).keys)
176
+
177
+ end
178
+ end
179
+
@@ -0,0 +1,102 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+
4
+ $:.unshift('../lib')
5
+ require 'bc3'
6
+ $log.level = Log4r::WARN
7
+
8
+ #<just a timestamp for testing
9
+ $timestamp= Time.local(2010,1,1)
10
+ class Time
11
+ def self.now()
12
+ $timestamp
13
+ end
14
+ end
15
+
16
+ #Start with local sources
17
+ $call_bc3_merge = 'ruby -I ../lib ../bin/bc3_merge.rb'
18
+
19
+
20
+ class Test_merge < Test::Unit::TestCase
21
+ Snap1 = {
22
+ snapshot: 'c:\usr\snap1',
23
+ timestamp: $timestamp,
24
+ content: [
25
+ {
26
+ dirname: 'dir1',
27
+ timestamp: $timestamp,
28
+ attributes: BC3::Attrib::Directory,
29
+ content: [
30
+ { filename: 'file1_1', filesize: 1, timestamp: $timestamp, crc: 111, attributes: BC3::Attrib::Archive },
31
+ ]
32
+ }
33
+ ],
34
+ }
35
+ Snap2 = {
36
+ snapshot: 'c:\usr\snap2',
37
+ timestamp: $timestamp,
38
+ content: [
39
+ {
40
+ dirname: 'dir2',
41
+ timestamp: $timestamp,
42
+ attributes: BC3::Attrib::Directory,
43
+ content: [
44
+ { filename: 'file2_1', filesize: 2, timestamp: $timestamp, crc: 222, attributes: BC3::Attrib::Archive },
45
+ ]
46
+ }
47
+ ],
48
+ }
49
+ TestFiles = %w{snap1.bcss snap2.bcss
50
+ merge_i.bcss merge_b_1.bcss merge_b_2.bcss merge_r.bcss
51
+ }
52
+
53
+ def self.startup
54
+ TestFiles.each{|f| File.delete(f) if File.exist?(f)}
55
+ BC3::Snapshot.newh(Snap1).save('snap1.bcss')
56
+ BC3::Snapshot.newh(Snap2).save('snap2.bcss')
57
+ end
58
+
59
+ def self.shutdown
60
+ TestFiles.each{|f| File.delete(f) if File.exist?(f)}
61
+ end
62
+
63
+ def test_merge_b_1()
64
+ mergetest_b('merge_b_1.bcss', "-s merge_b -t merge_b_1.bcss -b 'snap?.bcss'")
65
+ end
66
+ def test_merge_b_2()
67
+ mergetest_b('merge_b_2.bcss', '-s merge_b -t merge_b_2.bcss snap?.bcss')
68
+ end
69
+ #~ def test_merge_r()
70
+ #~ mergetest_b('merge_r.bcss', "-s merge_b -t merge_r.bcss -r 'snap?.bcss'")
71
+ #~ end
72
+ def mergetest_b(filename, par)
73
+ assert_false(File.exist?(filename))
74
+ `#{$call_bc3_merge} #{par}`
75
+ assert_true(File.exist?(filename))
76
+
77
+ result = BC3::SnapshotParser.new(filename).snapshot.to_hash
78
+ merge = {
79
+ snapshot: 'merge_b',
80
+ timestamp: result[:timestamp],
81
+ content: [
82
+ { dirname: 'snap1', timestamp: $timestamp, attributes: 16, content: Snap1[:content] },
83
+ { dirname: 'snap2', timestamp: $timestamp, attributes: 16, content: Snap2[:content] },
84
+ ]
85
+ }
86
+ assert_equal(merge, result)
87
+ end
88
+
89
+ def test_merge_i()
90
+ assert_false(File.exist?('merge_i.bcss'))
91
+ `#{$call_bc3_merge} -s merge_i -t merge_i.bcss -i 'snap?.bcss'`
92
+ assert_true(File.exist?('merge_i.bcss'))
93
+
94
+ result = BC3::SnapshotParser.new('merge_i.bcss').snapshot.to_hash
95
+ merge = {
96
+ snapshot: 'merge_i',
97
+ timestamp: result[:timestamp],
98
+ content: Snap1[:content] + Snap2[:content]
99
+ }
100
+ assert_equal(merge, result)
101
+ end
102
+ end
@@ -0,0 +1,121 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+
5
+ $:.unshift('../lib')
6
+ require 'bc3'
7
+ $log.level = Log4r::FATAL
8
+
9
+ #<just a timestamp for testing
10
+ $timestamp= Time.local(2010,1,1)
11
+
12
+ class Test_snapshot < Test::Unit::TestCase
13
+ def test_add_content()
14
+ bc3 = BC3::Snapshot.new('x')
15
+ assert_nothing_raised{ bc3 << BC3::Folder.new('dir')}
16
+ assert_nothing_raised{ bc3 << BC3::File.new( filename: 'file', filesize: 1 )}
17
+ assert_raise(ArgumentError){ bc3 << 1 }
18
+
19
+ assert_equal(2, bc3.each.size)
20
+ end
21
+ def test_hash()
22
+ bc3 = BC3::Snapshot.new('x', $timestamp)
23
+ bc3 << folder = BC3::Folder.new('dir', $timestamp)
24
+ folder << BC3::File.new( filename: 'f', filesize: 5, timestamp: $timestamp )
25
+
26
+ assert_equal(
27
+ { snapshot: 'x',
28
+ timestamp: $timestamp,
29
+ content: [
30
+ {
31
+ dirname: 'dir',
32
+ timestamp: $timestamp,
33
+ attributes: BC3::Attrib::Directory,
34
+ content: [
35
+ { filename: 'f', filesize: 5, timestamp: $timestamp, crc: nil, attributes: BC3::Attrib::Archive },
36
+ ]
37
+ }
38
+ ],
39
+ }, bc3.to_hash
40
+ )
41
+ end
42
+ def test_newh_interface()
43
+ #Test no-hash
44
+ assert_raise(ArgumentError){ BC3::Snapshot.newh('x')}
45
+ assert_raise(ArgumentError){ BC3::Snapshot.newh('1')}
46
+ #Hash must contain snapshot and content
47
+ assert_raise(ArgumentError){ BC3::Snapshot.newh({})}
48
+ assert_raise(ArgumentError){ BC3::Snapshot.newh({snapshot: 'x' })}
49
+ assert_raise(ArgumentError){ BC3::Snapshot.newh({snapshot: 'x' })}
50
+ assert_raise(ArgumentError){ BC3::Snapshot.newh({snapshot: 'x', timestamp: $timestamp })}
51
+
52
+ assert_nothing_raised{ bc3 = BC3::Snapshot.newh({ snapshot: 'x', content: [] })}
53
+ #Content must be an array
54
+ assert_raise(ArgumentError){ bc3 = BC3::Snapshot.newh({ snapshot: 'x', content: :x })}
55
+ end
56
+ def test_newh()
57
+ hash = { snapshot: 'x', timestamp: $timestamp,
58
+ content: [
59
+ filename: 'file1', filesize: 5, timestamp: $timestamp, crc: nil, attributes: BC3::Attrib::Archive,
60
+ ]
61
+ }
62
+ assert_equal(hash, BC3::Snapshot.newh( hash ).to_hash)
63
+
64
+ hash[:content] << { filename: 'file2', filesize: 15, timestamp: $timestamp, crc: nil, attributes: BC3::Attrib::Archive, }
65
+ assert_equal(hash, BC3::Snapshot.newh( hash ).to_hash)
66
+
67
+ hash[:content] << { dirname: 'dir1', timestamp: $timestamp, :content => [], attributes: BC3::Attrib::Directory, }
68
+ assert_equal(hash, BC3::Snapshot.newh( hash ).to_hash)
69
+ end
70
+ def test_newd()
71
+ bc3 = BC3::Snapshot.newd( '../examples/folder1' )
72
+
73
+ #timestamp change
74
+ #~ assert_equal(%{
75
+ #~ :snapshot: C:/usr/Script/bc3/examples/folder1
76
+ #~ :timestamp: 2011-01-21 23:53:45.875000 +01:00
77
+ #~ :content:
78
+ #~ - :filename: file1.txt
79
+ #~ :filesize: 0
80
+ #~ :crc: 0
81
+ #~ :attributes: 32
82
+ #~ :timestamp: 2011-01-21 16:58:38 +01:00
83
+ #~ }, bc3.to_hash.to_yaml)
84
+
85
+ assert_equal(1, bc3.each.size)
86
+ assert_equal(1, bc3.each(:files).size)
87
+ assert_equal(1, bc3.each(:recursive).size)
88
+ assert_equal(1, bc3.each(:recursive, :files).size)
89
+
90
+ assert_equal('file1.txt', bc3.each.first.first)
91
+
92
+ end
93
+ def test_new_list()
94
+ pend 'BC3::Snapshot.new_list'
95
+ bc3 = BC3::Snapshot.new_list( 'dir', %w{
96
+ file
97
+ folder/file1
98
+ folder/subfolder/subfile1
99
+ folder/subfolder2/
100
+ }
101
+ )
102
+
103
+ assert_equal(2, bc3.each.size)
104
+ assert_equal(1, bc3.each(:files).size)
105
+ assert_equal(5, bc3.each(:recursive).size)
106
+ assert_equal(2, bc3.each(:recursive, :files).size)
107
+ assert_equal(3, bc3.each(:recursive, :folders).size)
108
+
109
+ assert_equal( %w{
110
+ file
111
+ folder
112
+ folder/file1
113
+ folder/subfolder
114
+ folder/subfolder/subfile1
115
+ folder/subfolder2
116
+ }, bc3.each(:recursive, :files).keys)
117
+
118
+
119
+ end
120
+ end
121
+