-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from mikesimons/finer-expiry
Adds expiry parser with ability to specify hours, days, months and years
- Loading branch information
Showing
11 changed files
with
204 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
var nowFunc = time.Now | ||
|
||
func parseExpiry(fromNow string) (time.Time, error) { | ||
now := nowFunc().UTC() | ||
re := regexp.MustCompile("\\s*(\\d+)\\s*(day|month|year|hour)s?") | ||
matches := re.FindAllStringSubmatch(fromNow, -1) | ||
addDate := map[string]int{ | ||
"day": 0, | ||
"month": 0, | ||
"year": 0, | ||
"hour": 0, | ||
} | ||
for _, r := range matches { | ||
number, err := strconv.ParseInt(r[1], 10, 32) | ||
if err != nil { | ||
return now, err | ||
} | ||
addDate[r[2]] = int(number) | ||
} | ||
|
||
// Ensure that we do not overflow time.Duration. | ||
// Doing so is silent and causes signed integer overflow like issues. | ||
if _, err := time.ParseDuration(fmt.Sprintf("%dh", addDate["hour"])); err != nil { | ||
return now, fmt.Errorf("hour unit too large to process") | ||
} | ||
|
||
result := now. | ||
AddDate(addDate["year"], addDate["month"], addDate["day"]). | ||
Add(time.Duration(addDate["hour"]) * time.Hour) | ||
|
||
if now == result { | ||
return now, fmt.Errorf("invalid or empty format") | ||
} | ||
|
||
// ASN.1 (encoding format used by SSL) only supports up to year 9999 | ||
// https://www.openssl.org/docs/man1.1.0/crypto/ASN1_TIME_check.html | ||
if result.Year() > 9999 { | ||
return now, fmt.Errorf("proposed date too far in to the future: %s. Expiry year must be less than or equal to 9999", result) | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const dateFormat = "2006-01-02" | ||
|
||
func init() { | ||
nowFunc = func() time.Time { | ||
t, _ := time.Parse(dateFormat, "2017-01-01") | ||
return t | ||
} | ||
} | ||
|
||
func TestParseExpiryWithDays(t *testing.T) { | ||
t1, _ := parseExpiry("1 day") | ||
t2, _ := parseExpiry("1 days") | ||
expected, _ := time.Parse(dateFormat, "2017-01-02") | ||
|
||
if t1 != expected { | ||
t.Fatalf("Parsing expiry 1 day from now (singular) did not return expected value (wanted: %s, got: %s)", expected, t1) | ||
} | ||
|
||
if t2 != expected { | ||
t.Fatalf("Parsing expiry 1 day from now (plural) did not return expected value (wanted: %s, got: %s)", expected, t2) | ||
} | ||
} | ||
|
||
func TestParseExpiryWithMonths(t *testing.T) { | ||
t1, _ := parseExpiry("1 month") | ||
t2, _ := parseExpiry("1 months") | ||
expected, _ := time.Parse(dateFormat, "2017-02-01") | ||
|
||
if t1 != expected { | ||
t.Fatalf("Parsing expiry 1 month from now (singular) did not return expected value (wanted: %s, got: %s)", expected, t1) | ||
} | ||
|
||
if t2 != expected { | ||
t.Fatalf("Parsing expiry 1 month from now (plural) did not return expected value (wanted: %s, got: %s)", expected, t2) | ||
} | ||
} | ||
|
||
func TestParseExpiryWithYears(t *testing.T) { | ||
t1, _ := parseExpiry("1 year") | ||
t2, _ := parseExpiry("1 years") | ||
expected, _ := time.Parse(dateFormat, "2018-01-01") | ||
|
||
if t1 != expected { | ||
t.Fatalf("Parsing expiry 1 year from now (singular) did not return expected value (wanted: %s, got: %s)", expected, t1) | ||
} | ||
|
||
if t2 != expected { | ||
t.Fatalf("Parsing expiry 1 year from now (plural) did not return expected value (wanted: %s, got: %s)", expected, t2) | ||
} | ||
} | ||
|
||
func TestParseExpiryWithMixed(t *testing.T) { | ||
t1, _ := parseExpiry("2 days 3 months 1 year") | ||
t2, _ := parseExpiry("5 years 5 days 6 months") | ||
expectedt1, _ := time.Parse(dateFormat, "2018-04-03") | ||
expectedt2, _ := time.Parse(dateFormat, "2022-07-06") | ||
|
||
if t1 != expectedt1 { | ||
t.Fatalf("Parsing expiry for mixed format t1 did not return expected value (wanted: %s, got: %s)", expectedt1, t1) | ||
} | ||
|
||
if t2 != expectedt2 { | ||
t.Fatalf("Parsing expiry for mixed format t2 did not return expected value (wanted: %s, got: %s)", expectedt2, t2) | ||
} | ||
} | ||
|
||
func TestParseInvalidExpiry(t *testing.T) { | ||
errorTime := onlyTime(time.Parse(dateFormat, "2017-01-01")) | ||
cases := []struct { | ||
Input string | ||
Expected time.Time | ||
ExpectedErr string | ||
}{ | ||
{"53257284647843897", errorTime, "invalid or empty format"}, | ||
{"5y", errorTime, "invalid or empty format"}, | ||
{"53257284647843897 days", errorTime, ".*value out of range"}, | ||
{"2147483647 hours", errorTime, ".*hour unit too large.*"}, | ||
{"2147483647 days", errorTime, ".*proposed date too far in to the future.*"}, | ||
} | ||
|
||
for _, c := range cases { | ||
result, err := parseExpiry(c.Input) | ||
if result != c.Expected { | ||
t.Fatalf("Invalid expiry '%s' did not have expected value (wanted: %s, got: %s)", c.Input, c.Expected, result) | ||
} | ||
|
||
if match, _ := regexp.MatchString(c.ExpectedErr, fmt.Sprintf("%s", err)); !match { | ||
t.Fatalf("Invalid expiry '%s' did not have expected error (wanted: %s, got: %s)", c.Input, c.ExpectedErr, err) | ||
} | ||
} | ||
} | ||
|
||
func onlyTime(a time.Time, b error) time.Time { | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters