Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 127 lines (106 sloc) 4.825 kb
957c2c9 Brian Shirai Standardize require lines.
brixen authored
1 require File.expand_path('../../../spec_helper', __FILE__)
2 require File.expand_path('../fixtures/classes.rb', __FILE__)
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
3
4 describe "String#crypt" do
5 # Note: MRI's documentation just says that the C stdlib function crypt() is
6 # called.
7 #
8 # I'm not sure if crypt() is guaranteed to produce the same result across
9 # different platforms. It seems that there is one standard UNIX implementation
10 # of crypt(), but that alternative implementations are possible. See
11 # http://www.unix.org.ua/orelly/networking/puis/ch08_06.htm
12 it "returns a cryptographic hash of self by applying the UNIX crypt algorithm with the specified salt" do
13 "".crypt("aa").should == "aaQSqAReePlq6"
14 "nutmeg".crypt("Mi").should == "MiqkFWCm1fNJI"
15 "ellen1".crypt("ri").should == "ri79kNd7V6.Sk"
16 "Sharon".crypt("./").should == "./UY9Q7TvYJDg"
17 "norahs".crypt("am").should == "amfIADT2iqjA."
18 "norahs".crypt("7a").should == "7azfT5tIdyh0I"
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
19
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
20 # Only uses first 8 chars of string
21 "01234567".crypt("aa").should == "aa4c4gpuvCkSE"
22 "012345678".crypt("aa").should == "aa4c4gpuvCkSE"
23 "0123456789".crypt("aa").should == "aa4c4gpuvCkSE"
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
24
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
25 # Only uses first 2 chars of salt
26 "hello world".crypt("aa").should == "aayPz4hyPS1wI"
27 "hello world".crypt("aab").should == "aayPz4hyPS1wI"
28 "hello world".crypt("aabc").should == "aayPz4hyPS1wI"
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
29 end
30
554870a Brian Shirai Reorganized String#crypt specs based on #207.
brixen authored
31 platform_is :java do
32 it "returns NULL bytes prepended to the string when the salt contains NULL bytes" do
33 "hello".crypt("\x00\x00").should == "\x00\x00dR0/E99ehpU"
34 "hello".crypt("\x00a").should == "\x00aeipc4xPxhGY"
35 "hello".crypt("a\x00").should == "a\x00GJVggM8eWwo"
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
36 end
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
37 end
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
38
554870a Brian Shirai Reorganized String#crypt specs based on #207.
brixen authored
39 platform_is_not :java do
1bba189 NARUSE, Yui Follow OpenBSD 5.0's behavior.
nurse authored
40 platform_is :openbsd do
41 it "returns empty string if the first byte of the salt" do
42 "hello".crypt("\x00\x00").should == ""
43 "hello".crypt("\x00a").should == ""
44 end
45
9617e93 NARUSE, Yui Refix syntax error.
nurse authored
46 it "returns the same character prepended to the string for the salt if the second character of the salt is a NULL byte" do
1bba189 NARUSE, Yui Follow OpenBSD 5.0's behavior.
nurse authored
47 "hello".crypt("a\x00").should == "aaGJVggM8eWwo"
48 "hello".crypt("b\x00").should == "bb.LIhrI2NKCo"
49 end
50 end
51
20e452f NARUSE, Yui NetBSD 6 or later returns "*0" on error.
nurse authored
52 platform_is :darwin, /netbsd[a-z]*[1-5]\./ do
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
53 it "returns '.' prepended to the string for each NULL byte the salt contains" do
54 "hello".crypt("\x00\x00").should == "..dR0/E99ehpU"
55 "hello".crypt("\x00a").should == ".aeipc4xPxhGY"
56 "hello".crypt("a\x00").should == "a.GJVggM8eWwo"
57 end
58 end
59
20e452f NARUSE, Yui NetBSD 6 or later returns "*0" on error.
nurse authored
60 platform_is /netbsd[a-z]*(?![1-5]\.)/ do
61 it "returns '*0' when the salt contains NULL bytes" do
62 "hello".crypt("\x00\x00").should == "*0"
63 "hello".crypt("\x00a").should == "*0"
64 "hello".crypt("a\x00").should == "*0"
65 end
66 end
67
554870a Brian Shirai Reorganized String#crypt specs based on #207.
brixen authored
68 platform_is :freebsd do
5bd2b9c NARUSE, Yui Add String#crypt's spec for FreeBSD. #207
nurse authored
69 it "returns an empty string when the salt starts with NULL bytes" do
70 "hello".crypt("\x00\x00").should == ""
71 "hello".crypt("\x00a").should == ""
72 end
554870a Brian Shirai Reorganized String#crypt specs based on #207.
brixen authored
73
74 it "ignores trailing NULL bytes in the salt but counts them for the 2 character minimum" do
75 "hello".crypt("a\x00").should == "aaGJVggM8eWwo"
76 end
77 end
78
79 platform_is :linux do
80 it "returns an empty string when the salt starts with NULL bytes" do
81 "hello".crypt("\x00\x00").should == ""
82 "hello".crypt("\x00a").should == ""
83 end
84
85 it "ignores trailing NULL bytes in the salt but counts them for the 2 character minimum" do
86 "hello".crypt("a\x00").should == "aa1dYAU.hgL3A"
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
87 end
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
88 end
89 end
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
90
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
91 it "raises an ArgumentError when the salt is shorter than two characters" do
92 lambda { "hello".crypt("") }.should raise_error(ArgumentError)
93 lambda { "hello".crypt("f") }.should raise_error(ArgumentError)
94 end
95
627d205 Brian Shirai Removed specs mocking #respond_to? and #method_missing.
brixen authored
96 it "calls #to_str to converts the salt arg to a String" do
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
97 obj = mock('aa')
627d205 Brian Shirai Removed specs mocking #respond_to? and #method_missing.
brixen authored
98 obj.should_receive(:to_str).and_return("aa")
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
99
100 "".crypt(obj).should == "aaQSqAReePlq6"
101 end
102
103 it "raises a type error when the salt arg can't be converted to a string" do
104 lambda { "".crypt(5) }.should raise_error(TypeError)
105 lambda { "".crypt(mock('x')) }.should raise_error(TypeError)
106 end
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
107
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
108 it "taints the result if either salt or self is tainted" do
109 tainted_salt = "aa"
110 tainted_str = "hello"
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
111
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
112 tainted_salt.taint
113 tainted_str.taint
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
114
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
115 "hello".crypt("aa").tainted?.should == false
116 tainted_str.crypt("aa").tainted?.should == true
117 "hello".crypt(tainted_salt).tainted?.should == true
118 tainted_str.crypt(tainted_salt).tainted?.should == true
119 end
326a538 Brian Shirai Fixed use of guards in the specs.
brixen authored
120
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
121 it "doesn't return subclass instances" do
3ccbfd1 Hiro Asari Avoid result.class.should == Class
BanzaiMan authored
122 StringSpecs::MyString.new("hello").crypt("aa").should be_kind_of(String)
123 "hello".crypt(StringSpecs::MyString.new("aa")).should be_kind_of(String)
124 StringSpecs::MyString.new("hello").crypt(StringSpecs::MyString.new("aa")).should be_kind_of(String)
6c1c34a Brian Shirai Ask not what RubySpec can do for you, but what you can do for RubySpec.
brixen authored
125 end
126 end
Something went wrong with that request. Please try again.