Skip to content

Commit 273046d

Browse files
committed
Add a class for generating random identifiers
Will be useful for all kinds of things, but brought about in discussions specifically for Util::EXE in rapid7#2037.
1 parent 8d7396d commit 273046d

File tree

2 files changed

+278
-0
lines changed

2 files changed

+278
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
2+
# A quick way to produce unique random strings that follow the rules of
3+
# identifiers, i.e., begin with a letter and contain only alphanumeric
4+
# characters and underscore.
5+
#
6+
# The advantage of using this class over, say, {Rex::Text.rand_text_alpha}
7+
# each time you need a new identifier is that it ensures you don't have
8+
# collisions.
9+
#
10+
# @example
11+
# vars = Rex::RandomIdentifierGenerator.new
12+
# asp_code = <<-END_CODE
13+
# Sub #{vars[:func]}()
14+
# Dim #{vars[:fso]}
15+
# Set #{vars[:fso]} = CreateObject("Scripting.FileSystemObject")
16+
# ...
17+
# End Sub
18+
# #{vars[:func]}
19+
# END_CODE
20+
#
21+
class Rex::RandomIdentifierGenerator
22+
23+
# Raised when a RandomIdentifierGenerator cannot create any more
24+
# identifiers without collisions.
25+
class ExhaustedSpaceError < StandardError; end
26+
27+
# Default options
28+
DefaultOpts = {
29+
# Arbitrary
30+
:max_length => 12,
31+
:min_length => 3,
32+
# This should be pretty universal for identifier rules
33+
:char_set => Rex::Text::AlphaNumeric+"_",
34+
}
35+
36+
# @param opts [Hash] Options, see {DefaultOpts} for default values
37+
# @option opts :max_length [Fixnum]
38+
# @option opts :min_length [Fixnum]
39+
# @option opts :char_set [String]
40+
def initialize(opts={})
41+
# Holds all identifiers.
42+
@identifiers = {}
43+
44+
@opts = DefaultOpts.merge(opts)
45+
if @opts[:min_length] < 1 || @opts[:max_length] < 1 || @opts[:max_length] < @opts[:min_length]
46+
raise ArgumentError, "Invalid length options"
47+
end
48+
49+
# This is really just the maximum number of shortest names. Since
50+
# this will still be a pretty big number most of the time, don't
51+
# bother calculating the real one, which will potentially be
52+
# expensive, since we're talking about a 36-digit decimal number to
53+
# represent the total possibilities for the range of 10- to
54+
# 20-character identifiers.
55+
#
56+
# 26 because the first char is lowercase alpha, (min_length - 1) and
57+
# not just min_length because it includes that first alpha char.
58+
@max_permutations = 26 * (@opts[:char_set].length ** (@opts[:min_length]-1))
59+
# The real number of permutations could be calculated thusly:
60+
#((@opts[:min_length]-1) .. (@opts[:max_length]-1)).reduce(0) { |a, e|
61+
# a + (26 * @opts[:char_set].length ** e)
62+
#}
63+
end
64+
65+
# Return a unique random identifier for +name+, generating a new one
66+
# if necessary.
67+
#
68+
# @param name [Symbol] A descriptive, intention-revealing name for an
69+
# identifier. This is what you would normally call the variable if
70+
# you weren't generating it.
71+
# @return [String]
72+
def get(name)
73+
return @identifiers[name] if @identifiers[name]
74+
75+
@identifiers[name] = generate
76+
77+
@identifiers[name]
78+
end
79+
alias [] get
80+
81+
# Add a new identifier. Its name will be checked for uniqueness among
82+
# previously-generated names.
83+
#
84+
# @note This should be called *before* any calls to {#get} to avoid
85+
# potential collisions. If you do hit a collision, this method will
86+
# raise.
87+
#
88+
# @param name (see #get)
89+
# @param value [String] The identifier that will be returned by
90+
# subsequent calls to {#get} with the sane +name+.
91+
# @raise RuntimeError if +value+ already exists
92+
# @return [void]
93+
def store(name, value)
94+
95+
case @identifiers.key(value)
96+
when name
97+
# we already have this value and it is associated with this name
98+
# nothing to do here
99+
when nil
100+
# don't have this value yet, so go ahead and just insert
101+
@identifiers[name] = value
102+
else
103+
# then the caller is trying to insert a duplicate
104+
raise RuntimeError, "Value is not unique!"
105+
end
106+
107+
self
108+
end
109+
110+
# Create a random string that satisfies most languages' requirements
111+
# for identifiers.
112+
#
113+
# Note that the first character will always be lowercase alpha.
114+
#
115+
# @example
116+
# rig = Rex::RandomIdentifierGenerator.new
117+
# const = rig.generate { |val| val.capitalize }
118+
# rig.insert(:SOME_CONSTANT, const)
119+
# ruby_code = <<-EOC
120+
# #{rig[:SOME_CONSTANT]} = %q^generated ruby constant^
121+
# def #{rig[:my_method]}; ...; end
122+
# EOC
123+
#
124+
# @param len [Fixnum] Avoid setting this unless a specific size is
125+
# necessary. Default is random within range of min .. max
126+
# @return [String] A string that matches <tt>[a-z][a-zA-Z0-9_]*</tt>
127+
# @yield [String] The identifier before uniqueness checks. This allows
128+
# you to modify the value and still avoid collisions.
129+
def generate(len=nil)
130+
raise ArgumentError, "len must be positive integer" if len && len < 1
131+
raise ExhaustedSpaceError if @identifiers.length >= @max_permutations
132+
133+
# pick a random length within the limits
134+
len ||= rand(@opts[:min_length] .. (@opts[:max_length]))
135+
136+
ident = ""
137+
138+
# XXX: infinite loop if we've exhausted the space. Mitigated by the
139+
# fact that you'd have to call generate at least 26*62 times (in the
140+
# case of 2-character names) to hit it with the default :char_set.
141+
loop do
142+
ident = Rex::Text.rand_text_alpha_lower(1)
143+
ident << Rex::Text.rand_base(len-1, "", @opts[:char_set])
144+
if block_given?
145+
ident = yield ident
146+
end
147+
# Try to make another one if it collides with a previously
148+
# generated one.
149+
break unless @identifiers.value?(ident)
150+
end
151+
152+
ident
153+
end
154+
155+
end
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
require 'spec_helper'
2+
require 'rex/random_identifier_generator'
3+
4+
describe Rex::RandomIdentifierGenerator do
5+
let(:options) do
6+
{ :min_length => 10, :max_length => 20 }
7+
end
8+
9+
subject(:rig) { described_class.new(options) }
10+
11+
it { should respond_to(:generate) }
12+
it { should respond_to(:[]) }
13+
it { should respond_to(:get) }
14+
15+
describe "#generate" do
16+
it "should respect :min_length" do
17+
1000.times do
18+
rig.generate.length.should >= options[:min_length]
19+
end
20+
end
21+
22+
it "should respect :max_length" do
23+
1000.times do
24+
rig.generate.length.should <= options[:max_length]
25+
end
26+
end
27+
28+
it "should allow mangling in a block" do
29+
ident = rig.generate { |identifier| identifier.upcase }
30+
ident.should match(/\A[A-Z0-9_]*\Z/)
31+
32+
ident = subject.generate { |identifier| identifier.downcase }
33+
ident.should match(/\A[a-z0-9_]*\Z/)
34+
35+
ident = subject.generate { |identifier| identifier.gsub("A","B") }
36+
ident.should_not include("A")
37+
end
38+
end
39+
40+
describe "#get" do
41+
let(:options) do
42+
{ :min_length=>3, :max_length=>3 }
43+
end
44+
it "should return the same thing for subsequent calls" do
45+
rig.get(:rspec).should == rig.get(:rspec)
46+
end
47+
it "should not return the same for different names" do
48+
# Statistically...
49+
count = 1000
50+
a = Set.new
51+
count.times do |n|
52+
a.add rig.get(n)
53+
end
54+
a.size.should == count
55+
end
56+
57+
context "with an exhausted set" do
58+
let(:options) do
59+
{ :char_set => "abcd", :min_length=>2, :max_length=>2 }
60+
end
61+
let(:max_permutations) do
62+
# 26 because first char is hardcoded to be lowercase alpha
63+
26 * (options[:char_set].length ** options[:min_length])
64+
end
65+
66+
it "doesn't infinite loop" do
67+
Timeout.timeout(1) do
68+
expect {
69+
(max_permutations + 1).times { |i| rig.get(i) }
70+
}.to raise_error(Rex::RandomIdentifierGenerator::ExhaustedSpaceError)
71+
# don't rescue TimeoutError here because we want that to be a
72+
# failure case
73+
end
74+
end
75+
76+
end
77+
78+
end
79+
80+
describe "#store" do
81+
let(:options) do
82+
{ :char_set => "abcd", :min_length=>8, :max_length=>20 }
83+
end
84+
85+
it "should allow smaller than minimum length" do
86+
value = "a"*(options[:min_length]-1)
87+
rig.store(:spec, value)
88+
rig.get(:spec).should == value
89+
end
90+
91+
it "should allow bigger than maximum length" do
92+
value = "a"*(options[:max_length]+1)
93+
rig.store(:spec, value)
94+
rig.get(:spec).should == value
95+
end
96+
97+
it "should raise if value is not unique" do
98+
value = "a"*(options[:max_length]+1)
99+
rig.store(:spec0, value)
100+
rig.get(:spec0).should == value
101+
expect { rig.store(:spec1, value) }.to raise_error
102+
end
103+
104+
it "should overwrite a previously stored value" do
105+
orig_value = "a"*(options[:max_length])
106+
rig.store(:spec, orig_value)
107+
rig.get(:spec).should == orig_value
108+
109+
new_value = "b"*(options[:max_length])
110+
rig.store(:spec, new_value)
111+
rig.get(:spec).should == new_value
112+
end
113+
114+
it "should overwrite a previously generated value" do
115+
rig.get(:spec)
116+
117+
new_value = "a"*(options[:max_length])
118+
rig.store(:spec, new_value)
119+
rig.get(:spec).should == new_value
120+
end
121+
122+
end
123+
end

0 commit comments

Comments
 (0)