github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/runsc/boot/compat_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 boot
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestOnceTracker(t *testing.T) {
    22  	o := onceTracker{}
    23  	if !o.shouldReport(nil) {
    24  		t.Error("first call to checkAndMark, got: false, want: true")
    25  	}
    26  	o.onReported(nil)
    27  	for i := 0; i < 2; i++ {
    28  		if o.shouldReport(nil) {
    29  			t.Error("after first call to checkAndMark, got: true, want: false")
    30  		}
    31  	}
    32  }
    33  
    34  func TestArgsTracker(t *testing.T) {
    35  	for _, tc := range []struct {
    36  		name   string
    37  		idx    []int
    38  		arg1_1 uint64
    39  		arg1_2 uint64
    40  		arg2_1 uint64
    41  		arg2_2 uint64
    42  		want   bool
    43  	}{
    44  		{name: "same arg1", idx: []int{0}, arg1_1: 123, arg1_2: 123, want: false},
    45  		{name: "same arg2", idx: []int{1}, arg2_1: 123, arg2_2: 123, want: false},
    46  		{name: "diff arg1", idx: []int{0}, arg1_1: 123, arg1_2: 321, want: true},
    47  		{name: "diff arg2", idx: []int{1}, arg2_1: 123, arg2_2: 321, want: true},
    48  		{name: "cmd is uint32", idx: []int{0}, arg2_1: 0xdead00000123, arg2_2: 0xbeef00000123, want: false},
    49  		{name: "same 2 args", idx: []int{0, 1}, arg2_1: 123, arg1_1: 321, arg2_2: 123, arg1_2: 321, want: false},
    50  		{name: "diff 2 args", idx: []int{0, 1}, arg2_1: 123, arg1_1: 321, arg2_2: 789, arg1_2: 987, want: true},
    51  	} {
    52  		t.Run(tc.name, func(t *testing.T) {
    53  			c := newArgsTracker(tc.idx...)
    54  			regs := newRegs()
    55  			setArgVal(0, tc.arg1_1, regs)
    56  			setArgVal(1, tc.arg2_1, regs)
    57  			if !c.shouldReport(regs) {
    58  				t.Error("first call to shouldReport, got: false, want: true")
    59  			}
    60  			c.onReported(regs)
    61  
    62  			setArgVal(0, tc.arg1_2, regs)
    63  			setArgVal(1, tc.arg2_2, regs)
    64  			if got := c.shouldReport(regs); tc.want != got {
    65  				t.Errorf("second call to shouldReport, got: %t, want: %t", got, tc.want)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestArgsTrackerLimit(t *testing.T) {
    72  	c := newArgsTracker(0, 1)
    73  	for i := 0; i < reportLimit; i++ {
    74  		regs := newRegs()
    75  		setArgVal(0, 123, regs)
    76  		setArgVal(1, uint64(i), regs)
    77  		if !c.shouldReport(regs) {
    78  			t.Error("shouldReport before limit was reached, got: false, want: true")
    79  		}
    80  		c.onReported(regs)
    81  	}
    82  
    83  	// Should hit the count limit now.
    84  	regs := newRegs()
    85  	setArgVal(0, 123, regs)
    86  	setArgVal(1, 123456, regs)
    87  	if c.shouldReport(regs) {
    88  		t.Error("shouldReport after limit was reached, got: true, want: false")
    89  	}
    90  }