github.com/apptainer/singularity@v3.1.1+incompatible/pkg/util/loop/loop_linux_test.go (about)

     1  // Copyright (c) 2019, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package loop
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"syscall"
    12  	"testing"
    13  
    14  	"github.com/sylabs/singularity/internal/pkg/test"
    15  )
    16  
    17  func TestLoop(t *testing.T) {
    18  	test.EnsurePrivilege(t)
    19  
    20  	var i1 *Info64
    21  
    22  	info := &Info64{
    23  		Flags: FlagsAutoClear | FlagsReadOnly,
    24  	}
    25  	loopDev := &Device{
    26  		MaxLoopDevices: 256,
    27  		Info:           info,
    28  	}
    29  
    30  	loopOne := -1
    31  	loopTwo := -1
    32  
    33  	// With wrong path and file pointer
    34  	if err := loopDev.AttachFromPath("", os.O_RDONLY, &loopOne); err == nil {
    35  		t.Errorf("unexpected success with a wrong path")
    36  	}
    37  	if err := loopDev.AttachFromFile(nil, os.O_RDONLY, &loopOne); err == nil {
    38  		t.Errorf("unexpected success with a nil file pointer")
    39  	}
    40  
    41  	// With good file
    42  	if err := loopDev.AttachFromPath("/etc/passwd", os.O_RDONLY, &loopOne); err != nil {
    43  		t.Error(err)
    44  	}
    45  
    46  	f, err := os.Open("/etc/passwd")
    47  	if err != nil {
    48  		t.Error(err)
    49  	}
    50  	fi, err := f.Stat()
    51  	if err != nil {
    52  		t.Error(err)
    53  	}
    54  
    55  	// With correct file pointer
    56  	if err := loopDev.AttachFromFile(f, os.O_RDONLY, &loopTwo); err != nil {
    57  		t.Error(err)
    58  	}
    59  	if loopOne == loopTwo {
    60  		t.Errorf("attached to the same loop block device /dev/loop%d", loopOne)
    61  	}
    62  
    63  	// Test if loop devices matches associated file
    64  	_, err = GetStatusFromPath("")
    65  	if err == nil {
    66  		t.Errorf("unexpected success while returning status with non existent loop device")
    67  	}
    68  
    69  	path := fmt.Sprintf("/dev/loop%d", loopTwo)
    70  	i1, err = GetStatusFromPath(path)
    71  	if err != nil {
    72  		t.Error(err)
    73  	}
    74  
    75  	st := fi.Sys().(*syscall.Stat_t)
    76  	if st.Dev != i1.Device || st.Ino != i1.Inode {
    77  		t.Errorf("bad file association for %s", path)
    78  	}
    79  
    80  	// With shared loop device
    81  	loopDev.Shared = true
    82  	loopTwo = -1
    83  	if err := loopDev.AttachFromPath("/etc/passwd", os.O_RDONLY, &loopTwo); err != nil {
    84  		t.Error(err)
    85  	}
    86  	if loopOne != loopTwo {
    87  		t.Errorf("not attached to the same loop block device /dev/loop%d", loopOne)
    88  	}
    89  
    90  	loopTwo = -1
    91  	if err := loopDev.AttachFromPath("/etc/group", os.O_RDONLY, &loopTwo); err != nil {
    92  		t.Error(err)
    93  	}
    94  	if loopOne == loopTwo {
    95  		t.Errorf("attached to the same loop block device /dev/loop%d", loopOne)
    96  	}
    97  
    98  	// With MaxLoopDevices set to zero
    99  	loopDev.MaxLoopDevices = 0
   100  	if err := loopDev.AttachFromPath("/etc/group", os.O_RDONLY, &loopTwo); err == nil {
   101  		t.Errorf("unexpected success with MaxLoopDevices = 0")
   102  	}
   103  }