github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  	"bytes"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/mvdan/u-root-coreutils/pkg/ls"
    14  	"github.com/mvdan/u-root-coreutils/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  
    28  // Test listName func
    29  func TestListNameLinux(t *testing.T) {
    30  	testutil.SkipIfNotRoot(t)
    31  	// Create a directory
    32  	d := t.TempDir()
    33  	if err := unix.Mknod(filepath.Join(d, "large_node"), 0o660|unix.S_IFBLK, 0x12345678); err != nil {
    34  		t.Fatalf("err in unix.Mknod: %v", err)
    35  	}
    36  
    37  	// Setting the flags
    38  	*long = true
    39  	// Running the tests
    40  	// Write output in buffer.
    41  	var buf bytes.Buffer
    42  	var s ls.Stringer = ls.NameStringer{}
    43  	if *quoted {
    44  		s = ls.QuotedStringer{}
    45  	}
    46  	if *long {
    47  		s = ls.LongStringer{Human: *human, Name: s}
    48  	}
    49  	_ = listName(s, d, &buf, false)
    50  	if !strings.Contains(buf.String(), "1110, 74616") {
    51  		t.Errorf("Expected value: %s, got: %s", "1110, 74616", buf.String())
    52  	}
    53  }