Skip to content

Commit

Permalink
Merge pull request #50 from ddosify/step_id_uniqueness
Browse files Browse the repository at this point in the history
step id uniqueness check
  • Loading branch information
kursataktas authored Feb 21, 2022
2 parents 867beef + b0068a0 commit a5a4619
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
31 changes: 29 additions & 2 deletions core/types/hammer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestHammerEmptyScenarioItemID(t *testing.T) {
},
}
if err := h.Validate(); err == nil {
t.Errorf("TestHammerInvalidScenarioItemID errored")
t.Errorf("1- TestHammerEmptyScenarioItemID should be errored")
}

// Multi Scenario
Expand All @@ -254,7 +254,34 @@ func TestHammerEmptyScenarioItemID(t *testing.T) {
},
}
if err := h.Validate(); err == nil {
t.Errorf("TestHammerInvalidScenarioItemID errored")
t.Errorf("2- TestHammerEmptyScenarioItemID should be errored")
}
}

func TestHammerDuplicateScenarioItemID(t *testing.T) {
// Single Scenario
h := newDummyHammer()
h.Scenario = Scenario{
Scenario: []ScenarioItem{
{
ID: 1,
Protocol: SupportedProtocols[0],
Method: supportedProtocolMethods["HTTP"][1],
},
{
ID: 2,
Protocol: SupportedProtocols[0],
Method: supportedProtocolMethods["HTTP"][1],
},
{
ID: 2,
Protocol: SupportedProtocols[0],
Method: supportedProtocolMethods["HTTP"][1],
},
},
}
if err := h.Validate(); err == nil {
t.Errorf("TestHammerDuplicateScenarioItemID should be errored")
}
}

Expand Down
8 changes: 7 additions & 1 deletion core/types/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,16 @@ type Scenario struct {
}

func (s *Scenario) validate() error {
stepIds := make(map[int16]struct{}, len(s.Scenario))
for _, si := range s.Scenario {
if err := si.validate(); err != nil {
return err
}

if _, ok := stepIds[si.ID]; ok {
return fmt.Errorf("duplicate step id: %d", si.ID)
}
stepIds[si.ID] = struct{}{}
}
return nil
}
Expand Down Expand Up @@ -132,7 +138,7 @@ func (si *ScenarioItem) validate() error {
return fmt.Errorf("unsupported Authentication Method (%s) For Protocol (%s) ", si.Auth.Type, si.Protocol)
}
if si.ID == 0 {
return fmt.Errorf("each scenario item should have an unique ID")
return fmt.Errorf("step ID should be greater than zero")
}
if si.Sleep != "" {
sleep := strings.Split(si.Sleep, "-")
Expand Down

0 comments on commit a5a4619

Please sign in to comment.