Skip to content

Commit ac0d090

Browse files
committed
config: treat BufferWindowDays as int and validate range
Signed-off-by: S m, Aruna <arun.s.m.cse@gmail.com>
1 parent c9ed3f5 commit ac0d090

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

cmd/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ func main() {
5959
log.Printf("current: %v requested: %v", currentTime, requestedTime)
6060
log.Printf("difference: %v configured: %v", numberOfDays, config.BufferWindowDays)
6161

62-
// found a case where action is needed
63-
if uint8(numberOfDays) == config.BufferWindowDays {
62+
// found a case where action is needed; both sides are ints now so
63+
// the comparison is straightforward and cannot wrap around.
64+
if numberOfDays == config.BufferWindowDays {
6465
// do as scheduled on this date
6566
projectInstance := client.GetProject(project)
6667
errInfo := projectInstance.CreateIssue(schedule)

internal/pkg/configs/configuration.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package configs
22

33
import (
4+
"fmt"
45
"github-issue-schedule/internal/pkg/utils"
56
"io/ioutil"
67
"log"
@@ -10,7 +11,12 @@ import (
1011

1112
// Configuration to run the program
1213
type Configuration struct {
13-
BufferWindowDays uint8 `yaml:"buffer_window_days"`
14+
// BufferWindowDays indicates how many days in advance a reminder
15+
// issue should be created. Use a signed integer so configs can
16+
// express reasonable values without worrying about overflow when
17+
// comparing against days-calculated-from-the-clock. Negative values
18+
// are invalid and the reader enforces this.
19+
BufferWindowDays int `yaml:"buffer_window_days"`
1420
Projects []Project `yaml:"projects"`
1521
}
1622

@@ -44,5 +50,23 @@ func ReadConfiguration() Configuration {
4450
if err != nil {
4551
log.Fatalf("Error while parsing the config file %v, Err: %v", configFile, err)
4652
}
53+
54+
if err := config.validate(); err != nil {
55+
log.Fatalf("invalid configuration: %v", err)
56+
}
57+
4758
return config
4859
}
60+
61+
// validate checks that fields in Configuration contain reasonable
62+
// values. callers may use this directly in tests; ReadConfiguration
63+
// itself fatals on any error.
64+
func (c *Configuration) validate() error {
65+
if c.BufferWindowDays < 0 {
66+
return fmt.Errorf("buffer_window_days must be non-negative")
67+
}
68+
if c.BufferWindowDays > 100000 {
69+
return fmt.Errorf("buffer_window_days is unreasonably large (%d)", c.BufferWindowDays)
70+
}
71+
return nil
72+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package configs
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestConfigurationValidate(t *testing.T) {
8+
cases := []struct {
9+
desc string
10+
conf Configuration
11+
broken bool
12+
}{
13+
{"negative", Configuration{BufferWindowDays: -1}, true},
14+
{"too large", Configuration{BufferWindowDays: 100001}, true},
15+
{"zero", Configuration{BufferWindowDays: 0}, false},
16+
{"normal", Configuration{BufferWindowDays: 7}, false},
17+
}
18+
19+
for _, c := range cases {
20+
t.Run(c.desc, func(t *testing.T) {
21+
err := c.conf.validate()
22+
if c.broken && err == nil {
23+
t.Fatalf("expected error for config %+v", c.conf)
24+
}
25+
if !c.broken && err != nil {
26+
t.Fatalf("did not expect error: %v", err)
27+
}
28+
})
29+
}
30+
}

0 commit comments

Comments
 (0)