|
| 1 | +#! /usr/bin/env ruby -S rspec |
| 2 | +require 'spec_helper' |
| 3 | + |
| 4 | +describe "the pw_hash function" do |
| 5 | + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } |
| 6 | + |
| 7 | + it "should exist" do |
| 8 | + expect(Puppet::Parser::Functions.function("pw_hash")).to eq("function_pw_hash") |
| 9 | + end |
| 10 | + |
| 11 | + it "should raise an ArgumentError if there are less than 3 arguments" do |
| 12 | + expect { scope.function_pw_hash([]) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) ) |
| 13 | + expect { scope.function_pw_hash(['password']) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) ) |
| 14 | + expect { scope.function_pw_hash(['password', 6]) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) ) |
| 15 | + end |
| 16 | + |
| 17 | + it "should raise an ArgumentError if there are more than 3 arguments" do |
| 18 | + expect { scope.function_pw_hash(['password', 6, 'salt', 5]) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) ) |
| 19 | + end |
| 20 | + |
| 21 | + it "should raise an ArgumentError if the first argument is not a string" do |
| 22 | + expect { scope.function_pw_hash([['password'], 6, 'salt']) }.to( raise_error(ArgumentError, /first argument must be a string/) ) |
| 23 | + # in Puppet 3, numbers are passed as strings, so we can't test that |
| 24 | + end |
| 25 | + |
| 26 | + it "should return nil if the first argument is empty" do |
| 27 | + expect(scope.function_pw_hash(['', 6, 'salt'])).to eq(nil) |
| 28 | + end |
| 29 | + |
| 30 | + it "should raise an ArgumentError if the second argument is an invalid hash type" do |
| 31 | + expect { scope.function_pw_hash(['', 3, 'salt']) }.to( raise_error(ArgumentError, /not a valid hash type/) ) |
| 32 | + end |
| 33 | + |
| 34 | + it "should raise an ArgumentError if the third argument is not a string" do |
| 35 | + expect { scope.function_pw_hash(['password', 6, ['salt']]) }.to( raise_error(ArgumentError, /third argument must be a string/) ) |
| 36 | + # in Puppet 3, numbers are passed as strings, so we can't test that |
| 37 | + end |
| 38 | + |
| 39 | + it "should raise an ArgumentError if the third argument is empty" do |
| 40 | + expect { scope.function_pw_hash(['password', 6, '']) }.to( raise_error(ArgumentError, /third argument must not be empty/) ) |
| 41 | + end |
| 42 | + |
| 43 | + it "should raise an ArgumentError if the third argument has invalid characters" do |
| 44 | + expect { scope.function_pw_hash(['password', 6, '%']) }.to( raise_error(ArgumentError, /characters in salt must be in the set/) ) |
| 45 | + end |
| 46 | + |
| 47 | + it "should return a hashed password" do |
| 48 | + result = scope.function_pw_hash(['password', 6, 'salt']) |
| 49 | + expect(result).to eql('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.') |
| 50 | + end |
| 51 | + |
| 52 | + it "should use the specified salt" do |
| 53 | + result = scope.function_pw_hash(['password', 6, 'salt']) |
| 54 | + expect(result).to match('salt') |
| 55 | + end |
| 56 | + |
| 57 | + it "should use the specified hash type" do |
| 58 | + result1 = scope.function_pw_hash(['password', 1, 'salt']) |
| 59 | + result5 = scope.function_pw_hash(['password', 5, 'salt']) |
| 60 | + result6 = scope.function_pw_hash(['password', 6, 'salt']) |
| 61 | + |
| 62 | + expect(result1).to eql('$1$salt$qJH7.N4xYta3aEG/dfqo/0') |
| 63 | + expect(result5).to eql('$5$salt$Gcm6FsVtF/Qa77ZKD.iwsJlCVPY0XSMgLJL0Hnww/c1') |
| 64 | + expect(result6).to eql('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.') |
| 65 | + end |
| 66 | + |
| 67 | + it "should generate a valid hash" do |
| 68 | + password_hash = scope.function_pw_hash(['password', 6, 'salt']) |
| 69 | + |
| 70 | + hash_parts = password_hash.match(%r{\A\$(.*)\$([a-zA-Z0-9./]+)\$([a-zA-Z0-9./]+)\z}) |
| 71 | + |
| 72 | + expect(hash_parts).not_to eql(nil) |
| 73 | + end |
| 74 | +end |
0 commit comments