1
0

SimpleCensor.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Text.Unicode;
  2. using Content.Shared.Chat.V2.Moderation;
  3. using NUnit.Framework;
  4. namespace Content.Tests.Shared.Chat.V2.Moderation;
  5. public sealed class SimpleCensorTests
  6. {
  7. [Test]
  8. public void CanCensorASingleWord()
  9. {
  10. var sut = new SimpleCensor().WithCustomDictionary(["amogus"]);
  11. var output = sut.Censor("hello amogus");
  12. Assert.That(output, Is.EqualTo("hello ******"));
  13. }
  14. // Basics - use custom dictionary
  15. [Test]
  16. public void CanCensorMultipleWordInstances()
  17. {
  18. var sut= new SimpleCensor().WithCustomDictionary(["amogus"]);
  19. var output = sut.Censor("amogus hello amogus");
  20. Assert.That(output, Is.EqualTo("****** hello ******"));
  21. }
  22. [Test]
  23. public void CanCensorMultipleWords()
  24. {
  25. var sut= new SimpleCensor().WithCustomDictionary(["amogus", "sus"]);
  26. var output = sut.Censor("amogus hello sus");
  27. Assert.That(output, Is.EqualTo("****** hello ***"));
  28. }
  29. [Test]
  30. public void CanUseDifferentCensorSymbols()
  31. {
  32. var sut= new SimpleCensor().WithCustomDictionary(["amogus", "sus"]);
  33. var output = sut.Censor("amogus hello sus", '#');
  34. Assert.That(output, Is.EqualTo("###### hello ###"));
  35. }
  36. [Test]
  37. public void CanCatchCapitalizedWords()
  38. {
  39. var sut= new SimpleCensor().WithCustomDictionary(["amogus", "sus"]);
  40. var output = sut.Censor("AMOGUS hello SUS");
  41. Assert.That(output, Is.EqualTo("****** hello ***"));
  42. }
  43. [Test]
  44. public void CanCatchWordsWithSomeCaptialsInThem()
  45. {
  46. var sut= new SimpleCensor().WithCustomDictionary(["amogus", "sus"]);
  47. var output = sut.Censor("AmoGuS hello SuS");
  48. Assert.That(output, Is.EqualTo("****** hello ***"));
  49. }
  50. [Test]
  51. public void CanCatchWordsHiddenInsideOtherWords()
  52. {
  53. var sut= new SimpleCensor().WithCustomDictionary(["amogus", "sus"]);
  54. var output = sut.Censor("helamoguslo suspicious");
  55. Assert.That(output, Is.EqualTo("hel******lo ***picious"));
  56. }
  57. // Sanitizing Leetspeak
  58. [Test]
  59. public void CanSanitizeLeetspeak()
  60. {
  61. var sut = new SimpleCensor().WithCustomDictionary(["amogus", "sus"]).WithSanitizeLeetSpeak();
  62. var output = sut.Censor("am0gu5 hello 5u5");
  63. Assert.That(output, Is.EqualTo("****** hello ***"));
  64. }
  65. [Test]
  66. public void SanitizingLeetspeakOnlyOccursWhenTheWordIsBlocked()
  67. {
  68. var sut = new SimpleCensor().WithCustomDictionary(["amogus", "sus"]).WithSanitizeLeetSpeak();
  69. var output = sut.Censor("he110");
  70. Assert.That(output, Is.EqualTo("he110"));
  71. }
  72. [Test]
  73. public void CanCatchLeetspeakReplacementsWithMoreThanOneLetter()
  74. {
  75. var sut = new SimpleCensor().WithCustomDictionary(["amogus", "sus"]).WithSanitizeLeetSpeak();
  76. var output = sut.Censor("am()gu5 hello 5u5");
  77. Assert.That(output, Is.EqualTo("******* hello ***"));
  78. }
  79. // Sanitizing special characters
  80. [Test]
  81. public void DoesNotSanitizeOutUncensoredSpecialCharacters()
  82. {
  83. var sut = new SimpleCensor().WithCustomDictionary(["amogus", "sus"]).WithSanitizeSpecialCharacters();
  84. var output = sut.Censor("amogus!hello!sus");
  85. Assert.That(output, Is.EqualTo("******!hello!***"));
  86. }
  87. [Test]
  88. public void DoesSanitizeOutCensoredSpecialCharacters()
  89. {
  90. var sut = new SimpleCensor().WithCustomDictionary(["amogus", "sus"]).WithSanitizeSpecialCharacters();
  91. var output = sut.Censor("amo!gus hello s?us");
  92. Assert.That(output, Is.EqualTo("***!*** hello *?**"));
  93. }
  94. // Unicode ranges
  95. [Test]
  96. public void SanitizesOutNonLatinCharaters()
  97. {
  98. var sut = new SimpleCensor().WithRanges([UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement]);
  99. var output = sut.Censor("amogus Україна sus 日本");
  100. Assert.That(output, Is.EqualTo("amogus sus "));
  101. }
  102. [Test]
  103. public void SanitizesOutNonLatinOrCyrillicCharaters()
  104. {
  105. var sut = new SimpleCensor().WithRanges([UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement, UnicodeRanges.Cyrillic]);
  106. var output = sut.Censor("amogus Україна sus 日本");
  107. Assert.That(output, Is.EqualTo("amogus Україна sus "));
  108. }
  109. // False positives
  110. [Test]
  111. public void CanHandleFalsePositives()
  112. {
  113. var sut = new SimpleCensor().WithCustomDictionary(["amogus", "sus"]).WithFalsePositives(["amogusus"]);
  114. var output = sut.Censor("amogusus hello amogus hello sus");
  115. Assert.That(output, Is.EqualTo("amogusus hello ****** hello ***"));
  116. }
  117. // False negatives
  118. [Test]
  119. public void CanHandleFalseNegatives()
  120. {
  121. var sut = new SimpleCensor().WithCustomDictionary(["amogus", "sus"]).WithFalsePositives(["amogusus"]).WithFalseNegatives(["susamogusus"]);
  122. var output = sut.Censor("susamogusus hello amogus hello sus amogusus");
  123. Assert.That(output, Is.EqualTo("*********** hello ****** hello *** ********"));
  124. }
  125. }