Adding support for git pushing with uppercased group/user names
[gitorious:yousource.git] / test / unit / lib / ssh_strainer_test.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18
19
20 require File.dirname(__FILE__) + '/../../test_helper'
21 require "gitorious/ssh/client"
22 require "gitorious/ssh/strainer"
23
24 class SSHStrainerTest < ActiveSupport::TestCase
25   
26   should "raises if command includes a newline" do
27     assert_raises(Gitorious::SSH::BadCommandError) do
28       Gitorious::SSH::Strainer.new("foo\nbar").parse!
29     end
30   end
31   
32   should "raises if command has more than one argument" do
33     assert_raises(Gitorious::SSH::BadCommandError) do
34       Gitorious::SSH::Strainer.new("git-upload-pack 'bar baz'").parse!
35     end
36   end
37   
38   should "raises if command doesn't have an argument" do
39     assert_raises(Gitorious::SSH::BadCommandError) do
40       Gitorious::SSH::Strainer.new("git-upload-pack").parse!
41     end
42   end
43   
44   should "raises if it gets a bad command" do
45     assert_raises(Gitorious::SSH::BadCommandError) do
46       Gitorious::SSH::Strainer.new("evil 'foo'").parse!
47     end
48   end
49   
50   should "raises if it receives an unsafe argument" do
51     assert_raises(Gitorious::SSH::BadCommandError) do
52       Gitorious::SSH::Strainer.new("git-upload-pack /evil/attack").parse!
53     end
54   end
55   
56   should "only allow the specified readonly command" do
57     assert_raises(Gitorious::SSH::BadCommandError) do
58       Gitorious::SSH::Strainer.new("git-pull foo bar").parse!
59     end
60     
61     assert_raises(Gitorious::SSH::BadCommandError) do
62       Gitorious::SSH::Strainer.new("rm -rf /tmp/*").parse!
63     end
64   end
65   
66   should "accept non-dashed version git upload-pack" do
67     assert_nothing_raised do
68       s = Gitorious::SSH::Strainer.new("git upload-pack 'foo/bar.git'").parse!
69       assert_equal "git upload-pack", s.verb
70     end
71     
72     assert_nothing_raised do
73       s = Gitorious::SSH::Strainer.new("git upload-pack '~foo/bar.git'").parse!
74       assert_equal "git upload-pack", s.verb
75     end
76     
77     assert_nothing_raised do
78       s = Gitorious::SSH::Strainer.new("git upload-pack '+foo/bar.git'").parse!
79       assert_equal "git upload-pack", s.verb
80     end
81   end
82   
83   should "accept non-dashed version git receive-pack" do
84     assert_nothing_raised do
85       s = Gitorious::SSH::Strainer.new("git receive-pack 'foo/bar.git'").parse!
86       assert_equal "git receive-pack", s.verb
87     end
88     
89     assert_nothing_raised do
90       s = Gitorious::SSH::Strainer.new("git receive-pack '~foo/bar.git'").parse!
91       assert_equal "git receive-pack", s.verb
92     end
93     
94     assert_nothing_raised do
95       s = Gitorious::SSH::Strainer.new("git receive-pack '+foo/bar.git'").parse!
96       assert_equal "git receive-pack", s.verb
97     end
98   end
99   
100   should "accept git+ssh style urls" do
101     s = Gitorious::SSH::Strainer.new("git-receive-pack '/foo/bar.git'").parse!
102     assert_equal "foo/bar.git", s.path
103     
104     s = Gitorious::SSH::Strainer.new("git-receive-pack '/+foo/bar.git'").parse!
105     assert_equal "+foo/bar.git", s.path
106     
107     s = Gitorious::SSH::Strainer.new("git-receive-pack '/~foo/bar.git'").parse!
108     assert_equal "~foo/bar.git", s.path
109   end
110   
111   should "raise if it receives too many arguments" do
112     assert_raises(Gitorious::SSH::BadCommandError) do
113       Gitorious::SSH::Strainer.new("git-receive-pack 'foo/bar.git' baz").parse!
114     end
115   end
116   
117   should "raises if it receives an unsafe argument that almost looks kosher" do
118     assert_raises(Gitorious::SSH::BadCommandError) do
119       Gitorious::SSH::Strainer.new("git-upload-pack '/evil/path'").parse!
120     end
121     
122     assert_raises(Gitorious::SSH::BadCommandError) do
123       Gitorious::SSH::Strainer.new("git-upload-pack '+/evil/path'").parse!
124     end
125     
126     assert_raises(Gitorious::SSH::BadCommandError) do
127       Gitorious::SSH::Strainer.new("git-upload-pack '~/evil/path'").parse!
128     end
129     
130     assert_raises(Gitorious::SSH::BadCommandError) do
131       Gitorious::SSH::Strainer.new("git-upload-pack /evil/\\\\\\//path").parse!
132     end
133     
134     assert_raises(Gitorious::SSH::BadCommandError) do
135       Gitorious::SSH::Strainer.new("git-upload-pack +/evil/\\\\\\//path").parse!
136     end
137     
138     assert_raises(Gitorious::SSH::BadCommandError) do
139       Gitorious::SSH::Strainer.new("git-upload-pack ~/evil/\\\\\\//path").parse!
140     end
141     
142     assert_raises(Gitorious::SSH::BadCommandError) do
143       Gitorious::SSH::Strainer.new("git-upload-pack ../../evil/path").parse!
144     end
145     
146     assert_raises(Gitorious::SSH::BadCommandError) do
147       Gitorious::SSH::Strainer.new("git-upload-pack 'evil/path.git.bar'").parse!
148     end
149     
150     assert_raises(Gitorious::SSH::BadCommandError) do
151       Gitorious::SSH::Strainer.new("git-upload-pack +../../evil/path").parse!
152     end
153     
154     assert_raises(Gitorious::SSH::BadCommandError) do
155       Gitorious::SSH::Strainer.new("git-upload-pack ~../../evil/path").parse!
156     end
157   end
158   
159   should "raises if it receives an empty path" do
160     assert_raises(Gitorious::SSH::BadCommandError) do
161       Gitorious::SSH::Strainer.new("git-upload-pack ''").parse!
162     end
163     
164     assert_raises(Gitorious::SSH::BadCommandError) do
165       Gitorious::SSH::Strainer.new("git-upload-pack ").parse!
166     end
167   end
168   
169   should "returns self when running #parse" do
170     strainer = Gitorious::SSH::Strainer.new("git-upload-pack 'foo/bar.git'")
171     strainer2 = strainer.parse!
172     assert_instance_of Gitorious::SSH::Strainer, strainer2
173     assert_equal strainer, strainer2
174   end
175   
176   should "sets the path of the parsed command" do
177     cmd = Gitorious::SSH::Strainer.new("git-upload-pack 'foo/bar.git'").parse!
178     assert_equal "foo/bar.git", cmd.path
179   end
180
181   should "can parse user-style urls prefixed with a tilde" do
182     assert_nothing_raised(Gitorious::SSH::BadCommandError) do
183       cmd = Gitorious::SSH::Strainer.new("git-upload-pack '~foo/bar.git'").parse!
184       assert_equal "~foo/bar.git", cmd.path
185     end
186   end
187
188   should "allow user names with an uppercase first letter" do
189     assert_nothing_raised do
190       strainer = Gitorious::SSH::Strainer.new("git-upload-pack '~Oldtimer/repo.git'").parse!
191       assert_equal "~Oldtimer/repo.git", strainer.path
192     end
193   end
194
195   should "allow group names with an uppercase first letter" do
196     assert_nothing_raised do
197       strainer = Gitorious::SSH::Strainer.new("git upload-pack '+Oldtimers/repo.git'").parse!
198       assert_equal "+Oldtimers/repo.git", strainer.path
199     end
200   end
201   
202   should "can parse team-style urls prefixed with a plus" do
203     assert_nothing_raised(Gitorious::SSH::BadCommandError) do
204       cmd = Gitorious::SSH::Strainer.new("git-upload-pack '+foo/bar.git'").parse!
205       assert_equal "+foo/bar.git", cmd.path
206     end
207   end
208   
209   should "can parse user-style urls with project name and prefixed with a tilde" do
210     assert_nothing_raised(Gitorious::SSH::BadCommandError) do
211       cmd = Gitorious::SSH::Strainer.new("git-upload-pack '~foo/bar/baz.git'").parse!
212       assert_equal "~foo/bar/baz.git", cmd.path
213     end
214   end
215   
216   should "can parse team-style urls with project name and prefixed with a plus" do
217     assert_nothing_raised(Gitorious::SSH::BadCommandError) do
218       cmd = Gitorious::SSH::Strainer.new("git-upload-pack '+foo/bar/baz.git'").parse!
219       assert_equal "+foo/bar/baz.git", cmd.path
220     end
221   end
222 end