git-lfs/t
Chris Darroch 11092ef2b1 t/{t-path,testenv}.sh: refactor and fix path tests
In commits 74d5f2397f9abe4834bf1fe1fa02fd6c141b77ce and
10c4ffc6b888eee8f2134a7009a0db1bc393e17b the t/t-path.sh tests
were added to validate the remediations of the security issues
from CVE-2020-27955 and CVE-2021-21237, respectively.  On Windows,
both of these tests make use of a "git.bat" script which stands in for
Git, and if the script is executed instead of the real Git during a
test, then that indicates that a security problem still exists.

However, in a previous commit we have now added a new helper program,
lfstest-badpathcheck, which can be used for the same purpose, and which
has the advantage of having a .exe extension on Windows.  We will
make use of this helper program in a new test accompanying the
remediation of CVE-2022-24826.

The existence of this new helper also means we can now use it as a
fake "malicious" binary named "git.exe" in our existing t/t-path.sh
tests.  This will help ensure that these tests are robust against
unexpected values of the Windows PATHEXT environment variable or
future changes to the set of executable file extensions we support
in Git LFS.

Using this new helper program instead of a "git.bat" script does mean
we need to be careful how and when it might run, because it mirrors
the name of the real Git executable.  We therefore try to keep it out
of any possible execution path until we run our final concluding checks
in each test.  In other words, rather than try to manipulate PATH (and
PATHEXT on Windows), we take steps to keep our "malicious" executable
out of any possible search path prior to when we want to explicitly
test Git LFS's behaviour in a known state relating to one of the CVEs.

(One challenge with manipulating PATH to try to remove paths that
resolve to the current directory is that it must be done iteratively
to deal with pathological cases like ":.:.:" and ":::".  The "sed"
regular expressions in our existing tests would also need to use "\."
to match only the "." character, as well as additional expressions to
match "." at the beginning and end of PATH.)

It is easier, therefore, to simply avoid putting our "malicious"
executable into the current directory until the last moment.  Also,
in the second test where we want to add it to a repository which
we will then clone, we can remove it as soon as we've run "git add",
and we can make sure to run the real Git (i.e., whatever is found
first using the extant PATH and PATHEXT variables) rather than ours
to perform that "git add".

When we reach the end of each test, where we want to explicitly run
Git LFS and see if it incorrectly searches in the current directory
even when not asked to, we now reset PATH and PATHEXT just for those
specific invocations of Git LFS.  For PATH we use either BINPATH,
which contains only our compiled "git-lfs" binary, or BINPATH plus
the path to the real Git as returned by "command -v".  For PATHEXT
we just use the primary executable file extension (if any) for the
current operating system.

To determine that primary executable file extension we add an X
variable which we set in t/testenv.sh and which parallels the one
set in the main Makefile.  On Windows, we set X to contain ".exe",
and on Unix we set it to the empty string.  We can then use this X
variable throughout our tests wherever we want to refer to a specific
executable's full filename.

With these changes, even when PATH includes "." as the first
directory to be searched, both of our tests should now always reach
their concluding checks and should function as expected at that
point.  Specifically, they should fail by detecting the output of
our "malicious" Git program when run without the Git LFS code changes
corresponding to their respective CVEs, and should succeed otherwise.

(Technically, the second test will fail for a different reason
if the remediation for CVE-2020-27955 is not in effect than if only
the remediation for CVE-2021-21237 is not in effect.  But the test
will fail at the same point in both cases, i.e., in its concluding
final check.)

In the final checks in both tests we search for the text string
"exploit" in the output log file captured after running a Git LFS
command, using the shell command "! grep -q 'exploit' output.log".
The "!" reverses the exit code from "grep", so if the word "exploit"
is found, the test should fail.  This works in the first test because
the command is the last one in the test, so the inverted exit code
from "grep" is returned as the exit code from whole test's subshell.

However, in the second test several other commands follow this command,
and because "set -e" (which is standard at the top of every test)
ignores commands' exit codes when they are inverted with "!", the
test proceeds even if the word "exploit" is seen in the output log.

To resolve this problem we instead use a command pipeline and ensure
that when the "grep" succeeds, the exit code from the final command
in the pipeline is generated by "false".  This successfully causes
the test to fail immediately when the word "exploit" is seen in the
output log file.  Moreover, in both tests we now follow the "grep"
check with checks for the presence of a file named "exploit"; this
provides a second level of assurance that our "malicious" Git
program has not executed.

