ntminer 5 years ago
parent
commit
8c2bc7af19

+ 2 - 2
src/AppModels/Vms/KernelOutputViewModel.cs

@@ -148,8 +148,8 @@ namespace NTMiner.Vms {
             get {
                 var query = AppRoot.KernelOutputTranslaterVms.GetListByKernelId(this.Id).AsQueryable();
                 if (!string.IsNullOrEmpty(TranslaterKeyword)) {
-                    query = query.Where(a => (a.RegexPattern != null && a.RegexPattern.Contains(TranslaterKeyword))
-                        || (a.Replacement != null && a.Replacement.Contains(TranslaterKeyword)));
+                    query = query.Where(a => (a.RegexPattern != null && a.RegexPattern.IgnoreCaseContains(TranslaterKeyword))
+                        || (a.Replacement != null && a.Replacement.IgnoreCaseContains(TranslaterKeyword)));
                 }
                 return query.OrderBy(a => a.SortNumber).ToList();
             }

+ 2 - 2
src/AppModels/Vms/LocalMessagesViewModel.cs

@@ -164,7 +164,7 @@
             if (_queyResults == _localMessageVms) {
                 return false;
             }
-            if (SelectedChannel == vm.ChannelEnum.GetEnumItem() && _count[SelectedChannel][vm.MessageTypeEnum].IsChecked && (string.IsNullOrEmpty(Keyword) || vm.Content.Contains(Keyword))) {
+            if (SelectedChannel == vm.ChannelEnum.GetEnumItem() && _count[SelectedChannel][vm.MessageTypeEnum].IsChecked && (string.IsNullOrEmpty(Keyword) || vm.Content.IgnoreCaseContains(Keyword))) {
                 return true;
             }
             return false;
@@ -194,7 +194,7 @@
                 query = query.Where(a => _count[SelectedChannel][a.MessageTypeEnum].IsChecked);
             }
             if (!string.IsNullOrEmpty(Keyword)) {
-                query = query.Where(a => a.Content != null && a.Content.Contains(Keyword));
+                query = query.Where(a => a.Content != null && a.Content.IgnoreCaseContains(Keyword));
             }
             _queyResults = new ObservableCollection<LocalMessageViewModel>(query);
             OnPropertyChanged(nameof(IsNoRecord));

+ 2 - 2
src/AppModels/Vms/ServerMessagesViewModel.cs

@@ -144,7 +144,7 @@ namespace NTMiner.Vms {
             if (_queyResults == _serverMessageVms) {
                 return false;
             }
-            if (_count[vm.MessageTypeEnum].IsChecked && (string.IsNullOrEmpty(Keyword) || vm.Content.Contains(Keyword))) {
+            if (_count[vm.MessageTypeEnum].IsChecked && (string.IsNullOrEmpty(Keyword) || vm.Content.IgnoreCaseContains(Keyword))) {
                 return true;
             }
             return false;
@@ -171,7 +171,7 @@ namespace NTMiner.Vms {
                 query = query.Where(a => _count[a.MessageTypeEnum].IsChecked);
             }
             if (!string.IsNullOrEmpty(Keyword)) {
-                query = query.Where(a => a.Content != null && a.Content.Contains(Keyword));
+                query = query.Where(a => a.Content != null && a.Content.IgnoreCaseContains(Keyword));
             }
             _queyResults = new ObservableCollection<ServerMessageViewModel>(query);
             OnPropertyChanged(nameof(IsNoRecord));

+ 1 - 1
src/NTMinerClient/Mine/MineContext.cs

@@ -54,7 +54,7 @@ namespace NTMiner.Mine {
 
         private void NewLogFileName() {
             string logFileName;
-            if (this.CommandLine.Contains(NTKeyword.LogFileParameterName)) {
+            if (this.CommandLine.IgnoreCaseContains(NTKeyword.LogFileParameterName)) {
                 this.KernelProcessType = KernelProcessType.Logfile;
                 logFileName = $"{this.Kernel.Code}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss_fff")}.log";
             }

+ 1 - 1
src/NTMinerClient/NTMinerContext.cs

@@ -628,7 +628,7 @@ namespace NTMiner {
                     using (var mos = new ManagementObjectSearcher("SELECT Caption FROM Win32_VideoController")) {
                         foreach (ManagementBaseObject item in mos.Get()) {
                             foreach (var property in item.Properties) {
-                                if ((property.Value ?? string.Empty).ToString().Contains("NVIDIA")) {
+                                if ((property.Value ?? string.Empty).ToString().IgnoreCaseContains("NVIDIA")) {
                                     return true;
                                 }
                             }

+ 1 - 1
src/NTMinerDaemon/VirtualRoot.cs

@@ -40,7 +40,7 @@ namespace NTMiner {
             HomePath.SetHomeDirFullName(AppDomain.CurrentDomain.BaseDirectory);
             SetOut(new ConsoleOut());
             if (args.Length != 0) {
-                if (args.Contains("--sha1")) {
+                if (args.Contains("--sha1", StringComparer.OrdinalIgnoreCase)) {
                     File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sha1"), Sha1);
                     return;
                 }

+ 0 - 1
src/NTMinerDataSchemas/NTMinerDataSchemas.csproj

@@ -53,7 +53,6 @@
     <Compile Include="Core\MinerServer\GetWorkJsonResponse.cs" />
     <Compile Include="Core\MinerServer\IClientData.cs" />
     <Compile Include="Core\MinerServer\IMinerSign.cs" />
-    <Compile Include="Core\MinerServer\ClientDataComparer.cs" />
     <Compile Include="Core\MinerServer\MinerSign.cs" />
     <Compile Include="DataSchemaIdAttribute.cs" />
     <Compile Include="ServerNode\CpuData.cs" />

+ 1 - 1
src/NTMinerNoDevFee/VirtualRoot.cs

@@ -26,7 +26,7 @@ namespace NTMiner {
         static void Main(string[] args) {
             HomePath.SetHomeDirFullName(AppDomain.CurrentDomain.BaseDirectory);
             if (args.Length != 0) {
-                if (args.Contains("--sha1")) {
+                if (args.Contains("--sha1", StringComparer.OrdinalIgnoreCase)) {
                     File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sha1"), Sha1);
                     return;
                 }

+ 3 - 2
src/NTMinerDataSchemas/Core/MinerServer/ClientDataComparer.cs → src/NTMinerlib/ClientDataComparer.cs

@@ -1,6 +1,7 @@
-using System.Collections.Generic;
+using NTMiner.Core.MinerServer;
+using System.Collections.Generic;
 
-namespace NTMiner.Core.MinerServer {
+namespace NTMiner {
     public class ClientDataComparer : IComparer<IClientData> {
         private readonly SortDirection _sortDirection;
         private readonly ClientDataSortField _sortField;

+ 1 - 0
src/NTMinerlib/NTMinerlib.csproj

@@ -43,6 +43,7 @@
   <ItemGroup>
     <Compile Include="AppSetting\LocalAppSettingSet.cs" />
     <Compile Include="AppSetting\IAppSettingSet.cs" />
+    <Compile Include="ClientDataComparer.cs" />
     <Compile Include="Cryptography\AESUtil.cs" />
     <Compile Include="Cryptography\BigInteger.cs" />
     <Compile Include="Cryptography\QuickUtil.cs" />

+ 24 - 8
src/NTMinerlib/StringExtensions.cs

@@ -1,16 +1,32 @@
-namespace NTMiner {
+using System;
+
+namespace NTMiner {
     public static class StringExtensions {
         public static bool IgnoreCaseContains(this string source, string key) {
-            if (string.IsNullOrEmpty(source)) {
-                if (string.IsNullOrEmpty(key)) {
-                    return true;
-                }
+            if (source == null && key == null) {
+                return true;
+            }
+            else if(source == null || key == null) {
                 return false;
             }
-            if (string.IsNullOrEmpty(key)) {
-                return true;
+            return source.IndexOf(key, StringComparison.OrdinalIgnoreCase) != -1;
+        }
+
+        public static string IgnoreCaseReplace(this string source, string oldValue, string newValue) {
+            if (string.IsNullOrEmpty(source)) {
+                return source;
+            }
+            if (string.IsNullOrEmpty(oldValue)) {
+                return source;
+            }
+            int startIndex = 0;
+            int index = source.IndexOf(oldValue, startIndex, StringComparison.OrdinalIgnoreCase);
+            while (index != -1) {
+                source = source.Replace(source.Substring(index, oldValue.Length), newValue);
+                startIndex = index + newValue.Length;
+                index = source.IndexOf(oldValue, startIndex, StringComparison.OrdinalIgnoreCase);
             }
-            return source.ToLower().Contains(key.ToLower());
+            return source;
         }
     }
 }

+ 3 - 2
src/NTMinerlib/User/LoginedUserExtensions.cs

@@ -1,4 +1,5 @@
-using System.Linq;
+using System;
+using System.Linq;
 
 namespace NTMiner.User {
     public static class LoginedUserExtensions {
@@ -9,7 +10,7 @@ namespace NTMiner.User {
             if (user.LoginName == Role.RoleEnum.admin.GetName()) {
                 return true;
             }
-            return user.Roles.Split(',').Contains(Role.RoleEnum.admin.GetName());
+            return user.Roles.Split(',').Contains(Role.RoleEnum.admin.GetName(), StringComparer.OrdinalIgnoreCase);
         }
     }
 }

+ 3 - 2
src/UnitTests/LocalMessageTests.cs

@@ -1,5 +1,4 @@
 using Microsoft.VisualStudio.TestTools.UnitTesting;
-using NTMiner;
 using System.Linq;
 
 namespace NTMiner {
@@ -10,11 +9,13 @@ namespace NTMiner {
             VirtualRoot.Execute(new ClearLocalMessageSetCommand());
             int times = 2000;
             Assert.IsTrue(times > NTKeyword.LocalMessageSetCapacity);
+            // 触发LocalMessageSet对AddLocalMessageCommand命令的订阅
+            _ = NTMinerContext.Instance.LocalMessageSet;
             string content = "this is a test";
             for (int i = 0; i < times; i++) {
                 VirtualRoot.ThisLocalInfo(nameof(LocalMessageTests), content);
             }
-            Assert.IsTrue(NTMinerContext.Instance.LocalMessageSet.AsEnumerable().Count() == NTKeyword.LocalMessageSetCapacity);
+            Assert.AreEqual(NTKeyword.LocalMessageSetCapacity, NTMinerContext.Instance.LocalMessageSet.AsEnumerable().Count());
         }
     }
 }

+ 60 - 0
src/UnitTests/RoleTests.cs

@@ -0,0 +1,60 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Linq;
+
+namespace NTMiner {
+    [TestClass]
+    public class RoleTests {
+        [TestMethod]
+        public void StringIgnoreCaseComparerTest() {
+            string left = "Hello";
+            string right = "hello";
+            Assert.IsTrue(StringComparer.OrdinalIgnoreCase.Equals(left, right));
+            left = null;
+            right = "hello";
+            Assert.IsFalse(StringComparer.OrdinalIgnoreCase.Equals(left, right));
+            left = "Hello";
+            right = null;
+            Assert.IsFalse(StringComparer.OrdinalIgnoreCase.Equals(left, right));
+            left = "";
+            right = "hello";
+            Assert.IsFalse(StringComparer.OrdinalIgnoreCase.Equals(left, right));
+        }
+
+        [TestMethod]
+        public void StringContainsTest() {
+            Assert.IsTrue("Admin".IgnoreCaseContains("admin"));
+        }
+
+        [TestMethod]
+        public void LinqContainsTest() {
+            string roles = "admin";
+            Assert.IsTrue(roles.Split(',').Contains(Role.RoleEnum.admin.ToString(), StringComparer.OrdinalIgnoreCase));
+            roles = "Admin";
+            Assert.IsTrue(roles.Split(',').Contains(Role.RoleEnum.admin.ToString(), StringComparer.OrdinalIgnoreCase));
+            roles = "Admin,user";
+            Assert.IsTrue(roles.Split(',').Contains(Role.RoleEnum.admin.ToString(), StringComparer.OrdinalIgnoreCase));
+            roles = "user";
+            Assert.IsFalse(roles.Split(',').Contains(Role.RoleEnum.admin.ToString(), StringComparer.OrdinalIgnoreCase));
+        }
+
+        [TestMethod]
+        [ExpectedException(typeof(ArgumentException), "Replace的oldValue参数不能为空")]
+        public void ReplaceTest() {
+            string roles = "Admin,user";
+            Assert.AreEqual(roles, roles.Replace(string.Empty, "hello"));
+        }
+
+        [TestMethod]
+        public void IgnoreCaseReplaceTest() {
+            string roles = "Admin,user";
+            Assert.AreEqual("user", roles.IgnoreCaseReplace("admin,", string.Empty));
+            roles = "Admin,user";
+            Assert.AreEqual("admin,user", roles.IgnoreCaseReplace("admin,", "admin,"));
+            roles = "Admin,user";
+            Assert.AreEqual("admin,user", roles.IgnoreCaseReplace("Admin,", "admin,"));
+            roles = "Admin,user";
+            Assert.AreEqual(roles, roles.IgnoreCaseReplace(string.Empty, "admin,"));
+        }
+    }
+}

+ 1 - 0
src/UnitTests/UnitTests.csproj

@@ -94,6 +94,7 @@
     <Compile Include="CryptographyTests.cs" />
     <Compile Include="RandomTests.cs" />
     <Compile Include="ReflectionTests.cs" />
+    <Compile Include="RoleTests.cs" />
     <Compile Include="SecureStringTests.cs" />
     <Compile Include="StackTraceTests.cs" />
     <Compile Include="UnitTest1.cs" />

+ 8 - 8
src/WebApiServer/Core/Impl/UserSet.cs

@@ -58,13 +58,13 @@ namespace NTMiner.Core.Impl {
                     break;
             }
             if (!string.IsNullOrEmpty(query.Email)) {
-                data = data.Where(a => !string.IsNullOrEmpty(a.Email) && a.Email.Contains(query.Email));
+                data = data.Where(a => !string.IsNullOrEmpty(a.Email) && a.Email.IgnoreCaseContains(query.Email));
             }
             if (!string.IsNullOrEmpty(query.Mobile)) {
                 data = data.Where(a => !string.IsNullOrEmpty(a.Mobile) && a.Mobile.Contains(query.Mobile));
             }
             if (!string.IsNullOrEmpty(query.Role)) {
-                data = data.Where(a => !string.IsNullOrEmpty(a.Roles) && a.Roles.Contains(query.Role));
+                data = data.Where(a => !string.IsNullOrEmpty(a.Roles) && a.Roles.IgnoreCaseContains(query.Role));
             }
             total = data.Count();
             var results = data.OrderBy(a => a.LoginName).Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).ToList();
@@ -91,14 +91,14 @@ namespace NTMiner.Core.Impl {
             if (string.IsNullOrEmpty(user.Roles)) {
                 return;
             }
-            else if (user.Roles.Contains("," + role.GetName())) {
-                user.Roles = user.Roles.Replace("," + role.GetName(), string.Empty);
+            else if (user.Roles.IgnoreCaseContains("," + role.GetName())) {
+                user.Roles = user.Roles.IgnoreCaseReplace("," + role.GetName(), string.Empty);
             }
-            else if (user.Roles.Contains(role.GetName() + ",")) {
-                user.Roles = user.Roles.Replace(role.GetName() + ",", string.Empty);
+            else if (user.Roles.IgnoreCaseContains(role.GetName() + ",")) {
+                user.Roles = user.Roles.IgnoreCaseReplace(role.GetName() + ",", string.Empty);
             }
-            else if (user.Roles.Contains(role.GetName())) {
-                user.Roles = user.Roles.Replace(role.GetName(), string.Empty);
+            else if (user.Roles.IgnoreCaseContains(role.GetName())) {
+                user.Roles = user.Roles.IgnoreCaseReplace(role.GetName(), string.Empty);
             }
         }