diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs
index 88b0e1195..4a50136cf 100644
--- a/Emby.Dlna/DlnaManager.cs
+++ b/Emby.Dlna/DlnaManager.cs
@@ -106,19 +106,28 @@ namespace Emby.Dlna
throw new ArgumentNullException(nameof(deviceInfo));
}
- var profile = GetProfiles()
- .FirstOrDefault(i => i.Identification != null && IsMatch(i.Identification, deviceInfo));
+ try
+ {
+ var profile = GetProfiles()
+ .FirstOrDefault(i => i.Identification != null && IsMatch(deviceInfo, i.Identification));
- if (profile != null)
- {
- _logger.LogDebug("Found matching device profile: {0}", profile.Name);
+ if (profile != null)
+ {
+ _logger.LogDebug("Found matching device profile: {0}", profile.Name);
+ }
+ else
+ {
+ LogUnmatchedProfile(deviceInfo);
+ }
+
+ return profile;
}
- else
+ catch (ArgumentException ex)
{
- LogUnmatchedProfile(deviceInfo);
+ _logger.LogError(ex, "Error in profile comparison.");
}
- return profile;
+ return null;
}
private void LogUnmatchedProfile(DeviceIdentification profile)
@@ -138,85 +147,82 @@ namespace Emby.Dlna
_logger.LogInformation(builder.ToString());
}
- private bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
+ ///
+ /// Attempts to match a device with a profile.
+ /// Rules:
+ /// - If the profile field has no value, the field matches irregardless of its contents.
+ /// - the profile field can be an exact match, or a reg exp.
+ ///
+ /// The of the device.
+ /// The of the profile.
+ /// True if they match.
+ public static bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
{
- if (!string.IsNullOrEmpty(profileInfo.FriendlyName))
+ if (!IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
{
- if (deviceInfo.FriendlyName == null || !IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
- {
- return false;
- }
+ return false;
}
- if (!string.IsNullOrEmpty(profileInfo.Manufacturer))
+ if (!IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
{
- if (deviceInfo.Manufacturer == null || !IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
- {
- return false;
- }
+ return false;
}
- if (!string.IsNullOrEmpty(profileInfo.ManufacturerUrl))
+ if (!IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
{
- if (deviceInfo.ManufacturerUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
- {
- return false;
- }
+ return false;
}
- if (!string.IsNullOrEmpty(profileInfo.ModelDescription))
+ if (!IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
{
- if (deviceInfo.ModelDescription == null || !IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
- {
- return false;
- }
+ return false;
}
- if (!string.IsNullOrEmpty(profileInfo.ModelName))
+ if (!IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
{
- if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
- {
- return false;
- }
+ return false;
}
- if (!string.IsNullOrEmpty(profileInfo.ModelNumber))
+ if (!IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
{
- if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
- {
- return false;
- }
+ return false;
}
- if (!string.IsNullOrEmpty(profileInfo.ModelUrl))
+ if (!IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
{
- if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
- {
- return false;
- }
+ return false;
}
- if (!string.IsNullOrEmpty(profileInfo.SerialNumber))
+ if (!IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
{
- if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
- {
- return false;
- }
+ return false;
}
return true;
}
- private bool IsRegexOrSubstringMatch(string input, string pattern)
+ public static bool IsRegexOrSubstringMatch(string input, string pattern)
{
+ if (string.IsNullOrEmpty(pattern))
+ {
+ // In profile identification: An empty pattern matches anything.
+ return true;
+ }
+
+ if (string.IsNullOrEmpty(input))
+ {
+ // The profile contains a value, and the device doesn't.
+ return false;
+ }
+
try
{
- return input.Contains(pattern, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
+ return input.Equals(pattern, StringComparison.OrdinalIgnoreCase)
+ || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
catch (ArgumentException ex)
{
- _logger.LogError(ex, "Error evaluating regex pattern {Pattern}", pattern);
- return false;
+ throw new ArgumentException("Error evaluating regex pattern " + pattern, ex);
}
}
diff --git a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs
index 83638c7c4..cc7cf92e7 100644
--- a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs
+++ b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs
@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
+using Emby.Dlna;
using Emby.Dlna.PlayTo;
using MediaBrowser.Model.Dlna;
using Xunit;
@@ -12,92 +13,23 @@ namespace Jellyfin.Dlna.Tests
{
public class ProfileTester
{
- private bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
- {
- if (!string.IsNullOrEmpty(profileInfo.FriendlyName))
- {
- if (deviceInfo.FriendlyName == null || !IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
- {
- return false;
- }
- }
-
- if (!string.IsNullOrEmpty(profileInfo.Manufacturer))
- {
- if (deviceInfo.Manufacturer == null || !IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
- {
- return false;
- }
- }
-
- if (!string.IsNullOrEmpty(profileInfo.ManufacturerUrl))
- {
- if (deviceInfo.ManufacturerUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
- {
- return false;
- }
- }
-
- if (!string.IsNullOrEmpty(profileInfo.ModelDescription))
- {
- if (deviceInfo.ModelDescription == null || !IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
- {
- return false;
- }
- }
-
- if (!string.IsNullOrEmpty(profileInfo.ModelName))
- {
- if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
- {
- return false;
- }
- }
-
- if (!string.IsNullOrEmpty(profileInfo.ModelNumber))
- {
- if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
- {
- return false;
- }
- }
-
- if (!string.IsNullOrEmpty(profileInfo.ModelUrl))
- {
- if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
- {
- return false;
- }
- }
-
- if (!string.IsNullOrEmpty(profileInfo.SerialNumber))
- {
- if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
- {
- return false;
- }
- }
-
- return true;
- }
-
- private bool IsRegexOrSubstringMatch(string input, string pattern)
- {
- return input.Contains(pattern, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
- }
-
[Fact]
public void Test_Profile_Matches()
{
- var source = new DeviceInfo()
+ var device = new DeviceInfo()
{
- Name = "HelloWorld"
+ Name = "My Device",
+ Manufacturer = "LG Electronics",
+ ManufacturerUrl = "http://www.lge.com",
+ ModelDescription = "LG WebOSTV DMRplus",
+ ModelName = "LG TV",
+ ModelNumber = "1.0",
};
- var dest = new DeviceProfile()
+ var profile = new DeviceProfile()
{
- Name = "Test Subject 1",
- FriendlyName = "HelloWorld",
+ Name = "Test Profile",
+ FriendlyName = "My Device",
Manufacturer = "LG Electronics",
ManufacturerUrl = "http://www.lge.com",
ModelDescription = "LG WebOSTV DMRplus",
@@ -105,7 +37,7 @@ namespace Jellyfin.Dlna.Tests
ModelNumber = "1.0",
Identification = new DeviceIdentification()
{
- FriendlyName = "HelloWorld",
+ FriendlyName = "My Device",
Manufacturer = "LG Electronics",
ManufacturerUrl = "http://www.lge.com",
ModelDescription = "LG WebOSTV DMRplus",
@@ -114,7 +46,51 @@ namespace Jellyfin.Dlna.Tests
}
};
- Assert.True(IsMatch(dest.Identification, source.ToDeviceIdentification()));
+ Assert.True(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile.Identification));
+
+ var profile2 = new DeviceProfile()
+ {
+ Name = "Test Profile",
+ FriendlyName = "My Device",
+ Identification = new DeviceIdentification()
+ {
+ FriendlyName = "My Device",
+ }
+ };
+
+ Assert.True(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile2.Identification));
+ }
+
+ [Fact]
+ public void Test_Profile_NoMatch()
+ {
+ var device = new DeviceInfo()
+ {
+ Name = "My Device",
+ Manufacturer = "JVC"
+ };
+
+ var profile = new DeviceProfile()
+ {
+ Name = "Test Profile",
+ FriendlyName = "My Device",
+ Manufacturer = "LG Electronics",
+ ManufacturerUrl = "http://www.lge.com",
+ ModelDescription = "LG WebOSTV DMRplus",
+ ModelName = "LG TV",
+ ModelNumber = "1.0",
+ Identification = new DeviceIdentification()
+ {
+ FriendlyName = "My Device",
+ Manufacturer = "LG Electronics",
+ ManufacturerUrl = "http://www.lge.com",
+ ModelDescription = "LG WebOSTV DMRplus",
+ ModelName = "LG TV",
+ ModelNumber = "1.0",
+ }
+ };
+
+ Assert.False(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile.Identification));
}
}
}