summaryrefslogtreecommitdiff
path: root/spec/functions
diff options
context:
space:
mode:
Diffstat (limited to 'spec/functions')
-rw-r--r--spec/functions/fqdn_rand_string_spec.rb91
-rw-r--r--spec/functions/pw_hash_spec.rb87
2 files changed, 178 insertions, 0 deletions
diff --git a/spec/functions/fqdn_rand_string_spec.rb b/spec/functions/fqdn_rand_string_spec.rb
new file mode 100644
index 0000000..949d930
--- /dev/null
+++ b/spec/functions/fqdn_rand_string_spec.rb
@@ -0,0 +1,91 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the fqdn_rand_string function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ expect(Puppet::Parser::Functions.function("fqdn_rand_string")).to eq("function_fqdn_rand_string")
+ end
+
+ it "should raise an ArgumentError if there is less than 1 argument" do
+ expect { fqdn_rand_string() }.to( raise_error(ArgumentError, /wrong number of arguments/))
+ end
+
+ it "should raise an ArgumentError if argument 1 isn't a positive integer" do
+ expect { fqdn_rand_string(0) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
+ expect { fqdn_rand_string(-1) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
+ expect { fqdn_rand_string(0.5) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
+ end
+
+ it "provides a valid alphanumeric string when no character set is provided" do
+ length = 100
+ string = %r{\A[a-zA-Z0-9]{#{length}}\z}
+ expect(fqdn_rand_string(length).match(string)).not_to eq(nil)
+ end
+
+ it "provides a valid alphanumeric string when an undef character set is provided" do
+ length = 100
+ string = %r{\A[a-zA-Z0-9]{#{length}}\z}
+ expect(fqdn_rand_string(length, :charset => nil).match(string)).not_to eq(nil)
+ end
+
+ it "provides a valid alphanumeric string when an empty character set is provided" do
+ length = 100
+ string = %r{\A[a-zA-Z0-9]{#{length}}\z}
+ expect(fqdn_rand_string(length, :charset => '').match(string)).not_to eq(nil)
+ end
+
+ it "uses a provided character set" do
+ length = 100
+ charset = '!@#$%^&*()-_=+'
+ string = %r{\A[#{charset}]{#{length}}\z}
+ expect(fqdn_rand_string(length, :charset => charset).match(string)).not_to eq(nil)
+ end
+
+ it "provides a random string exactly as long as the given length" do
+ expect(fqdn_rand_string(10).size).to eql(10)
+ end
+
+ it "provides the same 'random' value on subsequent calls for the same host" do
+ expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10))
+ end
+
+ it "considers the same host and same extra arguments to have the same random sequence" do
+ first_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
+ second_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
+
+ expect(first_random).to eql(second_random)
+ end
+
+ it "allows extra arguments to control the random value on a single host" do
+ first_random = fqdn_rand_string(10, :extra_identifier => [1, "different", "host"])
+ second_different_random = fqdn_rand_string(10, :extra_identifier => [2, "different", "host"])
+
+ expect(first_random).not_to eql(second_different_random)
+ end
+
+ it "should return different strings for different hosts" do
+ val1 = fqdn_rand_string(10, :host => "first.host.com")
+ val2 = fqdn_rand_string(10, :host => "second.host.com")
+
+ expect(val1).not_to eql(val2)
+ end
+
+ def fqdn_rand_string(max, args = {})
+ host = args[:host] || '127.0.0.1'
+ charset = args[:charset]
+ extra = args[:extra_identifier] || []
+
+ scope = PuppetlabsSpec::PuppetInternals.scope
+ scope.stubs(:[]).with("::fqdn").returns(host)
+ scope.stubs(:lookupvar).with("::fqdn").returns(host)
+
+ function_args = [max]
+ if args.has_key?(:charset) or !extra.empty?
+ function_args << charset
+ end
+ function_args += extra
+ scope.function_fqdn_rand_string(function_args)
+ end
+end
diff --git a/spec/functions/pw_hash_spec.rb b/spec/functions/pw_hash_spec.rb
new file mode 100644
index 0000000..01a1105
--- /dev/null
+++ b/spec/functions/pw_hash_spec.rb
@@ -0,0 +1,87 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the pw_hash function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ expect(Puppet::Parser::Functions.function("pw_hash")).to eq("function_pw_hash")
+ end
+
+ it "should raise an ArgumentError if there are less than 3 arguments" do
+ expect { scope.function_pw_hash([]) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) )
+ expect { scope.function_pw_hash(['password']) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) )
+ expect { scope.function_pw_hash(['password', 'sha-512']) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) )
+ end
+
+ it "should raise an ArgumentError if there are more than 3 arguments" do
+ expect { scope.function_pw_hash(['password', 'sha-512', 'salt', 5]) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) )
+ end
+
+ it "should raise an ArgumentError if the first argument is not a string" do
+ expect { scope.function_pw_hash([['password'], 'sha-512', 'salt']) }.to( raise_error(ArgumentError, /first argument must be a string/) )
+ # in Puppet 3, numbers are passed as strings, so we can't test that
+ end
+
+ it "should return nil if the first argument is empty" do
+ expect(scope.function_pw_hash(['', 'sha-512', 'salt'])).to eq(nil)
+ end
+
+ it "should return nil if the first argument is undef" do
+ expect(scope.function_pw_hash([nil, 'sha-512', 'salt'])).to eq(nil)
+ end
+
+ it "should raise an ArgumentError if the second argument is an invalid hash type" do
+ expect { scope.function_pw_hash(['', 'invalid', 'salt']) }.to( raise_error(ArgumentError, /not a valid hash type/) )
+ end
+
+ it "should raise an ArgumentError if the second argument is not a string" do
+ expect { scope.function_pw_hash(['', [], 'salt']) }.to( raise_error(ArgumentError, /second argument must be a string/) )
+ end
+
+ it "should raise an ArgumentError if the third argument is not a string" do
+ expect { scope.function_pw_hash(['password', 'sha-512', ['salt']]) }.to( raise_error(ArgumentError, /third argument must be a string/) )
+ # in Puppet 3, numbers are passed as strings, so we can't test that
+ end
+
+ it "should raise an ArgumentError if the third argument is empty" do
+ expect { scope.function_pw_hash(['password', 'sha-512', '']) }.to( raise_error(ArgumentError, /third argument must not be empty/) )
+ end
+
+ it "should raise an ArgumentError if the third argument has invalid characters" do
+ expect { scope.function_pw_hash(['password', 'sha-512', '%']) }.to( raise_error(ArgumentError, /characters in salt must be in the set/) )
+ end
+
+ it "should fail on platforms with weak implementations of String#crypt" do
+ String.any_instance.expects(:crypt).with('$1$1').returns('$1SoNol0Ye6Xk')
+ expect { scope.function_pw_hash(['password', 'sha-512', 'salt']) }.to( raise_error(Puppet::ParseError, /system does not support enhanced salts/) )
+ end
+
+ it "should return a hashed password" do
+ result = scope.function_pw_hash(['password', 'sha-512', 'salt'])
+ expect(result).to eql('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.')
+ end
+
+ it "should use the specified salt" do
+ result = scope.function_pw_hash(['password', 'sha-512', 'salt'])
+ expect(result).to match('salt')
+ end
+
+ it "should use the specified hash type" do
+ resultmd5 = scope.function_pw_hash(['password', 'md5', 'salt'])
+ resultsha256 = scope.function_pw_hash(['password', 'sha-256', 'salt'])
+ resultsha512 = scope.function_pw_hash(['password', 'sha-512', 'salt'])
+
+ expect(resultmd5).to eql('$1$salt$qJH7.N4xYta3aEG/dfqo/0')
+ expect(resultsha256).to eql('$5$salt$Gcm6FsVtF/Qa77ZKD.iwsJlCVPY0XSMgLJL0Hnww/c1')
+ expect(resultsha512).to eql('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.')
+ end
+
+ it "should generate a valid hash" do
+ password_hash = scope.function_pw_hash(['password', 'sha-512', 'salt'])
+
+ hash_parts = password_hash.match(%r{\A\$(.*)\$([a-zA-Z0-9./]+)\$([a-zA-Z0-9./]+)\z})
+
+ expect(hash_parts).not_to eql(nil)
+ end
+end