PageRenderTime 66ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/NuGetGallery.Facts/EmailValidationRegex.cs

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