Пример #1
0
func TestEncodeExtensions(t *testing.T) {
	var buf bytes.Buffer
	exts := []*PointerExtension{
		NewPointerExtension("foo", 0, "foo_oid"),
		NewPointerExtension("bar", 1, "bar_oid"),
		NewPointerExtension("baz", 2, "baz_oid"),
	}
	pointer := NewPointer("main_oid", 12345, exts)
	_, err := EncodePointer(&buf, pointer)
	assert.Equal(t, nil, err)

	bufReader := bufio.NewReader(&buf)
	assertLine(t, bufReader, "version https://git-lfs.github.com/spec/v1\n")
	assertLine(t, bufReader, "ext-0-foo sha256:foo_oid\n")
	assertLine(t, bufReader, "ext-1-bar sha256:bar_oid\n")
	assertLine(t, bufReader, "ext-2-baz sha256:baz_oid\n")
	assertLine(t, bufReader, "oid sha256:main_oid\n")
	assertLine(t, bufReader, "size 12345\n")

	line, err := bufReader.ReadString('\n')
	if err == nil {
		t.Fatalf("More to read: %s", line)
	}
	assert.Equal(t, "EOF", err.Error())
}
Пример #2
0
func TestCertFromSSLCAInfoEnv(t *testing.T) {

	tempfile, err := ioutil.TempFile("", "testcert")
	assert.Equal(t, nil, err, "Error creating temp cert file")
	defer os.Remove(tempfile.Name())

	_, err = tempfile.WriteString(testCert)
	assert.Equal(t, nil, err, "Error writing temp cert file")
	tempfile.Close()

	oldEnv := Config.envVars
	defer func() {
		Config.envVars = oldEnv
	}()

	Config.envVars = map[string]string{"GIT_SSL_CAINFO": tempfile.Name()}

	// Should match any host at all
	pool := getRootCAsForHost("git-lfs.local")
	assert.NotEqual(t, (*x509.CertPool)(nil), pool)
	pool = getRootCAsForHost("wronghost.com")
	assert.NotEqual(t, (*x509.CertPool)(nil), pool)
	pool = getRootCAsForHost("notthisone.com:8888")
	assert.NotEqual(t, (*x509.CertPool)(nil), pool)

}
Пример #3
0
func TestCertFromSSLCAPathEnv(t *testing.T) {

	tempdir, err := ioutil.TempDir("", "testcertdir")
	assert.Equal(t, nil, err, "Error creating temp cert dir")
	defer os.RemoveAll(tempdir)

	err = ioutil.WriteFile(filepath.Join(tempdir, "cert1.pem"), []byte(testCert), 0644)
	assert.Equal(t, nil, err, "Error creating cert file")

	oldEnv := Config.envVars
	defer func() {
		Config.envVars = oldEnv
	}()

	Config.envVars = map[string]string{"GIT_SSL_CAPATH": tempdir}

	// Should match any host at all
	pool := getRootCAsForHost("git-lfs.local")
	assert.NotEqual(t, (*x509.CertPool)(nil), pool)
	pool = getRootCAsForHost("wronghost.com")
	assert.NotEqual(t, (*x509.CertPool)(nil), pool)
	pool = getRootCAsForHost("notthisone.com:8888")
	assert.NotEqual(t, (*x509.CertPool)(nil), pool)

}
Пример #4
0
func TestAccessAbsentConfig(t *testing.T) {
	config := &Configuration{}
	assert.Equal(t, "none", config.Access("download"))
	assert.Equal(t, "none", config.Access("upload"))
	assert.Equal(t, false, config.PrivateAccess("download"))
	assert.Equal(t, false, config.PrivateAccess("upload"))
}
Пример #5
0
func TestCurrentRefAndCurrentRemoteRef(t *testing.T) {
	repo := test.NewRepo(t)
	repo.Pushd()
	defer func() {
		repo.Popd()
		repo.Cleanup()
	}()

	// test commits; we'll just modify the same file each time since we're
	// only interested in branches
	inputs := []*test.CommitInput{
		{ // 0
			Files: []*test.FileInput{
				{Filename: "file1.txt", Size: 20},
			},
		},
		{ // 1
			NewBranch: "branch2",
			Files: []*test.FileInput{
				{Filename: "file1.txt", Size: 25},
			},
		},
		{ // 2
			ParentBranches: []string{"master"}, // back on master
			Files: []*test.FileInput{
				{Filename: "file1.txt", Size: 30},
			},
		},
		{ // 3
			NewBranch: "branch3",
			Files: []*test.FileInput{
				{Filename: "file1.txt", Size: 32},
			},
		},
	}
	outputs := repo.AddCommits(inputs)
	// last commit was on branch3
	ref, err := CurrentRef()
	assert.Equal(t, nil, err)
	assert.Equal(t, &Ref{"branch3", RefTypeLocalBranch, outputs[3].Sha}, ref)
	test.RunGitCommand(t, true, "checkout", "master")
	ref, err = CurrentRef()
	assert.Equal(t, nil, err)
	assert.Equal(t, &Ref{"master", RefTypeLocalBranch, outputs[2].Sha}, ref)
	// Check remote
	repo.AddRemote("origin")
	test.RunGitCommand(t, true, "push", "-u", "origin", "master:someremotebranch")
	ref, err = CurrentRemoteRef()
	assert.Equal(t, nil, err)
	assert.Equal(t, &Ref{"origin/someremotebranch", RefTypeRemoteBranch, outputs[2].Sha}, ref)

	refname, err := RemoteRefNameForCurrentBranch()
	assert.Equal(t, nil, err)
	assert.Equal(t, "origin/someremotebranch", refname)

	remote, err := RemoteForCurrentBranch()
	assert.Equal(t, nil, err)
	assert.Equal(t, "origin", remote)
}
Пример #6
0
func TestNtlmHeaderParseValid(t *testing.T) {
	res := http.Response{}
	res.Header = make(map[string][]string)
	res.Header.Add("Www-Authenticate", "NTLM "+base64.StdEncoding.EncodeToString([]byte("I am a moose")))
	bytes, err := parseChallengeResponse(&res)
	assert.Equal(t, err, nil)
	assert.Equal(t, strings.HasPrefix(string(bytes), "NTLM"), false)
}
Пример #7
0
func TestFetchPruneConfigDefault(t *testing.T) {
	config := &Configuration{}
	fp := config.FetchPruneConfig()

	assert.Equal(t, 7, fp.FetchRecentRefsDays)
	assert.Equal(t, 0, fp.FetchRecentCommitsDays)
	assert.Equal(t, 3, fp.PruneOffsetDays)
	assert.Equal(t, true, fp.FetchRecentRefsIncludeRemotes)

}
Пример #8
0
func TestLoadInvalidExtension(t *testing.T) {
	config := &Configuration{}

	ext := config.Extensions()["foo"]

	assert.Equal(t, "", ext.Name)
	assert.Equal(t, "", ext.Clean)
	assert.Equal(t, "", ext.Smudge)
	assert.Equal(t, 0, ext.Priority)
}
Пример #9
0
func TestWriterWithCallback(t *testing.T) {
	called := 0
	calledRead := make([]int64, 0, 2)

	reader := &CallbackReader{
		TotalSize: 5,
		Reader:    bytes.NewBufferString("BOOYA"),
		C: func(total int64, read int64, current int) error {
			called += 1
			calledRead = append(calledRead, read)
			assert.Equal(t, 5, int(total))
			return nil
		},
	}

	readBuf := make([]byte, 3)
	n, err := reader.Read(readBuf)
	assert.Equal(t, nil, err)
	assert.Equal(t, "BOO", string(readBuf[0:n]))

	n, err = reader.Read(readBuf)
	assert.Equal(t, nil, err)
	assert.Equal(t, "YA", string(readBuf[0:n]))

	assert.Equal(t, 2, called)
	assert.Equal(t, 2, len(calledRead))
	assert.Equal(t, 3, int(calledRead[0]))
	assert.Equal(t, 5, int(calledRead[1]))
}
Пример #10
0
func TestSSHGetExeAndArgsSsh(t *testing.T) {
	endpoint := Config.Endpoint("download")
	endpoint.SshUserAndHost = "*****@*****.**"
	oldGITSSH := Config.Getenv("GIT_SSH")
	Config.Setenv("GIT_SSH", "")
	exe, args := sshGetExeAndArgs(endpoint)
	assert.Equal(t, "ssh", exe)
	assert.Equal(t, []string{"*****@*****.**"}, args)

	Config.Setenv("GIT_SSH", oldGITSSH)
}
Пример #11
0
func TestBareEndpointAddsLfsSuffix(t *testing.T) {
	config := &Configuration{
		gitConfig: map[string]string{"remote.origin.url": "https://example.com/foo/bar.git"},
		remotes:   []string{},
	}

	endpoint := config.Endpoint()
	assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", endpoint.Url)
	assert.Equal(t, "", endpoint.SshUserAndHost)
	assert.Equal(t, "", endpoint.SshPath)
}
Пример #12
0
func TestEncodeEmpty(t *testing.T) {
	var buf bytes.Buffer
	pointer := NewPointer("", 0, nil)
	_, err := EncodePointer(&buf, pointer)
	assert.Equal(t, nil, err)

	bufReader := bufio.NewReader(&buf)
	val, err := bufReader.ReadString('\n')
	assert.Equal(t, "", val)
	assert.Equal(t, "EOF", err.Error())
}
Пример #13
0
func TestEndpointDefaultsToOrigin(t *testing.T) {
	config := &Configuration{
		gitConfig: map[string]string{"remote.origin.lfsurl": "abc"},
		remotes:   []string{},
	}

	endpoint := config.Endpoint()
	assert.Equal(t, "abc", endpoint.Url)
	assert.Equal(t, "", endpoint.SshUserAndHost)
	assert.Equal(t, "", endpoint.SshPath)
}
Пример #14
0
func TestNtlmCloneRequest(t *testing.T) {
	req1, _ := http.NewRequest("Method", "url", nil)
	cloneOfReq1, err := cloneRequest(req1)
	assert.Equal(t, err, nil)
	assertRequestsEqual(t, req1, cloneOfReq1)

	req2, _ := http.NewRequest("Method", "url", bytes.NewReader([]byte("Moose can be request bodies")))
	cloneOfReq2, err := cloneRequest(req2)
	assert.Equal(t, err, nil)
	assertRequestsEqual(t, req2, cloneOfReq2)
}
Пример #15
0
func TestCertVerifyDisabledGlobalEnv(t *testing.T) {

	assert.Equal(t, false, isCertVerificationDisabledForHost("anyhost.com"))

	oldEnv := Config.envVars
	defer func() {
		Config.envVars = oldEnv
	}()
	Config.envVars = map[string]string{"GIT_SSL_NO_VERIFY": "1"}

	assert.Equal(t, true, isCertVerificationDisabledForHost("anyhost.com"))
}
Пример #16
0
func TestCertVerifyDisabledGlobalConfig(t *testing.T) {

	assert.Equal(t, false, isCertVerificationDisabledForHost("anyhost.com"))

	oldGitConfig := Config.gitConfig
	defer func() {
		Config.gitConfig = oldGitConfig
	}()
	Config.gitConfig = map[string]string{"http.sslverify": "false"}

	assert.Equal(t, true, isCertVerificationDisabledForHost("anyhost.com"))
}
Пример #17
0
func TestSSHCustomPortEndpointAddsLfsSuffix(t *testing.T) {
	config := &Configuration{
		gitConfig: map[string]string{"remote.origin.url": "ssh://[email protected]:9000/foo/bar"},
		remotes:   []string{},
	}

	endpoint := config.Endpoint()
	assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", endpoint.Url)
	assert.Equal(t, "*****@*****.**", endpoint.SshUserAndHost)
	assert.Equal(t, "foo/bar", endpoint.SshPath)
	assert.Equal(t, "9000", endpoint.SshPort)
}
Пример #18
0
func TestSSHEndpointFromGlobalLfsUrl(t *testing.T) {
	config := &Configuration{
		gitConfig: map[string]string{"lfs.url": "[email protected]:foo/bar.git"},
		remotes:   []string{},
	}

	endpoint := config.Endpoint()
	assert.Equal(t, "https://example.com/foo/bar.git", endpoint.Url)
	assert.Equal(t, "*****@*****.**", endpoint.SshUserAndHost)
	assert.Equal(t, "foo/bar.git", endpoint.SshPath)
	assert.Equal(t, "", endpoint.SshPort)
}
Пример #19
0
func TestSSHGetExeAndArgsSshCustomPort(t *testing.T) {
	endpoint := Config.Endpoint()
	endpoint.SshUserAndHost = "*****@*****.**"
	endpoint.SshPort = "8888"
	oldGITSSH := Config.Getenv("GIT_SSH")
	Config.Setenv("GIT_SSH", "")
	exe, args := sshGetExeAndArgs(endpoint)
	assert.Equal(t, "ssh", exe)
	assert.Equal(t, []string{"-p", "8888", "*****@*****.**"}, args)

	Config.Setenv("GIT_SSH", oldGITSSH)
}
Пример #20
0
func TestFilterIncludeExclude(t *testing.T) {

	cases := []TestIncludeExcludeCase{
		// Null case
		TestIncludeExcludeCase{true, nil, nil},
		// Inclusion
		TestIncludeExcludeCase{true, []string{"test/filename.dat"}, nil},
		TestIncludeExcludeCase{true, []string{"test/filename.dat"}, nil},
		TestIncludeExcludeCase{false, []string{"blank", "something", "foo"}, nil},
		TestIncludeExcludeCase{false, []string{"test/notfilename.dat"}, nil},
		TestIncludeExcludeCase{true, []string{"test"}, nil},
		TestIncludeExcludeCase{true, []string{"test/*"}, nil},
		TestIncludeExcludeCase{false, []string{"nottest"}, nil},
		TestIncludeExcludeCase{false, []string{"nottest/*"}, nil},
		TestIncludeExcludeCase{true, []string{"test/fil*"}, nil},
		TestIncludeExcludeCase{false, []string{"test/g*"}, nil},
		TestIncludeExcludeCase{true, []string{"tes*/*"}, nil},
		// Exclusion
		TestIncludeExcludeCase{false, nil, []string{"test/filename.dat"}},
		TestIncludeExcludeCase{false, nil, []string{"blank", "something", "test/filename.dat", "foo"}},
		TestIncludeExcludeCase{true, nil, []string{"blank", "something", "foo"}},
		TestIncludeExcludeCase{true, nil, []string{"test/notfilename.dat"}},
		TestIncludeExcludeCase{false, nil, []string{"test"}},
		TestIncludeExcludeCase{false, nil, []string{"test/*"}},
		TestIncludeExcludeCase{true, nil, []string{"nottest"}},
		TestIncludeExcludeCase{true, nil, []string{"nottest/*"}},
		TestIncludeExcludeCase{false, nil, []string{"test/fil*"}},
		TestIncludeExcludeCase{true, nil, []string{"test/g*"}},
		TestIncludeExcludeCase{false, nil, []string{"tes*/*"}},

		// Both
		TestIncludeExcludeCase{true, []string{"test/filename.dat"}, []string{"test/notfilename.dat"}},
		TestIncludeExcludeCase{false, []string{"test"}, []string{"test/filename.dat"}},
		TestIncludeExcludeCase{true, []string{"test/*"}, []string{"test/notfile*"}},
		TestIncludeExcludeCase{false, []string{"test/*"}, []string{"test/file*"}},
		TestIncludeExcludeCase{false, []string{"another/*", "test/*"}, []string{"test/notfilename.dat", "test/filename.dat"}},
	}

	for _, c := range cases {
		assert.Equal(t, c.expectedResult, FilenamePassesIncludeExcludeFilter("test/filename.dat", c.includes, c.excludes), c)
		if IsWindows() {
			// also test with \ path separators, tolerate mixed separators
			for i, inc := range c.includes {
				c.includes[i] = strings.Replace(inc, "/", "\\", -1)
			}
			for i, ex := range c.excludes {
				c.excludes[i] = strings.Replace(ex, "/", "\\", -1)
			}
			assert.Equal(t, c.expectedResult, FilenamePassesIncludeExcludeFilter("test/filename.dat", c.includes, c.excludes), c)
		}
	}
}
Пример #21
0
func TestBatcherSizeMet(t *testing.T) {
	b := NewBatcher(2)

	for i := 0; i < 4; i++ {
		b.Add(&Downloadable{})
	}

	group := b.Next()
	assert.Equal(t, 2, len(group))

	group = b.Next()
	assert.Equal(t, 2, len(group))
}
Пример #22
0
func TestSSHGetExeAndArgsTortoisePlink(t *testing.T) {
	endpoint := Config.Endpoint("download")
	endpoint.SshUserAndHost = "*****@*****.**"
	oldGITSSH := Config.Getenv("GIT_SSH")
	// this will run on non-Windows platforms too but no biggie
	plink := filepath.Join("Users", "joebloggs", "bin", "tortoiseplink.exe")
	Config.Setenv("GIT_SSH", plink)
	exe, args := sshGetExeAndArgs(endpoint)
	assert.Equal(t, plink, exe)
	assert.Equal(t, []string{"-batch", "*****@*****.**"}, args)

	Config.Setenv("GIT_SSH", oldGITSSH)
}
Пример #23
0
func TestCertVerifyDisabledHostConfig(t *testing.T) {

	assert.Equal(t, false, isCertVerificationDisabledForHost("specifichost.com"))
	assert.Equal(t, false, isCertVerificationDisabledForHost("otherhost.com"))

	oldGitConfig := Config.gitConfig
	defer func() {
		Config.gitConfig = oldGitConfig
	}()
	Config.gitConfig = map[string]string{"http.https://specifichost.com/.sslverify": "false"}

	assert.Equal(t, true, isCertVerificationDisabledForHost("specifichost.com"))
	assert.Equal(t, false, isCertVerificationDisabledForHost("otherhost.com"))
}
Пример #24
0
func TestEndpointNoOverrideDefaultRemote(t *testing.T) {
	config := &Configuration{
		gitConfig: map[string]string{
			"remote.origin.lfsurl": "abc",
			"remote.other.lfsurl":  "def",
		},
		remotes: []string{},
	}

	endpoint := config.Endpoint()
	assert.Equal(t, "abc", endpoint.Url)
	assert.Equal(t, "", endpoint.SshUserAndHost)
	assert.Equal(t, "", endpoint.SshPath)
}
Пример #25
0
func TestSSHGetExeAndArgsPlinkCustomPort(t *testing.T) {
	endpoint := Config.Endpoint()
	endpoint.SshUserAndHost = "*****@*****.**"
	endpoint.SshPort = "8888"
	oldGITSSH := Config.Getenv("GIT_SSH")
	// this will run on non-Windows platforms too but no biggie
	plink := filepath.Join("Users", "joebloggs", "bin", "plink")
	Config.Setenv("GIT_SSH", plink)
	exe, args := sshGetExeAndArgs(endpoint)
	assert.Equal(t, plink, exe)
	assert.Equal(t, []string{"-P", "8888", "*****@*****.**"}, args)

	Config.Setenv("GIT_SSH", oldGITSSH)
}
Пример #26
0
func TestEndpointOverridesOrigin(t *testing.T) {
	config := &Configuration{
		gitConfig: map[string]string{
			"lfs.url":              "abc",
			"remote.origin.lfsurl": "def",
		},
		remotes: []string{},
	}

	endpoint := config.Endpoint("download")
	assert.Equal(t, "abc", endpoint.Url)
	assert.Equal(t, "", endpoint.SshUserAndHost)
	assert.Equal(t, "", endpoint.SshPath)
}
Пример #27
0
func TestSSHEndpointOverridden(t *testing.T) {
	config := &Configuration{
		gitConfig: map[string]string{
			"remote.origin.url":    "[email protected]:foo/bar",
			"remote.origin.lfsurl": "lfs",
		},
		remotes: []string{},
	}

	endpoint := config.Endpoint()
	assert.Equal(t, "lfs", endpoint.Url)
	assert.Equal(t, "", endpoint.SshUserAndHost)
	assert.Equal(t, "", endpoint.SshPath)
}
Пример #28
0
func TestGitEndpointAddsLfsSuffixWithCustomProtocol(t *testing.T) {
	config := &Configuration{
		gitConfig: map[string]string{
			"remote.origin.url": "git://example.com/foo/bar",
			"lfs.gitprotocol":   "http",
		},
		remotes: []string{},
	}

	endpoint := config.Endpoint("download")
	assert.Equal(t, "http://example.com/foo/bar.git/info/lfs", endpoint.Url)
	assert.Equal(t, "", endpoint.SshUserAndHost)
	assert.Equal(t, "", endpoint.SshPath)
	assert.Equal(t, "", endpoint.SshPort)
}
Пример #29
0
func assertRequestsEqual(t *testing.T, req1 *http.Request, req2 *http.Request) {
	assert.Equal(t, req1.Method, req2.Method)

	for k, v := range req1.Header {
		assert.Equal(t, v, req2.Header[k])
	}

	if req1.Body == nil {
		assert.Equal(t, req2.Body, nil)
	} else {
		bytes1, _ := ioutil.ReadAll(req1.Body)
		bytes2, _ := ioutil.ReadAll(req2.Body)
		assert.Equal(t, bytes.Compare(bytes1, bytes2), 0)
	}
}
Пример #30
0
func TestVersionCompare(t *testing.T) {
	assert.Equal(t, true, IsVersionAtLeast("2.6.0", "2.6.0"))
	assert.Equal(t, true, IsVersionAtLeast("2.6.0", "2.6"))
	assert.Equal(t, true, IsVersionAtLeast("2.6.0", "2"))
	assert.Equal(t, true, IsVersionAtLeast("2.6.10", "2.6.5"))
	assert.Equal(t, true, IsVersionAtLeast("2.8.1", "2.7.2"))

	assert.Equal(t, false, IsVersionAtLeast("1.6.0", "2"))
	assert.Equal(t, false, IsVersionAtLeast("2.5.0", "2.6"))
	assert.Equal(t, false, IsVersionAtLeast("2.5.0", "2.5.1"))
	assert.Equal(t, false, IsVersionAtLeast("2.5.2", "2.5.10"))
}