fixed jobprovider teardown breaking in mono.

This commit is contained in:
kay.one 2013-02-17 08:09:49 -08:00
parent cb0c3b85de
commit aeae9d75d1
2 changed files with 171 additions and 176 deletions

View File

@ -3,7 +3,10 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Security.Principal; using System.Security.Principal;
using NLog; using NLog;
#if __MonoCS__
#else
using NetFwTypeLib; using NetFwTypeLib;
#endif
namespace NzbDrone.Common namespace NzbDrone.Common
{ {
@ -29,18 +32,15 @@ namespace NzbDrone.Common
public virtual void MakeAccessible () public virtual void MakeAccessible ()
{ {
if (!IsCurrentUserAdmin()) if (!IsCurrentUserAdmin ()) {
{
Logger.Trace ("User is not an admin, skipping."); Logger.Trace ("User is not an admin, skipping.");
return; return;
} }
int port = 0; int port = 0;
if (IsFirewallEnabled()) if (IsFirewallEnabled ()) {
{ if (IsNzbDronePortOpen ()) {
if(IsNzbDronePortOpen())
{
Logger.Trace ("NzbDrone port is already open, skipping."); Logger.Trace ("NzbDrone port is already open, skipping.");
return; return;
} }
@ -66,13 +66,10 @@ namespace NzbDrone.Common
public virtual bool IsCurrentUserAdmin () public virtual bool IsCurrentUserAdmin ()
{ {
try try {
{
var principal = new WindowsPrincipal (WindowsIdentity.GetCurrent ()); var principal = new WindowsPrincipal (WindowsIdentity.GetCurrent ());
return principal.IsInRole (WindowsBuiltInRole.Administrator); return principal.IsInRole (WindowsBuiltInRole.Administrator);
} } catch (Exception ex) {
catch(Exception ex)
{
Logger.WarnException ("Error checking if the current user is an administrator.", ex); Logger.WarnException ("Error checking if the current user is an administrator.", ex);
return false; return false;
} }
@ -80,9 +77,13 @@ namespace NzbDrone.Common
public virtual bool IsNzbDronePortOpen () public virtual bool IsNzbDronePortOpen ()
{ {
try #if __MonoCS__
{ #else
try {
var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false); var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType); var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
if (!mgr.LocalPolicy.CurrentProfile.FirewallEnabled) if (!mgr.LocalPolicy.CurrentProfile.FirewallEnabled)
@ -90,23 +91,23 @@ namespace NzbDrone.Common
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
foreach (INetFwOpenPort p in ports) foreach (INetFwOpenPort p in ports) {
{
if (p.Port == _configFileProvider.Port) if (p.Port == _configFileProvider.Port)
return true; return true;
} }
} } catch (Exception ex) {
catch(Exception ex)
{
Logger.WarnException ("Failed to check for open port in firewall", ex); Logger.WarnException ("Failed to check for open port in firewall", ex);
} }
#endif
return false; return false;
} }
private bool OpenFirewallPort (int portNumber) private bool OpenFirewallPort (int portNumber)
{ {
try #if __MonoCS__
{ return true;
#else
try {
var type = Type.GetTypeFromProgID ("HNetCfg.FWOpenPort", false); var type = Type.GetTypeFromProgID ("HNetCfg.FWOpenPort", false);
var port = Activator.CreateInstance (type) as INetFwOpenPort; var port = Activator.CreateInstance (type) as INetFwOpenPort;
@ -121,67 +122,64 @@ namespace NzbDrone.Common
ports.Add (port); ports.Add (port);
return true; return true;
} } catch (Exception ex) {
catch(Exception ex)
{
Logger.WarnException ("Failed to open port in firewall for NzbDrone " + portNumber, ex); Logger.WarnException ("Failed to open port in firewall for NzbDrone " + portNumber, ex);
return false; return false;
} }
#endif
} }
private int CloseFirewallPort () private int CloseFirewallPort ()
{ {
try
{ #if __MonoCS__
#else
try {
var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false); var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType); var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; var ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
var portNumber = 8989; var portNumber = 8989;
foreach (INetFwOpenPort p in ports) foreach (INetFwOpenPort p in ports) {
{ if (p.Name == "NzbDrone") {
if (p.Name == "NzbDrone")
{
portNumber = p.Port; portNumber = p.Port;
break; break;
} }
} }
if (portNumber != _configFileProvider.Port) if (portNumber != _configFileProvider.Port) {
{
ports.Remove (portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); ports.Remove (portNumber, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
return portNumber; return portNumber;
} }
} } catch (Exception ex) {
catch(Exception ex)
{
Logger.WarnException ("Failed to close port in firewall for NzbDrone", ex); Logger.WarnException ("Failed to close port in firewall for NzbDrone", ex);
} }
#endif
return 0; return 0;
} }
private bool IsFirewallEnabled () private bool IsFirewallEnabled ()
{ {
try #if __MonoCS__
{ return true;
#else
try {
var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false); var netFwMgrType = Type.GetTypeFromProgID ("HNetCfg.FwMgr", false);
var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType); var mgr = (INetFwMgr)Activator.CreateInstance (netFwMgrType);
return mgr.LocalPolicy.CurrentProfile.FirewallEnabled; return mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
} } catch (Exception ex) {
catch(Exception ex)
{
Logger.WarnException ("Failed to check if the firewall is enabled", ex); Logger.WarnException ("Failed to check if the firewall is enabled", ex);
return false; return false;
} }
#endif
} }
private bool RegisterUrl (int portNumber) private bool RegisterUrl (int portNumber)
{ {
try try {
{
var startInfo = new ProcessStartInfo () var startInfo = new ProcessStartInfo ()
{ {
FileName = "netsh.exe", FileName = "netsh.exe",
@ -191,10 +189,7 @@ namespace NzbDrone.Common
var process = _processProvider.Start (startInfo); var process = _processProvider.Start (startInfo);
process.WaitForExit (5000); process.WaitForExit (5000);
return true; return true;
} } catch (Exception ex) {
catch(Exception ex)
{
Logger.WarnException ("Error registering URL", ex); Logger.WarnException ("Error registering URL", ex);
} }
@ -203,8 +198,7 @@ namespace NzbDrone.Common
private bool UnregisterUrl (int portNumber) private bool UnregisterUrl (int portNumber)
{ {
try try {
{
var startInfo = new ProcessStartInfo () var startInfo = new ProcessStartInfo ()
{ {
FileName = "netsh.exe", FileName = "netsh.exe",
@ -214,10 +208,7 @@ namespace NzbDrone.Common
var process = _processProvider.Start (startInfo); var process = _processProvider.Start (startInfo);
process.WaitForExit (5000); process.WaitForExit (5000);
return true; return true;
} } catch (Exception ex) {
catch (Exception ex)
{
Logger.WarnException ("Error registering URL", ex); Logger.WarnException ("Error registering URL", ex);
} }

View File

@ -8,6 +8,7 @@ using FizzWare.NBuilder;
using FluentAssertions; using FluentAssertions;
using NCrunch.Framework; using NCrunch.Framework;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Jobs; using NzbDrone.Core.Jobs;
using NzbDrone.Core.Model; using NzbDrone.Core.Model;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
@ -39,9 +40,12 @@ namespace NzbDrone.Core.Test.ProviderTests.JobProviderTests
[TearDown] [TearDown]
public void TearDown() public void TearDown()
{
if(!EnvironmentProvider.IsMono)
{ {
Mocker.Resolve<JobProvider>().Queue.Should().BeEmpty(); Mocker.Resolve<JobProvider>().Queue.Should().BeEmpty();
} }
}
private void ResetLastExecution() private void ResetLastExecution()
{ {