gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/seccheck/metadata_test.go (about)

     1  // Copyright 2022 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package seccheck
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestSinkRegistration(t *testing.T) {
    22  	sink := SinkDesc{Name: "test"}
    23  	RegisterSink(sink)
    24  	if _, ok := Sinks["test"]; !ok {
    25  		t.Errorf("sink registration failed")
    26  	}
    27  
    28  	defer func() {
    29  		recover()
    30  	}()
    31  	RegisterSink(sink)
    32  	t.Errorf("Registering the same sink twice should panic")
    33  }
    34  
    35  func TestPointRegistration(t *testing.T) {
    36  	point := PointDesc{Name: "test"}
    37  	registerPoint(point)
    38  	if _, ok := Points["test"]; !ok {
    39  		t.Errorf("point registration failed")
    40  	}
    41  
    42  	defer func() {
    43  		recover()
    44  	}()
    45  	registerPoint(point)
    46  	t.Errorf("Registering the same point twice should panic")
    47  }
    48  
    49  func TestPointRegistrationFields(t *testing.T) {
    50  	for _, tc := range []struct {
    51  		name  string
    52  		point PointDesc
    53  	}{
    54  		{
    55  			name: "optional_name",
    56  			point: PointDesc{
    57  				Name: "test",
    58  				OptionalFields: []FieldDesc{
    59  					{ID: 123, Name: "field1"},
    60  					{ID: 456, Name: "field1"},
    61  				},
    62  			},
    63  		},
    64  		{
    65  			name: "optional_id",
    66  			point: PointDesc{
    67  				Name: "test",
    68  				OptionalFields: []FieldDesc{
    69  					{ID: 123, Name: "field1"},
    70  					{ID: 123, Name: "field2"},
    71  				},
    72  			},
    73  		},
    74  		{
    75  			name: "context_name",
    76  			point: PointDesc{
    77  				Name: "test",
    78  				ContextFields: []FieldDesc{
    79  					{ID: 123, Name: "field1"},
    80  					{ID: 456, Name: "field1"},
    81  				},
    82  			},
    83  		},
    84  		{
    85  			name: "context_id",
    86  			point: PointDesc{
    87  				Name: "test",
    88  				ContextFields: []FieldDesc{
    89  					{ID: 123, Name: "field1"},
    90  					{ID: 123, Name: "field2"},
    91  				},
    92  			},
    93  		},
    94  	} {
    95  		t.Run(tc.name, func(t *testing.T) {
    96  			defer func() {
    97  				recover()
    98  			}()
    99  			registerPoint(tc.point)
   100  			t.Errorf("Registering the same point twice should panic")
   101  
   102  		})
   103  	}
   104  }