Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / unit / commit_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6 require 'helpers/git_test_helper'
7
8 # NOTE: calling Commit.find_commit_range(nil, nil, 'rev')
9 # produces an error message "fatal: bad object 'rev'" on stderr if
10 # 'rev' does not exist in a given repository.  Many of these tests
11 # report such errors; their presence does not represent a fatal
12 # condition.
13
14 class CommitTest < ActiveSupport::TestCase
15   # See git_setup.rb for the commit log for test.git.tar
16   include GitTestHelper
17
18   setup do
19     authorize_with :active
20   end
21
22   test 'find_commit_range does not bypass permissions' do
23     authorize_with :inactive
24     assert_raises ArgumentError do
25       Commit.find_commit_range 'foo', nil, 'master', []
26     end
27   end
28
29   [
30    'https://github.com/curoverse/arvados.git',
31    'http://github.com/curoverse/arvados.git',
32    'git://github.com/curoverse/arvados.git',
33   ].each do |url|
34     test "find_commit_range uses fetch_remote_repository to get #{url}" do
35       fake_gitdir = repositories(:foo).server_path
36       Commit.expects(:cache_dir_for).once.with(url).returns fake_gitdir
37       Commit.expects(:fetch_remote_repository).once.with(fake_gitdir, url).returns true
38       c = Commit.find_commit_range url, nil, 'master', []
39       refute_empty c
40     end
41   end
42
43   [
44    'bogus/repo',
45    '/bogus/repo',
46    '/not/allowed/.git',
47    'file:///not/allowed.git',
48    'git.curoverse.com/arvados.git',
49    'github.com/curoverse/arvados.git',
50   ].each do |url|
51     test "find_commit_range skips fetch_remote_repository for #{url}" do
52       Commit.expects(:fetch_remote_repository).never
53       assert_raises ArgumentError do
54         Commit.find_commit_range url, nil, 'master', []
55       end
56     end
57   end
58
59   test 'fetch_remote_repository does not leak commits across repositories' do
60     url = "http://localhost:1/fake/fake.git"
61     fetch_remote_from_local_repo url, :foo
62     c = Commit.find_commit_range url, nil, 'master', []
63     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57'], c
64
65     url = "http://localhost:2/fake/fake.git"
66     fetch_remote_from_local_repo url, 'file://' + File.expand_path('../../.git', Rails.root)
67     c = Commit.find_commit_range url, nil, '077ba2ad3ea24a929091a9e6ce545c93199b8e57', []
68     assert_equal [], c
69   end
70
71   test 'tag_in_internal_repository creates and updates tags in internal.git' do
72     authorize_with :active
73     gitint = "git --git-dir #{Rails.configuration.git_internal_dir}"
74     IO.read("|#{gitint} tag -d testtag 2>/dev/null") # "no such tag", fine
75     assert_match(/^fatal: /, IO.read("|#{gitint} show testtag 2>&1"))
76     refute $?.success?
77     Commit.tag_in_internal_repository 'active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', 'testtag'
78     assert_match(/^commit 31ce37f/, IO.read("|#{gitint} show testtag"))
79     assert $?.success?
80   end
81
82   # In active/shabranchnames, "7387838c69a21827834586cc42b467ff6c63293b" is
83   # both a commit hash, and the name of a branch that begins from that same
84   # commit.
85   COMMIT_BRANCH_NAME = "7387838c69a21827834586cc42b467ff6c63293b"
86   # A commit that appears in the branch after 7387838c.
87   COMMIT_BRANCH_COMMIT_2 = "abec49829bf1758413509b7ffcab32a771b71e81"
88   # "738783" is another branch that starts from the above commit.
89   SHORT_COMMIT_BRANCH_NAME = COMMIT_BRANCH_NAME[0, 6]
90   # A commit that appears in branch 738783 after 7387838c.
91   SHORT_BRANCH_COMMIT_2 = "77e1a93093663705a63bb4d505698047e109dedd"
92
93   test "find_commit_range min_version prefers commits over branch names" do
94     assert_equal([COMMIT_BRANCH_NAME],
95                  Commit.find_commit_range("active/shabranchnames",
96                                           COMMIT_BRANCH_NAME, nil, nil))
97   end
98
99   test "find_commit_range max_version prefers commits over branch names" do
100     assert_equal([COMMIT_BRANCH_NAME],
101                  Commit.find_commit_range("active/shabranchnames",
102                                           nil, COMMIT_BRANCH_NAME, nil))
103   end
104
105   test "find_commit_range min_version with short branch name" do
106     assert_equal([SHORT_BRANCH_COMMIT_2],
107                  Commit.find_commit_range("active/shabranchnames",
108                                           SHORT_COMMIT_BRANCH_NAME, nil, nil))
109   end
110
111   test "find_commit_range max_version with short branch name" do
112     assert_equal([SHORT_BRANCH_COMMIT_2],
113                  Commit.find_commit_range("active/shabranchnames",
114                                           nil, SHORT_COMMIT_BRANCH_NAME, nil))
115   end
116
117   test "find_commit_range min_version with disambiguated branch name" do
118     assert_equal([COMMIT_BRANCH_COMMIT_2],
119                  Commit.find_commit_range("active/shabranchnames",
120                                           "heads/#{COMMIT_BRANCH_NAME}",
121                                           nil, nil))
122   end
123
124   test "find_commit_range max_version with disambiguated branch name" do
125     assert_equal([COMMIT_BRANCH_COMMIT_2],
126                  Commit.find_commit_range("active/shabranchnames", nil,
127                                           "heads/#{COMMIT_BRANCH_NAME}", nil))
128   end
129
130   test "find_commit_range min_version with unambiguous short name" do
131     assert_equal([COMMIT_BRANCH_NAME],
132                  Commit.find_commit_range("active/shabranchnames",
133                                           COMMIT_BRANCH_NAME[0..-2], nil, nil))
134   end
135
136   test "find_commit_range max_version with unambiguous short name" do
137     assert_equal([COMMIT_BRANCH_NAME],
138                  Commit.find_commit_range("active/shabranchnames", nil,
139                                           COMMIT_BRANCH_NAME[0..-2], nil))
140   end
141
142   test "find_commit_range laundry list" do
143     authorize_with :active
144
145     # single
146     a = Commit.find_commit_range('active/foo', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
147     assert_equal ['31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
148
149     #test "test_branch1" do
150     a = Commit.find_commit_range('active/foo', nil, 'master', nil)
151     assert_includes(a, '077ba2ad3ea24a929091a9e6ce545c93199b8e57')
152
153     #test "test_branch2" do
154     a = Commit.find_commit_range('active/foo', nil, 'b1', nil)
155     assert_equal ['1de84a854e2b440dc53bf42f8548afa4c17da332'], a
156
157     #test "test_branch3" do
158     a = Commit.find_commit_range('active/foo', nil, 'HEAD', nil)
159     assert_equal ['1de84a854e2b440dc53bf42f8548afa4c17da332'], a
160
161     #test "test_single_revision_repo" do
162     a = Commit.find_commit_range('active/foo', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
163     assert_equal ['31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
164     a = Commit.find_commit_range('arvados', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
165     assert_equal [], a
166
167     #test "test_multi_revision" do
168     # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
169     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', nil)
170     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '4fe459abe02d9b365932b8f5dc419439ab4e2577', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
171
172     #test "test_tag" do
173     # complains "fatal: ambiguous argument 'tag1': unknown revision or path
174     # not in the working tree."
175     a = Commit.find_commit_range('active/foo', 'tag1', 'master', nil)
176     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '4fe459abe02d9b365932b8f5dc419439ab4e2577'], a
177
178     #test "test_multi_revision_exclude" do
179     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', ['4fe459abe02d9b365932b8f5dc419439ab4e2577'])
180     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
181
182     #test "test_multi_revision_tagged_exclude" do
183     # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
184     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', ['tag1'])
185     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
186
187     Dir.mktmpdir do |touchdir|
188       # invalid input to maximum
189       a = Commit.find_commit_range('active/foo', nil, "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{touchdir}/uh_oh", nil)
190       assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable"
191       assert_equal [], a
192
193       # invalid input to maximum
194       a = Commit.find_commit_range('active/foo', nil, "$(uname>#{touchdir}/uh_oh)", nil)
195       assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable"
196       assert_equal [], a
197
198       # invalid input to minimum
199       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{touchdir}/uh_oh", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil)
200       assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable"
201       assert_equal [], a
202
203       # invalid input to minimum
204       a = Commit.find_commit_range('active/foo', "$(uname>#{touchdir}/uh_oh)", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil)
205       assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable"
206       assert_equal [], a
207
208       # invalid input to 'excludes'
209       # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
210       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["4fe459abe02d9b365932b8f5dc419439ab4e2577 ; touch #{touchdir}/uh_oh"])
211       assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable"
212       assert_equal [], a
213
214       # invalid input to 'excludes'
215       # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
216       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["$(uname>#{touchdir}/uh_oh)"])
217       assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable"
218       assert_equal [], a
219     end
220   end
221 end