using Perforce.P4; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections; using System.IO; using System.Diagnostics; namespace p4api.net.unit.test { /// ///This is a test class for P4ServerTest and is intended ///to contain all P4ServerTest Unit Tests /// [TestClass()] public class P4ServerTest { String TestDir = "c:\\MyTestDir"; private TestContext testContextInstance; /// ///Gets or sets the test context which provides ///information about and functionality for the current test run. /// public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } #region Additional test attributes // //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // // #endregion //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} /// ///A test for P4Server Constructor. Connect to a server check if it supports Unicode and disconnect /// [TestMethod()] public void P4ServerConstructorTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); Assert.IsTrue( target.RunCommand( "dirs", false, new String[] { "//depot/*" }, 1 ), "\"dirs\" command failed" ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } /// ///A test for RunCommand /// [TestMethod()] public void RunCommandTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for (int i = 0; i < 2; i++) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer(TestDir, unicode); try { using (P4Server target = new P4Server(server, user, pass, ws_client)) { if (unicode) Assert.IsTrue(target.UseUnicode, "Unicode server detected as not supporting Unicode"); else Assert.IsFalse(target.UseUnicode, "Non Unicode server detected as supporting Unicode"); Assert.IsTrue(target.RunCommand("dirs", false, new String[] { "//depot/*" }, 1), "\"dirs\" command failed"); } } finally { Utilities.RemoveTestServer(p4d, TestDir); } unicode = !unicode; } } #if DEBUG_TIMEOUT /// ///A test for RunCommand timeout /// [TestMethod()] public void RunCommandTimeOutTest() { string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; Process p4d = Utilities.DeployP4TestServer(TestDir, false); try { using (P4Server target = new P4Server(server, user, pass, ws_client)) { try { Assert.IsTrue(target.RunCommand("TimeOutTest", false, new String[] { "-1" }, 1)); } catch (P4CommandTimeOutException) { Assert.Fail("Should not have timed out"); } catch (Exception) { Assert.Fail("Wrong exception thrown for timeout"); } try { Assert.IsFalse(target.RunCommand("TimeOutTest", false, new String[] { "1" }, 1)); Assert.Fail("Didn't timeout"); } catch (P4CommandTimeOutException) { } catch (Exception) { Assert.Fail("Wrong exception thrown for timeout"); } } } finally { Utilities.RemoveTestServer(p4d, TestDir); } } /// ///A test for RunCommand timeout /// [TestMethod()] public void RunCommandLongTimeOutTest() { string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; Process p4d = Utilities.DeployP4TestServer(TestDir, false); try { using (P4Server target = new P4Server(server, user, pass, ws_client)) { try { Assert.IsTrue(target.RunCommand("LongTimeOutTest", false, new String[] { "10" }, 1)); } catch (P4CommandTimeOutException) { Assert.Fail("Should not have timed out"); } catch (Exception) { Assert.Fail("Wrong exception thrown for timeout"); } } } finally { Utilities.RemoveTestServer(p4d, TestDir); } } #endif /// ///A test for GetBinaryResults /// [TestMethod()] public void GetBinaryResultsTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String[] parms = new String[] { "//depot/MyCode/Silly.bmp" }; Assert.IsTrue( target.RunCommand( "print", false, parms, 1 ), "\"print\" command failed" ); byte[] results = target.GetBinaryResults(); Assert.IsNotNull( results, "GetBinaryResults returned null data" ); Assert.AreEqual( results.Length, 3126 ); Assert.AreEqual( results[ 0 ], 0x42 ); Assert.AreEqual( results[ 1 ], 0x4d ); Assert.AreEqual( results[ 2 ], 0x36 ); Assert.AreEqual( results[ 0x10 ], 0x00 ); Assert.AreEqual( results[ 0xA0 ], 0xC0 ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } /// ///A test for GetErrorResults /// [TestMethod()] public void GetErrorResultsTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String[] parms = new String[] { "//depot/MyCode/NoSuchFile.bmp" }; target.RunCommand("print", false, parms, 1); P4ClientErrorList results = target.GetErrorResults(); Assert.IsNotNull( results, "GetErrorResults returned null data" ); Assert.AreEqual( results.Count, 1 ); P4ClientError firstError = (P4ClientError)results[ 0 ]; Assert.AreEqual( firstError.ErrorMessage.TrimEnd(new char [] {'\r','\n'}), "//depot/MyCode/NoSuchFile.bmp - no such file(s)." ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } /// ///A test for GetInfoResults /// [TestMethod()] public void GetInfoResultsTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String[] parms = new String[] { "//depot/mycode/*" }; Assert.IsTrue( target.RunCommand( "files", false, parms, 1 ), "\"files\" command failed" ); String[] results = target.GetInfoResults(); Assert.IsNotNull( results, "GetInfoResults returned null data" ); if( unicode ) Assert.AreEqual( 3, results.Length ); else Assert.AreEqual( 3, results.Length ); String firstResult = results[ 0 ]; Assert.IsTrue( firstResult.StartsWith( "0://depot/MyCode" )); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } /// ///A test for GetTaggedOutput /// [TestMethod()] public void GetTaggedOutputTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String[] parms = new String[] { "//depot/mycode/*" }; Assert.IsTrue( target.RunCommand( "files", true, parms, 1 ), "\"files\" command failed" ); TaggedObjectList results = target.GetTaggedOutput(); Assert.IsNotNull( results, "GetTaggedOutput returned null data" ); if( unicode ) Assert.AreEqual( 3, results.Count ); else Assert.AreEqual( 3, results.Count ); //TaggedObject result = results[ 0 ]; //String depotFile = result[ "depotFile" ]; //Assert.AreEqual( depotFile, "//depot/MyCode/ReadMe.txt" ); //result = (TaggedObject)results[ 1 ]; //depotFile = result[ "depotFile" ]; //Assert.AreEqual( depotFile, "//depot/MyCode/Silly.bmp" ); //if( unicode ) //{ // result = (TaggedObject)results[ 2 ]; // depotFile = result[ "depotFile" ]; // Assert.AreEqual( depotFile, "//depot/MyCode/Пюп.txt" ); //} } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } /// ///A test for GetTextResults /// [TestMethod()] public void GetTextResultsTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String[] parms = new String[] { "//depot/MyCode/ReadMe.txt" }; Assert.IsTrue( target.RunCommand( "print", false, parms, 1 ), "\"print\" command failed" ); String results = target.GetTextResults(); Assert.IsNotNull( results, "GetErrorResults GetTextResults null data" ); if( unicode ) Assert.AreEqual( results.Length, 30 ); else Assert.AreEqual( results.Length, 30 ); Assert.IsTrue( results.StartsWith("Don't Read This!") ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } private byte[] BinaryCallbackResults; private void BinaryResultsCallback( byte[] data ) { if ((data != null) && (data.Length > 0)) BinaryCallbackResults = data; } /// ///A test for SetBinaryResultsCallback /// [TestMethod()] public void SetBinaryResultsCallbackTest() { P4Server.BinaryResultsDelegate cb = new P4Server.BinaryResultsDelegate( BinaryResultsCallback ); bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); target.BinaryResultsReceived += cb; String[] parms = new String[] { "//depot/MyCode/Silly.bmp" }; Assert.IsTrue( target.RunCommand( "print", false, parms, 1 ), "\"print\" command failed" ); byte[] results = target.GetBinaryResults(); Assert.IsNotNull( BinaryCallbackResults, "BinaryCallbackResults is null" ); Assert.IsNotNull( results, "InfoCallbackResults is null" ); Assert.AreEqual( results.Length, BinaryCallbackResults.Length ); Assert.AreEqual( BinaryCallbackResults.Length, 3126 ); for( int idx = 0; idx < BinaryCallbackResults.Length; idx++ ) { Assert.AreEqual( results[ idx ], BinaryCallbackResults[ idx ] ); } } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } private string ErrorCallbackResultsMessage; private int ErrorCallbackResultsSeverity; private void ErrorResultsCallback( int severity, String message ) { ErrorCallbackResultsMessage = message; ErrorCallbackResultsSeverity = severity; } /// ///A test for SetErrorCallback /// [TestMethod()] public void SetErrorCallbackTest() { P4Server.ErrorDelegate cb = new P4Server.ErrorDelegate( ErrorResultsCallback ); bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); target.ErrorReceived += cb; String[] parms = new String[] { "//depot/MyCode/NoSuchFile.bmp" }; Assert.IsTrue( target.RunCommand( "fstat", false, parms, 1 ), "\"fstat\" command failed" ); P4ClientErrorList results = target.GetErrorResults(); Assert.IsFalse( String.IsNullOrEmpty( ErrorCallbackResultsMessage ), "ErrorCallbackResultsMessage is null or empty" ); Assert.IsNotNull( results, "GetErrorResults returned null" ); Assert.AreEqual( results.Count, 1 ); P4ClientError firstError = (P4ClientError)results[ 0 ]; Assert.AreEqual( firstError.ErrorMessage.TrimEnd( new char[] { '\r', '\n' } ), ErrorCallbackResultsMessage.TrimEnd( new char[] { '\r', '\n' } ) ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } private class InfoCbData { public InfoCbData(String m, int l) { Message = m; Level = l; } public String Message; public int Level; public override string ToString() { return String.Format("{0}:{1}", Level, Message); } } private ArrayList InfoCallbackResults; private void InfoResultsCallback( int level, String message ) { InfoCallbackResults.Add( new InfoCbData( message, level ) ); } private void BadInfoResultsCallback( int level, String message ) { throw new Exception("I'm a bad delegate"); } /// ///A test for SetInfoResultsCallback /// [TestMethod()] public void SetInfoResultsCallbackTest() { P4Server.InfoResultsDelegate cb = new P4Server.InfoResultsDelegate( InfoResultsCallback ); P4Server.InfoResultsDelegate bcb = new P4Server.InfoResultsDelegate( BadInfoResultsCallback ); bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); target.InfoResultsReceived += cb; // add in the bad handler that throws an exception, to // make sure the event broadcaster can handle it. target.InfoResultsReceived += bcb; String[] parms = new String[] { "//depot/mycode/*" }; InfoCallbackResults = new ArrayList(); Assert.IsTrue( target.RunCommand( "files", false, parms, 1 ), "\"files\" command failed" ); String[] results = target.GetInfoResults(); Assert.IsNotNull( InfoCallbackResults, "InfoCallbackResults is null" ); Assert.IsNotNull( results, "GetInfoResults returned null" ); Assert.AreEqual( results.Length, InfoCallbackResults.Count ); for( int idx = 0; idx < InfoCallbackResults.Count; idx++ ) { Assert.AreEqual( results[idx], InfoCallbackResults[idx].ToString() ); } } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } private TaggedObjectList TaggedCallbackResults; private void TaggedOutputCallback( int objId, TaggedObject obj ) { TaggedCallbackResults.Add( obj ); } /// ///A test for SetTaggedOutputCallback /// [TestMethod()] public void SetTaggedOutputCallbackTest() { P4Server.TaggedOutputDelegate cb = new P4Server.TaggedOutputDelegate( TaggedOutputCallback ); bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); target.TaggedOutputReceived += cb; StringList parms = new String[] { "//depot/mycode/*" }; TaggedCallbackResults = new TaggedObjectList(); Assert.IsTrue( target.RunCommand( "fstat", true, parms, 1 ), "\"fstat\" command failed" ); TaggedObjectList results = target.GetTaggedOutput(); Assert.IsNotNull( TaggedCallbackResults, "TaggedCallbackResults is null" ); Assert.IsNotNull( results, "GetTaggedOutput returned null" ); Assert.AreEqual( results.Count, TaggedCallbackResults.Count ); for( int idx = 0; idx < TaggedCallbackResults.Count; idx++ ) { Assert.AreEqual( ( results[ idx ] )[ "depotFile" ], ( TaggedCallbackResults[ idx ] )[ "depotFile" ] ); } } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } private String TextCallbackResults; private void TextResultsCallback( String info ) { TextCallbackResults += info; } /// ///A test for SetTextResultsCallback /// [TestMethod()] public void SetTextResultsCallbackTest() { P4Server.TextResultsDelegate cb = new P4Server.TextResultsDelegate( TextResultsCallback ); bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); target.TextResultsReceived += cb; String[] parms = new String[] { "//depot/mycode/ReadMe.txt" }; TextCallbackResults = String.Empty; Assert.IsTrue( target.RunCommand( "print", true, parms, 1 ), "\"print\" command failed" ); String results = target.GetTextResults(); Assert.IsFalse( String.IsNullOrEmpty( TextCallbackResults ), "TextCallbackResults is null" ); Assert.IsFalse( String.IsNullOrEmpty( results ), "GetTextResults is null" ); Assert.AreEqual( results.Length, TextCallbackResults.Length ); Assert.AreEqual( TextCallbackResults, results ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } /// ///A test for Client /// [TestMethod()] public void ClientTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; // turn off exceptions for this test ErrorSeverity oldExceptionLevel = P4Exception.MinThrowLevel; P4Exception.MinThrowLevel = ErrorSeverity.E_NOEXC; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); Assert.IsTrue( target.RunCommand( "dirs", false, new String[] { "//depot/*" }, 1 ), "\"dirs\" command failed" ); String actual = target.Client; Assert.AreEqual( actual, "admin_space" ); target.Client = "admin_space2"; // run a command to trigure a reconnect Assert.IsTrue( target.RunCommand( "dirs", false, new String[] { "//admin_space2/*" }, 1 ), "\"dirs\" command failed" ); /// try a bad value target.Client = "admin_space3"; Assert.IsTrue( target.RunCommand( "dirs", false, new String[] { "//admin_space3/*" }, 1 ), "\"dirs\" command failed" ); Assert.IsNotNull(target.ErrorList); Assert.AreEqual(ErrorSeverity.E_WARN, target.ErrorList[0].SeverityLevel); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } // reset the exception level P4Exception.MinThrowLevel = oldExceptionLevel; } /// ///A test for DataSet /// [TestMethod()] public void DataSetTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String expected = "The quick brown fox jumped over the tall white fence."; target.DataSet = expected; String actual = target.DataSet; Assert.AreEqual( actual, expected ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } } /// ///A test for Password /// [TestMethod()] public void PasswordTest() { bool unicode = false; string server = "localhost:6666"; string user = "Alex"; string pass = "pass"; string ws_client = "admin_space"; // turn off exceptions for this test ErrorSeverity oldExceptionLevel = P4Exception.MinThrowLevel; P4Exception.MinThrowLevel = ErrorSeverity.E_NOEXC; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { if( unicode ) user = "Алексей"; Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String actual = target.Password; Assert.IsNotNull( actual ); /// try a bad value target.Password = "ssap"; // command triggers a reconnect Assert.IsFalse( target.RunCommand( "dirs", false, new String[] { "//depot/*" }, 1 ), "\"dirs\" command failed" ); // try a user with no password target.User = "admin"; target.Password = String.Empty; // command triggers a reconnect Assert.IsTrue( target.RunCommand( "dirs", false, new String[] { "//depot/*" }, 1 ), "\"dirs\" command failed" ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } // reset the exception level P4Exception.MinThrowLevel = oldExceptionLevel; } /// ///A test for Port /// [TestMethod()] public void PortTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; // turn off exceptions for this test ErrorSeverity oldExceptionLevel = P4Exception.MinThrowLevel; P4Exception.MinThrowLevel = ErrorSeverity.E_NOEXC; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String expected = "localhost:6666"; target.Port = expected; String actual = target.Port; Assert.AreEqual( actual, expected ); // try a bad value target.Port = "null:0"; // command triggers a reconnect bool reconectSucceeded = true; try { reconectSucceeded = target.RunCommand("dirs", false, new String[] { "//depot/*" }, 1); } catch { reconectSucceeded = false; } Assert.IsFalse(reconectSucceeded, "Reconnect to \"null:0\" did not fail"); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } // reset the exception level P4Exception.MinThrowLevel = oldExceptionLevel; } /// ///A test for User /// [TestMethod()] public void UserTest() { bool unicode = false; string server = "localhost:6666"; string user = "Alex"; string pass = "pass"; string ws_client = "admin_space"; // turn off exceptions for this test ErrorSeverity oldExceptionLevel = P4Exception.MinThrowLevel; P4Exception.MinThrowLevel = ErrorSeverity.E_NOEXC; for( int i = 0; i < 2; i++ ) // run once for ascii, once for unicode { if( unicode ) user = "Алексей"; Process p4d = Utilities.DeployP4TestServer( TestDir, unicode ); try { using( P4Server target = new P4Server( server, user, pass, ws_client ) ) { if( unicode ) Assert.IsTrue( target.UseUnicode, "Unicode server detected as not supporting Unicode" ); else Assert.IsFalse( target.UseUnicode, "Non Unicode server detected as supporting Unicode" ); String actual = target.User; Assert.AreEqual( actual, user ); // try a bad value target.User = "John"; // command triggers a reconnect Assert.IsTrue( target.RunCommand( "dirs", false, new String[] { "//depot/*" }, 1 ), "\"dirs\" command failed" ); // try a user with no password target.User = "admin"; target.Password = String.Empty; // command triggers a reconnect Assert.IsTrue( target.RunCommand( "dirs", false, new String[] { "//depot/*" }, 1 ), "\"dirs\" command failed" ); } } finally { Utilities.RemoveTestServer( p4d, TestDir ); } unicode = !unicode; } // reset the exception level P4Exception.MinThrowLevel = oldExceptionLevel; } /// ///A test for ErrorList /// [TestMethod()] public void ErrorListTest() { bool unicode = false; string server = "localhost:6666"; string user = "Alex"; string pass = "pass"; string ws_client = "admin_space"; // turn off exceptions for this test ErrorSeverity oldExceptionLevel = P4Exception.MinThrowLevel; P4Exception.MinThrowLevel = ErrorSeverity.E_NOEXC; for (int i = 0; i < 3; i++) // run once for ascii, once for unicode, once for the security level 3 server { String zippedFile = "a.exe"; if (i == 1) { zippedFile = "u.exe"; user = "Алексей"; pass = "pass"; } if (i == 2) { zippedFile = "s3.exe"; user = "alex"; pass = "Password"; } Process p4d = Utilities.DeployP4TestServer(TestDir, 10, zippedFile); try { using (P4Server target = new P4Server(server, user, pass, ws_client)) { if (unicode) Assert.IsTrue(target.UseUnicode, "Unicode server detected as not supporting Unicode"); else Assert.IsFalse(target.UseUnicode, "Non Unicode server detected as supporting Unicode"); P4ClientErrorList errors = null; // a bad user will not fail on the servers fro a.exe and u.exe, // so only test on s3 if (i == 2) { // setting the user name will trigger a disconnect target.User = "badboy"; // nonexistent user // command triggers an attempt to reconnect Assert.IsFalse(target.RunCommand("dirs", false, new String[] { "//depot/*" }, 1)); errors = target.ErrorList; Assert.IsNotNull(errors); Assert.IsTrue(errors[0].ErrorMessage.Contains("Password must be set before access can be granted")); target.User = user; // back to the good user } target.Password = "NoWayThisWillWork"; // bad password if (i == 2) { Assert.IsFalse(target.Login("NoWayThisWillWork", null)); errors = target.ErrorList; Assert.IsNotNull(errors); Assert.IsTrue(errors[0].ErrorMessage.Contains("Password invalid")); } // command triggers an attempt to reconnect Assert.IsFalse(target.RunCommand("dirs", false, new String[] { "//depot/*" }, 1)); errors = target.ErrorList; Assert.IsNotNull(errors); if (i == 2) { Assert.IsTrue(errors[0].ErrorMessage.Contains("Password invalid")); } else { Assert.IsTrue(errors[0].ErrorMessage.Contains("Perforce password (P4PASSWD) invalid or unset")); } target.Password = pass; // back to the good password if (i == 2) { Assert.IsTrue(target.Login(pass, null)); } target.Client = "AintNoClientNamedLikeThis"; // command triggers an attempt to reconnect (have requires a client) Assert.IsFalse(target.RunCommand("have", false, null, 0)); errors = target.ErrorList; Assert.IsNotNull(errors); Assert.IsTrue(errors[0].ErrorMessage.Contains("unknown - use 'client' command to create it")); target.Client = ws_client; // back to the good client name target.Port = "NoServerAtThisAddress:666"; // command triggers an attempt to reconnect Assert.IsFalse(target.RunCommand("dirs", false, new String[] { "//depot/*" }, 1)); errors = target.ErrorList; Assert.IsNotNull(errors); Assert.IsTrue(errors[0].ErrorMessage.Contains("Connect to server failed")); target.Port = server;// back to the good port name // command triggers an attempt to reconnect // run a good command to make sure the connection is solid bool success = target.RunCommand("dirs", false, new String[] { "//depot/*" }, 1); errors = target.ErrorList; Assert.IsTrue(success); Assert.IsNull(errors); // try a bad command name Assert.IsFalse(target.RunCommand("dirrrrrs", false, new String[] { "//depot/*" }, 1)); errors = target.ErrorList; Assert.IsNotNull(errors); Assert.IsTrue(errors[0].ErrorMessage.Contains("Unknown command. Try 'p4 help' for info")); target.Port = server;// back to the good port name // try a bad command parameter Assert.IsFalse(target.RunCommand("dirs", false, new String[] { "//freebird/*" }, 1)); errors = target.ErrorList; Assert.IsNotNull(errors); Assert.IsTrue(errors[0].ErrorMessage.Contains("must refer to client")); // try a bad command flax Assert.IsFalse(target.RunCommand("dirs", false, new String[] { "-UX", "//depot/*" }, 1)); errors = target.ErrorList; Assert.IsNotNull(errors); Assert.IsTrue(errors[0].ErrorMessage.Contains("Invalid option: -UX")); } } finally { Utilities.RemoveTestServer(p4d, TestDir); } unicode = !unicode; } // reset the exception level P4Exception.MinThrowLevel = oldExceptionLevel; } /// ///A test for ApiLevel /// [TestMethod()] public void ApiLevelTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for (int i = 0; i < 2; i++) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer(TestDir, unicode); try { using (P4Server target = new P4Server(server, user, pass, ws_client)) { if (unicode) Assert.IsTrue(target.UseUnicode, "Unicode server detected as not supporting Unicode"); else Assert.IsFalse(target.UseUnicode, "Non Unicode server detected as supporting Unicode"); Assert.IsTrue(target.ApiLevel > 0); } } finally { Utilities.RemoveTestServer(p4d, TestDir); } unicode = !unicode; } } /// ///A test for GetTaggedOutput /// [TestMethod()] public void TestTaggedUntaggedOutputTest() { bool unicode = false; string server = "localhost:6666"; string user = "admin"; string pass = string.Empty; string ws_client = "admin_space"; for (int i = 0; i < 2; i++) // run once for ascii, once for unicode { Process p4d = Utilities.DeployP4TestServer(TestDir, unicode); try { using (P4Server target = new P4Server(server, user, pass, ws_client)) { if (unicode) Assert.IsTrue(target.UseUnicode, "Unicode server detected as not supporting Unicode"); else Assert.IsFalse(target.UseUnicode, "Non Unicode server detected as supporting Unicode"); String[] parms = new String[] { "//depot/mycode/*" }; Assert.IsTrue(target.RunCommand("files", false, parms, 1), "\"files\" command failed"); TaggedObjectList results = target.GetTaggedOutput(); Assert.IsNull(results, "GetTaggedOutput did not return null data"); Assert.IsTrue(target.RunCommand("files", true, parms, 1), "\"files\" command failed"); results = target.GetTaggedOutput(); Assert.IsNotNull(results, "GetTaggedOutput returned null data"); if (unicode) Assert.AreEqual(3, results.Count); else Assert.AreEqual(3, results.Count); Assert.IsTrue(target.RunCommand("files", false, parms, 1), "\"files\" command failed"); results = target.GetTaggedOutput(); Assert.IsNull(results, "GetTaggedOutput did not return null data"); //TaggedObject result = results[ 0 ]; //String depotFile = result[ "depotFile" ]; //Assert.AreEqual( depotFile, "//depot/MyCode/ReadMe.txt" ); //result = (TaggedObject)results[ 1 ]; //depotFile = result[ "depotFile" ]; //Assert.AreEqual( depotFile, "//depot/MyCode/Silly.bmp" ); //if( unicode ) //{ // result = (TaggedObject)results[ 2 ]; // depotFile = result[ "depotFile" ]; // Assert.AreEqual( depotFile, "//depot/MyCode/Пюп.txt" ); //} } } finally { Utilities.RemoveTestServer(p4d, TestDir); } unicode = !unicode; } } } }