Skip to content

Commit 35f788a

Browse files
committed
Land rapid7#3848 - Add specs for Rex::Arch
2 parents fe18425 + db587af commit 35f788a

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

spec/lib/rex/arch_spec.rb

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# -*- coding:binary -*-
2+
require 'spec_helper'
3+
4+
require 'rex/arch'
5+
6+
describe Rex::Arch do
7+
8+
describe ".adjust_stack_pointer" do
9+
subject { described_class.adjust_stack_pointer(arch, adjustment) }
10+
let(:adjustment) { 100 }
11+
12+
context "when arch is ARCH_X86" do
13+
let(:arch) { ARCH_X86 }
14+
15+
it "emits an ESP adjustment instruction" do
16+
is_expected.to be_a_kind_of(String)
17+
end
18+
end
19+
20+
context "when arch isn't ARCH_X86" do
21+
let(:arch) { ARCH_FIREFOX }
22+
23+
it "returns nil" do
24+
is_expected.to be_nil
25+
end
26+
end
27+
28+
context "when arch is an array" do
29+
let(:arch) { [ARCH_X86, ARCH_FIREFOX] }
30+
31+
it "uses the first arch in the array" do
32+
is_expected.to be_a_kind_of(String)
33+
end
34+
end
35+
end
36+
37+
describe ".pack_addr" do
38+
subject { described_class.pack_addr(arch, addr) }
39+
40+
context "when arch is ARCH_X86" do
41+
let(:arch) { ARCH_X86 }
42+
let(:addr) { 0x41424344 }
43+
it "packs addr as 32-bit unsigned, little-endian" do
44+
is_expected.to eq("DCBA")
45+
end
46+
end
47+
48+
context "when arch is ARCH_X86_64" do
49+
let(:arch) { ARCH_X86_64 }
50+
let(:addr) { 0x4142434445464748 }
51+
it "packs addr as 62-bit unsigned, little-endian" do
52+
is_expected.to eq("HGFEDCBA")
53+
end
54+
end
55+
56+
context "when arch is ARCH_X64" do
57+
let(:arch) { ARCH_X64 }
58+
let(:addr) { 0x4142434445464748 }
59+
it "packs addr as 62-bit unsigned, little-endian" do
60+
is_expected.to eq("HGFEDCBA")
61+
end
62+
end
63+
64+
context "when arch is ARCH_MIPS" do
65+
let(:arch) { ARCH_MIPS }
66+
let(:addr) { 0x41424344 }
67+
it "packs addr as 32-bit unsigned, big-endian" do
68+
is_expected.to eq("ABCD")
69+
end
70+
end
71+
72+
context "when arch is ARCH_MIPSBE" do
73+
let(:arch) { ARCH_MIPSBE }
74+
let(:addr) { 0x41424344 }
75+
it "packs addr as 32-bit unsigned, big-endian" do
76+
is_expected.to eq("ABCD")
77+
end
78+
end
79+
80+
context "when arch is ARCH_MIPSLE" do
81+
let(:arch) { ARCH_MIPSLE }
82+
let(:addr) { 0x41424344 }
83+
it "packs addr as 32-bit unsigned, little-endian" do
84+
is_expected.to eq("DCBA")
85+
end
86+
end
87+
88+
context "when arch is ARCH_PPC" do
89+
let(:arch) { ARCH_PPC }
90+
let(:addr) { 0x41424344 }
91+
it "packs addr as 32-bit unsigned, big-endian" do
92+
is_expected.to eq("ABCD")
93+
end
94+
end
95+
96+
context "when arch is ARCH_SPARC" do
97+
let(:arch) { ARCH_SPARC }
98+
let(:addr) { 0x41424344 }
99+
it "packs addr as 32-bit unsigned, big-endian" do
100+
is_expected.to eq("ABCD")
101+
end
102+
end
103+
104+
context "when arch is ARCH_ARMLE" do
105+
let(:arch) { ARCH_ARMLE }
106+
let(:addr) { 0x41424344 }
107+
it "packs addr as 32-bit unsigned, little-endian" do
108+
is_expected.to eq("DCBA")
109+
end
110+
end
111+
112+
context "when arch is ARCH_ARMBE" do
113+
let(:arch) { ARCH_ARMBE }
114+
let(:addr) { 0x41424344 }
115+
it "packs addr as 32-bit unsigned, big-endian" do
116+
is_expected.to eq("ABCD")
117+
end
118+
end
119+
120+
context "when arch is invalid" do
121+
let(:arch) { ARCH_FIREFOX }
122+
let(:addr) { 0x41424344 }
123+
124+
it "packs addr as 32-bit unsigned, big-endian" do
125+
is_expected.to be_nil
126+
end
127+
end
128+
129+
context "when arch is an Array" do
130+
let(:arch) { [ARCH_ARMLE, ARCH_ARMBE, ARCH_X86_64] }
131+
let(:addr) { 0x41424344 }
132+
it "packs addr using the first architecture in the array" do
133+
is_expected.to eq("DCBA")
134+
end
135+
end
136+
end
137+
138+
describe ".endian" do
139+
140+
let(:endianesses) do
141+
{
142+
ARCH_X86 => ENDIAN_LITTLE,
143+
ARCH_X86_64 => ENDIAN_LITTLE,
144+
ARCH_MIPS => ENDIAN_BIG,
145+
ARCH_MIPSLE => ENDIAN_LITTLE,
146+
ARCH_MIPSBE => ENDIAN_BIG,
147+
ARCH_PPC => ENDIAN_BIG,
148+
ARCH_SPARC => ENDIAN_BIG,
149+
ARCH_ARMLE => ENDIAN_LITTLE,
150+
ARCH_ARMBE => ENDIAN_BIG
151+
}
152+
end
153+
subject { described_class.endian(arch) }
154+
155+
context "when recognized arch" do
156+
it "returns its endianess" do
157+
endianesses.each_key do |arch|
158+
expect(described_class.endian(arch)).to eq(endianesses[arch])
159+
end
160+
end
161+
end
162+
163+
context "when not recognized arch" do
164+
let(:arch) { ARCH_FIREFOX }
165+
it "returns ENDIAN_LITTLE" do
166+
is_expected.to eq(ENDIAN_LITTLE)
167+
end
168+
end
169+
170+
context "when arch is an array" do
171+
let(:arch) { [ARCH_X86, ARCH_MIPSBE] }
172+
it "returns first arch endianess" do
173+
is_expected.to eq(ENDIAN_LITTLE)
174+
end
175+
end
176+
end
177+
end

0 commit comments

Comments
 (0)