Signed-off-by: Brice Figureau <[email protected]>
---
 lib/puppet/util/checksum_stream.rb |   20 ++++++++++++++++++++
 lib/puppet/util/checksums.rb       |   12 ++++++++++++
 spec/unit/util/checksum_stream.rb  |   23 +++++++++++++++++++++++
 spec/unit/util/checksums.rb        |   12 ++++++++++++
 4 files changed, 67 insertions(+), 0 deletions(-)
 create mode 100644 lib/puppet/util/checksum_stream.rb
 create mode 100644 spec/unit/util/checksum_stream.rb

diff --git a/lib/puppet/util/checksum_stream.rb 
b/lib/puppet/util/checksum_stream.rb
new file mode 100644
index 0000000..0add5fd
--- /dev/null
+++ b/lib/puppet/util/checksum_stream.rb
@@ -0,0 +1,20 @@
+
+class Puppet::Util::ChecksumStream
+    attr_accessor :digest
+
+    def initialize(digest)
+        @digest = digest.reset
+    end
+
+    def update(chunk)
+        digest << chunk
+    end
+
+    def checksum
+        digest.hexdigest
+    end
+
+    def to_s
+        "checksum: #{digest}"
+    end
+end
\ No newline at end of file
diff --git a/lib/puppet/util/checksums.rb b/lib/puppet/util/checksums.rb
index 98bf5de..1b73a86 100644
--- a/lib/puppet/util/checksums.rb
+++ b/lib/puppet/util/checksums.rb
@@ -1,3 +1,5 @@
+require 'puppet/util/checksum_stream'
+
 # A stand-alone module for calculating checksums
 # in a generic way.
 module Puppet::Util::Checksums
@@ -34,6 +36,11 @@ module Puppet::Util::Checksums
         md5_file(filename, true)
     end
 
+    def md5_stream
+        require 'digest/md5'
+        Puppet::Util::ChecksumStream.new(Digest::MD5.new())
+    end
+
     # Return the :mtime timestamp of a file.
     def mtime_file(filename)
         File.stat(filename).send(:mtime)
@@ -63,6 +70,11 @@ module Puppet::Util::Checksums
         sha1_file(filename, true)
     end
 
+    def sha1_stream
+        require 'digest/sha1'
+        Puppet::Util::ChecksumStream.new(Digest::SHA1.new())
+    end
+
     # Return the :ctime of a file.
     def ctime_file(filename)
         File.stat(filename).send(:ctime)
diff --git a/spec/unit/util/checksum_stream.rb 
b/spec/unit/util/checksum_stream.rb
new file mode 100644
index 0000000..0ace637
--- /dev/null
+++ b/spec/unit/util/checksum_stream.rb
@@ -0,0 +1,23 @@
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'puppet/util/checksum_stream'
+
+describe Puppet::Util::ChecksumStream do
+
+    before(:each) do
+        @digest = stub 'digest'
+        @digest.stubs(:reset).returns(@digest)
+        @sum = Puppet::Util::ChecksumStream.new(@digest)
+    end
+
+    it "should add to the digest when update" do
+        @digest.expects(:<<).with("content")
+        @sum.update("content")
+    end
+
+    it "should produce the final checksum" do
+        @digest.expects(:hexdigest).returns("DEADBEEF")
+        @sum.checksum.should == "DEADBEEF"
+    end
+end
\ No newline at end of file
diff --git a/spec/unit/util/checksums.rb b/spec/unit/util/checksums.rb
index d31d7a0..a27229e 100755
--- a/spec/unit/util/checksums.rb
+++ b/spec/unit/util/checksums.rb
@@ -15,6 +15,7 @@ describe Puppet::Util::Checksums do
 
     content_sums = [:md5, :md5lite, :sha1, :sha1lite]
     file_only = [:ctime, :mtime]
+    stream = [:md5_stream, :sha1_stream]
 
     content_sums.each do |sumtype|
         it "should be able to calculate %s sums from strings" % sumtype do
@@ -28,6 +29,12 @@ describe Puppet::Util::Checksums do
         end
     end
 
+    stream.each do |sumtype|
+        it "should be able to calculate %s sums from streams" % sumtype do
+            @summer.should be_respond_to(sumtype.to_s)
+        end
+    end
+
     it "should have a method for stripping a sum type from an existing 
checksum" do
         @summer.sumtype("{md5}asdfasdfa").should == "md5"
     end
@@ -62,6 +69,11 @@ describe Puppet::Util::Checksums do
 
                 @summer.send(sum.to_s + "_file", file).should == :mydigest
             end
+
+            it "should use #{klass} to seed checksum stream" do
+                @summer.send("#{sum}_stream").should 
be_instance_of(Puppet::Util::ChecksumStream)
+                @summer.send("#{sum}_stream").digest.should 
be_instance_of(klass)
+            end
         end
     end
 
-- 
1.6.6.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to