Skip to content

Commit 4f71eff

Browse files
committed
fixed following review
1 parent 0b103fd commit 4f71eff

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

reporthandling/attacktrack/v1alpha1/attacktrackmethods.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ func (handler *AttackTrackAllPathsHandler) CalculatePathsRootToLeaf() [][]IAttac
192192
currentPath = append(currentPath, step)
193193
}
194194

195-
if step.Length() == 0 {
195+
if step.IsLeaf() {
196196
// Reached a leaf node
197-
if len(step.GetControls()) > 0 {
197+
if step.IsPartOfAttackTrackPath() {
198198
// Add current path to paths only if controls are not empty
199199
path := make([]IAttackTrackStep, len(currentPath))
200200
copy(path, currentPath)
@@ -213,7 +213,7 @@ func (handler *AttackTrackAllPathsHandler) CalculatePathsRootToLeaf() [][]IAttac
213213
handler.visited[subStep.GetName()] = true
214214

215215
// Only include nodes with controls in the path
216-
if len(subStep.GetControls()) > 0 {
216+
if step.IsPartOfAttackTrackPath() {
217217
traverse(subStep)
218218
traversedSubstepWithControls = true
219219
} else {
@@ -225,20 +225,24 @@ func (handler *AttackTrackAllPathsHandler) CalculatePathsRootToLeaf() [][]IAttac
225225
}
226226

227227
// Add the current path to paths only if it ends with a leaf node with controls
228-
if len(step.GetControls()) > 0 && !traversedSubstepWithControls && step.Length() == 0 {
228+
if step.IsPartOfAttackTrackPath() && !traversedSubstepWithControls && step.IsLeaf() {
229229
path := make([]IAttackTrackStep, len(currentPath))
230230
copy(path, currentPath)
231231
paths = append(paths, path)
232232
}
233233
}
234234

235-
if len(step.GetControls()) > 0 {
235+
if step.IsPartOfAttackTrackPath() {
236236
currentPath = currentPath[:len(currentPath)-1] // Remove last step from current path in order to explore other paths
237237
}
238238
}
239239

240240
traverse(handler.attackTrack.GetData())
241241

242+
if len(paths) == 0 {
243+
return nil
244+
}
245+
242246
return paths
243247
}
244248

reporthandling/attacktrack/v1alpha1/attacktrackmethods_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,12 @@ func TestCalculatePathsRootToLeaf(t *testing.T) {
406406
t.Run(tt.name, func(t *testing.T) {
407407
handler := NewAttackTrackAllPathsHandler(tt.attackTrack, &tt.controlsMap)
408408
paths := handler.CalculatePathsRootToLeaf()
409-
// newAttackTrack := handler.GenerateAttackTrackFromPaths(paths)
410409

411-
if !(tt.want == nil && paths == nil) {
412-
assert.Equalf(t, len(tt.want), len(paths), "CalculatePathsToLeaf should return the correct number of paths. expected: %v, actual: %v", len(tt.want), len(paths))
410+
if tt.want != nil || paths != nil {
411+
assert.Equalf(t, len(tt.want), len(paths), "CalculatePathsRootToLeaf should return the correct number of paths. expected: %v, actual: %v", len(tt.want), len(paths))
413412
for i, path := range paths {
414413
for j, step := range path {
415-
assert.Equalf(t, tt.want[i][j], step.GetName(), "CalculatePathsToLeaf should return the correct paths. expected: %v, actual: %v", tt.want, paths)
414+
assert.Equalf(t, tt.want[i][j], step.GetName(), "CalculatePathsRootToLeaf should return the correct paths. expected: %v, actual: %v", tt.want, paths)
416415
}
417416
}
418417

@@ -681,7 +680,6 @@ func TestFilterNodesWithControls(t *testing.T) {
681680
result := handler.filterNodesWithControls(handler.attackTrack.GetData(), pathsCopy)
682681

683682
if !(result == nil && tc.expectedResult == nil) && result.Equal(tc.expectedResult, true) == false {
684-
// if !compareAttackTrackStep(result, tc.expectedResult) {
685683
t.Errorf("Unexpected result.\nExpected: %+v\nGot: %+v", tc.expectedResult, result)
686684
}
687685
})

reporthandling/attacktrack/v1alpha1/attacktrackmocks.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func (s AttackTrackStepMock) SubStepAt(index int) IAttackTrackStep {
6464
return s.SubSteps[index]
6565
}
6666

67+
func (s AttackTrackStepMock) IsLeaf() bool {
68+
return len(s.SubSteps) == 0
69+
}
70+
6771
func (a AttackTrackStepMock) IsPartOfAttackTrackPath() bool {
6872
return len(a.Controls) > 0
6973
}

reporthandling/attacktrack/v1alpha1/attacktrackstepmethods.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func (step *AttackTrackStep) SubStepAt(index int) IAttackTrackStep {
2929
return &step.SubSteps[index]
3030
}
3131

32+
func (step *AttackTrackStep) IsLeaf() bool {
33+
return step.Length() == 0
34+
}
35+
3236
// Equal checks if the given attack track step is equal to the current one
3337
// If compareControls is true, the controls are also compared
3438
func (s *AttackTrackStep) Equal(other *AttackTrackStep, compareControls bool) bool {
@@ -50,7 +54,7 @@ func (s *AttackTrackStep) Equal(other *AttackTrackStep, compareControls bool) bo
5054

5155
for i := range s.Controls {
5256

53-
if !(s.Controls[i] == other.Controls[i]) {
57+
if s.Controls[i] != other.Controls[i] {
5458
return false
5559
}
5660
}

reporthandling/attacktrack/v1alpha1/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type IAttackTrackStep interface {
2020
Length() int // returns the number of sub steps
2121
SubStepAt(index int) IAttackTrackStep // returns a sub step at the given index
2222
IsPartOfAttackTrackPath() bool // checks if the step can be a part of an attack track path
23+
IsLeaf() bool // checks if the step is a leaf node
2324
}
2425

2526
// A control related to an attack track step

0 commit comments

Comments
 (0)