tlv 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/tlv'
3
+
4
+ class TestTLVTag < Test::Unit::TestCase
5
+ include TLV
6
+
7
+ def setup
8
+ end
9
+
10
+ class TLVTagTest < TLV
11
+ tlv "41", "Test TLV"
12
+ b 8, "first field", :first
13
+ b 8, "second field", :second
14
+ end
15
+ class TLVTestNoTag < TLV
16
+ b 8, "first field", :first
17
+ b 8, "second field", :second
18
+ end
19
+
20
+ def test_basics
21
+ t = TLVTagTest.new
22
+ 0.upto(0x3f) {|i|
23
+ next if (i & 0x1f) ==0x1f # two byte tags
24
+ TLVTagTest.tlv(("%02x"%i), "a")
25
+ assert TLVTagTest.universal?, i.to_s
26
+ assert !TLVTagTest.application?, i.to_s
27
+ assert !TLVTagTest.context_specific?, i.to_s
28
+ assert !TLVTagTest.private?, i.to_s
29
+ }
30
+ 0x40.upto(0x4f) {|i|
31
+ TLVTagTest.tlv(("%02x"%i), "b")
32
+ assert !TLVTagTest.universal?, i.to_s
33
+ assert TLVTagTest.application?, i.to_s
34
+ assert !TLVTagTest.context_specific?, i.to_s
35
+ assert !TLVTagTest.private?, i.to_s
36
+ }
37
+ 0x80.upto(0xBf) {|i|
38
+ next if (i & 0x1f) ==0x1f # two byte tags
39
+ TLVTagTest.tlv(("%02x"%i), "c")
40
+ assert !TLVTagTest.universal?, i.to_s
41
+ assert !TLVTagTest.application?, i.to_s
42
+ assert TLVTagTest.context_specific?, i.to_s
43
+ assert !TLVTagTest.private?, i.to_s
44
+ }
45
+ 0xC0.upto(0xFF) {|i|
46
+ next if (i & 0x1f) ==0x1f # two byte tags
47
+ TLVTagTest.tlv(("%02x"%i), "d")
48
+ assert !TLVTagTest.universal?, i.to_s
49
+ assert !TLVTagTest.application?, i.to_s
50
+ assert !TLVTagTest.context_specific?, i.to_s
51
+ assert TLVTagTest.private?, i.to_s
52
+ }
53
+
54
+
55
+ end
56
+
57
+ def test_invalid_tag
58
+
59
+ assert_raise (RuntimeError) {
60
+
61
+ TLVTagTest.tlv "9F", ""
62
+ }
63
+ assert_raise (RuntimeError) {
64
+ TLVTagTest.tlv "9F0000", ""
65
+ }
66
+ assert_raise (RuntimeError) {
67
+ TLVTagTest.tlv "9FF0", ""
68
+ }
69
+ assert_raise (RuntimeError) {
70
+ TLVTagTest.tlv "9FF00000", ""
71
+ }
72
+ assert_raise (RuntimeError) {
73
+ TLVTagTest.tlv "9FF0F000", ""
74
+ }
75
+ assert_raise (RuntimeError) {
76
+ TLVTagTest.tlv "0000", ""
77
+ }
78
+ end
79
+
80
+ def test_no_tag
81
+ t = TLVTestNoTag.new
82
+ assert !TLVTestNoTag.universal?
83
+ assert !TLVTestNoTag.application?
84
+ assert !TLVTestNoTag.context_specific?
85
+ assert !TLVTestNoTag.private?
86
+ assert TLVTestNoTag.primitive?
87
+ end
88
+
89
+ end
@@ -0,0 +1,217 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/tlv'
3
+
4
+ class TestTLV < Test::Unit::TestCase
5
+ include TLV
6
+
7
+ def setup
8
+ end
9
+
10
+ class TLVTest < TLV
11
+ tlv "11", "Test TLV"
12
+ b 8, "first field", :first
13
+ b 8, "second field", :second
14
+ end
15
+
16
+ class TLVTest2 < TLV
17
+ tlv "42", "Test Rubify"
18
+ b 8, "My Test"
19
+ b 8, "Oh M@i!"
20
+ end
21
+
22
+ class TLVTest3 < TLV
23
+ tlv "9F7F", "Test Raw"
24
+ raw
25
+ end
26
+ class TLVTest4 < TLV
27
+ tlv 0x9F71, "Test Fixnum"
28
+ raw
29
+ end
30
+ class DGITest4 < DGI
31
+ tlv 0x0101, "Test Fixnum"
32
+ raw
33
+ end
34
+
35
+ class TLVTest5 < TLV
36
+ tlv 0x70, "Test Fixnum 2"
37
+ raw
38
+ end
39
+ class TLVTestNoTag < TLV
40
+ b 8, "first field", :first
41
+ b 8, "second field", :second
42
+ end
43
+
44
+ def basics tlv
45
+ tlv.first="\x01"
46
+ tlv.second="\xAA"
47
+ assert_equal "\x01", tlv.first
48
+ assert_equal "\xaa", tlv.second
49
+
50
+ assert_raise(RuntimeError) {
51
+ tlv.first="\x02\x03"
52
+ }
53
+ assert_raise(RuntimeError) {
54
+ tlv.first=Time.new
55
+ }
56
+ assert_raise(RuntimeError) {
57
+ tlv.second=1
58
+ }
59
+ end
60
+
61
+ def test_basics
62
+ t = TLVTest.new
63
+ basics t
64
+ assert_equal "\x11\x02\x01\xaa", t.to_b
65
+ assert_equal "\x01\xaa", t.get_bytes
66
+
67
+ t = TLVTestNoTag.new
68
+ basics t
69
+ assert_equal "\x01\xaa", t.to_b
70
+ assert_equal t.to_b, t.get_bytes
71
+ end
72
+
73
+ def test_parse_tag
74
+ bytes = "\x01\x00\x00"
75
+ tag, rest = TLV.get_tag bytes
76
+ assert_equal "\x01", tag
77
+ assert_equal "\x00\x00", rest
78
+
79
+ bytes = "\xFF\x00\x00"
80
+ tag, rest = TLV.get_tag bytes
81
+ assert_equal "\xff\x00", tag
82
+ assert_equal "\x00", rest
83
+
84
+
85
+ bytes = "\xFF\x85\xAA"
86
+ tag, rest = TLV.get_tag bytes
87
+ assert_equal "\xff\x85\xaa", tag
88
+ assert_equal "", rest
89
+
90
+ bytes = "\x11\x02\x01\xaa"
91
+ tag, rest = TLV.get_tag bytes
92
+ assert_equal "\x11", tag
93
+ assert_equal "\x02\x01\xaa", rest
94
+
95
+
96
+ bytes = TLV.s2b "9f7f2aff"
97
+ tag, rest = TLV.get_tag bytes
98
+ assert_equal "\x9f\x7f", tag
99
+ assert_equal "\x2a\xff", rest
100
+
101
+ length, rest = TLV.get_length rest
102
+ assert_equal 0x2a, length
103
+
104
+
105
+ end
106
+ def test_parse_length
107
+ bytes = "\x03\x02"
108
+ len, rest = TLV.get_length bytes
109
+ assert_equal 3, len
110
+ assert_equal "\x02", rest
111
+
112
+ bytes = "\x81\x02\x11\x22"
113
+ len, rest = TLV.get_length bytes
114
+ assert_equal 2, len
115
+ assert_equal "\x11\x22", rest
116
+
117
+
118
+ bytes = "\x84\x00\x00\x00\x02\x11\x22"
119
+ len, rest = TLV.get_length bytes
120
+ assert_equal 2, len
121
+ assert_equal "\x11\x22", rest
122
+
123
+
124
+ bytes = "\x83\x00\x00\x02\x11\x22"
125
+ len, rest = TLV.get_length bytes
126
+ assert_equal 2, len
127
+ assert_equal "\x11\x22", rest
128
+
129
+ bytes = "\x82\x10\x01\x11\x22"
130
+ len, rest = TLV.get_length bytes
131
+ assert_equal 4097, len
132
+ assert_equal "\x11\x22", rest
133
+
134
+ bytes = "\x83\x00\x10\x01\x11\x22"
135
+ len, rest = TLV.get_length bytes
136
+ assert_equal 4097, len
137
+ assert_equal "\x11\x22", rest
138
+ end
139
+
140
+ def test_length
141
+ t = TLVTest3.new
142
+ t.value = ""
143
+ assert_equal "\x00", t.to_b[2,1]
144
+ t.value = "1"
145
+ assert_equal "\x01\x31", t.to_b[2,2]
146
+ t.value = "1"*127
147
+ assert_equal "\x7F\x31", t.to_b[2,2]
148
+ t.value = "1"*128
149
+ assert_equal "\x81\x80\x31", t.to_b[2,3]
150
+ t.value = "1"*255
151
+ assert_equal "\x81\xFF\x31", t.to_b[2,3]
152
+ t.value = "1"*256
153
+ assert_equal "\x82\x01\x00\x31", t.to_b[2,4]
154
+ t.value = "1"*65535
155
+ assert_equal "\x82\xFF\xFF\x31", t.to_b[2,4]
156
+ t.value = "1"*65536
157
+ assert_equal "\x84\x00\x01\x00\x00\x31", t.to_b[2,6]
158
+
159
+ o = Object.new
160
+ def o.length
161
+ return 4294967296
162
+ end
163
+
164
+ assert_raises (RuntimeError) {
165
+ t.value=o
166
+ t.to_b
167
+ }
168
+ end
169
+
170
+ def test_parse
171
+ t = TLVTest.new
172
+ assert_equal "\x00", t.first
173
+ t.first="\x01"
174
+ t.second="\xAA"
175
+
176
+ assert "\x01", t.first
177
+ bytes = t.to_b
178
+ t, rest = TLV.parse bytes
179
+ assert_equal TLVTest, t.class
180
+ assert_equal "\x01", t.first
181
+ assert_equal "\xAA", t.second
182
+ end
183
+ def test_rubify
184
+ t = TLVTest2.new
185
+ t.my_test = "\x01"
186
+ assert_equal "\x01", t.my_test
187
+ t.oh_mi = "\x02"
188
+ assert_equal "\x02", t.oh_mi
189
+ end
190
+ def test_raw
191
+ t = TLVTest3.new
192
+ #puts t.methods.sort
193
+ t.value= "bumsi"
194
+ assert_equal "Test Raw", TLVTest3.display_name
195
+ assert_equal "bumsi", t.value
196
+ bytes = t.to_b
197
+ t, rest = TLV.parse bytes
198
+ assert_equal "bumsi", t.value
199
+ assert_equal TLVTest3, t.class
200
+ end
201
+
202
+ def test_fixnum
203
+ t = TLVTest4.new
204
+ t.value = "123"
205
+ assert_equal(TLV.s2b("9f7103313233"), t.to_b)
206
+
207
+ t2 = TLVTest5.new
208
+ t2.value = "321"
209
+ assert_equal(TLV.s2b("7003333231"), t2.to_b)
210
+
211
+ d = DGITest4.new
212
+ assert_equal("\x01\x01", d.tag)
213
+ end
214
+
215
+
216
+
217
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tlv
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
11
+ platform: ruby
12
+ authors:
13
+ - Tim Becker
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-13 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: hexy
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: " lib for handling tlv (der) and dgi encoded data\t\n"
36
+ email: tim@kuriositaet.de
37
+ executables:
38
+ - parse_tlv
39
+ - parse_dgi
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/tlv/b.rb
46
+ - lib/tlv/constructed.rb
47
+ - lib/tlv/dgi.rb
48
+ - lib/tlv/field.rb
49
+ - lib/tlv/parse.rb
50
+ - lib/tlv/parser/dictionaries/asn.rb
51
+ - lib/tlv/parser/dictionaries/dictionaries.rb
52
+ - lib/tlv/parser/dictionaries/emv_tags.rb
53
+ - lib/tlv/parser/parser.rb
54
+ - lib/tlv/raw.rb
55
+ - lib/tlv/tag.rb
56
+ - lib/tlv/tlv.rb
57
+ - lib/tlv/to_bytes.rb
58
+ - lib/tlv.rb
59
+ - bin/parse_dgi
60
+ - bin/parse_tlv
61
+ - AUTHORS
62
+ - CHANGELOG
63
+ - COPYING
64
+ - LICENSE
65
+ - Rakefile
66
+ - README
67
+ - THANKS
68
+ - TODO
69
+ - test/constructed_test.rb
70
+ - test/dgi_test.rb
71
+ - test/tlv_tag_test.rb
72
+ - test/tlv_test.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/a2800276/tlv
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements:
101
+ - none
102
+ rubyforge_project:
103
+ rubygems_version: 1.6.2
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: "tlv: lib for handling tlv data"
107
+ test_files: []
108
+