/* * P4.Net * Copyright (c) 2006 Shawn Hladky Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using NUnit.Framework; using P4API; using System.IO; using System.Threading; namespace P4.Net.TestDriver { /// Some simple Tests. /// [TestFixture] public class StandardTest { private P4Connection p4 = null; private string p4ClientRootDirectory = null; private const string _port = "5791"; public static ManualResetEvent mre = new ManualResetEvent(false); public static ManualResetEvent mre2 = new ManualResetEvent(false); [Test] public void Test001_SetupConnection() { p4 = new P4Connection(); p4.Port = "5791"; p4.CallingProgram = "P4.Net Test Harness"; p4.CallingVersion = "1.0"; p4.Connect(); p4.Client = "TestClient1"; p4.User = "TestUser1"; P4Record s = p4.Run("info")[0]; Assert.AreEqual(s["userName"], "*unknown*", "User name should be *unknown*. This test requires an empty repository"); Assert.AreEqual(s["clientName"], "*unknown*", "Client spec should be *unknown*. This test requires an empty repository"); } [Test] public void Test002_CreateClient() { p4ClientRootDirectory = Path.Combine(Path.GetTempPath(), "p4temproot"); if (Directory.Exists(p4ClientRootDirectory)) { // what to do??? } else { Directory.CreateDirectory(p4ClientRootDirectory); } P4Form client = p4.Fetch_Form("client"); //test the indexer client["Root"] = p4ClientRootDirectory; //test the Fields collection client.Fields["Description"] = "Created from unit test"; //Test setting an array string[] view = { @"//depot/... //TestClient1/..." }; client.ArrayFields["View"] = view; P4UnParsedRecordSet result = p4.Save_Form(client); Assert.AreEqual(result.Messages.Length, 1, "While saving client spec, unexpected number of messages"); Assert.AreEqual(result[0], "Client TestClient1 saved.", "Unable to save client spec"); //now lets pull the form again and verify values client = p4.Fetch_Form("client"); Assert.AreEqual(client["Root"], p4ClientRootDirectory, "Unexpected client root."); Assert.AreEqual(client.Fields["Description"], "Created from unit test\n", "Unexpected client description."); Assert.AreEqual(client.ArrayFields["View"].Length, 1, "Unexpected client view."); // lets save this with a new name client["Client"] = "TestClient2"; // now, lets test the view as a multi-valued array // yes, I know this is a dumb view string[] view2 = { @"//depot/shawn/... //TestClient2/shawn2/...", @"//depot/shawn/a/... //TestClient2/shawn/a/...", @"//depot/shawn/b/... //TestClient2/shawn/b/...", @"//depot/shawn/c/... //TestClient2/shawn/c/...", @"//depot/shawn/d/... //TestClient2/shawn/d/...", @"//depot/shawn/e/... //TestClient2/shawn/e/...", @"//depot/shawn/f/... //TestClient2/shawn/f/...", @"//depot/shawn/g/... //TestClient2/shawn/g/...", @"//depot/shawn/h/... //TestClient2/shawn/h/...", @"//depot/shawn/i/... //TestClient2/shawn/i/...", @"//depot/shawn/j/... //TestClient2/shawn/j/...", @"//depot/shawn/k/... //TestClient2/shawn/k/...", @"//depot/shawn/l/... //TestClient2/shawn/l/...", @"//depot/shawn/m/... //TestClient2/shawn/m/..."}; client.ArrayFields["View"] = view2; result = p4.Save_Form(client); Assert.AreEqual(result.Messages.Length, 1, "While saving client spec, unexpected number of messages"); Assert.AreEqual(result[0], "Client TestClient2 saved.", "Unable to save client spec"); //now lets pull the form again and verify values client = p4.Fetch_Form("client", "TestClient2"); Assert.AreEqual(client.ArrayFields["View"].Length, 14, "Unexpected client view."); } [Test] public void Test003_AddFiles() { string testPath = Path.Combine(p4ClientRootDirectory, "test003"); //Create some test files Directory.CreateDirectory(testPath); P4PendingChangelist cl = p4.CreatePendingChangelist("Test003"); string[] args = new string[12]; args[0] = "-c"; args[1] = cl.Number.ToString(); for (int i = 0; i < 10; i++) { string fn = string.Format("File{0}", i); StreamWriter sw = File.CreateText(Path.Combine(testPath, fn)); sw.WriteLine("this is file: {0}", fn); sw.Close(); args[i + 2] = Path.Combine(testPath, fn); } P4UnParsedRecordSet r = p4.RunUnParsed("add", args); Assert.AreEqual(r.Messages.Length, 10, "Invalid response from p4 add"); for (int i = 0; i < 10; i++) { Assert.AreEqual(string.Format("//depot/test003/File{0}#1 - opened for add", i), r.Messages[i], "Invalid response from p4 add"); } r = cl.Submit(); Assert.AreEqual("Submitting change 1.", r.Messages[0], "Unexpected output from p4 submit"); } [Test] public void Test004_IntegFiles() { P4PendingChangelist cl = p4.CreatePendingChangelist("Test004"); P4UnParsedRecordSet r = p4.RunUnParsed("integ", "-c", cl.Number.ToString(), "//depot/test003/...", "//depot/test004/..."); Assert.AreEqual(r.Messages.Length, 10, "Invalid response from p4 add"); for (int i = 0; i < 10; i++) { Assert.AreEqual(string.Format("//depot/test004/File{0}#1 - branch/sync from //depot/test003/File{0}#1", i), r.Messages[i], "Invalid response from p4 add"); } r = cl.Submit(); Assert.AreEqual("Submitting change 2.", r.Messages[0], "Unexpected output from p4 submit"); } [Test] public void Test005_EditFiles() { string testPath = Path.Combine(p4ClientRootDirectory, "test003"); P4PendingChangelist cl = p4.CreatePendingChangelist("Test005"); // Add lines to a file P4UnParsedRecordSet r = p4.RunUnParsed("edit", "-c", cl.Number.ToString(), "//depot/test003/File0"); string fn = Path.Combine(testPath, "File0"); StreamWriter sw = File.AppendText(fn); sw.WriteLine("Line 2", fn); sw.WriteLine("Line 3", fn); sw.WriteLine("Line 4", fn); sw.Close(); //Modify a line r = p4.RunUnParsed("edit", "-c", cl.Number.ToString(), "//depot/test003/File1"); fn = Path.Combine(testPath, "File1"); sw = File.CreateText(fn); sw.WriteLine("Modified File1", fn); sw.Close(); //Delete a file r = p4.RunUnParsed("delete", "-c", cl.Number.ToString(), "//depot/test003/File2"); r = cl.Submit(); Assert.AreEqual("Submitting change 3.", r.Messages[0], "Unexpected output from p4 submit"); } [Test] public void Test006_Password() { string testPath = Path.Combine(p4ClientRootDirectory, "test003"); p4.OnPrompt += new OnPromptEventHandler(OnPrompt); try { //change the password P4UnParsedRecordSet r = p4.RunUnParsed("passwd"); //now login p4.Login("password"); } catch (Exception e) { Assert.Fail(e.Message); } } [Test] [ExpectedException(typeof(P4API.Exceptions.RunException))] public void Test007_WarningLevels() { p4.ExceptionLevel = P4ExceptionLevels.ExceptionOnBothErrorsAndWarnings; p4.Run("sync"); } [Test] public void Test008_WarningLevels() { p4.ExceptionLevel = P4ExceptionLevels.NoExceptionOnWarnings; P4RecordSet r = p4.Run("sync"); Assert.AreEqual(r.Warnings[0], "File(s) up-to-date."); P4UnParsedRecordSet r2 = p4.RunUnParsed("sync"); Assert.AreEqual(r2.Warnings[0], "File(s) up-to-date."); } [Test] [ExpectedException(typeof(P4API.Exceptions.RunException))] public void Test009_WarningLevels() { p4.ExceptionLevel = P4ExceptionLevels.NoExceptionOnWarnings; P4RecordSet r = p4.Run("bogus"); } [Test] public void Test010_WarningLevels() { p4.ExceptionLevel = P4ExceptionLevels.NoExceptionOnErrors; P4UnParsedRecordSet r = p4.RunUnParsed("bogus"); Assert.AreEqual(r.Errors[0], "Unknown command. Try 'p4 help' for info."); P4RecordSet r2 = p4.Run("bogus"); Assert.AreEqual(r2.Errors[0], "Unknown command. Try 'p4 help' for info."); } [Test] public void Test011_DateConversions() { DateTime res = p4.ConvertDate("1159361459"); DateTime ans = new DateTime(2006, 9, 27, 5, 50, 59); Assert.AreEqual(res, ans); int tics = p4.ConvertDate(ans); Assert.AreEqual(tics, 1159361459); } //[Test] public void Test012_DroppedConnection() { System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(RunOnNewThread)); t.Start(); mre.WaitOne(); Thread.Sleep(100); Console.WriteLine("ready for some football"); //find the PID and kill the new connection P4RecordSet monitor = p4.Run("monitor", "show", "-ale"); foreach (P4Record r in monitor) { if (r["command"] == "login") { P4UnParsedRecordSet s = p4.RunUnParsed("monitor", "terminate", r["id"]); Console.WriteLine(s.Messages[0]); } } mre2.WaitOne(); } [Test] public void Test013_RecordsetReporting() { string s = ""; P4RecordSet fstat = p4.Run("filelog", "//..."); foreach (P4Record r in fstat) { foreach (string k in r.Fields.Keys) { s = string.Format("{0} : {1}", k, r[k]); //Console.WriteLine(s); } foreach (string k in r.ArrayFields.Keys) { for (int i = 0; i < r.ArrayFields[k].Length; i++) { s = string.Format("{0}{1} : {2}", k, i, r.ArrayFields[k][i]); //Console.WriteLine(s); } } } } [Test] public void Test100_ConnectionProperties() { p4.Host = "host"; //p4.CallingProgram = "P4.NetTestHarness"; Assert.AreEqual(p4.IsValidConnection(true, true), true); p4.Client = "client"; string dir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase); p4.CWD = dir; Assert.AreEqual(p4.Host, "host"); Assert.AreEqual(p4.Client, "client"); Assert.AreEqual(p4.CWD, dir); Assert.AreEqual(p4.Port, "5791"); Assert.AreEqual(p4.CallingProgram ,"P4.Net Test Harness"); Assert.AreEqual(p4.CallingVersion , "1.0"); Assert.AreEqual(p4.IsValidConnection(true, true), false); } [Test] [ExpectedException(typeof(P4API.Exceptions.PerforceInitializationError))] public void Test101_BogusConnection() { P4Connection bogus = new P4Connection(); bogus.Port = "1234"; bogus.Connect(); } [Test] public void Test888_CleanUpFileSystem() { // Remove all Read-Only attitude RemoveReadOnly(p4ClientRootDirectory); Directory.Delete(p4ClientRootDirectory, true); } [Test] public void Test999_Disconnect() { //duh p4.Disconnect(); } private void OnPrompt(object sender, P4PromptEventArgs e) { switch (e.Message) { case ("Enter new password: "): e.Response = "password"; break; case ("Re-enter new password: "): e.Response = "password"; break; } } private static void OnPromptPause(object sender, P4PromptEventArgs e) { Console.WriteLine("On Prompt..."); System.Threading.Thread.Sleep(11000); mre.Set(); System.Threading.Thread.Sleep(500); e.Response = "password"; Console.WriteLine("Exiting On Prompt..."); } private static void RunOnNewThread() { P4Connection p4 = new P4Connection(); p4.Port = _port; p4.User = "TestUser1"; p4.Connect(); p4.OnPrompt += new OnPromptEventHandler(OnPromptPause); try { Console.WriteLine("Running login"); p4.RunUnParsed("login"); Console.WriteLine("after login"); // should have been killed in the other connection Console.WriteLine("testing connection"); P4UnParsedRecordSet r = p4.RunUnParsed("info"); Console.WriteLine(r.Messages[0]); /*if (!p4.IsValidConnection(true, false)) { Console.WriteLine("connection failed"); Assert.Fail("Did not re-establish dropped connection."); } */ } catch (Exception e) { // not to worry here... we didn't supply a valid password //Console.WriteLine(e.Result.ErrorMessage); Console.WriteLine(e.StackTrace); } finally { Console.WriteLine("dude"); p4.Disconnect(); } mre2.Set(); } private void RemoveReadOnly(string path) { try { DirectoryInfo current = new DirectoryInfo(path); current.Attributes = FileAttributes.Normal; foreach (FileSystemInfo file in current.GetFileSystemInfos()) file.Attributes = FileAttributes.Normal; foreach (DirectoryInfo folder in current.GetDirectories()) RemoveReadOnly(folder.FullName); } catch (System.Exception excpt) { Console.WriteLine(excpt.Message); } } } class PasswordSetter { private P4Connection _p4 = null; private string _oldPassword = ""; private string _newPassword = ""; PasswordSetter(P4Connection p4) { _p4 = p4; } public void SetPassword(string OldPassword, string NewPassword) { OnPromptEventHandler eh = new OnPromptEventHandler(OnPrompt); _p4.OnPrompt += eh; _oldPassword = OldPassword; _newPassword = NewPassword; //run passwd P4UnParsedRecordSet r = _p4.RunUnParsed("passwd"); //Clear the event just in case _p4.OnPrompt -= eh; //Clear the passwords from memory _newPassword = ""; _oldPassword = ""; } private void OnPrompt(object sender, P4PromptEventArgs e) { switch (e.Message) { case ("Enter old password: "): e.Response = _oldPassword; break; case ("Enter new password: "): e.Response = _newPassword; break; case ("Re-enter new password: "): e.Response = _newPassword; break; } } } }