Finally, we add detailed comments regarding specific steps in both
tests where the intention and purpose may not be clear just from
the context.
2022-04-19 09:45:20 -07:00
..
cmd t: add lfstest-badpathcheck helper for path tests 2022-04-19 09:45:20 -07:00
fixtures commands,git,t: fail migration on gitattrs symlink 2022-01-30 18:46:02 -08:00
git-lfs-test-server-api move err checking to before the value was used 2021-12-10 12:33:17 +02:00
Makefile t: add lfstest-badpathcheck helper for path tests 2022-04-19 09:45:20 -07:00
README.md t/README.md: link to 'Test Lifecycle' appropriately 2018-07-16 16:39:51 -05:00
t-alternates.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-askpass.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-attributes.sh commands/track: read all attributes files to parse macros 2018-12-03 17:18:50 +00:00
t-batch-error-handling.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-batch-retries-ratelimit.sh Add tests coverage for retry-after batch endpoint 2021-08-15 10:18:32 +02:00
t-batch-retries.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-batch-storage-retries-ratelimit.sh Add tests coverage for retry-after batch endpoint 2021-08-15 10:18:32 +02:00
t-batch-transfer.sh tq: pass hash algorithm during batch requests 2021-09-10 14:39:01 +00:00
t-batch-unknown-oids.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-checkout.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-cherry-pick-commits.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-chunked-transfer-encoding.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-clean.sh t: load shell scripts from $(dirname) instead of 't' 2018-07-10 13:51:40 -05:00
t-clone-deprecated.sh use backticks around commands in messages 2022-01-29 22:35:10 -08:00
t-clone.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-commit-delete-push.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-config.sh commands,config,t: use single quotes in messages 2022-01-29 22:35:10 -08:00
t-content-type.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-credentials-no-prompt.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-credentials.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-custom-transfers.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-dedup.sh commands/command_dedup.go,t: consistent messages 2022-01-29 22:32:57 -08:00
t-duplicate-oids.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-env.sh lfsapi: make strings translatable 2022-01-18 17:38:24 +00:00
t-expired.sh t: label expiration test using git-lfs-authenticate 2021-07-20 19:16:00 +00:00
t-ext.sh t: load shell scripts from $(dirname) instead of 't' 2018-07-10 13:51:40 -05:00
t-extra-header.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-fetch-include.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-fetch-paths.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-fetch-recent.sh t: test fetch/prune diff parsing with ANSI escapes 2021-08-17 19:47:36 -07:00
t-fetch-refspec.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-fetch.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-filter-branch.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-filter-process.sh smudge: honor GIT_LFS_SKIP_SMUDGE with checkout-index 2022-02-07 19:22:02 +00:00
t-fsck.sh t/t-fsck.sh: add whitespace 2022-02-07 11:25:06 -08:00
t-happy-path.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-install-custom-hooks-path-unsupported.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-install-custom-hooks-path.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-install-worktree-unsupported.sh t: add tests for --worktree install and uninstall 2020-06-26 14:56:06 -07:00
t-install-worktree.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-install.sh use backticks around commands in messages 2022-01-29 22:35:10 -08:00
t-lock.sh Correct t-lock regular expression to be musl compatible 2021-10-07 16:37:23 +02:00
t-locks.sh t: test locking with git-lfs-transfer 2021-07-20 19:16:00 +00:00
t-logs.sh t: load shell scripts from $(dirname) instead of 't' 2018-07-10 13:51:40 -05:00
t-ls-files.sh use backticks around commands in messages 2022-01-29 22:35:10 -08:00
t-malformed-pointers.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-mergetool.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-migrate-export.sh commands,git,t: fail migration on gitattrs symlink 2022-01-30 18:46:02 -08:00
t-migrate-fixup.sh commands,git,t: fail migration on gitattrs symlink 2022-01-30 18:46:02 -08:00
t-migrate-import-no-rewrite.sh commands,config,t: use single quotes in messages 2022-01-29 22:35:10 -08:00
t-migrate-import.sh commands,git,t: fail migration on gitattrs symlink 2022-01-30 18:46:02 -08:00
t-migrate-info.sh commands,git,t: fail migration on gitattrs symlink 2022-01-30 18:46:02 -08:00
t-object-authenticated.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-path.sh t/{t-path,testenv}.sh: refactor and fix path tests 2022-04-19 09:45:20 -07:00
t-pointer.sh commands/pointer: add an option for strict checking 2021-07-14 18:12:07 +00:00
t-post-checkout.sh post-checkout: don't modify permissions of untracked files 2021-12-06 13:44:33 +00:00
t-post-commit.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-post-merge.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-pre-push.sh commands,t: use Git LFS project name in messages 2022-01-29 22:30:22 -08:00
t-progress-meter.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-progress.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-prune-worktree.sh commands/prune: make strings translatable 2022-01-18 17:03:38 +00:00
t-prune.sh commands/prune: make strings translatable 2022-01-18 17:03:38 +00:00
t-pull.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-push-bad-dns.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-push-failures-local.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-push-failures-remote.sh Fix typos 2022-01-05 08:49:08 +02:00
t-push-file-with-branch-name.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-push.sh tq,t: remove transfer queue message prefix 2022-01-29 21:59:22 -08:00
t-reference-clone.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-repo-format.sh Add support for an LFS repository format version 2021-07-21 13:28:51 +00:00
t-resume-http-range.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-resume-tus.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-smudge.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-ssh.sh t/t-ssh.sh: also perform test without ssh variant 2021-11-30 20:23:35 +00:00
t-standalone-file.sh t: move the urlify function to shared code 2021-07-20 19:16:00 +00:00
t-status.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-submodule-lfsconfig.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-submodule-recurse.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-submodule.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-tempfile.sh fs: be a little less aggressive with cleanup 2021-04-30 14:36:06 +00:00
t-track-attrs.sh t: load shell scripts from $(dirname) instead of 't' 2018-07-10 13:51:40 -05:00
t-track-wildcards.sh t: load shell scripts from $(dirname) instead of 't' 2018-07-10 13:51:40 -05:00
t-track.sh commands,config,t: use single quotes in messages 2022-01-29 22:35:10 -08:00
t-umask.sh TST: Trim security context when checking permissions. 2019-01-15 01:20:13 -05:00
t-uninstall-worktree-unsupported.sh t: add tests for --worktree install and uninstall 2020-06-26 14:56:06 -07:00
t-uninstall-worktree.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-uninstall.sh commands,t: use capitalized Git in messages 2022-01-29 22:30:22 -08:00
t-unlock.sh commands/unlock: make strings translatable 2022-01-18 17:03:27 +00:00
t-untrack.sh commands: make sure we're in the working tree 2020-10-14 20:58:28 +00:00
t-unusual-filenames.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-update.sh use backticks around commands in messages 2022-01-29 22:35:10 -08:00
t-upload-redirect.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-verify.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
t-version.sh t: load shell scripts from $(dirname) instead of 't' 2018-07-10 13:51:40 -05:00
t-worktree.sh Make CI environment GIT prefix grep more specific 2021-10-07 23:48:30 +02:00
t-zero-len-file.sh Rename default branch in tests to "main" 2020-07-08 15:38:17 +00:00
testenv.sh t/{t-path,testenv}.sh: refactor and fix path tests 2022-04-19 09:45:20 -07:00
testhelpers.sh Allow git-lfs-transfer integration tests to be skipped 2021-10-12 20:33:12 +02:00
testlib.sh t: add an environment variable to dump test output 2018-09-18 20:48:18 +00:00

