Skip to content

Commit

Permalink
fix off by one error in activity log nil padding for month data (#15731)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hridoy Roy authored Jun 1, 2022
1 parent df79e2c commit 2a8a8a8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vault/activity_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,9 @@ func modifyResponseMonths(months []*ResponseMonth, start time.Time, end time.Tim
return months
}
start = timeutil.StartOfMonth(start)
if timeutil.IsCurrentMonth(end, time.Now().UTC()) {
end = timeutil.StartOfMonth(end).AddDate(0, -1, 0)
}
end = timeutil.EndOfMonth(end)
modifiedResponseMonths := make([]*ResponseMonth, 0)
firstMonth, err := time.Parse(time.RFC3339, months[0].Timestamp)
Expand All @@ -1753,6 +1756,10 @@ func modifyResponseMonths(months []*ResponseMonth, start time.Time, end time.Tim
lastMonth = timeutil.StartOfMonth(lastMonth.AddDate(0, 1, 0))
monthPlaceholder := &ResponseMonth{Timestamp: lastMonth.UTC().Format(time.RFC3339)}
modifiedResponseMonths = append(modifiedResponseMonths, monthPlaceholder)

// reset lastMonth to be the end of the month so we can make an apt comparison
// in the next loop iteration
lastMonth = timeutil.EndOfMonth(lastMonth)
}
return modifiedResponseMonths
}
Expand Down
30 changes: 30 additions & 0 deletions vault/activity_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,36 @@ func TestActivityLog_SaveEntitiesToStorage(t *testing.T) {
expectedEntityIDs(t, out, ids)
}

func TestModifyResponseMonthsNilAppend(t *testing.T) {
end := time.Now().UTC()
start := timeutil.StartOfMonth(end).AddDate(0, -5, 0)
responseMonthTimestamp := timeutil.StartOfMonth(end).AddDate(0, -3, 0).Format(time.RFC3339)
responseMonths := []*ResponseMonth{{Timestamp: responseMonthTimestamp}}
months := modifyResponseMonths(responseMonths, start, end)
if len(months) != 5 {
t.Fatal("wrong number of months padded")
}
for _, m := range months {
ts, err := time.Parse(time.RFC3339, m.Timestamp)
if err != nil {
t.Fatal(err)
}
if !ts.Equal(start) {
t.Fatalf("incorrect time in month sequence timestamps: expected %+v, got %+v", start, ts)
}
start = timeutil.StartOfMonth(start).AddDate(0, 1, 0)
}
// The following is a redundant check, but for posterity and readability I've
// made it explicit.
lastMonth, err := time.Parse(time.RFC3339, months[4].Timestamp)
if err != nil {
t.Fatal(err)
}
if timeutil.IsCurrentMonth(lastMonth, time.Now().UTC()) {
t.Fatalf("do not include current month timestamp in nil padding for months")
}
}

func TestActivityLog_ReceivedFragment(t *testing.T) {
core, _, _ := TestCoreUnsealed(t)
a := core.activityLog
Expand Down

0 comments on commit 2a8a8a8

Please sign in to comment.