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

     1  // Copyright 2018 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 limits
    16  
    17  import (
    18  	"testing"
    19  
    20  	"golang.org/x/sys/unix"
    21  )
    22  
    23  func TestSet(t *testing.T) {
    24  	testCases := []struct {
    25  		limit       Limit
    26  		privileged  bool
    27  		expectedErr error
    28  	}{
    29  		{limit: Limit{Cur: 50, Max: 50}, privileged: false, expectedErr: nil},
    30  		{limit: Limit{Cur: 20, Max: 50}, privileged: false, expectedErr: nil},
    31  		{limit: Limit{Cur: 20, Max: 60}, privileged: false, expectedErr: unix.EPERM},
    32  		{limit: Limit{Cur: 60, Max: 50}, privileged: false, expectedErr: unix.EINVAL},
    33  		{limit: Limit{Cur: 11, Max: 10}, privileged: false, expectedErr: unix.EINVAL},
    34  		{limit: Limit{Cur: 20, Max: 60}, privileged: true, expectedErr: nil},
    35  	}
    36  
    37  	ls := NewLimitSet()
    38  	for _, tc := range testCases {
    39  		if _, err := ls.Set(1, tc.limit, tc.privileged); err != tc.expectedErr {
    40  			t.Fatalf("Tried to set Limit to %+v and privilege %t: got %v, wanted %v", tc.limit, tc.privileged, err, tc.expectedErr)
    41  		}
    42  	}
    43  
    44  }