[or-cvs] r10923: fuzz permutations (topf/trunk/lib)

benedikt at seul.org benedikt at seul.org
Tue Jul 24 16:47:27 UTC 2007


Author: benedikt
Date: 2007-07-24 12:47:27 -0400 (Tue, 24 Jul 2007)
New Revision: 10923

Modified:
   topf/trunk/lib/fuzz.rb
Log:
fuzz permutations

Modified: topf/trunk/lib/fuzz.rb
===================================================================
--- topf/trunk/lib/fuzz.rb	2007-07-24 10:13:46 UTC (rev 10922)
+++ topf/trunk/lib/fuzz.rb	2007-07-24 16:47:27 UTC (rev 10923)
@@ -4,11 +4,16 @@
 
     # This Class holds all tests to related to a given field-type
     class Tests
-        def initialize
+        attr_reader :tests, :permutations
+        def initialize(struct)
             @char_tests      = []
             @unsigned_tests  = []
             @signed_tests    = []
             @octet_tests     = []
+            @permutations    = [] 
+            @tests           = []
+            @struct          = struct
+            @count           = 0
         end
         def register(test)
             raise "argument must be a Fuzz::Test object" if !test.is_a?(Fuzz::Test)
@@ -23,10 +28,78 @@
                     @octet_tests.push test
             end
         end
-        def test_struct(struct)
-            raise "argument must be a BitStruct object" if !struct.is_a?(BitStruct)
-             
+        def generate_test
+            generate_test_data
+            permutate
+            @count = 0
         end
+
+        def get_test
+            raise "no tests have been generated yet" if !@permutations or !@tests
+            test = []
+            perm = @permutations.collect{|element| element[@count] }
+            tempcount = 0
+            @count += 1
+            @tests.collect{ |element| 
+                result = element[ perm[tempcount] ]
+                tempcount+=1
+                result
+            }
+
+        end
+    private
+        def generate_test_data
+            result = []
+            raise "argument must be a BitStruct object" if !@struct.is_a?(BitStruct)
+            @struct.fields.each do |field|
+                temp = []
+                arg = @struct.method(field.name).call
+                length = field.length
+                case field.class.to_s
+                when "BitStruct::SignedField"
+                    @signed_tests.each do |test|
+                        temp.push( test.run(arg, length) )
+                    end
+                    result.push(temp)
+                when "BitStruct::UnsignedField"
+                    @unsigned_tests.each do |test|
+                        temp.push( test.run(arg, length) )
+                    end
+                    result.push(temp)
+                when "BitStruct::TextField" || "BitStruct::CharField"
+                    @char_tests.each do |test|
+                        temp.push( test.run(arg, length) )
+                    end
+                    result.push(temp)
+                end
+            end
+            @tests = result
+        end
+        def permutate
+            raise "tests have not been generated yet" if !@tests
+
+            sizes =  @tests.collect {|element| element.size}
+            dimension = sizes.inject(1){|prod, element| prod*element }
+            permutations = Array.new(sizes.size, Array.new(dimension, 1) )
+            result = []
+            permutations.each_with_index do |element, index|
+                count = 0 
+                temp  = 1
+                tempdimension = sizes[(index+1)..-1].inject(1){|prod, el| prod*el}
+                result.push element.collect{|entry|
+                    if count == tempdimension
+                       temp+=1
+                       temp = 1 if temp == (sizes[index]+1) 
+                       
+                       count = 0
+                    end
+                    count+=1
+                    temp  
+                }
+            end
+            @permutations = result
+        end
+
     end
 
     # This Class holds our test which is a ruby-block that must be able to process one argument
@@ -48,8 +121,8 @@
                 end
             @test = block
         end
-        def run(arg)
-                @test.call arg
+        def run(arg, size)
+                @test.call arg, size
         end
     end
     # sample test-case
@@ -203,3 +276,7 @@
         end        
     end 
 end
+
+begin
+    
+end 



More information about the tor-commits mailing list