From c68f6163774e4d6534a5a68d2c0f6b7435240ed3 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Mon, 19 Apr 2021 12:36:30 +0100 Subject: [PATCH 01/13] Flip fields --- Emby.Dlna/DlnaManager.cs | 2 +- tests/Jellyfin.Dlna.Tests/ProfileTester.cs | 120 +++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/Jellyfin.Dlna.Tests/ProfileTester.cs diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index 9ab3240388..88b0e11951 100644 --- a/Emby.Dlna/DlnaManager.cs +++ b/Emby.Dlna/DlnaManager.cs @@ -107,7 +107,7 @@ namespace Emby.Dlna } var profile = GetProfiles() - .FirstOrDefault(i => i.Identification != null && IsMatch(deviceInfo, i.Identification)); + .FirstOrDefault(i => i.Identification != null && IsMatch(i.Identification, deviceInfo)); if (profile != null) { diff --git a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs new file mode 100644 index 0000000000..83638c7c44 --- /dev/null +++ b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Emby.Dlna.PlayTo; +using MediaBrowser.Model.Dlna; +using Xunit; + +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() + { + Name = "HelloWorld" + }; + + var dest = new DeviceProfile() + { + Name = "Test Subject 1", + FriendlyName = "HelloWorld", + Manufacturer = "LG Electronics", + ManufacturerUrl = "http://www.lge.com", + ModelDescription = "LG WebOSTV DMRplus", + ModelName = "LG TV", + ModelNumber = "1.0", + Identification = new DeviceIdentification() + { + FriendlyName = "HelloWorld", + Manufacturer = "LG Electronics", + ManufacturerUrl = "http://www.lge.com", + ModelDescription = "LG WebOSTV DMRplus", + ModelName = "LG TV", + ModelNumber = "1.0", + } + }; + + Assert.True(IsMatch(dest.Identification, source.ToDeviceIdentification())); + } + } +} From 95b733ad4cbbe35b638de51e0b84d2794d28c34d Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Mon, 19 Apr 2021 14:07:14 +0100 Subject: [PATCH 02/13] reworked code --- Emby.Dlna/DlnaManager.cs | 112 +++++++++-------- tests/Jellyfin.Dlna.Tests/ProfileTester.cs | 138 +++++++++------------ 2 files changed, 116 insertions(+), 134 deletions(-) diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index 88b0e11951..4a50136cf7 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 83638c7c44..cc7cf92e70 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)); } } } From 4449217f8ff736e98a50fc686331c5240f6fe33c Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Mon, 19 Apr 2021 14:24:58 +0100 Subject: [PATCH 03/13] simplified isMatch --- Emby.Dlna/DlnaManager.cs | 49 +++++++--------------------------------- 1 file changed, 8 insertions(+), 41 deletions(-) diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index 4a50136cf7..374e04210d 100644 --- a/Emby.Dlna/DlnaManager.cs +++ b/Emby.Dlna/DlnaManager.cs @@ -158,47 +158,14 @@ namespace Emby.Dlna /// True if they match. public static bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo) { - if (!IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName)) - { - return false; - } - - if (!IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer)) - { - return false; - } - - if (!IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl)) - { - return false; - } - - if (!IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription)) - { - return false; - } - - if (!IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName)) - { - return false; - } - - if (!IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber)) - { - return false; - } - - if (!IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl)) - { - return false; - } - - if (!IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber)) - { - return false; - } - - return true; + return IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName) + && IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer) + && IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl) + && IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription) + && IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName) + && IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber) + && IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl) + && IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber); } public static bool IsRegexOrSubstringMatch(string input, string pattern) From 092c610fbf8268d5ff4518c2a6882764b89183d1 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Tue, 20 Apr 2021 08:26:41 +0100 Subject: [PATCH 04/13] Update Emby.Dlna/DlnaManager.cs Co-authored-by: Claus Vium --- Emby.Dlna/DlnaManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index 374e04210d..82dc30fb51 100644 --- a/Emby.Dlna/DlnaManager.cs +++ b/Emby.Dlna/DlnaManager.cs @@ -113,7 +113,7 @@ namespace Emby.Dlna if (profile != null) { - _logger.LogDebug("Found matching device profile: {0}", profile.Name); + _logger.LogDebug("Found matching device profile: {ProfileName}", profile.Name); } else { From a99caa0daae7c47afdf7d0cf5cf182976211ffa2 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Tue, 20 Apr 2021 18:04:16 +0100 Subject: [PATCH 05/13] Changed testing --- Emby.Dlna/DlnaManager.cs | 15 +++------- .../Jellyfin.Dlna.Tests.csproj | 1 + tests/Jellyfin.Dlna.Tests/ProfileTester.cs | 29 +++++++++++++------ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index 82dc30fb51..e80f1aaeed 100644 --- a/Emby.Dlna/DlnaManager.cs +++ b/Emby.Dlna/DlnaManager.cs @@ -156,7 +156,7 @@ namespace Emby.Dlna /// The of the device. /// The of the profile. /// True if they match. - public static bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo) + public bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo) { return IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName) && IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer) @@ -168,7 +168,7 @@ namespace Emby.Dlna && IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber); } - public static bool IsRegexOrSubstringMatch(string input, string pattern) + private bool IsRegexOrSubstringMatch(string input, string pattern) { if (string.IsNullOrEmpty(pattern)) { @@ -182,15 +182,8 @@ namespace Emby.Dlna return false; } - try - { - return input.Equals(pattern, StringComparison.OrdinalIgnoreCase) - || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); - } - catch (ArgumentException ex) - { - throw new ArgumentException("Error evaluating regex pattern " + pattern, ex); - } + return input.Equals(pattern, StringComparison.OrdinalIgnoreCase) + || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); } public DeviceProfile GetProfile(IHeaderDictionary headers) diff --git a/tests/Jellyfin.Dlna.Tests/Jellyfin.Dlna.Tests.csproj b/tests/Jellyfin.Dlna.Tests/Jellyfin.Dlna.Tests.csproj index 850db1c75b..a1255a8586 100644 --- a/tests/Jellyfin.Dlna.Tests/Jellyfin.Dlna.Tests.csproj +++ b/tests/Jellyfin.Dlna.Tests/Jellyfin.Dlna.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs index cc7cf92e70..3676cc9ef7 100644 --- a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs +++ b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs @@ -1,18 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; using Emby.Dlna; using Emby.Dlna.PlayTo; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Controller; using MediaBrowser.Model.Dlna; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Serialization; +using Microsoft.Extensions.Logging; +using Moq; using Xunit; namespace Jellyfin.Dlna.Tests { public class ProfileTester { + private DlnaManager GetManager() + { + var xmlSerializer = new Mock(); + var fileSystem = new Mock(); + var appPaths = new Mock(); + var loggerFactory = new Mock(); + var appHost = new Mock(); + + return new DlnaManager(xmlSerializer.Object, fileSystem.Object, appPaths.Object, loggerFactory.Object, appHost.Object); + } + [Fact] public void Test_Profile_Matches() { @@ -46,7 +57,7 @@ namespace Jellyfin.Dlna.Tests } }; - Assert.True(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile.Identification)); + Assert.True(GetManager().IsMatch(device.ToDeviceIdentification(), profile.Identification)); var profile2 = new DeviceProfile() { @@ -58,7 +69,7 @@ namespace Jellyfin.Dlna.Tests } }; - Assert.True(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile2.Identification)); + Assert.True(GetManager().IsMatch(device.ToDeviceIdentification(), profile2.Identification)); } [Fact] @@ -90,7 +101,7 @@ namespace Jellyfin.Dlna.Tests } }; - Assert.False(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile.Identification)); + Assert.False(GetManager().IsMatch(device.ToDeviceIdentification(), profile.Identification)); } } } From 7848ea148438baf8929e1c2939e6979cb84aad1a Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Tue, 20 Apr 2021 18:08:19 +0100 Subject: [PATCH 06/13] missed one. --- Emby.Dlna/DlnaManager.cs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index e80f1aaeed..7f8dba35fb 100644 --- a/Emby.Dlna/DlnaManager.cs +++ b/Emby.Dlna/DlnaManager.cs @@ -106,28 +106,19 @@ namespace Emby.Dlna throw new ArgumentNullException(nameof(deviceInfo)); } - try + var profile = GetProfiles() + .FirstOrDefault(i => i.Identification != null && IsMatch(deviceInfo, i.Identification)); + + if (profile != null) { - var profile = GetProfiles() - .FirstOrDefault(i => i.Identification != null && IsMatch(deviceInfo, i.Identification)); - - if (profile != null) - { - _logger.LogDebug("Found matching device profile: {ProfileName}", profile.Name); - } - else - { - LogUnmatchedProfile(deviceInfo); - } - - return profile; + _logger.LogDebug("Found matching device profile: {ProfileName}", profile.Name); } - catch (ArgumentException ex) + else { - _logger.LogError(ex, "Error in profile comparison."); + LogUnmatchedProfile(deviceInfo); } - return null; + return profile; } private void LogUnmatchedProfile(DeviceIdentification profile) From 41246909dcbf71a85140b5354e5f76e33ea4801e Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Tue, 20 Apr 2021 18:14:23 +0100 Subject: [PATCH 07/13] fixed merge --- Emby.Dlna/DlnaManager.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index 8aed4a50fa..a554a4d5b7 100644 --- a/Emby.Dlna/DlnaManager.cs +++ b/Emby.Dlna/DlnaManager.cs @@ -173,8 +173,16 @@ namespace Emby.Dlna return false; } - return input.Equals(pattern, StringComparison.OrdinalIgnoreCase) + try + { + 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; + } } public DeviceProfile GetProfile(IHeaderDictionary headers) From 522d5a71582fa4b610c75b0029812a9c10619002 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Tue, 20 Apr 2021 18:17:48 +0100 Subject: [PATCH 08/13] Fixed indent --- Emby.Dlna/DlnaManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index a554a4d5b7..57a6c20598 100644 --- a/Emby.Dlna/DlnaManager.cs +++ b/Emby.Dlna/DlnaManager.cs @@ -176,7 +176,7 @@ namespace Emby.Dlna try { return input.Equals(pattern, StringComparison.OrdinalIgnoreCase) - || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); + || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); } catch (ArgumentException ex) { From 2559ceffab778933a6122d2989cfa4d58f699839 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Wed, 21 Apr 2021 10:09:05 +0100 Subject: [PATCH 09/13] Update tests/Jellyfin.Dlna.Tests/ProfileTester.cs Co-authored-by: Claus Vium --- tests/Jellyfin.Dlna.Tests/ProfileTester.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs index 3676cc9ef7..74868bc0e5 100644 --- a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs +++ b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs @@ -11,7 +11,7 @@ using Xunit; namespace Jellyfin.Dlna.Tests { - public class ProfileTester + public class DlnaManagerTests { private DlnaManager GetManager() { From 740e5ec167fc8a147cfaca2904220baee6ccc509 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Wed, 21 Apr 2021 10:09:14 +0100 Subject: [PATCH 10/13] Update tests/Jellyfin.Dlna.Tests/ProfileTester.cs Co-authored-by: Claus Vium --- tests/Jellyfin.Dlna.Tests/ProfileTester.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs index 74868bc0e5..2f27a0534a 100644 --- a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs +++ b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs @@ -73,7 +73,7 @@ namespace Jellyfin.Dlna.Tests } [Fact] - public void Test_Profile_NoMatch() + public void IsMatch_GivenNamesAndManufacturersDoNotMatch_ReturnsFalse() { var device = new DeviceInfo() { From 39eb5da44f8e931aa6a89f9bb3d0785ca5e84465 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Wed, 21 Apr 2021 10:09:21 +0100 Subject: [PATCH 11/13] Update tests/Jellyfin.Dlna.Tests/ProfileTester.cs Co-authored-by: Claus Vium --- tests/Jellyfin.Dlna.Tests/ProfileTester.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs index 2f27a0534a..004933ca95 100644 --- a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs +++ b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs @@ -25,7 +25,7 @@ namespace Jellyfin.Dlna.Tests } [Fact] - public void Test_Profile_Matches() + public void IsMatch_GivenMatchingName_ReturnsTrue() { var device = new DeviceInfo() { From 53e1b302cc08181c7909719df0cd837fd4b182f4 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Wed, 21 Apr 2021 10:18:29 +0100 Subject: [PATCH 12/13] Changes as requested --- Emby.Dlna/DlnaManager.cs | 14 ++++----- .../{ProfileTester.cs => DlnaManagerTests.cs} | 31 ++++++++++++++++--- 2 files changed, 34 insertions(+), 11 deletions(-) rename tests/Jellyfin.Dlna.Tests/{ProfileTester.cs => DlnaManagerTests.cs} (78%) diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.cs index 57a6c20598..5f2be07cf3 100644 --- a/Emby.Dlna/DlnaManager.cs +++ b/Emby.Dlna/DlnaManager.cs @@ -150,13 +150,13 @@ namespace Emby.Dlna public bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo) { return IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName) - && IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer) - && IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl) - && IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription) - && IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName) - && IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber) - && IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl) - && IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber); + && IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer) + && IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl) + && IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription) + && IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName) + && IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber) + && IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl) + && IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber); } private bool IsRegexOrSubstringMatch(string input, string pattern) diff --git a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs b/tests/Jellyfin.Dlna.Tests/DlnaManagerTests.cs similarity index 78% rename from tests/Jellyfin.Dlna.Tests/ProfileTester.cs rename to tests/Jellyfin.Dlna.Tests/DlnaManagerTests.cs index 004933ca95..087d43a779 100644 --- a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs +++ b/tests/Jellyfin.Dlna.Tests/DlnaManagerTests.cs @@ -46,7 +46,7 @@ namespace Jellyfin.Dlna.Tests ModelDescription = "LG WebOSTV DMRplus", ModelName = "LG TV", ModelNumber = "1.0", - Identification = new DeviceIdentification() + Identification = new () { FriendlyName = "My Device", Manufacturer = "LG Electronics", @@ -69,7 +69,8 @@ namespace Jellyfin.Dlna.Tests } }; - Assert.True(GetManager().IsMatch(device.ToDeviceIdentification(), profile2.Identification)); + var deviceMatch = GetManager().IsMatch(device.ToDeviceIdentification(), profile2.Identification); + Assert.True(deviceMatch); } [Fact] @@ -90,7 +91,7 @@ namespace Jellyfin.Dlna.Tests ModelDescription = "LG WebOSTV DMRplus", ModelName = "LG TV", ModelNumber = "1.0", - Identification = new DeviceIdentification() + Identification = new () { FriendlyName = "My Device", Manufacturer = "LG Electronics", @@ -101,7 +102,29 @@ namespace Jellyfin.Dlna.Tests } }; - Assert.False(GetManager().IsMatch(device.ToDeviceIdentification(), profile.Identification)); + var deviceMatch = GetManager().IsMatch(device.ToDeviceIdentification(), profile.Identification); + + Assert.False(deviceMatch); + } + + [Fact] + public void IsMatch_GivenNamesAndRegExMatch_ReturnsTrue() + { + var device = new DeviceInfo() + { + Name = "My Device" + }; + + var profile = new DeviceProfile() + { + Name = "Test Profile", + FriendlyName = "My .*", + Identification = new () + }; + + var deviceMatch = GetManager().IsMatch(device.ToDeviceIdentification(), profile.Identification); + + Assert.True(deviceMatch); } } } From 20e19ae9b3a89e4c9aac00f2352eb6e3ec52230a Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Wed, 21 Apr 2021 12:08:02 +0100 Subject: [PATCH 13/13] Moved Assert --- tests/Jellyfin.Dlna.Tests/DlnaManagerTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Jellyfin.Dlna.Tests/DlnaManagerTests.cs b/tests/Jellyfin.Dlna.Tests/DlnaManagerTests.cs index 087d43a779..668bd8f878 100644 --- a/tests/Jellyfin.Dlna.Tests/DlnaManagerTests.cs +++ b/tests/Jellyfin.Dlna.Tests/DlnaManagerTests.cs @@ -57,8 +57,6 @@ namespace Jellyfin.Dlna.Tests } }; - Assert.True(GetManager().IsMatch(device.ToDeviceIdentification(), profile.Identification)); - var profile2 = new DeviceProfile() { Name = "Test Profile", @@ -70,7 +68,10 @@ namespace Jellyfin.Dlna.Tests }; var deviceMatch = GetManager().IsMatch(device.ToDeviceIdentification(), profile2.Identification); + var deviceMatch2 = GetManager().IsMatch(device.ToDeviceIdentification(), profile.Identification); + Assert.True(deviceMatch); + Assert.True(deviceMatch2); } [Fact]