gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/sentry/fsimpl/cgroupfs/bitmap_test.go (about)

     1  // Copyright 2021 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 cgroupfs
    16  
    17  import (
    18  	"fmt"
    19  	"slices"
    20  	"testing"
    21  
    22  	"gvisor.dev/gvisor/pkg/bitmap"
    23  )
    24  
    25  func TestFormat(t *testing.T) {
    26  	tests := []struct {
    27  		input  []uint32
    28  		output string
    29  	}{
    30  		{[]uint32{1, 2, 3, 4, 7}, "1-4,7"},
    31  		{[]uint32{2}, "2"},
    32  		{[]uint32{0, 1, 2}, "0-2"},
    33  		{[]uint32{}, ""},
    34  		{[]uint32{1, 3, 4, 5, 6, 9, 11, 13, 14, 15, 16, 17}, "1,3-6,9,11,13-17"},
    35  		{[]uint32{2, 3, 10, 12, 13, 14, 15, 16, 20, 21, 33, 34, 47}, "2-3,10,12-16,20-21,33-34,47"},
    36  	}
    37  	for i, tt := range tests {
    38  		t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
    39  			b := bitmap.New(64)
    40  			for _, v := range tt.input {
    41  				b.Add(v)
    42  			}
    43  			s := formatBitmap(&b)
    44  			if s != tt.output {
    45  				t.Errorf("Expected %q, got %q", tt.output, s)
    46  			}
    47  			b1, err := parseBitmap(s, 64)
    48  			if err != nil {
    49  				t.Fatalf("Failed to parse formatted bitmap: %v", err)
    50  			}
    51  			if got, want := b1.ToSlice(), b.ToSlice(); !slices.Equal(got, want) {
    52  				t.Errorf("Parsing formatted output doesn't result in the original bitmap. Got %v, want %v", got, want)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestParse(t *testing.T) {
    59  	tests := []struct {
    60  		input      string
    61  		output     []uint32
    62  		shouldFail bool
    63  	}{
    64  		{"1", []uint32{1}, false},
    65  		{"", []uint32{}, false},
    66  		{"1,2,3,4", []uint32{1, 2, 3, 4}, false},
    67  		{"1-4", []uint32{1, 2, 3, 4}, false},
    68  		{"1,2-4", []uint32{1, 2, 3, 4}, false},
    69  		{"1,2-3,4", []uint32{1, 2, 3, 4}, false},
    70  		{"1-2,3,4,10,11", []uint32{1, 2, 3, 4, 10, 11}, false},
    71  		{"1,2-4,5,16", []uint32{1, 2, 3, 4, 5, 16}, false},
    72  		{"abc", []uint32{}, true},
    73  		{"1,3-2,4", []uint32{}, true},
    74  		{"1,3-3,4", []uint32{}, true},
    75  		{"1,2,3\000,4", []uint32{1, 2, 3}, false},
    76  		{"1,2,3\n,4", []uint32{1, 2, 3}, false},
    77  	}
    78  	for i, tt := range tests {
    79  		t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
    80  			b, err := parseBitmap(tt.input, 64)
    81  			if tt.shouldFail {
    82  				if err == nil {
    83  					t.Fatalf("Expected parsing of %q to fail, but it didn't", tt.input)
    84  				}
    85  				return
    86  			}
    87  			if err != nil {
    88  				t.Fatalf("Failed to parse bitmap: %v", err)
    89  				return
    90  			}
    91  
    92  			got := b.ToSlice()
    93  			if !slices.Equal(got, tt.output) {
    94  				t.Errorf("Parsed bitmap doesn't match what we expected. Got %v, want %v", got, tt.output)
    95  			}
    96  
    97  		})
    98  	}
    99  }