File: squashfs_singularity_test.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (130 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2022, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

//go:build singularity_engine

package unpacker

import (
	"reflect"
	"strings"
	"testing"
)

// Library listing on Fedora 35 AMD64 - simple case
// $ /lib64/ld-linux-x86-64.so.2 --list /usr/sbin/unsquashfs
const ldListSimple = `        linux-vdso.so.1 (0x00007ffe9ebcb000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f5dd1e6a000)
        libz.so.1 => /lib64/libz.so.1 (0x00007f5dd1e50000)
        liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f5dd1e24000)
        liblzo2.so.2 => /lib64/liblzo2.so.2 (0x00007f5dd1e03000)
        liblz4.so.1 => /lib64/liblz4.so.1 (0x00007f5dd1ddf000)
        libzstd.so.1 => /lib64/libzstd.so.1 (0x00007f5dd1d30000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f5dd1d13000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f5dd1b09000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f5dd2104000)`

// Library listing on EL8 POWER8 - complex case
// glibc-hwcaps and dependency filename not matching resolved filename
// $ /lib64/ld64.so.2 --list /usr/sbin/unsquashfs
const ldListComplex = `        linux-vdso64.so.1 (0x00007fff80d70000)
        libpthread.so.0 => /lib64/glibc-hwcaps/power9/libpthread-2.28.so (0x00007fff80b50000)
        libm.so.6 => /lib64/glibc-hwcaps/power9/libm-2.28.so (0x00007fff80a20000)
        libz.so.1 => /lib64/libz.so.1 (0x00007fff809e0000)
        liblzma.so.5 => /lib64/liblzma.so.5 (0x00007fff80980000)
        liblzo2.so.2 => /lib64/liblzo2.so.2 (0x00007fff80930000)
        liblz4.so.1 => /lib64/liblz4.so.1 (0x00007fff808e0000)
        libc.so.6 => /lib64/glibc-hwcaps/power9/libc-2.28.so (0x00007fff806d0000)
        /lib64/ld64.so.2 (0x00007fff80d90000)`

// Library listing on EL7 - old case
// The linux-vdso.so.1 line has a => field that doesn't point to an absolute path
const ldListOld = `        linux-vdso.so.1 =>  (0x00007ffccf1de000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f5ab0e3d000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f5ab0b3b000)
        libz.so.1 => /lib64/libz.so.1 (0x00007f5ab0925000)
        liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f5ab06ff000)
        liblzo2.so.2 => /lib64/liblzo2.so.2 (0x00007f5ab04de000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f5ab02c8000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f5aafefa000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f5ab1059000)`

func Test_parseLibraryBinds(t *testing.T) {
	tests := []struct {
		name    string
		ldList  string
		want    []libBind
		wantErr bool
	}{
		{
			name:    "empty",
			ldList:  "",
			want:    []libBind{},
			wantErr: false,
		},
		{
			name:   "simple",
			ldList: ldListSimple,
			want: []libBind{
				{"/lib64/libm.so.6", "/lib64/libm.so.6"},
				{"/lib64/libz.so.1", "/lib64/libz.so.1"},
				{"/lib64/liblzma.so.5", "/lib64/liblzma.so.5"},
				{"/lib64/liblzo2.so.2", "/lib64/liblzo2.so.2"},
				{"/lib64/liblz4.so.1", "/lib64/liblz4.so.1"},
				{"/lib64/libzstd.so.1", "/lib64/libzstd.so.1"},
				{"/lib64/libgcc_s.so.1", "/lib64/libgcc_s.so.1"},
				{"/lib64/libc.so.6", "/lib64/libc.so.6"},
				{"/lib64/ld-linux-x86-64.so.2", "/lib64/ld-linux-x86-64.so.2"},
			},
			wantErr: false,
		},
		{
			name:   "complex",
			ldList: ldListComplex,
			want: []libBind{
				{
					"/lib64/glibc-hwcaps/power9/libpthread-2.28.so",
					"/lib64/glibc-hwcaps/power9/libpthread.so.0",
				},
				{"/lib64/glibc-hwcaps/power9/libm-2.28.so", "/lib64/glibc-hwcaps/power9/libm.so.6"},
				{"/lib64/libz.so.1", "/lib64/libz.so.1"},
				{"/lib64/liblzma.so.5", "/lib64/liblzma.so.5"},
				{"/lib64/liblzo2.so.2", "/lib64/liblzo2.so.2"},
				{"/lib64/liblz4.so.1", "/lib64/liblz4.so.1"},
				{"/lib64/glibc-hwcaps/power9/libc-2.28.so", "/lib64/glibc-hwcaps/power9/libc.so.6"},
				{"/lib64/ld64.so.2", "/lib64/ld64.so.2"},
			},
			wantErr: false,
		},
		{
			name:   "old",
			ldList: ldListOld,
			want: []libBind{
				{"/lib64/libpthread.so.0", "/lib64/libpthread.so.0"},
				{"/lib64/libm.so.6", "/lib64/libm.so.6"},
				{"/lib64/libz.so.1", "/lib64/libz.so.1"},
				{"/lib64/liblzma.so.5", "/lib64/liblzma.so.5"},
				{"/lib64/liblzo2.so.2", "/lib64/liblzo2.so.2"},
				{"/lib64/libgcc_s.so.1", "/lib64/libgcc_s.so.1"},
				{"/lib64/libc.so.6", "/lib64/libc.so.6"},
				{"/lib64/ld-linux-x86-64.so.2", "/lib64/ld-linux-x86-64.so.2"},
			},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			buf := strings.NewReader(tt.ldList)
			got, err := parseLibraryBinds(buf)
			if (err != nil) != tt.wantErr {
				t.Errorf("parseLibraryBinds() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("parseLibraryBinds() = %v, want %v", got, tt.want)
			}
		})
	}
}