Skip to content

Commit 9cef210

Browse files
committed
Land rapid7#3170, Array#sample for Rex::Text
2 parents 2972220 + 1b0fe74 commit 9cef210

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

lib/rex/text.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,12 +1386,12 @@ def self.compress(str)
13861386
# Randomize the whitespace in a string
13871387
#
13881388
def self.randomize_space(str)
1389+
set = ["\x09", "\x20", "\x0d", "\x0a"]
13891390
str.gsub(/\s+/) { |s|
13901391
len = rand(50)+2
1391-
set = "\x09\x20\x0d\x0a"
13921392
buf = ''
13931393
while (buf.length < len)
1394-
buf << set[rand(set.length),1]
1394+
buf << set.sample
13951395
end
13961396

13971397
buf
@@ -1582,37 +1582,37 @@ def self.rand_hostname
15821582
(rand(5) + 1).times {
15831583
host.push(Rex::Text.rand_text_alphanumeric(rand(10) + 1))
15841584
}
1585-
host.push(TLDs[rand(TLDs.size)])
1585+
host.push(TLDs.sample)
15861586
host.join('.').downcase
15871587
end
15881588

15891589
# Generate a state
15901590
def self.rand_state()
1591-
States[rand(States.size)]
1591+
States.sample
15921592
end
15931593

15941594
# Generate a surname
15951595
def self.rand_surname
1596-
Surnames[rand(Surnames.size)]
1596+
Surnames.sample
15971597
end
15981598

15991599
# Generate a name
16001600
def self.rand_name
16011601
if rand(10) % 2 == 0
1602-
Names_Male[rand(Names_Male.size)]
1602+
Names_Male.sample
16031603
else
1604-
Names_Female[rand(Names_Female.size)]
1604+
Names_Female.sample
16051605
end
16061606
end
16071607

16081608
# Generate a male name
16091609
def self.rand_name_male
1610-
Names_Male[rand(Names_Male.size)]
1610+
Names_Male.sample
16111611
end
16121612

16131613
# Generate a female name
16141614
def self.rand_name_female
1615-
Names_Female[rand(Names_Female.size)]
1615+
Names_Female.sample
16161616
end
16171617

16181618
# Generate a random mail address

spec/lib/rex/text_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@
114114
end
115115
end
116116

117+
context ".randomize_space" do
118+
let (:sample_text) { "The quick brown sploit jumped over the lazy A/V" }
119+
let (:spaced_text) { described_class.randomize_space(sample_text) }
120+
it "should return a string with at least one new space characater" do
121+
spaced_text.should match /\x09\x0d\x0a/
122+
end
123+
124+
it "should not otherwise be mangled" do
125+
normalized_text = spaced_text.gsub(/[\x20\x09\x0d\x0a]+/m, " ")
126+
normalized_text.should eq(sample_text)
127+
end
128+
end
129+
117130
end
118131
end
119132

0 commit comments

Comments
 (0)