From 59157d9d63c0e038ca0a619bfae1be3ed6f77677 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Fri, 30 Mar 2012 00:40:19 +0100 Subject: Add simple login test with online friends. Add IFriendsModule.GrantRights() for granting rights via a module call. Rename IFriendsModule.GetFriendPerms() -> GetRightsGrantedByFriend() to be more self-documenting and consistent with friends module terminology. Add some method doc. --- .../CoreModules/Avatar/Friends/FriendsModule.cs | 50 ++++++++------- .../Avatar/Friends/Tests/FriendModuleTests.cs | 73 ++++++++++++++-------- 2 files changed, 73 insertions(+), 50 deletions(-) (limited to 'OpenSim/Region/CoreModules/Avatar') diff --git a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs index 86e04b9..9d7012e 100644 --- a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs +++ b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs @@ -51,6 +51,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends { public class FriendsModule : ISharedRegionModule, IFriendsModule { + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + protected bool m_Enabled = false; protected class UserFriendData @@ -72,7 +74,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends } protected static readonly FriendInfo[] EMPTY_FRIENDS = new FriendInfo[0]; - private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); protected List m_Scenes = new List(); @@ -156,7 +157,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends InitModule(config); m_Enabled = true; - m_log.InfoFormat("[FRIENDS MODULE]: {0} enabled.", Name); + m_log.DebugFormat("[FRIENDS MODULE]: {0} enabled.", Name); } } } @@ -201,7 +202,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends if (!m_Enabled) return; - m_log.DebugFormat("[FRIENDS MODULE]: AddRegion on {0}", Name); +// m_log.DebugFormat("[FRIENDS MODULE]: AddRegion on {0}", Name); m_Scenes.Add(scene); scene.RegisterModuleInterface(this); @@ -241,13 +242,13 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends #endregion - public virtual uint GetFriendPerms(UUID principalID, UUID friendID) + public virtual int GetRightsGrantedByFriend(UUID principalID, UUID friendID) { FriendInfo[] friends = GetFriends(principalID); FriendInfo finfo = GetFriend(friends, friendID); if (finfo != null) { - return (uint)finfo.TheirFlags; + return finfo.TheirFlags; } return 0; @@ -259,7 +260,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends client.OnApproveFriendRequest += OnApproveFriendRequest; client.OnDenyFriendRequest += OnDenyFriendRequest; client.OnTerminateFriendship += RemoveFriendship; - client.OnGrantUserRights += OnGrantUserRights; + client.OnGrantUserRights += GrantRights; // We need to cache information for child agents as well as root agents so that friend edit/move/delete // permissions will work across borders where both regions are on different simulators. @@ -356,10 +357,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends // Send the friends online List online = GetOnlineFriends(agentID); -// m_log.DebugFormat( -// "[FRIENDS MODULE]: User {0} in region {1} has {2} friends online", -// client.Name, client.Scene.RegionInfo.RegionName, online.Count); - if (online.Count > 0) client.SendAgentOnline(online.ToArray()); @@ -421,23 +418,30 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends List GetOnlineFriends(UUID userID) { List friendList = new List(); - List online = new List(); FriendInfo[] friends = GetFriends(userID); foreach (FriendInfo fi in friends) { - if (((fi.TheirFlags & 1) != 0) && (fi.TheirFlags != -1)) + if (((fi.TheirFlags & (int)FriendRights.CanSeeOnline) != 0) && (fi.TheirFlags != -1)) friendList.Add(fi.Friend); } + List online = new List(); + if (friendList.Count > 0) GetOnlineFriends(userID, friendList, online); +// m_log.DebugFormat( +// "[FRIENDS MODULE]: User {0} has {1} friends online", userID, online.Count); + return online; } protected virtual void GetOnlineFriends(UUID userID, List friendList, /*collector*/ List online) { +// m_log.DebugFormat( +// "[FRIENDS MODULE]: Looking for online presence of {0} users for {1}", friendList.Count, userID); + PresenceInfo[] presence = PresenceService.GetAgents(friendList.ToArray()); foreach (PresenceInfo pi in presence) { @@ -717,13 +721,13 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends } } - private void OnGrantUserRights(IClientAPI remoteClient, UUID target, int rights) + public void GrantRights(IClientAPI remoteClient, UUID friendID, int rights) { UUID requester = remoteClient.AgentId; m_log.DebugFormat( "[FRIENDS MODULE]: User {0} changing rights to {1} for friend {2}", - requester, rights, target); + requester, rights, friendID); FriendInfo[] friends = GetFriends(requester); if (friends.Length == 0) @@ -732,12 +736,12 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends } // Let's find the friend in this user's friend list - FriendInfo friend = GetFriend(friends, target); + FriendInfo friend = GetFriend(friends, friendID); if (friend != null) // Found it { // Store it on the DB - if (!StoreRights(requester, target, rights)) + if (!StoreRights(requester, friendID, rights)) { remoteClient.SendAlertMessage("Unable to grant rights."); return; @@ -748,17 +752,17 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends friend.MyFlags = rights; // Always send this back to the original client - remoteClient.SendChangeUserRights(requester, target, rights); + remoteClient.SendChangeUserRights(requester, friendID, rights); // // Notify the friend // // Try local - if (LocalGrantRights(requester, target, myFlags, rights)) + if (LocalGrantRights(requester, friendID, myFlags, rights)) return; - PresenceInfo[] friendSessions = PresenceService.GetAgents(new string[] { target.ToString() }); + PresenceInfo[] friendSessions = PresenceService.GetAgents(new string[] { friendID.ToString() }); if (friendSessions != null && friendSessions.Length > 0) { PresenceInfo friendSession = friendSessions[0]; @@ -767,13 +771,13 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends GridRegion region = GridService.GetRegionByUUID(m_Scenes[0].RegionInfo.ScopeID, friendSession.RegionID); // TODO: You might want to send the delta to save the lookup // on the other end!! - m_FriendsSimConnector.GrantRights(region, requester, target, myFlags, rights); + m_FriendsSimConnector.GrantRights(region, requester, friendID, myFlags, rights); } } } else { - m_log.DebugFormat("[FRIENDS MODULE]: friend {0} not found for {1}", target, requester); + m_log.DebugFormat("[FRIENDS MODULE]: friend {0} not found for {1}", friendID, requester); } } @@ -990,8 +994,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends protected virtual void StoreFriendships(UUID agentID, UUID friendID) { - FriendsService.StoreFriend(agentID.ToString(), friendID.ToString(), 1); - FriendsService.StoreFriend(friendID.ToString(), agentID.ToString(), 1); + FriendsService.StoreFriend(agentID.ToString(), friendID.ToString(), (int)FriendRights.CanSeeOnline); + FriendsService.StoreFriend(friendID.ToString(), agentID.ToString(), (int)FriendRights.CanSeeOnline); } protected virtual bool DeleteFriendship(UUID agentID, UUID exfriendID) diff --git a/OpenSim/Region/CoreModules/Avatar/Friends/Tests/FriendModuleTests.cs b/OpenSim/Region/CoreModules/Avatar/Friends/Tests/FriendModuleTests.cs index 94a78cb..45b4264 100644 --- a/OpenSim/Region/CoreModules/Avatar/Friends/Tests/FriendModuleTests.cs +++ b/OpenSim/Region/CoreModules/Avatar/Friends/Tests/FriendModuleTests.cs @@ -30,6 +30,7 @@ using System.Collections.Generic; using Nini.Config; using NUnit.Framework; using OpenMetaverse; +using OpenSim.Data.Null; using OpenSim.Framework; using OpenSim.Region.CoreModules.Avatar.Friends; using OpenSim.Region.Framework.Scenes; @@ -63,6 +64,11 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends.Tests [SetUp] public void Init() { + // We must clear friends data between tests since Data.Null holds it in static properties. This is necessary + // so that different services and simulator can share the data in standalone mode. This is pretty horrible + // effectively the statics are global variables. + NullFriendsData.Clear(); + IConfigSource config = new IniConfigSource(); config.AddConfig("Modules"); // Not strictly necessary since FriendsModule assumes it is the default (!) @@ -110,8 +116,15 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends.Tests m_fm.AddFriendship(sp1.ControllingClient, user2Id); - m_scene.RemoveClient(sp1.UUID, true); - m_scene.RemoveClient(sp2.UUID, true); + // Not necessary for this test. CanSeeOnline is automatically granted. +// m_fm.GrantRights(sp1.ControllingClient, user2Id, (int)FriendRights.CanSeeOnline); + + // We must logout from the client end so that the presence service is correctly updated by the presence + // detector. This is listening to the OnConnectionClosed event on the client. + ((TestClient)sp1.ControllingClient).Logout(); + ((TestClient)sp2.ControllingClient).Logout(); +// m_scene.RemoveClient(sp1.UUID, true); +// m_scene.RemoveClient(sp2.UUID, true); ScenePresence sp1Redux = SceneHelpers.AddScenePresence(m_scene, user1Id); @@ -120,33 +133,39 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends.Tests Assert.That(((TestClient)sp1Redux.ControllingClient).ReceivedOnlineNotifications.Count, Is.EqualTo(0)); } -// [Test] -// public void TestLoginWithOnlineFriends() -// { -// TestHelpers.InMethod(); + [Test] + public void TestLoginWithOnlineFriends() + { + TestHelpers.InMethod(); // log4net.Config.XmlConfigurator.Configure(); + + UUID user1Id = TestHelpers.ParseTail(0x1); + UUID user2Id = TestHelpers.ParseTail(0x2); + +// UserAccountHelpers.CreateUserWithInventory(m_scene, user1Id); +// UserAccountHelpers.CreateUserWithInventory(m_scene, user2Id); // -// UUID user1Id = TestHelpers.ParseTail(0x1); -// UUID user2Id = TestHelpers.ParseTail(0x2); -// -//// UserAccountHelpers.CreateUserWithInventory(m_scene, user1Id); -//// UserAccountHelpers.CreateUserWithInventory(m_scene, user2Id); -//// -//// m_fm.AddFriendship(user1Id, user2Id); -// -// ScenePresence sp1 = SceneHelpers.AddScenePresence(m_scene, user1Id); -// SceneHelpers.AddScenePresence(m_scene, user2Id); -// -// m_fm.AddFriendship(sp1.ControllingClient, user2Id); -//// m_fm.LocalGrantRights -// -// m_scene.RemoveClient(sp1.UUID, true); -// -// ScenePresence sp1Redux = SceneHelpers.AddScenePresence(m_scene, user1Id); -// -// Assert.That(((TestClient)sp1Redux.ControllingClient).ReceivedOfflineNotifications.Count, Is.EqualTo(0)); -// Assert.That(((TestClient)sp1Redux.ControllingClient).ReceivedOnlineNotifications.Count, Is.EqualTo(1)); -// } +// m_fm.AddFriendship(user1Id, user2Id); + + ScenePresence sp1 = SceneHelpers.AddScenePresence(m_scene, user1Id); + ScenePresence sp2 = SceneHelpers.AddScenePresence(m_scene, user2Id); + + m_fm.AddFriendship(sp1.ControllingClient, user2Id); + + // Not necessary for this test. CanSeeOnline is automatically granted. +// m_fm.GrantRights(sp1.ControllingClient, user2Id, (int)FriendRights.CanSeeOnline); + + // We must logout from the client end so that the presence service is correctly updated by the presence + // detector. This is listening to the OnConnectionClosed event on the client. +// ((TestClient)sp1.ControllingClient).Logout(); + ((TestClient)sp2.ControllingClient).Logout(); +// m_scene.RemoveClient(user2Id, true); + + ScenePresence sp2Redux = SceneHelpers.AddScenePresence(m_scene, user2Id); + + Assert.That(((TestClient)sp2Redux.ControllingClient).ReceivedOfflineNotifications.Count, Is.EqualTo(0)); + Assert.That(((TestClient)sp2Redux.ControllingClient).ReceivedOnlineNotifications.Count, Is.EqualTo(1)); + } [Test] public void TestAddFriendshipWhileOnline() -- cgit v1.1