From fa8c0d7683d567743f5138813f08a502aff8e3d0 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Wed, 15 Oct 2008 18:15:43 +0000 Subject: - Makes MySQL reject inserting UUID 0 - Makes SQLite mimick MySQL default behavior on first and last name already on db: it does not insert the new record. - Added tests for UUID 0 and for new UUID with existing first and last name. --- OpenSim/Data/MySQL/MySQLUserData.cs | 5 +++++ OpenSim/Data/SQLite/SQLiteUserData.cs | 35 +++++++++++++++++++++++++++++++++++ OpenSim/Data/Tests/BasicUserTest.cs | 24 +++++++++++++++++++----- 3 files changed, 59 insertions(+), 5 deletions(-) (limited to 'OpenSim/Data') diff --git a/OpenSim/Data/MySQL/MySQLUserData.cs b/OpenSim/Data/MySQL/MySQLUserData.cs index c668d56..3a088cf 100644 --- a/OpenSim/Data/MySQL/MySQLUserData.cs +++ b/OpenSim/Data/MySQL/MySQLUserData.cs @@ -582,6 +582,11 @@ namespace OpenSim.Data.MySQL /// The user profile to create public override void AddNewUserProfile(UserProfileData user) { + UUID zero = UUID.Zero; + if (user.ID == zero) + { + return; + } MySQLSuperManager dbm = GetLockedConnection(); try diff --git a/OpenSim/Data/SQLite/SQLiteUserData.cs b/OpenSim/Data/SQLite/SQLiteUserData.cs index 420b6e5..448ef85 100644 --- a/OpenSim/Data/SQLite/SQLiteUserData.cs +++ b/OpenSim/Data/SQLite/SQLiteUserData.cs @@ -481,6 +481,37 @@ namespace OpenSim.Data.SQLite } + private bool ExistsFirstLastName(String fname, String lname) + { + string FindUser = "select * from users where (username=:username and surname=:surname)"; + using (SqliteCommand cmd = new SqliteCommand(FindUser, g_conn)) + { + cmd.Parameters.Add(new SqliteParameter(":username", fname)); + cmd.Parameters.Add(new SqliteParameter(":surname", lname)); + try + { + using (IDataReader reader = cmd.ExecuteReader()) + { + if(reader.Read()) + { + reader.Close(); + return true; + } + else + { + reader.Close(); + return false; + } + } + } + catch (Exception ex) + { + m_log.Error("[USER DB]: Exception searching for user's first and last name: " + ex.ToString()); + return false; + } + } + } + /// /// Creates a new user profile /// @@ -488,6 +519,10 @@ namespace OpenSim.Data.SQLite override public void AddNewUserProfile(UserProfileData user) { DataTable users = ds.Tables["users"]; + UUID zero = UUID.Zero; + if (ExistsFirstLastName(user.FirstName, user.SurName) || user.ID == zero) + return; + lock (ds) { DataRow row = users.Rows.Find(Util.ToRawUuidString(user.ID)); diff --git a/OpenSim/Data/Tests/BasicUserTest.cs b/OpenSim/Data/Tests/BasicUserTest.cs index 24cb215..dc8ccc3 100644 --- a/OpenSim/Data/Tests/BasicUserTest.cs +++ b/OpenSim/Data/Tests/BasicUserTest.cs @@ -45,6 +45,7 @@ namespace OpenSim.Data.Tests public UUID user1; public UUID user2; public UUID user3; + public UUID user4; public UUID webkey; public UUID zero = UUID.Zero; public Random random; @@ -55,6 +56,8 @@ namespace OpenSim.Data.Tests public UUID region1; + public string fname0; + public string lname0; public string fname1; public string lname1; public string fname2; @@ -76,11 +79,14 @@ namespace OpenSim.Data.Tests user1 = UUID.Random(); user2 = UUID.Random(); user3 = UUID.Random(); + user4 = UUID.Random(); agent1 = UUID.Random(); agent2 = UUID.Random(); agent3 = UUID.Random(); webkey = UUID.Random(); region1 = UUID.Random(); + fname0 = RandomName(random); + lname0 = RandomName(random); fname1 = RandomName(random); lname1 = RandomName(random); fname2 = RandomName(random); @@ -115,19 +121,16 @@ namespace OpenSim.Data.Tests [Test] public void T010_CreateUser() { - UserProfileData u1 = NewUser(user1,fname1,lname1); // Must first and last names be unique? - //UserProfileData u0 = NewUser(zero,fname1,lname1); // Zero causes some weird behaviors + UserProfileData u1 = NewUser(user1,fname1,lname1); UserProfileData u2 = NewUser(user2,fname2,lname2); UserProfileData u3 = NewUser(user3,fname3,lname3); - //db.AddNewUserProfile(u0); + Console.WriteLine("Users are {0} {1} {2}",user1,user2,user3); db.AddNewUserProfile(u1); db.AddNewUserProfile(u2); db.AddNewUserProfile(u3); - //UserProfileData u1z = db.GetUserByUUID(zero); UserProfileData u1a = db.GetUserByUUID(user1); UserProfileData u2a = db.GetUserByUUID(user2); UserProfileData u3a = db.GetUserByUUID(user3); - //Assert.That(zero,Is.EqualTo(u1z.ID)); Assert.That(user1,Is.EqualTo(u1a.ID)); Assert.That(user2,Is.EqualTo(u2a.ID)); Assert.That(user3,Is.EqualTo(u3a.ID)); @@ -164,6 +167,17 @@ namespace OpenSim.Data.Tests u1 = db.GetUserByUUID(user1); Assert.That(u1.WebLoginKey,Is.EqualTo(webkey)); } + + [Test] + public void T014_ExpectedNullReferenceReturns() + { + UserProfileData u0 = NewUser(zero,fname0,lname0); + UserProfileData u4 = NewUser(user4,fname2,lname2); + db.AddNewUserProfile(u0); + db.AddNewUserProfile(u4); + Assert.That(db.GetUserByUUID(zero),Is.Null); + Assert.That(db.GetUserByUUID(user4),Is.Null); + } [Test] public void T020_CreateAgent() -- cgit v1.1