Skip to content

Commit

Permalink
Validate zone argument in GetNextOccurrence
Browse files Browse the repository at this point in the history
  • Loading branch information
odinserj committed Dec 26, 2023
1 parent d2978f4 commit eb6fa12
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Cronos/CronExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ public IEnumerable<DateTime> GetOccurrences(
{
if (fromUtc.Kind != DateTimeKind.Utc) ThrowWrongDateTimeKindException(nameof(fromUtc));
if (fromUtc.Year > MaxYear) ThrowDateTimeExceedsMaxException(nameof(fromUtc));
if (zone == null) ThrowArgumentNullException(nameof(zone));

if (ReferenceEquals(zone, UtcTimeZone))
{
Expand Down Expand Up @@ -283,6 +284,7 @@ public IEnumerable<DateTime> GetOccurrences(
public DateTimeOffset? GetNextOccurrence(DateTimeOffset from, TimeZoneInfo zone, bool inclusive = false)
{
if (from.Year > MaxYear) ThrowDateTimeExceedsMaxException(nameof(from));
if (zone == null) ThrowArgumentNullException(nameof(zone));

if (ReferenceEquals(zone, UtcTimeZone))
{
Expand Down Expand Up @@ -655,6 +657,12 @@ private static void ThrowDateTimeExceedsMaxException(string paramName)
throw new ArgumentException($"The supplied DateTime is after the supported year of {MaxYear}.", paramName);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentNullException(string paramName)
{
throw new ArgumentNullException(paramName);
}

private static bool GetBit(long value, int index)
{
return (value & (1L << index)) != 0;
Expand Down
18 changes: 18 additions & 0 deletions tests/Cronos.Tests/CronExpressionFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,15 @@ public void GetNextOccurrence_DateTimeTimeZone_ThrowsAnException_WhenFromGreater
Assert.Equal("fromUtc", exception.ParamName);
}

[Fact]
public void GetNextOccurrence_DateTimeTimeZone_ThrowsAnException_WhenZoneIsNull()
{
var exception = Assert.Throws<ArgumentNullException>(
() => CronExpression.EveryMinute.GetNextOccurrence(Today, null));

Assert.Equal("zone", exception.ParamName);
}

[Fact]
public void GetNextOccurrence_DateTimeOffset_ThrowsAnException_WhenToGreaterThanMaxYear()
{
Expand All @@ -601,6 +610,15 @@ public void GetNextOccurrence_DateTimeOffset_ThrowsAnException_WhenToGreaterThan
Assert.Equal("from", exception.ParamName);
}

[Fact]
public void GetNextOccurrence_DateTimeOffsetTimeZone_ThrowsAnException_WhenZoneIsNull()
{
var exception = Assert.Throws<ArgumentNullException>(
() => CronExpression.EveryMinute.GetNextOccurrence(new DateTimeOffset(Today), null));

Assert.Equal("zone", exception.ParamName);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand Down

0 comments on commit eb6fa12

Please sign in to comment.