t

This directory contains one of the two types of tests that the Git LFS project uses to protect against regression. The first, scattered in *_test.go files throughout the repository are unit tests, and written in Go, designed to uncover failures at the unit level.

The second kind--and the one contained in this directory--are integration tests, which are designed to exercise Git LFS in an end-to-end fashion, running the git, and git-lfs binaries, along with a mock Git server.

You can run all tests in this directory with any of the following:

$ make
$ make test
$ make PROVE_EXTRA_ARGS=-j9 test

Or run a single test (for example, t-checkout.sh) by any of the following:

$ make ./t-checkout.sh
$ make PROVE_EXTRA_ARGS=-v ./t-checkout.sh
$ ./t-checkout.sh

Alternatively, one can run a selection of tests (via explicitly listing them or making use of the built-in shell globbing) by any of the following:

$ make ./t-*.sh
$ make PROVE_EXTRA_ARGS=-j9 ./t-*.sh
$ ./t-*.sh

Test File(s)

There are a few important kinds of files to know about in the t directory:

  • cmd/: contains the source code of binaries that are useful during test time, like the mocked Git server, or the test counting binary. For more about the contents of this directory, see test lifecycle below.

    The file t/cmd/testutils.go is automatically linked and included during the build process of each file in cmd.

  • fixtures/: contains shell scripts that load fixture repositories useful for testing against.

  • t-*.sh: file(s) containing zero or more tests, typically related to a similar topic (c.f,. t/t-push.sh, t/t-pull.sh, etc.)

  • testenv.sh: loads environment variables useful during tests. This file is sourced by testlib.sh.

  • testhelpers.sh: loads shell functions useful during tests, like setup_remote_repo, and clone_repo.

  • testlib.sh: loads the begin_test, end_test, and similar functions useful for instantiating a particular test.

Test Lifecycle

When a test is run, the following occurs, in order:

  1. Missing test binaries are compiled into the bin directory in the repository root. Note: this does not include the git-lfs binary, which is re-compiled via script/boostrap.

  2. An integration server is started by either (1) the Makefile or (2) the cmd/lfstest-count-test.go program, which keeps track of the number of running tests and starts an integration server any time the number of active tests goes from 0 to 1, and stops the server when it goes from n to 0.

  3. After sourcing t/testlib.sh (& loading t/testenv.sh), each test is run in sequence per file. (In other words, multiple test files can be run in parallel, but the tests in a single file are run in sequence.)

  4. An individual test will finish, and (if running under prove) another will be started in its place. Once all tests are done, t/test_count will go to 0, and the test server will be torn down.

Test Environment

There are a few environment variables that you can set to change the test suite behavior:

  • GIT_LFS_TEST_DIR=path - This sets the directory that is used as the current working directory of the tests. By default, this will be in your temp dir. It's recommended that this is set to a directory outside of any Git repository.

  • KEEPTRASH=1 - This will leave the local repository data in a tmp directory and the remote repository data in test/remote.

Also ensure that your noproxy environment variable contains 127.0.0.1 host, to allow git commands to reach the local Git server lfstest-gitserver.

Writing new tests

A new test file should be named t/t-*.sh, where * is the topic of Git LFS being tested. It should look as follows:

#!/usr/bin/env bash

. "$(dirname "$0")/testlib.sh"

begin_test "my test"
(
  set -e

  # ...
)
end_test