public void ThrowTest1() { P4ClientError error = new P4ClientError(ErrorSeverity.E_FAILED, "This is a test"); P4Exception.MinThrowLevel = ErrorSeverity.E_FATAL; bool passed = true; try { P4Exception.Throw(error); } catch { passed = false; // should not have thrown } Assert.IsTrue(passed, "Threw an exception < MinThrowLevel"); P4Exception.MinThrowLevel = ErrorSeverity.E_FAILED; passed = false; try { P4Exception.Throw(error); } catch { passed = true; // should have thrown } Assert.IsTrue(passed, "Did not throw an exception >= MinThrowLevel"); }
/// <summary>Simple login check using `p4 login -s`</summary> /// <para>Return 'true' if logged in (with or without a password).</para> public bool isLoggedIn() { try { string[] loginOptions = new string[] { "-s" }; P4.P4Command LoginCmd = new P4Command(Repository, "login", true, loginOptions); P4CommandResult LoginResults = LoginCmd.Run(); if ((LoginResults == null) || (LoginResults.TaggedOutput == null)) { return(false); } string validated; LoginResults.TaggedOutput[0].TryGetValue("2FA", out validated); if (validated == "required") { P4ClientError err = new P4ClientError(ErrorSeverity.E_FAILED, "Second factor authentication required! Run 'p4 login2'."); err.ErrorCode = 807673723; P4Exception ex = new P4Exception(err); throw ex; } // logged in. May not have a ticket if no password was required. return(true); } catch (P4.P4Exception ex) { if (ex.ErrorCode == P4.P4ClientError.MsgServer_Login2Required) { throw ex; } return(false); } }
private bool Login(string password) { // if Swarm is null, don't attempt to set this if (Swarm != null) { Swarm.SwarmCredential = null; } // unsure if this currently works with legacy SSO script support, but // leaving it in as it is harmless if ssoScript is null and if not, // it should be the same as the ssoVar found in Login() string ssoScript = Environment.GetEnvironmentVariable("P4LOGINSSO"); if (ssoScript != null) { password = "******"; // dummy value server will not prompt for a password when using SSO logger.Debug("Using SSO: {0}", ssoScript); } try { bool UseAllHostTicket = !Preferences.LocalSettings.GetBool("Use_IP", false); if (string.IsNullOrEmpty(password) != true) { // always first login on the local machine for an 'all machines token, as we can't read the property // to see if swarm is enabled till we log in logger.Debug("Logging in to local machine"); LoginCmdOptions opts = new LoginCmdOptions(LoginCmdFlags.AllHosts, null); P4.Credential Credential = Repository.Connection.Login(password, opts); if (Credential != null) { // Now we can see if Swarm is enabled as since we logged in // the property to see if swarm is enabled Swarm.CheckForSwarm(); if (Swarm.SwarmEnabled) { Swarm.SwarmCredential = Credential; } if (Credential.Expires != DateTime.MaxValue) { string msg = string.Format(Resources.P4ScmProvider_LoginLoginExpireInfo, Credential.Expires); P4VsOutputWindow.AppendMessage(msg); } return(true); } else if (Repository.Connection.LastResults.ErrorList != null) { // the error code for an SSO login fail will be sent by the trigger // so it will be a trigger failed message. if (Repository.Connection.LastResults.ErrorList[0].ErrorCode == P4.P4ClientError.MsgServer_TriggerFailed) { P4VsOutputWindow.AppendMessage(Repository.Connection.LastResults.ErrorList[0].ErrorMessage); MessageBox.Show(Repository.Connection.LastResults.ErrorList[0].ErrorMessage, Resources.P4VS, MessageBoxButtons.OK, MessageBoxIcon.Error); ssoSuccess = false; return(true); } // Or if it is a fatal error, display that as well. This can // happen if the P4LOGINSSO is set but the agent does not // exist. Likely an edge case. foreach (P4ClientError e in Repository.Connection.LastResults.ErrorList) { if (e.SeverityLevel == ErrorSeverity.E_FATAL) { P4VsOutputWindow.AppendMessage(Repository.Connection.LastResults.ErrorList[0].ErrorMessage); MessageBox.Show(Repository.Connection.LastResults.ErrorList[0].ErrorMessage, Resources.P4VS, MessageBoxButtons.OK, MessageBoxIcon.Error); ssoSuccess = false; return(true); } } } } } catch (Exception ex) { P4Exception p4ex = ex as P4Exception; if (p4ex.ErrorCode == P4.P4ClientError.MsgServer_Login2Required) { throw p4ex; } P4VsOutputWindow.AppendMessage(ex.Message); MessageBox.Show(ex.Message, Resources.P4VS, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(false); }