This patch extends the stress profile generator to add variable

definition and references.
This commit is contained in:
Steve Beattie 2011-03-28 21:07:44 -07:00
parent 162b49b417
commit bb31faff1b

View file

@ -27,13 +27,16 @@ def get_random_regex()
end
end
def get_random_path()
def get_random_path(symtab)
# Always prefix with a non-regex element
out = "/#{get_random_name(rand(10) + 4)}"
0.upto(rand(20) + 2) do
if rand(4) == 0
case rand(50)
when 0
out = "#{out}/@{#{symtab.get_symbol}}"
when 1..4
out = "#{out}/#{get_random_regex}"
else
when 5..49
out = "#{out}/#{get_random_name(rand(10) + 4)}"
end
end
@ -53,15 +56,48 @@ def get_random_mode()
end
end
class SymTab
include Enumerable
def initialize()
@symtab = { }
end
def dump
@symtab.each do | symbol, values |
out = "@{#{symbol}}="
values.each { |v| out += " #{v}"}
puts out
end
end
def each_decl_s
@symtab.each do | symbol, values |
out = "@{#{symbol}}="
values.each { |v| out += " #{v}"}
yield(out)
end
end
def get_symbol
# variables need to be prefixed with letters, hence the VAR prefix
symbol = "VAR_" + get_random_name(rand(6) + 4)
values = []
0.upto(rand(4)) { values << get_random_name(rand(8) + 2) }
@symtab[symbol] = values
return symbol
end
end
# Abstract class, though may become a real class for generation of
# random types of rules
class Rule
end
class FileRule < Rule
def initialize(path=get_random_path(), mode=get_random_mode())
@path = path
@mode = mode
def initialize(symtab=nil)
@path = get_random_path(symtab)
@mode = get_random_mode()
end
def to_s
@ -69,6 +105,13 @@ class FileRule < Rule
end
end
class NamedFileRule < FileRule
def initialize(path, mode)
@path = path
@mode = mode
end
end
class CapRule < Rule
CAP_LIST = [
"chown",
@ -221,10 +264,11 @@ class Profile
@name = "/does/not/exist/#{@rvalue}"
@rules = []
@flags = Flags.new()
@symtab = SymTab.new()
end
def generate_rules
@rules << FileRule.new(@name, "rm").to_s
@rules << NamedFileRule.new(@name, "rm").to_s
0.upto(rand($max_rules - $min_rules) + $min_rules) do |x|
case rand(100)
when 0..14
@ -232,11 +276,21 @@ class Profile
when 15..24
@rules << RlimitRule.new.to_s
when 25..100
@rules << FileRule.new.to_s
@rules << FileRule.new(symtab=@symtab).to_s
end
end
end
def decl_s
out = []
out << "#"
out << "# variable declarations for #{@name}"
out << "# generated by #{__FILE__}"
out << "#"
@symtab.each_decl_s { | decl | out << decl }
out << ""
end
def to_s
out = []
out << "#"
@ -269,6 +323,7 @@ def gen_profiles_dir(profiles)
profiles.each do |p|
open("#{dirname}/#{p.rvalue}.sd", File::CREAT|File::EXCL|File::WRONLY, 0644) do |file|
file.puts(prefix_to_s(p.name))
file.puts(p.decl_s)
file.puts(p.to_s)
end
end
@ -282,6 +337,7 @@ def gen_profiles_file(profiles)
filename = "#{Dir.tmpdir}/#{$prefix}-#{get_random_name(32)}.sd"
File.open(filename, File::CREAT|File::EXCL|File::WRONLY, 0644) do |file|
file.puts(prefix_to_s(filename))
profiles.each { |p| file.puts(p.decl_s) }
profiles.each { |p| file.puts(p.to_s) }
end
rescue Errno::EEXIST