Merge branch 'master' into 3198-writable-fuse
[arvados.git] / services / api / test / unit / commit_test.rb
1 require 'test_helper'
2 require 'helpers/git_test_helper'
3
4 # NOTE: calling Commit.find_commit_range(nil, nil, 'rev')
5 # produces an error message "fatal: bad object 'rev'" on stderr if
6 # 'rev' does not exist in a given repository.  Many of these tests
7 # report such errors; their presence does not represent a fatal
8 # condition.
9
10 class CommitTest < ActiveSupport::TestCase
11   # See git_setup.rb for the commit log for test.git.tar
12   include GitTestHelper
13
14   setup do
15     authorize_with :active
16   end
17
18   test 'find_commit_range does not bypass permissions' do
19     authorize_with :inactive
20     assert_raises ArgumentError do
21       c = Commit.find_commit_range 'foo', nil, 'master', []
22     end
23   end
24
25   [
26    'https://github.com/curoverse/arvados.git',
27    'http://github.com/curoverse/arvados.git',
28    'git://github.com/curoverse/arvados.git',
29   ].each do |url|
30     test "find_commit_range uses fetch_remote_repository to get #{url}" do
31       fake_gitdir = repositories(:foo).server_path
32       Commit.expects(:cache_dir_for).once.with(url).returns fake_gitdir
33       Commit.expects(:fetch_remote_repository).once.with(fake_gitdir, url).returns true
34       c = Commit.find_commit_range url, nil, 'master', []
35       refute_empty c
36     end
37   end
38
39   [
40    'bogus/repo',
41    '/bogus/repo',
42    '/not/allowed/.git',
43    'file:///not/allowed.git',
44    'git.curoverse.com/arvados.git',
45    'github.com/curoverse/arvados.git',
46   ].each do |url|
47     test "find_commit_range skips fetch_remote_repository for #{url}" do
48       Commit.expects(:fetch_remote_repository).never
49       assert_raises ArgumentError do
50         Commit.find_commit_range url, nil, 'master', []
51       end
52     end
53   end
54
55   test 'fetch_remote_repository does not leak commits across repositories' do
56     url = "http://localhost:1/fake/fake.git"
57     fetch_remote_from_local_repo url, :foo
58     c = Commit.find_commit_range url, nil, 'master', []
59     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57'], c
60
61     url = "http://localhost:2/fake/fake.git"
62     fetch_remote_from_local_repo url, 'file://' + File.expand_path('../../.git', Rails.root)
63     c = Commit.find_commit_range url, nil, '077ba2ad3ea24a929091a9e6ce545c93199b8e57', []
64     assert_equal [], c
65   end
66
67   test 'tag_in_internal_repository creates and updates tags in internal.git' do
68     authorize_with :active
69     gitint = "git --git-dir #{Rails.configuration.git_internal_dir}"
70     IO.read("|#{gitint} tag -d testtag 2>/dev/null") # "no such tag", fine
71     assert_match /^fatal: /, IO.read("|#{gitint} show testtag 2>&1")
72     refute $?.success?
73     Commit.tag_in_internal_repository 'active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', 'testtag'
74     assert_match /^commit 31ce37f/, IO.read("|#{gitint} show testtag")
75     assert $?.success?
76   end
77
78   test "find_commit_range laundry list" do
79     authorize_with :active
80
81     # single
82     a = Commit.find_commit_range('active/foo', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
83     assert_equal ['31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
84
85     #test "test_branch1" do
86     a = Commit.find_commit_range('active/foo', nil, 'master', nil)
87     assert_includes(a, '077ba2ad3ea24a929091a9e6ce545c93199b8e57')
88
89     #test "test_branch2" do
90     a = Commit.find_commit_range('active/foo', nil, 'b1', nil)
91     assert_equal ['1de84a854e2b440dc53bf42f8548afa4c17da332'], a
92
93     #test "test_branch3" do
94     a = Commit.find_commit_range('active/foo', nil, 'HEAD', nil)
95     assert_equal ['1de84a854e2b440dc53bf42f8548afa4c17da332'], a
96
97     #test "test_single_revision_repo" do
98     a = Commit.find_commit_range('active/foo', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
99     assert_equal ['31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
100     a = Commit.find_commit_range('arvados', nil, '31ce37fe365b3dc204300a3e4c396ad333ed0556', nil)
101     assert_equal [], a
102
103     #test "test_multi_revision" do
104     # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
105     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', nil)
106     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '4fe459abe02d9b365932b8f5dc419439ab4e2577', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
107
108     #test "test_tag" do
109     # complains "fatal: ambiguous argument 'tag1': unknown revision or path
110     # not in the working tree."
111     a = Commit.find_commit_range('active/foo', 'tag1', 'master', nil)
112     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '4fe459abe02d9b365932b8f5dc419439ab4e2577'], a
113
114     #test "test_multi_revision_exclude" do
115     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', ['4fe459abe02d9b365932b8f5dc419439ab4e2577'])
116     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
117
118     #test "test_multi_revision_tagged_exclude" do
119     # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
120     a = Commit.find_commit_range('active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', '077ba2ad3ea24a929091a9e6ce545c93199b8e57', ['tag1'])
121     assert_equal ['077ba2ad3ea24a929091a9e6ce545c93199b8e57', '31ce37fe365b3dc204300a3e4c396ad333ed0556'], a
122
123     Dir.mktmpdir do |touchdir|
124       # invalid input to maximum
125       a = Commit.find_commit_range('active/foo', nil, "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{touchdir}/uh_oh", nil)
126       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable"
127       assert_equal [], a
128
129       # invalid input to maximum
130       a = Commit.find_commit_range('active/foo', nil, "$(uname>#{touchdir}/uh_oh)", nil)
131       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable"
132       assert_equal [], a
133
134       # invalid input to minimum
135       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{touchdir}/uh_oh", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil)
136       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable"
137       assert_equal [], a
138
139       # invalid input to minimum
140       a = Commit.find_commit_range('active/foo', "$(uname>#{touchdir}/uh_oh)", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil)
141       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable"
142       assert_equal [], a
143
144       # invalid input to 'excludes'
145       # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
146       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["4fe459abe02d9b365932b8f5dc419439ab4e2577 ; touch #{touchdir}/uh_oh"])
147       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable"
148       assert_equal [], a
149
150       # invalid input to 'excludes'
151       # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57"
152       a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["$(uname>#{touchdir}/uh_oh)"])
153       assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable"
154       assert_equal [], a
155     end
156   end
157 end