public virtual void TestFileProtocol() { // as defined by git docu URIish u = new URIish("file:///a/b.txt"); NUnit.Framework.Assert.AreEqual("file", u.GetScheme()); NUnit.Framework.Assert.IsFalse(u.IsRemote()); NUnit.Framework.Assert.IsNull(u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual("/a/b.txt", u.GetPath()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.AreEqual("b.txt", u.GetHumanishName()); FilePath tmp = FilePath.CreateTempFile("jgitUnitTest", ".tmp"); u = new URIish(tmp.ToURI().ToString()); NUnit.Framework.Assert.AreEqual("file", u.GetScheme()); NUnit.Framework.Assert.IsFalse(u.IsRemote()); NUnit.Framework.Assert.IsNull(u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.IsTrue(u.GetPath().Contains("jgitUnitTest")); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.IsTrue(u.GetHumanishName().StartsWith("jgitUnitTest")); u = new URIish("file:/a/b.txt"); NUnit.Framework.Assert.AreEqual("file", u.GetScheme()); NUnit.Framework.Assert.IsFalse(u.IsRemote()); NUnit.Framework.Assert.IsNull(u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual("/a/b.txt", u.GetPath()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.AreEqual("b.txt", u.GetHumanishName()); }
public virtual void TestGetSet() { string str = "ssh://DOMAIN\\user:[email protected]:33/some/p ath%20"; URIish u = new URIish(str); u = u.SetHost(u.GetHost()); u = u.SetPass(u.GetPass()); u = u.SetPort(u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); u = u.SetRawPath(u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some/p ath%20", u.GetRawPath()); u = u.SetPath(u.GetPath()); NUnit.Framework.Assert.AreEqual("/some/p ath ", u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some/p ath ", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual("DOMAIN\\user", u.GetUser()); NUnit.Framework.Assert.AreEqual("pass", u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh://DOMAIN\\user:[email protected]:33/some/p ath " , u.ToPrivateString()); NUnit.Framework.Assert.AreEqual("ssh://DOMAIN\\user:[email protected]:33/some/p%20ath%20" , u.ToPrivateASCIIString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateASCIIString(), u.ToASCIIString ()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestUserPasswordAndPort() { string str = "http://*****:*****@host.xy:80/some/path"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("http", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/path", u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some/path", u.GetPath()); NUnit.Framework.Assert.AreEqual("host.xy", u.GetHost()); NUnit.Framework.Assert.AreEqual(80, u.GetPort()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.AreEqual("secret", u.GetPass()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); str = "http://*****:*****@[email protected]:80/some/path"; u = new URIish(str); NUnit.Framework.Assert.AreEqual("http", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/path", u.GetPath()); NUnit.Framework.Assert.AreEqual("host.xy", u.GetHost()); NUnit.Framework.Assert.AreEqual(80, u.GetPort()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.AreEqual("secret@pass", u.GetPass()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
/// <summary>Update this method with the credentials from the URIish.</summary> /// <remarks>Update this method with the credentials from the URIish.</remarks> /// <param name="uri">the URI used to create the connection.</param> /// <param name="credentialsProvider"> /// the credentials provider, or null. If provided, /// <see cref="URIish.GetPass()">credentials in the URI</see> /// are ignored. /// </param> /// <returns> /// true if the authentication method is able to provide /// authorization for the given URI /// </returns> internal virtual bool Authorize(URIish uri, CredentialsProvider credentialsProvider ) { string username; string password; if (credentialsProvider != null) { CredentialItem.Username u = new CredentialItem.Username(); CredentialItem.Password p = new CredentialItem.Password(); if (credentialsProvider.Supports(u, p) && credentialsProvider.Get(uri, u, p)) { username = u.GetValue(); password = new string(p.GetValue()); p.Clear(); } else { return(false); } } else { username = uri.GetUser(); password = uri.GetPass(); } if (username != null) { Authorize(username, password); return(true); } return(false); }
public virtual void TestFileProtocol() { // as defined by git docu URIish u = new URIish("file:///a/b.txt"); NUnit.Framework.Assert.AreEqual("file", u.GetScheme()); NUnit.Framework.Assert.IsFalse(u.IsRemote()); NUnit.Framework.Assert.IsNull(u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual("/a/b.txt", u.GetPath()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.AreEqual("b.txt", u.GetHumanishName()); FilePath tmp = FilePath.CreateTempFile("jgitUnitTest", ".tmp"); u = new URIish(tmp.ToURI().ToString()); NUnit.Framework.Assert.AreEqual("file", u.GetScheme()); NUnit.Framework.Assert.IsFalse(u.IsRemote()); NUnit.Framework.Assert.IsNull(u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.IsTrue(u.GetPath().Contains("jgitUnitTest")); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.IsTrue(u.GetHumanishName().StartsWith("jgitUnitTest")); u = new URIish("file:/a/b.txt"); NUnit.Framework.Assert.AreEqual("file", u.GetScheme()); NUnit.Framework.Assert.IsFalse(u.IsRemote()); NUnit.Framework.Assert.IsNull(u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual("/a/b.txt", u.GetPath()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.AreEqual("b.txt", u.GetHumanishName()); }
public override bool CanHandle(URIish uri, Repository local, string remoteName) { if (uri.GetPath() == null || uri.GetPort() > 0 || uri.GetUser() != null || uri.GetPass () != null || uri.GetHost() != null || (uri.GetScheme() != null && !this.GetSchemes ().Contains(uri.GetScheme()))) { return false; } return true; }
//$NON-NLS-1$ public override bool CanHandle(URIish uri, Repository local, string remoteName) { if (uri.GetPath() == null || uri.GetPort() > 0 || uri.GetUser() != null || uri.GetPass () != null || uri.GetHost() != null || (uri.GetScheme() != null && !this.GetSchemes ().Contains(uri.GetScheme()))) { return(false); } return(true); }
internal static bool CanHandle(URIish uri, FS fs) { if (uri.GetHost() != null || uri.GetPort() > 0 || uri.GetUser() != null || uri.GetPass () != null || uri.GetPath() == null) { return(false); } if ("file".Equals(uri.GetScheme()) || uri.GetScheme() == null) { return(fs.Resolve(new FilePath(PWD), uri.GetPath()).IsDirectory()); } return(false); }
internal static bool CanHandle(URIish uri, FS fs) { if (uri.GetHost() != null || uri.GetPort() > 0 || uri.GetUser() != null || uri.GetPass () != null || uri.GetPath() == null) { return(false); } if ("file".Equals(uri.GetScheme()) || uri.GetScheme() == null) { FilePath f = fs.Resolve(new FilePath("."), uri.GetPath()); return(f.IsFile() || f.GetName().EndsWith(".bundle")); } return(false); }
public virtual void TestScpStyleWithUser() { string str = "[email protected]:some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.IsNull(u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.AreEqual(str, u.ToString()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
internal static bool CanHandle(URIish uri, FS fs) { if (uri.GetHost() != null || uri.GetPort() > 0 || uri.GetUser() != null || uri.GetPass () != null || uri.GetPath() == null) { return false; } if ("file".Equals(uri.GetScheme()) || uri.GetScheme() == null) { FilePath f = fs.Resolve(new FilePath("."), uri.GetPath()); return f.IsFile() || f.GetName().EndsWith(".bundle"); } return false; }
public virtual void TestSshProtoWithUserAndPort() { string str = "ssh://[email protected]:33/some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual(str, u.ToString()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestFileWithNoneUserHomeWithTilde() { string str = "/~some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.IsNull(u.GetScheme()); NUnit.Framework.Assert.IsFalse(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/~some/p ath", u.GetPath()); NUnit.Framework.Assert.IsNull(u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.AreEqual(str, u.ToPrivateString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestGitWithUserHome() { string str = "git://example.com/~some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("git", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("~some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.AreEqual(str, u.ToPrivateString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestURIEncodeDecode() { string str = "ssh://%3ax%25:%40%[email protected]:33/some%c3%a5/p%20a th"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some%c3%a5/p%20a th", u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some\u00e5/p a th", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual(":x%", u.GetUser()); NUnit.Framework.Assert.AreEqual("@Ax", u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh://%3ax%25:%[email protected]:33/some%c3%a5/p%20a th" , u.ToPrivateString()); NUnit.Framework.Assert.AreEqual("ssh://%3ax%25:%[email protected]:33/some%c3%a5/p%20a%20th" , u.ToPrivateASCIIString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateASCIIString(), u.ToASCIIString ()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestSshProtoWithEscapedADUserPassAndPort() { string str = "ssh://DOMAIN%5c\u00fcser:[email protected]:33/some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/p ath", u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual("DOMAIN\\\u00fcser", u.GetUser()); NUnit.Framework.Assert.AreEqual("pass", u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh://DOMAIN\\\u00fcser:[email protected]:33/some/p ath" , u.ToPrivateString()); NUnit.Framework.Assert.AreEqual("ssh://DOMAIN\\%c3%bcser:[email protected]:33/some/p%20ath" , u.ToPrivateASCIIString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateASCIIString(), u.ToASCIIString ()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestGitWithUserHome() { string str = "git://example.com/~some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("git", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("~some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.AreEqual(str, u.ToPrivateString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
/// <exception cref="NGit.Errors.TransportException"></exception> public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider , FS fs, int tms) { lock (this) { string user = uri.GetUser(); string pass = uri.GetPass(); string host = uri.GetHost(); int port = uri.GetPort(); try { if (config == null) { config = OpenSshConfig.Get(fs); } OpenSshConfig.Host hc = config.Lookup(host); host = hc.GetHostName(); if (port <= 0) { port = hc.GetPort(); } if (user == null) { user = hc.GetUser(); } Session session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc); int retries = 0; while (!session.IsConnected() && retries < 3) { try { retries++; session.Connect(tms); } catch (JSchException e) { session.Disconnect(); session = null; // if authentication failed maybe credentials changed at the // remote end therefore reset credentials and retry if (credentialsProvider != null && e.InnerException == null && e.Message.Equals("Auth fail" )) { credentialsProvider.Reset(uri); session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc); } else { throw; } } } return new JschSession(session, uri); } catch (JSchException je) { Exception c = je.InnerException; if (c is UnknownHostException) { throw new TransportException(uri, JGitText.Get().unknownHost); } if (c is ConnectException) { throw new TransportException(uri, c.Message); } throw new TransportException(uri, je.Message, je); } } }
/// <summary>Determine if this protocol can handle a particular URI.</summary> /// <remarks> /// Determine if this protocol can handle a particular URI. /// <p/> /// Implementations should try to avoid looking at the local filesystem, but /// may look at implementation specific configuration options in the remote /// block of /// <code>local.getConfig()</code> /// using /// <code>remoteName</code> /// if the name /// is non-null. /// <p/> /// The default implementation of this method matches the scheme against /// <see cref="GetSchemes()">GetSchemes()</see> /// , required fields against /// <see cref="GetRequiredFields()">GetRequiredFields()</see> /// , and optional fields against /// <see cref="GetOptionalFields()">GetOptionalFields()</see> /// , returning true only if all of the fields /// match the specification. /// </remarks> /// <param name="uri">address of the Git repository; never null.</param> /// <param name="local"> /// the local repository that will communicate with the other Git /// repository. May be null if the caller is only asking about a /// specific URI and does not have a local Repository. /// </param> /// <param name="remoteName"> /// name of the remote, if the remote as configured in /// <code>local</code> /// ; otherwise null. /// </param> /// <returns>true if this protocol can handle this URI; false otherwise.</returns> public virtual bool CanHandle(URIish uri, Repository local, string remoteName) { if (!GetSchemes().IsEmpty() && !GetSchemes().Contains(uri.GetScheme())) { return false; } foreach (TransportProtocol.URIishField field in GetRequiredFields()) { switch (field) { case TransportProtocol.URIishField.USER: { if (uri.GetUser() == null || uri.GetUser().Length == 0) { return false; } break; } case TransportProtocol.URIishField.PASS: { if (uri.GetPass() == null || uri.GetPass().Length == 0) { return false; } break; } case TransportProtocol.URIishField.HOST: { if (uri.GetHost() == null || uri.GetHost().Length == 0) { return false; } break; } case TransportProtocol.URIishField.PORT: { if (uri.GetPort() <= 0) { return false; } break; } case TransportProtocol.URIishField.PATH: { if (uri.GetPath() == null || uri.GetPath().Length == 0) { return false; } break; } default: { return false; break; } } } ICollection<TransportProtocol.URIishField> canHave = EnumSet.CopyOf(GetRequiredFields ()); Sharpen.Collections.AddAll(canHave, GetOptionalFields()); if (uri.GetUser() != null && !canHave.Contains(TransportProtocol.URIishField.USER )) { return false; } if (uri.GetPass() != null && !canHave.Contains(TransportProtocol.URIishField.PASS )) { return false; } if (uri.GetHost() != null && !canHave.Contains(TransportProtocol.URIishField.HOST )) { return false; } if (uri.GetPort() > 0 && !canHave.Contains(TransportProtocol.URIishField.PORT)) { return false; } if (uri.GetPath() != null && !canHave.Contains(TransportProtocol.URIishField.PATH )) { return false; } return true; }
public virtual void TestUserPasswordAndPort() { string str = "http://*****:*****@host.xy:80/some/path"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("http", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/path", u.GetPath()); NUnit.Framework.Assert.AreEqual("host.xy", u.GetHost()); NUnit.Framework.Assert.AreEqual(80, u.GetPort()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.AreEqual("secret", u.GetPass()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); str = "http://*****:*****@[email protected]:80/some/path"; u = new URIish(str); NUnit.Framework.Assert.AreEqual("http", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/path", u.GetPath()); NUnit.Framework.Assert.AreEqual("host.xy", u.GetHost()); NUnit.Framework.Assert.AreEqual(80, u.GetPort()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.AreEqual("secret@pass", u.GetPass()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestURIEncodeDecode() { string str = "ssh://%3ax%25:%40%[email protected]:33/some%c3%a5/p%20a th"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some%c3%a5/p%20a th", u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some\u00e5/p a th", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual(":x%", u.GetUser()); NUnit.Framework.Assert.AreEqual("@Ax", u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh://%3ax%25:%[email protected]:33/some%c3%a5/p%20a th" , u.ToPrivateString()); NUnit.Framework.Assert.AreEqual("ssh://%3ax%25:%[email protected]:33/some%c3%a5/p%20a%20th" , u.ToPrivateASCIIString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateASCIIString(), u.ToASCIIString ()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
/// <summary>Update this method with the credentials from the URIish.</summary> /// <remarks>Update this method with the credentials from the URIish.</remarks> /// <param name="uri">the URI used to create the connection.</param> /// <param name="credentialsProvider"> /// the credentials provider, or null. If provided, /// <see cref="URIish.GetPass()">credentials in the URI</see> /// are ignored. /// </param> /// <returns> /// true if the authentication method is able to provide /// authorization for the given URI /// </returns> internal virtual bool Authorize(URIish uri, CredentialsProvider credentialsProvider ) { string username; string password; if (credentialsProvider != null) { CredentialItem.Username u = new CredentialItem.Username(); CredentialItem.Password p = new CredentialItem.Password(); if (credentialsProvider.Supports(u, p) && credentialsProvider.Get(uri, u, p)) { username = u.GetValue(); password = new string(p.GetValue()); p.Clear(); } else { return false; } } else { username = uri.GetUser(); password = uri.GetPass(); } if (username != null) { Authorize(username, password); return true; } return false; }
public virtual void TestSshProtoWithUserPassAndPort() { string str = "ssh://*****:*****@example.com:33/some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.AreEqual("pass", u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual(str, u.ToPrivateString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestALot() { // user pass host port path // 1 2 3 4 5 string[][] tests = new string[][] { new string[] { "%1$s://%2$s:%3$s@%4$s:%5$s/%6$s" , "%1$s", "%2$s", "%3$s", "%4$s", "%5$s", "%6$s" }, new string[] { "%1$s://%2$s@%4$s:%5$s/%6$s" , "%1$s", "%2$s", null, "%4$s", "%5$s", "%6$s" }, new string[] { "%1$s://%2$s@%4$s/%6$s" , "%1$s", "%2$s", null, "%4$s", null, "%6$s" }, new string[] { "%1$s://%4$s/%6$s" , "%1$s", null, null, "%4$s", null, "%6$s" } }; for (int i = 0; i < tests.Length; i++) { for (int j = 0; j < tests [i].Length; j++) { for (int k = 0; k < 10; k++) { if (tests[i][j] != null) { tests [i][j] = tests[i][j].Replace("%" + k + "$s", "{" + (k - 1) + "}"); } } } } string[] schemes = new string[] { "ssh", "ssh+git", "http", "https" }; string[] users = new string[] { "me", "l usr\\example.com", "lusr\\example" }; string[] passes = new string[] { "wtf" }; string[] hosts = new string[] { "example.com", "1.2.3.4" }; string[] ports = new string[] { "1234", "80" }; string[] paths = new string[] { "/", "/abc", "D:/x", "D:\\x" }; foreach (string[] test in tests) { string fmt = test[0]; foreach (string scheme in schemes) { foreach (string user in users) { foreach (string pass in passes) { foreach (string host in hosts) { foreach (string port in ports) { foreach (string path in paths) { string url = string.Format(fmt, scheme, user, pass, host, port, path); string[] expect = new string[test.Length]; for (int i = 1; i < expect.Length; ++i) { if (test[i] != null) { expect[i] = string.Format(test[i], scheme, user, pass, host, port, path); } } URIish urIish = new URIish(url); NUnit.Framework.Assert.AreEqual(expect[1], urIish.GetScheme(), url); NUnit.Framework.Assert.AreEqual(expect[2], urIish.GetUser(), url); } } } } } } } }
public virtual void TestSshProtoWithEscapedADUserPassAndPort() { string str = "ssh://DOMAIN%5c\u00fcser:[email protected]:33/some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/p ath", u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual("DOMAIN\\\u00fcser", u.GetUser()); NUnit.Framework.Assert.AreEqual("pass", u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh://DOMAIN\\\u00fcser:[email protected]:33/some/p ath" , u.ToPrivateString()); NUnit.Framework.Assert.AreEqual("ssh://DOMAIN\\%c3%bcser:[email protected]:33/some/p%20ath" , u.ToPrivateASCIIString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateASCIIString(), u.ToASCIIString ()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestGetSet() { string str = "ssh://DOMAIN\\user:[email protected]:33/some/p ath%20"; URIish u = new URIish(str); u = u.SetHost(u.GetHost()); u = u.SetPass(u.GetPass()); u = u.SetPort(u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); u = u.SetRawPath(u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some/p ath%20", u.GetRawPath()); u = u.SetPath(u.GetPath()); NUnit.Framework.Assert.AreEqual("/some/p ath ", u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some/p ath ", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual("DOMAIN\\user", u.GetUser()); NUnit.Framework.Assert.AreEqual("pass", u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh://DOMAIN\\user:[email protected]:33/some/p ath " , u.ToPrivateString()); NUnit.Framework.Assert.AreEqual("ssh://DOMAIN\\user:[email protected]:33/some/p%20ath%20" , u.ToPrivateASCIIString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateASCIIString(), u.ToASCIIString ()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestALot() { // user pass host port path // 1 2 3 4 5 string[][] tests = new string[][] { new string[] { "%1$s://%2$s:%3$s@%4$s:%5$s/%6$s" , "%1$s", "%2$s", "%3$s", "%4$s", "%5$s", "%6$s" }, new string[] { "%1$s://%2$s@%4$s:%5$s/%6$s" , "%1$s", "%2$s", null, "%4$s", "%5$s", "%6$s" }, new string[] { "%1$s://%2$s@%4$s/%6$s" , "%1$s", "%2$s", null, "%4$s", null, "%6$s" }, new string[] { "%1$s://%4$s/%6$s" , "%1$s", null, null, "%4$s", null, "%6$s" } }; for (int i = 0; i < tests.Length; i++) { for (int j = 0; j < tests [i].Length; j++) { for (int k = 0; k < 10; k++) { if (tests[i][j] != null) tests [i][j] = tests[i][j].Replace ("%" + k + "$s", "{" + (k - 1) + "}"); } } } string[] schemes = new string[] { "ssh", "ssh+git", "http", "https" }; string[] users = new string[] { "me", "l usr\\example.com", "lusr\\example" }; string[] passes = new string[] { "wtf" }; string[] hosts = new string[] { "example.com", "1.2.3.4" }; string[] ports = new string[] { "1234", "80" }; string[] paths = new string[] { "/", "/abc", "D:/x", "D:\\x" }; foreach (string[] test in tests) { string fmt = test[0]; foreach (string scheme in schemes) { foreach (string user in users) { foreach (string pass in passes) { foreach (string host in hosts) { foreach (string port in ports) { foreach (string path in paths) { string url = string.Format(fmt, scheme, user, pass, host, port, path); string[] expect = new string[test.Length]; for (int i = 1; i < expect.Length; ++i) { if (test[i] != null) { expect[i] = string.Format(test[i], scheme, user, pass, host, port, path); } } URIish urIish = new URIish(url); NUnit.Framework.Assert.AreEqual(expect[1], urIish.GetScheme(), url); NUnit.Framework.Assert.AreEqual(expect[2], urIish.GetUser(), url); } } } } } } } }
/// <exception cref="NGit.Errors.TransportException"></exception> public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider , FS fs, int tms) { lock (this) { string user = uri.GetUser(); string pass = uri.GetPass(); string host = uri.GetHost(); int port = uri.GetPort(); try { if (config == null) { config = OpenSshConfig.Get(fs); } OpenSshConfig.Host hc = config.Lookup(host); host = hc.GetHostName(); if (port <= 0) { port = hc.GetPort(); } if (user == null) { user = hc.GetUser(); } Session session = CreateSession(hc, user, host, port, fs); if (pass != null) { session.SetPassword(pass); } string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking(); if (strictHostKeyCheckingPolicy != null) { session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy); } string pauth = hc.GetPreferredAuthentications(); if (pauth != null) { session.SetConfig("PreferredAuthentications", pauth); } if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive ())) { session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider) ); } Configure(hc, session); if (!session.IsConnected()) { session.Connect(tms); } return(new JschSession(session, uri)); } catch (JSchException je) { Exception c = je.InnerException; if (c is UnknownHostException) { throw new TransportException(uri, JGitText.Get().unknownHost); } if (c is ConnectException) { throw new TransportException(uri, c.Message); } throw new TransportException(uri, je.Message, je); } } }
public virtual void TestSshProtoWithUserAndPort() { string str = "ssh://[email protected]:33/some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.AreEqual("ssh", u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/some/p ath", u.GetRawPath()); NUnit.Framework.Assert.AreEqual("/some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual(33, u.GetPort()); NUnit.Framework.Assert.AreEqual("ssh://[email protected]:33/some/p ath", u.ToString ()); NUnit.Framework.Assert.AreEqual("ssh://[email protected]:33/some/p%20ath", u.ToASCIIString ()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
public virtual void TestScpStyleWithUser() { string str = "[email protected]:some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.IsNull(u.GetScheme()); NUnit.Framework.Assert.IsTrue(u.IsRemote()); NUnit.Framework.Assert.AreEqual("some/p ath", u.GetPath()); NUnit.Framework.Assert.AreEqual("user", u.GetUser()); NUnit.Framework.Assert.AreEqual("example.com", u.GetHost()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.AreEqual(str, u.ToString()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
internal static bool CanHandle(URIish uri, FS fs) { if (uri.GetHost() != null || uri.GetPort() > 0 || uri.GetUser() != null || uri.GetPass () != null || uri.GetPath() == null) { return false; } if ("file".Equals(uri.GetScheme()) || uri.GetScheme() == null) { return fs.Resolve(new FilePath(PWD), uri.GetPath()).IsDirectory(); } return false; }
/// <exception cref="NGit.Errors.TransportException"></exception> public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider , FS fs, int tms) { lock (this) { string user = uri.GetUser(); string pass = uri.GetPass(); string host = uri.GetHost(); int port = uri.GetPort(); try { if (config == null) { config = OpenSshConfig.Get(fs); } OpenSshConfig.Host hc = config.Lookup(host); host = hc.GetHostName(); if (port <= 0) { port = hc.GetPort(); } if (user == null) { user = hc.GetUser(); } Session session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc); int retries = 0; while (!session.IsConnected() && retries < 3) { try { retries++; session.Connect(tms); } catch (JSchException e) { session.Disconnect(); session = null; // if authentication failed maybe credentials changed at the // remote end therefore reset credentials and retry if (credentialsProvider != null && e.InnerException == null && e.Message.Equals("Auth fail" ) && retries < 3) { credentialsProvider.Reset(uri); session = CreateSession(credentialsProvider, fs, user, pass, host, port, hc); } else { throw; } } } return(new JschSession(session, uri)); } catch (JSchException je) { Exception c = je.InnerException; if (c is UnknownHostException) { throw new TransportException(uri, JGitText.Get().unknownHost); } if (c is ConnectException) { throw new TransportException(uri, c.Message); } throw new TransportException(uri, je.Message, je); } } }
public CredentialsDialog (URIish uri, IEnumerable<CredentialItem> credentials) { this.Build (); labelTop.Text = string.Format (labelTop.Text, uri.ToString ()); Gtk.Table table = new Gtk.Table (0, 0, false); table.ColumnSpacing = 6; vbox.PackStart (table, true, true, 0); uint r = 0; Widget firstEditor = null; foreach (CredentialItem c in credentials) { Label lab = new Label (c.GetPromptText () + ":"); lab.Xalign = 0; table.Attach (lab, 0, 1, r, r + 1); Table.TableChild tc = (Table.TableChild) table [lab]; tc.XOptions = AttachOptions.Shrink; Widget editor = null; if (c is CredentialItem.YesNoType) { CredentialItem.YesNoType cred = (CredentialItem.YesNoType) c; if (credentials.Count (i => i is CredentialItem.YesNoType) == 1) { singleYesNoCred = cred; buttonOk.Hide (); buttonYes.Show (); buttonNo.Show (); // Remove the last colon lab.Text = lab.Text.Substring (0, lab.Text.Length - 1); } else { CheckButton btn = new CheckButton (); editor = btn; btn.Toggled += delegate { cred.SetValue (btn.Active); }; } } else if (c is CredentialItem.StringType || c is CredentialItem.CharArrayType) { CredentialItem cred = c; Entry e = new Entry (); editor = e; e.ActivatesDefault = true; if (cred.IsValueSecure ()) e.Visibility = false; e.Changed += delegate { if (cred is CredentialItem.StringType) ((CredentialItem.StringType)cred).SetValue (e.Text); else ((CredentialItem.CharArrayType)cred).SetValue (e.Text.ToCharArray ()); }; if (c is CredentialItem.Username) e.Text = uri.GetUser () ?? ""; } if (editor != null) { table.Attach (editor, 1, 2, r, r + 1); tc = (Table.TableChild) table [lab]; tc.XOptions = AttachOptions.Fill; if (firstEditor == null) firstEditor = editor; } r++; } table.ShowAll (); Focus = firstEditor; Default = buttonOk; }
public virtual void TestFileWithNoneUserHomeWithTilde() { string str = "/~some/p ath"; URIish u = new URIish(str); NUnit.Framework.Assert.IsNull(u.GetScheme()); NUnit.Framework.Assert.IsFalse(u.IsRemote()); NUnit.Framework.Assert.AreEqual("/~some/p ath", u.GetPath()); NUnit.Framework.Assert.IsNull(u.GetHost()); NUnit.Framework.Assert.IsNull(u.GetUser()); NUnit.Framework.Assert.IsNull(u.GetPass()); NUnit.Framework.Assert.AreEqual(-1, u.GetPort()); NUnit.Framework.Assert.AreEqual(str, u.ToPrivateString()); NUnit.Framework.Assert.AreEqual(u.SetPass(null).ToPrivateString(), u.ToString()); NUnit.Framework.Assert.AreEqual(u, new URIish(str)); }
/// <summary>Determine if this protocol can handle a particular URI.</summary> /// <remarks> /// Determine if this protocol can handle a particular URI. /// <p> /// Implementations should try to avoid looking at the local filesystem, but /// may look at implementation specific configuration options in the remote /// block of /// <code>local.getConfig()</code> /// using /// <code>remoteName</code> /// if the name /// is non-null. /// <p> /// The default implementation of this method matches the scheme against /// <see cref="GetSchemes()">GetSchemes()</see> /// , required fields against /// <see cref="GetRequiredFields()">GetRequiredFields()</see> /// , and optional fields against /// <see cref="GetOptionalFields()">GetOptionalFields()</see> /// , returning true only if all of the fields /// match the specification. /// </remarks> /// <param name="uri">address of the Git repository; never null.</param> /// <param name="local"> /// the local repository that will communicate with the other Git /// repository. May be null if the caller is only asking about a /// specific URI and does not have a local Repository. /// </param> /// <param name="remoteName"> /// name of the remote, if the remote as configured in /// <code>local</code> /// ; otherwise null. /// </param> /// <returns>true if this protocol can handle this URI; false otherwise.</returns> public virtual bool CanHandle(URIish uri, Repository local, string remoteName) { if (!GetSchemes().IsEmpty() && !GetSchemes().Contains(uri.GetScheme())) { return(false); } foreach (TransportProtocol.URIishField field in GetRequiredFields()) { switch (field) { case TransportProtocol.URIishField.USER: { if (uri.GetUser() == null || uri.GetUser().Length == 0) { return(false); } break; } case TransportProtocol.URIishField.PASS: { if (uri.GetPass() == null || uri.GetPass().Length == 0) { return(false); } break; } case TransportProtocol.URIishField.HOST: { if (uri.GetHost() == null || uri.GetHost().Length == 0) { return(false); } break; } case TransportProtocol.URIishField.PORT: { if (uri.GetPort() <= 0) { return(false); } break; } case TransportProtocol.URIishField.PATH: { if (uri.GetPath() == null || uri.GetPath().Length == 0) { return(false); } break; } default: { return(false); break; } } } ICollection <TransportProtocol.URIishField> canHave = EnumSet.CopyOf(GetRequiredFields ()); Sharpen.Collections.AddAll(canHave, GetOptionalFields()); if (uri.GetUser() != null && !canHave.Contains(TransportProtocol.URIishField.USER )) { return(false); } if (uri.GetPass() != null && !canHave.Contains(TransportProtocol.URIishField.PASS )) { return(false); } if (uri.GetHost() != null && !canHave.Contains(TransportProtocol.URIishField.HOST )) { return(false); } if (uri.GetPort() > 0 && !canHave.Contains(TransportProtocol.URIishField.PORT)) { return(false); } if (uri.GetPath() != null && !canHave.Contains(TransportProtocol.URIishField.PATH )) { return(false); } return(true); }
/// <exception cref="NGit.Errors.TransportException"></exception> public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider , FS fs, int tms) { lock (this) { string user = uri.GetUser(); string pass = uri.GetPass(); string host = uri.GetHost(); int port = uri.GetPort(); try { if (config == null) { config = OpenSshConfig.Get(fs); } OpenSshConfig.Host hc = config.Lookup(host); host = hc.GetHostName(); if (port <= 0) { port = hc.GetPort(); } if (user == null) { user = hc.GetUser(); } Session session = CreateSession(hc, user, host, port, fs); if (pass != null) { session.SetPassword(pass); } string strictHostKeyCheckingPolicy = hc.GetStrictHostKeyChecking(); if (strictHostKeyCheckingPolicy != null) { session.SetConfig("StrictHostKeyChecking", strictHostKeyCheckingPolicy); } string pauth = hc.GetPreferredAuthentications(); if (pauth != null) { session.SetConfig("PreferredAuthentications", pauth); } if (credentialsProvider != null && (!hc.IsBatchMode() || !credentialsProvider.IsInteractive ())) { session.SetUserInfo(new CredentialsProviderUserInfo(session, credentialsProvider) ); } Configure(hc, session); if (!session.IsConnected()) { session.Connect(tms); } return new JschSession(session, uri); } catch (JSchException je) { Exception c = je.InnerException; if (c is UnknownHostException) { throw new TransportException(uri, JGitText.Get().unknownHost); } if (c is ConnectException) { throw new TransportException(uri, c.Message); } throw new TransportException(uri, je.Message, je); } } }