/tests/NuGetGallery.Facts/EmailValidationRegex.cs

https://github.com/ferventcoder/NuGetGallery · C# · 90 lines · 77 code · 6 blank · 7 comment · 0 complexity · 0d5a70285ff9ee04c6f3450c6e0340e3 MD5 · raw file

  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System.Text.RegularExpressions;
  4. using Xunit;
  5. using Xunit.Extensions;
  6. namespace NuGetGallery
  7. {
  8. /// <summary>
  9. /// Notes, we do not follow strictly the RFCs at this time, and we choose not to support many obscure email address variants,
  10. /// such as those with quotes and parentheses.
  11. /// We may add international character support in the function.
  12. /// </summary>
  13. public class EmailValidationRegex
  14. {
  15. [Theory]
  16. [InlineData("fred@fred.com")]
  17. [InlineData("a@b.co")]
  18. [InlineData("a@b.c.d.e.f")]
  19. public void TheWholeAllows(string address)
  20. {
  21. var match = new Regex(RegisterViewModel.EmailValidationRegex).IsMatch(address);
  22. Assert.True(match);
  23. }
  24. [Theory]
  25. [InlineData("fred@@fred.com")]
  26. [InlineData("fred@fred@fred.com")]
  27. [InlineData("fred@.com")]
  28. public void TheWholeDoesntAllow(string testWhole)
  29. {
  30. var match = new Regex(RegisterViewModel.EmailValidationRegex).IsMatch(testWhole);
  31. Assert.False(match);
  32. }
  33. [Theory]
  34. [InlineData("fred")]
  35. [InlineData(".fred")]
  36. [InlineData("fred.")]
  37. [InlineData("fr.ed")]
  38. [InlineData("fr..ed")]
  39. [InlineData("!#$%&'*+-/=?^_`{}|~")] // thanks Wikipedia
  40. [InlineData("fred~`'.baz")]
  41. public void TheFirstPartMatches(string testFirstPart)
  42. {
  43. var match = new Regex("^" + RegisterViewModel.FirstPart + "$").IsMatch(testFirstPart);
  44. Assert.True(match);
  45. }
  46. [Theory]
  47. [InlineData("fr@ed")]
  48. [InlineData("fr\\ed")]
  49. [InlineData("fr\"ed")]
  50. [InlineData("fr()ed")]
  51. [InlineData("fr[]ed")]
  52. [InlineData("abc\"defghi\"xyz")] // thanks Wikipedia
  53. [InlineData("abc.\"defghi\".xyz")] // thanks Wikipedia, but in practice nobody uses these email addresses.
  54. [InlineData("abc.\"def\\\"\"ghi\".xyz")] // thanks Wikipedia, but in practice nobody uses these email addresses.
  55. public void TheFirstPartDoesntAllow(string testFirstPart)
  56. {
  57. var match = new Regex("^" + RegisterViewModel.FirstPart + "$").IsMatch(testFirstPart);
  58. Assert.False(match);
  59. }
  60. [Theory]
  61. [InlineData("XYZ.com")]
  62. [InlineData("xyz.govt.nz")]
  63. [InlineData("X1-Y2-Z3.net")]
  64. [InlineData("b.co")]
  65. [InlineData("b.co.uk")]
  66. [InlineData("a.b.c.d.e.f")]
  67. public void TheSecondPartMatches(string testSecondPart)
  68. {
  69. var match = new Regex("^" + RegisterViewModel.SecondPart + "$").IsMatch(testSecondPart);
  70. Assert.True(match);
  71. }
  72. [Theory]
  73. [InlineData(".com")] //no top level domains
  74. [InlineData("com")] //no top level domains
  75. [InlineData("mailserver1")] //no hostname without top level domain
  76. [InlineData("[1.1.1.1]")] //no IP addresses
  77. [InlineData("[IPv6:2001:db8:1ff::a0b:dbd0]")] //no IP addresses
  78. public void TheSecondPartDoesntAllow(string testSecondPart)
  79. {
  80. var match = new Regex("^" + RegisterViewModel.SecondPart + "$").IsMatch(testSecondPart);
  81. Assert.False(match);
  82. }
  83. }
  84. }