|
| 1 | +#!/usr/bin/env rspec |
| 2 | +require 'spec_helper' |
| 3 | + |
| 4 | +describe "the str2saltedsha512 function" do |
| 5 | + before :all do |
| 6 | + Puppet::Parser::Functions.autoloader.loadall |
| 7 | + end |
| 8 | + |
| 9 | + before :each do |
| 10 | + @scope = Puppet::Parser::Scope.new |
| 11 | + end |
| 12 | + |
| 13 | + it "should exist" do |
| 14 | + Puppet::Parser::Functions.function("str2saltedsha512").should == "function_str2saltedsha512" |
| 15 | + end |
| 16 | + |
| 17 | + it "should raise a ParseError if there is less than 1 argument" do |
| 18 | + expect { @scope.function_str2saltedsha512([]) }.should( raise_error(Puppet::ParseError) ) |
| 19 | + end |
| 20 | + |
| 21 | + it "should raise a ParseError if there is more than 1 argument" do |
| 22 | + expect { @scope.function_str2saltedsha512(['foo', 'bar', 'baz']) }.should( raise_error(Puppet::ParseError) ) |
| 23 | + end |
| 24 | + |
| 25 | + it "should return a salted-sha512 password hash 136 characters in length" do |
| 26 | + result = @scope.function_str2saltedsha512(["password"]) |
| 27 | + result.length.should(eq(136)) |
| 28 | + end |
| 29 | + |
| 30 | + it "should raise an error if you pass a non-string password" do |
| 31 | + expect { @scope.function_str2saltedsha512([1234]) }.should( raise_error(Puppet::ParseError) ) |
| 32 | + end |
| 33 | + |
| 34 | + it "should generate a valid password" do |
| 35 | + # Allow the function to generate a password based on the string 'password' |
| 36 | + password_hash = @scope.function_str2saltedsha512(["password"]) |
| 37 | + |
| 38 | + # Separate the Salt and Password from the Password Hash |
| 39 | + salt = password_hash[0..7] |
| 40 | + password = password_hash[8..-1] |
| 41 | + |
| 42 | + # Convert the Salt and Password from Hex to Binary Data |
| 43 | + str_salt = Array(salt.lines).pack('H*') |
| 44 | + str_password = Array(password.lines).pack('H*') |
| 45 | + |
| 46 | + # Combine the Binary Salt with 'password' and compare the end result |
| 47 | + saltedpass = Digest::SHA512.digest(str_salt + 'password') |
| 48 | + result = (str_salt + saltedpass).unpack('H*')[0] |
| 49 | + result.should == password_hash |
| 50 | + end |
| 51 | +end |
0 commit comments