github.com/containers/podman/v4@v4.9.4/pkg/emulation/binfmtmisc_linux_test.go (about)

     1  //go:build !remote
     2  // +build !remote
     3  
     4  package emulation
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  // parseBinfmtMisc parses a binfmt_misc registry entry.  It returns the offset,
    16  // magic, and mask values, or an error if there was an error parsing the data.
    17  // If the returned offset is negative, the entry was disabled or should be
    18  // non-fatally ignored for some other reason.
    19  func TestParseBinfmtMisc(t *testing.T) {
    20  	vectors := []struct {
    21  		platform, contents string
    22  	}{
    23  		{
    24  			"linux/386",
    25  			`
    26  			enabled
    27  			interpreter /usr/bin/qemu-i386-static
    28  			flags: F
    29  			offset 0
    30  			magic 7f454c4601010100000000000000000002000300
    31  			mask  fffffffffffefe00fffffffffffffffffeffffff
    32  			`,
    33  		},
    34  		{
    35  			"linux/amd64",
    36  			`
    37  			enabled
    38  			interpreter /usr/bin/qemu-x86_64-static
    39  			flags: F
    40  			offset 0
    41  			magic 7f454c4602010100000000000000000002003e00
    42  			mask  fffffffffffefe00fffffffffffffffffeffffff
    43  			`,
    44  		},
    45  		{
    46  			"linux/arm",
    47  			`
    48  			enabled
    49  			interpreter /usr/bin/qemu-arm-static
    50  			flags: F
    51  			offset 0
    52  			magic 7f454c4601010100000000000000000002002800
    53  			mask  ffffffffffffff00fffffffffffffffffeffffff
    54  			`,
    55  		},
    56  		{
    57  			"linux/arm64",
    58  			`
    59  			enabled
    60  			interpreter /usr/bin/qemu-aarch64-static
    61  			flags: F
    62  			offset 0
    63  			magic 7f454c460201010000000000000000000200b700
    64  			mask  ffffffffffffff00fffffffffffffffffeffffff
    65  			`,
    66  		},
    67  		{
    68  			"linux/ppc64le",
    69  			`
    70  			enabled
    71  			interpreter /usr/bin/qemu-ppc64le-static
    72  			flags: F
    73  			offset 0
    74  			magic 7f454c4602010100000000000000000002001500
    75  			mask  ffffffffffffff00fffffffffffffffffeffff00
    76  			`,
    77  		},
    78  		{
    79  			"linux/s390x",
    80  			`
    81  			enabled
    82  			interpreter /usr/bin/qemu-s390x-static
    83  			flags: F
    84  			offset 0
    85  			magic 7f454c4602020100000000000000000000020016
    86  			mask  ffffffffffffff00fffffffffffffffffffeffff
    87  			`,
    88  		},
    89  	}
    90  	for i := range vectors {
    91  		v := vectors[i]
    92  		t.Run(v.platform, func(t *testing.T) {
    93  			offset, magic, mask, err := parseBinfmtMisc(fmt.Sprintf("test vector %d", i), strings.NewReader(v.contents))
    94  			require.NoError(t, err, "parseBinfmtMisc: %v", err)
    95  			require.GreaterOrEqual(t, offset, 0, "%q shouldn't have been disabled", v.platform)
    96  			headers := getKnownELFPlatformHeaders()[v.platform]
    97  			matched := false
    98  			for _, header := range headers {
    99  				if magicMatch(header, offset, mask, magic) {
   100  					matched = true
   101  				}
   102  			}
   103  			assert.True(t, matched, "%q did not match an expected header match", v.platform)
   104  		})
   105  	}
   106  }