github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+incompatible/cmds/core/ls/ls_linux_test.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"regexp"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/testutil"
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  // Test major and minor numbers greater then 255.
    19  //
    20  // This is supported since Linux 2.6. The major/minor numbers used for this
    21  // test are (1110, 74616). According to "kdev_t.h":
    22  //
    23  //       mkdev(1110, 74616)
    24  //     = mkdev(0x456, 0x12378)
    25  //     = (0x12378 & 0xff) | (0x456 << 8) | ((0x12378 & ~0xff) << 12)
    26  //     = 0x12345678
    27  func TestLargeDevNumber(t *testing.T) {
    28  	if uid := os.Getuid(); uid != 0 {
    29  		t.Skipf("test requires root, your uid is %d", uid)
    30  	}
    31  
    32  	// Make the node.
    33  	tmpDir, err := ioutil.TempDir("", "ls")
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	defer os.RemoveAll(tmpDir)
    38  	file := filepath.Join(tmpDir, "large_node")
    39  	if err := unix.Mknod(file, 0660|unix.S_IFBLK, 0x12345678); err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	defer os.Remove(file)
    43  
    44  	// Run "ls -l large_node".
    45  	out, err := testutil.Command(t, "-l", file).Output()
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	expected := regexp.MustCompile(`^\S+ \S+ \S+ 1110, 74616`)
    50  	if !expected.Match(out) {
    51  		t.Fatal("expected device number (1110, 74616), got:\n" + string(out))
    52  	}
    53  }