pwntools 0.1.0
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.
- checksums.yaml +7 -0
- data/README.md +49 -0
- data/Rakefile +40 -0
- data/lib/pwn.rb +24 -0
- data/lib/pwnlib/constants/constant.rb +45 -0
- data/lib/pwnlib/constants/constants.rb +82 -0
- data/lib/pwnlib/constants/linux/amd64.rb +1558 -0
- data/lib/pwnlib/constants/linux/i386.rb +1340 -0
- data/lib/pwnlib/context.rb +220 -0
- data/lib/pwnlib/dynelf.rb +110 -0
- data/lib/pwnlib/ext/array.rb +21 -0
- data/lib/pwnlib/ext/helper.rb +21 -0
- data/lib/pwnlib/ext/integer.rb +21 -0
- data/lib/pwnlib/ext/string.rb +23 -0
- data/lib/pwnlib/memleak.rb +61 -0
- data/lib/pwnlib/pwn.rb +26 -0
- data/lib/pwnlib/reg_sort.rb +147 -0
- data/lib/pwnlib/util/cyclic.rb +120 -0
- data/lib/pwnlib/util/fiddling.rb +262 -0
- data/lib/pwnlib/util/hexdump.rb +145 -0
- data/lib/pwnlib/util/packing.rb +284 -0
- data/lib/pwnlib/version.rb +5 -0
- data/test/constants/constant_test.rb +24 -0
- data/test/constants/constants_test.rb +31 -0
- data/test/context_test.rb +131 -0
- data/test/data/victim.c +8 -0
- data/test/data/victim32 +0 -0
- data/test/data/victim64 +0 -0
- data/test/dynelf_test.rb +48 -0
- data/test/ext_test.rb +26 -0
- data/test/files/use_pwn.rb +34 -0
- data/test/files/use_pwnlib.rb +19 -0
- data/test/full_file_test.rb +16 -0
- data/test/memleak_test.rb +72 -0
- data/test/reg_sort_test.rb +41 -0
- data/test/test_helper.rb +13 -0
- data/test/util/cyclic_test.rb +36 -0
- data/test/util/fiddling_test.rb +106 -0
- data/test/util/hexdump_test.rb +179 -0
- data/test/util/packing_test.rb +168 -0
- metadata +231 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: ASCII-8BIT
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'pwnlib/util/cyclic'
|
5
|
+
|
6
|
+
class CyclicTest < MiniTest::Test
|
7
|
+
include ::Pwnlib::Util::Cyclic::ClassMethods
|
8
|
+
|
9
|
+
def test_cyclic
|
10
|
+
assert_equal('AAABAACABBABCACBACCBBBCBCCC', cyclic(alphabet: 'ABC', n: 3))
|
11
|
+
assert_equal('aaaabaaacaaadaaaeaaa', cyclic(20))
|
12
|
+
assert_equal(27_000, cyclic(alphabet: (0...30).to_a, n: 3).size)
|
13
|
+
assert_equal([1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 2, 2, 2],
|
14
|
+
cyclic(alphabet: [1, 2], n: 4))
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_cyclic_find
|
18
|
+
r = cyclic(1000)
|
19
|
+
10.times do
|
20
|
+
idx = rand(0...1000 - 4)
|
21
|
+
assert_equal(idx, cyclic_find(r[idx, 4]))
|
22
|
+
end
|
23
|
+
|
24
|
+
r = cyclic(1000)
|
25
|
+
10.times do
|
26
|
+
idx = rand(0...1000 - 5)
|
27
|
+
assert_equal(idx, cyclic_find(r[idx, 5], n: 4))
|
28
|
+
end
|
29
|
+
|
30
|
+
r = cyclic(1000, alphabet: (0...10).to_a)
|
31
|
+
10.times do
|
32
|
+
idx = rand(0...1000 - 4)
|
33
|
+
assert_equal(idx, cyclic_find(r[idx, 4], alphabet: (0...10).to_a))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# encoding: ASCII-8BIT
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'pwnlib/util/fiddling'
|
5
|
+
|
6
|
+
class FiddlingTest < MiniTest::Test
|
7
|
+
include ::Pwnlib::Util::Fiddling::ClassMethods
|
8
|
+
|
9
|
+
def test_enhex
|
10
|
+
assert_equal('4141313233', enhex('AA123'))
|
11
|
+
assert_equal('74657374', enhex('test'))
|
12
|
+
assert_equal('122355ff4499', enhex("\x12\x23\x55\xff\x44\x99"))
|
13
|
+
assert_equal('21402324255e262a28295f2b5f29282a265e2524234040213f7b7d5d5b2f2f5c60607e',
|
14
|
+
enhex('!@#$%^&*()_+_)(*&^%$#@@!?{}][//\``~'))
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_unhex
|
18
|
+
assert_equal('AA123', unhex('4141313233'))
|
19
|
+
assert_equal('test', unhex('74657374'))
|
20
|
+
assert_equal("\x12\x23\x55\xff\x44\x99", unhex('122355ff4499'))
|
21
|
+
assert_equal('!@#$%^&*()_+_)(*&^%$#@@!?{}][//\``~',
|
22
|
+
unhex('21402324255e262a28295f2b5f29282a265e2524234040213f7b7d5d5b2f2f5c60607e'))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_hex
|
26
|
+
assert_equal('0x0', hex(0))
|
27
|
+
assert_equal('0x64', hex(100))
|
28
|
+
assert_equal('-0xa', hex(-10))
|
29
|
+
assert_equal('0xfaceb00cdeadbeef', hex(0xfaceb00cdeadbeef))
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_urlencode
|
33
|
+
assert_equal('%74%65%73%74%20%41', urlencode('test A'))
|
34
|
+
assert_equal('%00%ff%01%fe', urlencode("\x00\xff\x01\xfe"))
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_urldecode
|
38
|
+
assert_equal('test A', urldecode('te%73t%20%41'))
|
39
|
+
assert_equal("\x00\xff\x01\xfe", urldecode('%00%ff%01%fe'))
|
40
|
+
|
41
|
+
assert_equal('%qq', urldecode('%qq', true))
|
42
|
+
err = assert_raises(ArgumentError) { urldecode('%qq') }
|
43
|
+
assert_match(/Invalid input to urldecode/, err.message)
|
44
|
+
|
45
|
+
assert_equal('%%1z2%orz%%%%%#$!#)@%', urldecode('%%1z2%orz%%%%%#$!#)@%', true))
|
46
|
+
err = assert_raises(ArgumentError) { urldecode('%ff%') }
|
47
|
+
assert_match(/Invalid input to urldecode/, err.message)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_bits
|
51
|
+
assert_equal(['+', '+', '+', '+', '+', '+', '+', '-', '-', '-', '-', '-', '-', '-', '-', '-'],
|
52
|
+
bits(511, zero: '+', one: '-'))
|
53
|
+
assert_equal([0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], bits('XD'))
|
54
|
+
assert_equal([0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0],
|
55
|
+
bits('XD', endian: 'le'))
|
56
|
+
assert_equal([0, 0, 0, 0, 0, 0, 0, 0], bits(0))
|
57
|
+
|
58
|
+
err = assert_raises(ArgumentError) { bits(2.0) }
|
59
|
+
assert_match(/must be either String or Integer/, err.message)
|
60
|
+
|
61
|
+
err = assert_raises(ArgumentError) { bits(-1) }
|
62
|
+
assert_match(/must be non-negative/, err.message)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_bits_str
|
66
|
+
assert_equal('0000000111111111', bits_str(511))
|
67
|
+
assert_equal('0100011010010110001011101100111011111010110011100010111001001110',
|
68
|
+
bits_str('bits_str', endian: 'little'))
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_unbits
|
72
|
+
assert_equal("\x80", unbits([1]))
|
73
|
+
assert_equal("\x01", unbits([1], endian: 'le'))
|
74
|
+
assert_equal("\x16\xa666\xf6", unbits(bits('hello'), endian: 'le'))
|
75
|
+
|
76
|
+
err = assert_raises(ArgumentError) { unbits(%w(hi there)) }
|
77
|
+
assert_match(/cannot decode value/, err.message)
|
78
|
+
|
79
|
+
assert_equal("\xf0", unbits('11110000'))
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_bitswap
|
83
|
+
assert_equal("\x8cL\xcc,", bitswap('1234'))
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_bitswap_int
|
87
|
+
assert_equal(0x2c, bitswap_int(0x1234, bits: 8))
|
88
|
+
assert_equal(0x2c48, bitswap_int(0x1234, bits: 16))
|
89
|
+
assert_equal(0x2c4800, bitswap_int(0x1234, bits: 24))
|
90
|
+
assert_equal(0x589000, bitswap_int(0x1234, bits: 25))
|
91
|
+
|
92
|
+
context.local(bits: 36) do
|
93
|
+
assert_equal(0xf77db57b0, bitswap_int(0xdeadbeef))
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_b64e
|
98
|
+
assert_equal('dGVzdA==', b64e('test'))
|
99
|
+
assert_equal('shik' * 100, b64e("\xb2\x18\xa4" * 100))
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_b64d
|
103
|
+
assert_equal('test', b64d('dGVzdA=='))
|
104
|
+
assert_equal("\xb2\x18\xa4" * 100, b64d('shik' * 100))
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require 'rainbow'
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'pwnlib/util/hexdump'
|
5
|
+
|
6
|
+
class HexDumpTest < MiniTest::Test
|
7
|
+
include ::Pwnlib::Util::HexDump::ClassMethods
|
8
|
+
|
9
|
+
def setup
|
10
|
+
# Default to disable coloring for easier testing.
|
11
|
+
Rainbow.enabled = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def assert_lines_equal(s1, s2)
|
15
|
+
s1l = s1.chomp.lines
|
16
|
+
s2l = s2.chomp.lines
|
17
|
+
assert_equal(s1l, s2l)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_hexdump_basic
|
21
|
+
assert_lines_equal(<<-'EOS', hexdump('A'))
|
22
|
+
00000000 41 │A│
|
23
|
+
00000001
|
24
|
+
EOS
|
25
|
+
assert_lines_equal(<<-'EOS', hexdump('ABCD'))
|
26
|
+
00000000 41 42 43 44 │ABCD│
|
27
|
+
00000004
|
28
|
+
EOS
|
29
|
+
assert_lines_equal(<<-'EOS', hexdump('<3 Ruby :)'))
|
30
|
+
00000000 3c 33 20 52 75 62 79 20 3a 29 │<3 R│uby │:)│
|
31
|
+
0000000a
|
32
|
+
EOS
|
33
|
+
assert_lines_equal(<<-'EOS', hexdump('><>><>><>><>><>><>>'))
|
34
|
+
00000000 3e 3c 3e 3e 3c 3e 3e 3c 3e 3e 3c 3e 3e 3c 3e 3e │><>>│<>><│>><>│><>>│
|
35
|
+
00000010 3c 3e 3e │<>>│
|
36
|
+
00000013
|
37
|
+
EOS
|
38
|
+
assert_lines_equal(<<-'EOS', hexdump('ABCD' * 5))
|
39
|
+
00000000 41 42 43 44 41 42 43 44 41 42 43 44 41 42 43 44 │ABCD│ABCD│ABCD│ABCD│
|
40
|
+
00000010 41 42 43 44 │ABCD│
|
41
|
+
00000014
|
42
|
+
EOS
|
43
|
+
assert_lines_equal(<<-'EOS', hexdump((0..255).map(&:chr).join))
|
44
|
+
00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f │····│····│····│····│
|
45
|
+
00000010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f │····│····│····│····│
|
46
|
+
00000020 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f │ !"#│$%&'│()*+│,-./│
|
47
|
+
00000030 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f │0123│4567│89:;│<=>?│
|
48
|
+
00000040 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f │@ABC│DEFG│HIJK│LMNO│
|
49
|
+
00000050 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f │PQRS│TUVW│XYZ[│\]^_│
|
50
|
+
00000060 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f │`abc│defg│hijk│lmno│
|
51
|
+
00000070 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f │pqrs│tuvw│xyz{│|}~·│
|
52
|
+
00000080 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f │····│····│····│····│
|
53
|
+
00000090 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f │····│····│····│····│
|
54
|
+
000000a0 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af │····│····│····│····│
|
55
|
+
000000b0 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf │····│····│····│····│
|
56
|
+
000000c0 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf │····│····│····│····│
|
57
|
+
000000d0 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df │····│····│····│····│
|
58
|
+
000000e0 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef │····│····│····│····│
|
59
|
+
000000f0 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff │····│····│····│····│
|
60
|
+
00000100
|
61
|
+
EOS
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_hexdump_width
|
65
|
+
assert_lines_equal(<<-'EOS', hexdump('vert~', width: 1))
|
66
|
+
00000000 76 │v│
|
67
|
+
00000001 65 │e│
|
68
|
+
00000002 72 │r│
|
69
|
+
00000003 74 │t│
|
70
|
+
00000004 7e │~│
|
71
|
+
00000005
|
72
|
+
EOS
|
73
|
+
assert_lines_equal(<<-'EOS', hexdump('~!@$%^&*()_+{}:"<>?', width: 13))
|
74
|
+
00000000 7e 21 40 24 25 5e 26 2a 28 29 5f 2b 7b │~!@$│%^&*│()_+│{│
|
75
|
+
0000000d 7d 3a 22 3c 3e 3f │}:"<│>?│
|
76
|
+
00000013
|
77
|
+
EOS
|
78
|
+
assert_lines_equal(<<-'EOS', hexdump('xoxoxoxo', width: 6))
|
79
|
+
00000000 78 6f 78 6f 78 6f │xoxo│xo│
|
80
|
+
00000006 78 6f │xo│
|
81
|
+
00000008
|
82
|
+
EOS
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_hexdump_skip
|
86
|
+
assert_lines_equal(<<-'EOS', hexdump('A' * 49))
|
87
|
+
00000000 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 │AAAA│AAAA│AAAA│AAAA│
|
88
|
+
*
|
89
|
+
00000030 41 │A│
|
90
|
+
00000031
|
91
|
+
EOS
|
92
|
+
assert_lines_equal(<<-'EOS', hexdump('A' * 49, skip: false))
|
93
|
+
00000000 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 │AAAA│AAAA│AAAA│AAAA│
|
94
|
+
00000010 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 │AAAA│AAAA│AAAA│AAAA│
|
95
|
+
00000020 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 │AAAA│AAAA│AAAA│AAAA│
|
96
|
+
00000030 41 │A│
|
97
|
+
00000031
|
98
|
+
EOS
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_offset
|
102
|
+
assert_lines_equal(<<-'EOS', hexdump('ELF' * 10, offset: 0x400000))
|
103
|
+
00400000 45 4c 46 45 4c 46 45 4c 46 45 4c 46 45 4c 46 45 │ELFE│LFEL│FELF│ELFE│
|
104
|
+
00400010 4c 46 45 4c 46 45 4c 46 45 4c 46 45 4c 46 │LFEL│FELF│ELFE│LF│
|
105
|
+
0040001e
|
106
|
+
EOS
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_style_byte
|
110
|
+
assert_lines_equal(
|
111
|
+
<<-'EOS',
|
112
|
+
00000000 (3f) [21] 61 62 (3f) [21] 63 64 │(?)[!]ab│(?)[!]cd│
|
113
|
+
00000008
|
114
|
+
EOS
|
115
|
+
hexdump('?!ab?!cd', style: { 0x21 => ->(s) { "[#{s}]" }, 0x3f => ->(s) { "(#{s})" } })
|
116
|
+
)
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_style_marker
|
120
|
+
assert_lines_equal(
|
121
|
+
<<-'EOS',
|
122
|
+
00000000 74 65 73 74 74 65 73 74 74 65 73 74 │test☆test☆test│
|
123
|
+
0000000c
|
124
|
+
EOS
|
125
|
+
hexdump('testtesttest', style: { marker: ->(_) { '☆' } })
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_style_printable
|
130
|
+
assert_lines_equal(
|
131
|
+
<<-'EOS',
|
132
|
+
00000000 14 15 92 [65] [35] 89 [79] │···[e]│[5]·[y]│
|
133
|
+
00000007
|
134
|
+
EOS
|
135
|
+
hexdump("\x14\x15\x92\x65\x35\x89\x79", style: { printable: ->(s) { "[#{s}]" } })
|
136
|
+
)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_style_unprintable
|
140
|
+
assert_lines_equal(
|
141
|
+
<<-'EOS',
|
142
|
+
00000000 [14] [15] [92] 65 35 [89] 79 │[·][·][·]e│5[·]y│
|
143
|
+
00000007
|
144
|
+
EOS
|
145
|
+
hexdump("\x14\x15\x92\x65\x35\x89\x79", style: { unprintable: ->(s) { "[#{s}]" } })
|
146
|
+
)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_highlight
|
150
|
+
orig_verbose = $VERBOSE
|
151
|
+
orig_style = HIGHLIGHT_STYLE
|
152
|
+
begin
|
153
|
+
$VERBOSE = nil
|
154
|
+
::Pwnlib::Util::HexDump::ClassMethods.const_set(:HIGHLIGHT_STYLE, ->(s) { "#{s}!" })
|
155
|
+
assert_lines_equal(<<-'EOS', hexdump('abcdefghi', highlight: 'aeiou'))
|
156
|
+
00000000 61! 62 63 64 65! 66 67 68 69! │a!bcd│e!fgh│i!│
|
157
|
+
00000009
|
158
|
+
EOS
|
159
|
+
ensure
|
160
|
+
::Pwnlib::Util::HexDump::ClassMethods.const_set(:HIGHLIGHT_STYLE, orig_style)
|
161
|
+
$VERBOSE = orig_verbose
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
class QQStream
|
166
|
+
def read(n)
|
167
|
+
'Q' * n
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_iterator_lazy
|
172
|
+
assert_equal(<<-'EOS'.lines.map(&:chomp), hexdump_iter(QQStream.new, skip: false).take(4))
|
173
|
+
00000000 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 │QQQQ│QQQQ│QQQQ│QQQQ│
|
174
|
+
00000010 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 │QQQQ│QQQQ│QQQQ│QQQQ│
|
175
|
+
00000020 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 │QQQQ│QQQQ│QQQQ│QQQQ│
|
176
|
+
00000030 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 │QQQQ│QQQQ│QQQQ│QQQQ│
|
177
|
+
EOS
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
# encoding: ASCII-8BIT
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'pwnlib/util/packing'
|
5
|
+
|
6
|
+
class PackingTest < MiniTest::Test
|
7
|
+
include ::Pwnlib::Util::Packing::ClassMethods
|
8
|
+
|
9
|
+
def test_pack
|
10
|
+
assert_equal('ABC',
|
11
|
+
pack(0x414243, bits: 24, endian: 'big', signed: true))
|
12
|
+
assert_equal('CBA',
|
13
|
+
pack(0x414243, bits: 24, endian: 'little', signed: true))
|
14
|
+
|
15
|
+
assert_equal("\x81BC", pack(0x814243, bits: 24, endian: 'big', signed: false))
|
16
|
+
err = assert_raises(ArgumentError) do
|
17
|
+
pack(0x814243, bits: 23, endian: 'big', signed: false)
|
18
|
+
end
|
19
|
+
assert_match(/does not fit/, err.message)
|
20
|
+
|
21
|
+
assert_equal("\x00\x81BC", pack(0x814243, bits: 25, endian: 'big', signed: true))
|
22
|
+
err = assert_raises(ArgumentError) do
|
23
|
+
pack(0x814243, bits: 24, endian: 'big', signed: true)
|
24
|
+
end
|
25
|
+
assert_match(/does not fit/, err.message)
|
26
|
+
|
27
|
+
assert_equal("\xff", pack(-1, bits: 'all', endian: 'little', signed: true))
|
28
|
+
assert_equal("\xff\x00", pack(-256, bits: 'all', endian: 'big', signed: true))
|
29
|
+
assert_equal("\xde\xad\xbe\xef",
|
30
|
+
pack(0xdeadbeef, bits: 'all', endian: 'big', signed: false))
|
31
|
+
assert_equal("\x05\x04\x03\x02\x01",
|
32
|
+
pack(0x0102030405, bits: 'all', endian: 'little', signed: true))
|
33
|
+
assert_equal("\x00\x00\x00\x80\x00",
|
34
|
+
pack(0x80000000, bits: 'all', endian: 'little', signed: true))
|
35
|
+
|
36
|
+
assert_equal('ABC', pack(0x414243, bits: 24, endian: 'big'))
|
37
|
+
|
38
|
+
err = assert_raises(ArgumentError) { pack(-514, bits: 'all', signed: 'unsigned') }
|
39
|
+
assert_match(/Can't pack negative number/, err.message)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_unpack
|
43
|
+
assert_equal(0x55aa,
|
44
|
+
unpack("\xaa\x55", bits: 16, endian: 'little', signed: false))
|
45
|
+
assert_equal(0xaa55,
|
46
|
+
unpack("\xaa\x55", bits: 16, endian: 'big', signed: false))
|
47
|
+
assert_equal(-0x55ab, unpack("\xaa\x55", bits: 16, endian: 'big', signed: true))
|
48
|
+
assert_equal(0x2a55, unpack("\xaa\x55", bits: 15, endian: 'big', signed: true))
|
49
|
+
assert_equal(0x0302ff,
|
50
|
+
unpack("\xff\x02\x03", bits: 'all', endian: 'little', signed: true))
|
51
|
+
assert_equal(-0xfdfd, unpack("\xff\x02\x03", bits: 'all', endian: 'big', signed: true))
|
52
|
+
assert_equal(0x80000000,
|
53
|
+
unpack("\x00\x00\x00\x80\x00", bits: 'all', endian: 'little', signed: true))
|
54
|
+
|
55
|
+
err = assert_raises(ArgumentError) do
|
56
|
+
unpack("\xff\xff", bits: 8, endian: 'big', signed: false)
|
57
|
+
end
|
58
|
+
assert_match(/does not match/, err.message)
|
59
|
+
|
60
|
+
assert_equal(0x414243, unpack('ABC', bits: 'all', endian: 'big', signed: false))
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_unpack_many
|
64
|
+
assert_equal([0x55aa, 0x33cc],
|
65
|
+
unpack_many("\xaa\x55\xcc\x33", bits: 16, endian: 'little', signed: false))
|
66
|
+
assert_equal([0xaa55, 0xcc33],
|
67
|
+
unpack_many("\xaa\x55\xcc\x33", bits: 16, endian: 'big', signed: false))
|
68
|
+
assert_equal([-0x55ab, -0x33cd],
|
69
|
+
unpack_many("\xaa\x55\xcc\x33", bits: 16, endian: 'big', signed: true))
|
70
|
+
assert_equal([0x0302ff],
|
71
|
+
unpack_many("\xff\x02\x03", bits: 'all', endian: 'little', signed: true))
|
72
|
+
assert_equal([-0xfdfd],
|
73
|
+
unpack_many("\xff\x02\x03", bits: 'all', endian: 'big', signed: true))
|
74
|
+
|
75
|
+
err = assert_raises(ArgumentError) { unpack_many('ABCD', bits: 12) }
|
76
|
+
assert_match(/bits must be a multiple of 8/, err.message)
|
77
|
+
|
78
|
+
err = assert_raises(ArgumentError) { unpack_many('ABC', bits: 16) }
|
79
|
+
assert_match(/must be a multiple of bytes/, err.message)
|
80
|
+
|
81
|
+
assert_equal([0x41, 0x42, 0x43, 0x44], unpack_many('ABCD', bits: 8))
|
82
|
+
assert_equal([0x4142, 0x4344],
|
83
|
+
unpack_many('ABCD', bits: 16, endian: 'big', signed: 'signed'))
|
84
|
+
assert_equal([-2, -1],
|
85
|
+
unpack_many("\xff\xfe\xff\xff", bits: 16, endian: 'big', signed: 'signed'))
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_ps
|
89
|
+
assert_equal('A', p8(0x41))
|
90
|
+
assert_equal('BA', p16(0x4142))
|
91
|
+
assert_equal('DCBA', p32(0x41424344))
|
92
|
+
assert_equal('4321DCBA', p64(0x4142434431323334))
|
93
|
+
|
94
|
+
assert_equal('AB', p16(0x4142, endian: 'big'))
|
95
|
+
assert_equal('ABCD', p32(0x41424344, endian: 'big'))
|
96
|
+
assert_equal('ABCD1234', p64(0x4142434431323334, endian: 'big'))
|
97
|
+
|
98
|
+
assert_equal("\xff\xff\xff\xff", p32(-1))
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_us
|
102
|
+
assert_equal(0x41, u8('A'))
|
103
|
+
assert_equal(0x4142, u16('BA'))
|
104
|
+
assert_equal(0x41424344, u32('DCBA'))
|
105
|
+
assert_equal(0x4142434431323334, u64('4321DCBA'))
|
106
|
+
|
107
|
+
assert_equal(0x4142, u16('AB', endian: 'big'))
|
108
|
+
assert_equal(0x41424344, u32('ABCD', endian: 'big'))
|
109
|
+
assert_equal(0x4142434431323334, u64('ABCD1234', endian: 'big'))
|
110
|
+
|
111
|
+
assert_equal(0xFFFFFFFF, u32("\xff\xff\xff\xff", signed: false))
|
112
|
+
assert_equal(-1, u32("\xff\xff\xff\xff", signed: true))
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_up_rand
|
116
|
+
srand(217)
|
117
|
+
[8, 16, 32, 64].each do |sz|
|
118
|
+
u = ->(*x) { public_send("u#{sz}", *x) }
|
119
|
+
p = ->(*x) { public_send("p#{sz}", *x) }
|
120
|
+
100.times do
|
121
|
+
limit = (1 << sz)
|
122
|
+
val = rand(0...limit)
|
123
|
+
assert_equal(val, u[p[val, signed: false], signed: false])
|
124
|
+
|
125
|
+
limit = (1 << (sz - 1))
|
126
|
+
val = rand(-limit...limit)
|
127
|
+
assert_equal(val, u[p[val, signed: true], signed: true])
|
128
|
+
|
129
|
+
rs = Array.new(sz / 8) { rand(256).chr }.join
|
130
|
+
assert_equal(rs, p[u[rs, signed: false], signed: false])
|
131
|
+
assert_equal(rs, p[u[rs, signed: true], signed: true])
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_make_packer
|
137
|
+
p = context.local(bits: 32, signed: 'no') { make_packer(endian: 'be') }
|
138
|
+
assert_equal("\x00\x00\x00A", p[0x41])
|
139
|
+
|
140
|
+
context.local(bits: 64, endian: 'le', signed: true) do
|
141
|
+
assert_equal("\x00\x00\x00A", p[0x41])
|
142
|
+
end
|
143
|
+
|
144
|
+
p = make_packer(bits: 24)
|
145
|
+
assert_equal("B\x00\x00", p[0x42])
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_make_unpacker
|
149
|
+
u = context.local(bits: 32, signed: 'no') { make_unpacker(endian: 'be') }
|
150
|
+
assert_equal(0x41, u["\x00\x00\x00A"])
|
151
|
+
|
152
|
+
context.local(bits: 64, endian: 'le', signed: true) do
|
153
|
+
assert_equal(0x41, u["\x00\x00\x00A"])
|
154
|
+
end
|
155
|
+
|
156
|
+
u = make_unpacker(bits: 24)
|
157
|
+
assert_equal(0x42, u["B\x00\x00"])
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_flat
|
161
|
+
assert_equal("\x01\x00testABABABABABAB",
|
162
|
+
flat(1, 'test', [[['AB'] * 2] * 3], endian: 'le', bits: 16))
|
163
|
+
assert_equal('234', flat([1, [2, 3]]) { |x| (x + 1).to_s })
|
164
|
+
|
165
|
+
err = assert_raises(ArgumentError) { flat(1.23) }
|
166
|
+
assert_match(/flat does not support values of type/, err.message)
|
167
|
+
end
|
168
|
+
end
|