github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/util/fdutil_test.go (about) 1 /* 2 Copyright 2018 The OpenEBS Author 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "syscall" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func fdGetArray() []syscall.FdSet { 27 results := make([]syscall.FdSet, 0) 28 fd1, fd2, fd6, fd128 := syscall.FdSet{}, syscall.FdSet{}, syscall.FdSet{}, syscall.FdSet{} 29 fd1.Bits[0] = 1 30 fd2.Bits[0] = 2 31 fd6.Bits[0] = 6 32 fd128.Bits[0] = 128 33 results = append(results, fd1, fd2, fd6, fd128) 34 35 return results 36 } 37 38 func TestFD_SET(t *testing.T) { 39 tests := map[string]struct { 40 x int 41 expected syscall.FdSet 42 }{ 43 "fd set test for fd value 0": {x: 0, expected: fdGetArray()[0]}, 44 "fd set test for fd value 1": {x: 1, expected: fdGetArray()[1]}, 45 "fd set test for fd value 7": {x: 7, expected: fdGetArray()[3]}, 46 } 47 for name, test := range tests { 48 t.Run(name, func(t *testing.T) { 49 result := &syscall.FdSet{} 50 FD_SET(result, test.x) 51 assert.Equal(t, test.expected, *result) 52 }) 53 } 54 } 55 56 func TestFD_ISSET(t *testing.T) { 57 tests := map[string]struct { 58 x int 59 fdset syscall.FdSet 60 expected bool 61 }{ 62 "set 0 and check with 2^0": {x: 0, fdset: fdGetArray()[0], expected: true}, 63 "set 1 and check with 2^1": {x: 1, fdset: fdGetArray()[1], expected: true}, 64 "set 7 and check with 2^7": {x: 7, fdset: fdGetArray()[3], expected: true}, 65 "set 4 and check with 6": {x: 4, fdset: fdGetArray()[2], expected: false}, 66 "set 2 and check with 2": {x: 2, fdset: fdGetArray()[1], expected: false}, 67 } 68 for name, test := range tests { 69 t.Run(name, func(t *testing.T) { 70 result := FD_ISSET(&test.fdset, test.x) 71 assert.Equal(t, test.expected, result) 72 }) 73 } 74 } 75 76 func TestFD_ZERO(t *testing.T) { 77 tests := map[string]struct { 78 x int 79 expected syscall.FdSet 80 }{ 81 "fd zero test for fdset value {[16]int64{1}}": {x: 0, expected: fdGetArray()[0]}, 82 "fd zero test for fdset value {[16]int64{2}}": {x: 1, expected: fdGetArray()[1]}, 83 "fd zero test for fdset value {[16]int64{128}}": {x: 7, expected: fdGetArray()[3]}, 84 } 85 for name, test := range tests { 86 t.Run(name, func(t *testing.T) { 87 result := &syscall.FdSet{} 88 FD_ZERO(&test.expected) 89 assert.Equal(t, test.expected, *result) 90 }) 91 } 92 }