@@ -12,6 +12,10 @@ import (
1212 "os"
1313 "path/filepath"
1414 "strings"
15+
16+ "bytes"
17+ "crypto/sha256"
18+ "hash"
1519 "time"
1620
1721 // git2go must be aligned with libgit2 version:
@@ -25,6 +29,11 @@ import (
2529 "golang.org/x/crypto/ssh/knownhosts"
2630)
2731
32+ const (
33+ TestUser = "test-user"
34+ TestPass = "test-pass"
35+ )
36+
2837func main () {
2938 fmt .Println ("Running tests..." )
3039 testsDir , err := filepath .Abs ("./build/tests" )
@@ -45,7 +54,16 @@ func main() {
4554 test ("HTTPS clone with no options" ,
4655 filepath .Join (testsDir , "/https-clone-no-options" ),
4756 httpRepoURL ,
48- & git2go.CloneOptions {Bare : true })
57+ & git2go.CloneOptions {
58+ Bare : true ,
59+ FetchOptions : git2go.FetchOptions {
60+ RemoteCallbacks : git2go.RemoteCallbacks {
61+ CredentialsCallback : func (url string , username string , allowedTypes git2go.CredentialType ) (* git2go.Credential , error ) {
62+ return git2go .NewCredentialUserpassPlaintext (TestUser , TestPass )
63+ },
64+ },
65+ },
66+ })
4967
5068 if err := server .ListenSSH (); err != nil {
5169 panic (fmt .Errorf ("listenSSH: %w" , err ))
@@ -80,7 +98,11 @@ func main() {
8098 FetchOptions : git2go.FetchOptions {
8199 RemoteCallbacks : git2go.RemoteCallbacks {
82100 CredentialsCallback : func (url string , username string , allowedTypes git2go.CredentialType ) (* git2go.Credential , error ) {
83- return git2go .NewCredentialSSHKeyFromMemory ("git" , string (rsa .PublicKey ), string (rsa .PrivateKey ), "" )
101+ signer , err := cryptossh .ParsePrivateKey (rsa .PrivateKey )
102+ if err != nil {
103+ return nil , err
104+ }
105+ return git2go .NewCredentialSSHKeyFromSigner ("git" , signer )
84106 },
85107 CertificateCheckCallback : knownHostsCallback (u .Host , knownHosts ),
86108 },
@@ -99,7 +121,11 @@ func main() {
99121 FetchOptions : git2go.FetchOptions {
100122 RemoteCallbacks : git2go.RemoteCallbacks {
101123 CredentialsCallback : func (url string , username string , allowedTypes git2go.CredentialType ) (* git2go.Credential , error ) {
102- return git2go .NewCredentialSSHKeyFromMemory ("git" , string (ed25519 .PublicKey ), string (ed25519 .PrivateKey ), "" )
124+ signer , err := cryptossh .ParsePrivateKey (ed25519 .PrivateKey )
125+ if err != nil {
126+ return nil , err
127+ }
128+ return git2go .NewCredentialSSHKeyFromSigner ("git" , signer )
103129 },
104130 CertificateCheckCallback : knownHostsCallback (u .Host , knownHosts ),
105131 },
@@ -117,7 +143,7 @@ func createTestServer(repoPath string) *gittestserver.GitServer {
117143 }
118144 defer os .RemoveAll (server .Root ())
119145
120- server .Auth ("test-user" , "test-pswd" )
146+ server .Auth (TestUser , TestPass )
121147 server .AutoCreate ()
122148 server .KeyDir (filepath .Join (server .Root (), "keys" ))
123149
@@ -242,20 +268,13 @@ func (k knownKey) matches(host string, hostkey git2go.HostkeyCertificate) bool {
242268 return false
243269 }
244270
245- if hostkey .Kind & git2go .HostkeySHA256 > 0 {
246- knownFingerprint := cryptossh .FingerprintSHA256 (k .key )
247- returnedFingerprint := cryptossh .FingerprintSHA256 (hostkey .SSHPublicKey )
248-
249- fmt .Printf ("known and found fingerprints:\n %q\n %q\n " ,
250- knownFingerprint ,
251- returnedFingerprint )
252- if returnedFingerprint == knownFingerprint {
253- return true
254- }
255- }
271+ var fingerprint []byte
272+ var hasher hash.Hash
256273
257- fmt .Println ("host kind not supported" )
258- return false
274+ fingerprint = hostkey .HashSHA256 [:]
275+ hasher = sha256 .New ()
276+ hasher .Write (k .key .Marshal ())
277+ return bytes .Equal (hasher .Sum (nil ), fingerprint )
259278}
260279
261280func containsHost (hosts []string , host string ) bool {
0 commit comments