PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/verifydeep_spec.rb

https://bitbucket.org/torresj/hashar-deep
Ruby | 77 lines | 43 code | 2 blank | 32 comment | 0 complexity | 42dac110f2f24b056018bfd641687653 MD5 | raw file
  1. #
  2. # verifydeep_spec.rb
  3. # https://bitbucket.org/torresj/hashar-deep
  4. # Licensed under the terms of the MIT License, as specified below.
  5. #
  6. # Copyright (c) 2012 Jeremy Torres, https://bitbucket.org/torresj/hashar-deep
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining
  9. # a copy of this software and associated documentation files (the
  10. # "Software"), to deal in the Software without restriction, including
  11. # without limitation the rights to use, copy, modify, merge, publish,
  12. # distribute, sublicense, and/or sell copies of the Software, and to
  13. # permit persons to whom the Software is furnished to do so, subject to
  14. # the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be
  17. # included in all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. #
  27. require "rspec"
  28. require 'openssl'
  29. require_relative "../verify_deep"
  30. describe VerifyDeep do
  31. describe ".algos" do
  32. it "returns a non-null instance" do
  33. VerifyDeep.algos.should_not be_nil
  34. end
  35. it "returns algorithm-to-executable hashes" do
  36. VerifyDeep.algos.keys.should satisfy do |keys|
  37. (keys - [:sha1, :sha256]).empty?
  38. end
  39. end
  40. end
  41. describe ".hash_dir" do
  42. context "when invalid directory" do
  43. it "raises invalid directory error" do
  44. expect {
  45. VerifyDeep.hash_dir("invalid_dir", nil, nil) }.to raise_error
  46. end
  47. end
  48. context "when valid directory" do
  49. before(:all) do
  50. @directory = File.join(File.dirname(__FILE__), "test_files")
  51. @test_files = Dir.glob(File.join(@directory, "*.out"))
  52. end
  53. it "generates successful sha1 hash" do
  54. @test_files.each do |file|
  55. #puts "File: #{file}"
  56. sha1 = OpenSSL::Digest::SHA1.hexdigest(File.read(file))
  57. #puts "Open SSL sha1 #{sha1}"
  58. hash = VerifyDeep.hash_dir(@directory, "*.out", :sha1, false)
  59. #puts hash
  60. hash[sha1].should eq file
  61. end
  62. end
  63. it "generates successful sha256 hash" do
  64. @test_files.each do |file|
  65. #puts "File: #{file}"
  66. sha1 = OpenSSL::Digest::SHA256.hexdigest(File.read(file))
  67. #puts "Open SSL sha1 #{sha1}"
  68. hash = VerifyDeep.hash_dir(@directory, "*.out", :sha256, false)
  69. #puts hash
  70. hash[sha1].should eq file
  71. end
  72. end
  73. end
  74. end
  75. end