X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/8464bb48ba769c8bf0984c8b1493ca43fd902891..e838828374ceed5ef6da260939251e86f72b6f27:/services/api/test/unit/commit_test.rb diff --git a/services/api/test/unit/commit_test.rb b/services/api/test/unit/commit_test.rb index b57c23b453..c5d72c3bfe 100644 --- a/services/api/test/unit/commit_test.rb +++ b/services/api/test/unit/commit_test.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + require 'test_helper' require 'helpers/git_test_helper' @@ -18,7 +22,15 @@ class CommitTest < ActiveSupport::TestCase test 'find_commit_range does not bypass permissions' do authorize_with :inactive assert_raises ArgumentError do - c = Commit.find_commit_range 'foo', nil, 'master', [] + Commit.find_commit_range 'foo', nil, 'master', [] + end + end + + def must_pipe(cmd) + begin + return IO.read("|#{cmd}") + ensure + assert $?.success? end end @@ -66,12 +78,53 @@ class CommitTest < ActiveSupport::TestCase test 'tag_in_internal_repository creates and updates tags in internal.git' do authorize_with :active - gitint = "git --git-dir #{Rails.configuration.git_internal_dir}" + gitint = "git --git-dir #{Rails.configuration.Containers.JobsAPI.GitInternalDir}" IO.read("|#{gitint} tag -d testtag 2>/dev/null") # "no such tag", fine - assert_match /^fatal: /, IO.read("|#{gitint} show testtag 2>&1") + assert_match(/^fatal: /, IO.read("|#{gitint} show testtag 2>&1")) refute $?.success? Commit.tag_in_internal_repository 'active/foo', '31ce37fe365b3dc204300a3e4c396ad333ed0556', 'testtag' - assert_match /^commit 31ce37f/, IO.read("|#{gitint} show testtag") + assert_match(/^commit 31ce37f/, IO.read("|#{gitint} show testtag")) + assert $?.success? + end + + def with_foo_repository + Dir.chdir("#{Rails.configuration.Git.Repositories}/#{repositories(:foo).uuid}") do + must_pipe("git checkout master 2>&1") + yield + end + end + + test 'tag_in_internal_repository, new non-tip sha1 in local repo' do + tag = "tag#{rand(10**10)}" + sha1 = nil + with_foo_repository do + must_pipe("git checkout -b branch-#{rand(10**10)} 2>&1") + must_pipe("echo -n #{tag.shellescape} >bar") + must_pipe("git add bar") + must_pipe("git -c user.email=x@x -c user.name=X commit -m -") + sha1 = must_pipe("git log -n1 --format=%H").strip + must_pipe("git rm bar") + must_pipe("git -c user.email=x@x -c user.name=X commit -m -") + end + Commit.tag_in_internal_repository 'active/foo', sha1, tag + gitint = "git --git-dir #{Rails.configuration.Containers.JobsAPI.GitInternalDir.shellescape}" + assert_match(/^commit /, IO.read("|#{gitint} show #{tag.shellescape}")) + assert $?.success? + end + + test 'tag_in_internal_repository, new unreferenced sha1 in local repo' do + tag = "tag#{rand(10**10)}" + sha1 = nil + with_foo_repository do + must_pipe("echo -n #{tag.shellescape} >bar") + must_pipe("git add bar") + must_pipe("git -c user.email=x@x -c user.name=X commit -m -") + sha1 = must_pipe("git log -n1 --format=%H").strip + must_pipe("git reset --hard HEAD^") + end + Commit.tag_in_internal_repository 'active/foo', sha1, tag + gitint = "git --git-dir #{Rails.configuration.Containers.JobsAPI.GitInternalDir.shellescape}" + assert_match(/^commit /, IO.read("|#{gitint} show #{tag.shellescape}")) assert $?.success? end @@ -183,34 +236,34 @@ class CommitTest < ActiveSupport::TestCase Dir.mktmpdir do |touchdir| # invalid input to maximum a = Commit.find_commit_range('active/foo', nil, "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{touchdir}/uh_oh", nil) - assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable" + assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable" assert_equal [], a # invalid input to maximum a = Commit.find_commit_range('active/foo', nil, "$(uname>#{touchdir}/uh_oh)", nil) - assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable" + assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'maximum' parameter of find_commit_range is exploitable" assert_equal [], a # invalid input to minimum a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556 ; touch #{touchdir}/uh_oh", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil) - assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable" + assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable" assert_equal [], a # invalid input to minimum a = Commit.find_commit_range('active/foo', "$(uname>#{touchdir}/uh_oh)", "31ce37fe365b3dc204300a3e4c396ad333ed0556", nil) - assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable" + assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'minimum' parameter of find_commit_range is exploitable" assert_equal [], a # invalid input to 'excludes' # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57" a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["4fe459abe02d9b365932b8f5dc419439ab4e2577 ; touch #{touchdir}/uh_oh"]) - assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable" + assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable" assert_equal [], a # invalid input to 'excludes' # complains "fatal: bad object 077ba2ad3ea24a929091a9e6ce545c93199b8e57" a = Commit.find_commit_range('active/foo', "31ce37fe365b3dc204300a3e4c396ad333ed0556", "077ba2ad3ea24a929091a9e6ce545c93199b8e57", ["$(uname>#{touchdir}/uh_oh)"]) - assert !File.exists?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable" + assert !File.exist?("#{touchdir}/uh_oh"), "#{touchdir}/uh_oh should not exist, 'excludes' parameter of find_commit_range is exploitable" assert_equal [], a end end