github.com/DataDog/datadog-agent/pkg/security/secl@v0.55.0-devel.0.20240517055856-10c4965fea94/model/process_cache_entry_unix_test.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the Apache License Version 2.0.
     3  // This product includes software developed at Datadog (https://www.datadoghq.com/).
     4  // Copyright 2016-present Datadog, Inc.
     5  
     6  //go:build unix
     7  
     8  // Package model holds model related files
     9  package model
    10  
    11  import (
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestHasValidLineage(t *testing.T) {
    18  	newPCE := func(pid uint32, parent *ProcessCacheEntry, isParentMissing bool) *ProcessCacheEntry {
    19  		pce := &ProcessCacheEntry{
    20  			ProcessContext: ProcessContext{
    21  				Process: Process{
    22  					PIDContext: PIDContext{
    23  						Pid: pid,
    24  					},
    25  
    26  					IsParentMissing: isParentMissing,
    27  				},
    28  				Ancestor: parent,
    29  			},
    30  		}
    31  		if parent != nil {
    32  			pce.PPid = parent.Pid
    33  		}
    34  
    35  		return pce
    36  	}
    37  
    38  	t.Run("valid", func(t *testing.T) {
    39  		pid1 := newPCE(1, nil, false)
    40  		child1 := newPCE(2, pid1, false)
    41  		child2 := newPCE(3, child1, false)
    42  
    43  		isValid, err := child2.HasValidLineage()
    44  		assert.True(t, isValid)
    45  		assert.NoError(t, err)
    46  	})
    47  
    48  	t.Run("pid1-missing", func(t *testing.T) {
    49  		child1 := newPCE(2, nil, false)
    50  		child2 := newPCE(3, child1, false)
    51  
    52  		isValid, err := child2.HasValidLineage()
    53  		assert.False(t, isValid)
    54  		assert.NotNil(t, err)
    55  
    56  		var mn *ErrProcessIncompleteLineage
    57  		assert.ErrorAs(t, err, &mn)
    58  	})
    59  
    60  	t.Run("two-pid1", func(t *testing.T) {
    61  		pid1 := newPCE(1, nil, false)
    62  		child1 := newPCE(1, pid1, false)
    63  		child2 := newPCE(3, child1, false)
    64  
    65  		isValid, err := child2.HasValidLineage()
    66  		assert.False(t, isValid)
    67  		assert.NotNil(t, err)
    68  
    69  		var mn *ErrProcessWrongParentNode
    70  		assert.ErrorAs(t, err, &mn)
    71  	})
    72  
    73  	t.Run("parent-missing", func(t *testing.T) {
    74  		pid1 := newPCE(1, nil, false)
    75  		child1 := newPCE(2, pid1, true)
    76  		child2 := newPCE(3, child1, false)
    77  
    78  		isValid, err := child2.HasValidLineage()
    79  		assert.False(t, isValid)
    80  		assert.NotNil(t, err)
    81  
    82  		var mn *ErrProcessMissingParentNode
    83  		assert.ErrorAs(t, err, &mn)
    84  	})
    85  }