github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/alpm/parse_alpm_db_test.go (about)

     1  package alpm
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/anchore/syft/syft/file"
    13  	"github.com/anchore/syft/syft/pkg"
    14  )
    15  
    16  func TestDatabaseParser(t *testing.T) {
    17  	tests := []struct {
    18  		name     string
    19  		fixture  string
    20  		expected pkg.AlpmMetadata
    21  	}{
    22  		{
    23  			name:    "test alpm database parsing",
    24  			fixture: "test-fixtures/files",
    25  			expected: pkg.AlpmMetadata{
    26  				Backup: []pkg.AlpmFileRecord{
    27  					{
    28  						Path: "/etc/pacman.conf",
    29  						Digests: []file.Digest{{
    30  							Algorithm: "md5",
    31  							Value:     "de541390e52468165b96511c4665bff4",
    32  						}},
    33  					},
    34  					{
    35  						Path: "/etc/makepkg.conf",
    36  						Digests: []file.Digest{{
    37  							Algorithm: "md5",
    38  							Value:     "79fce043df7dfc676ae5ecb903762d8b",
    39  						}},
    40  					},
    41  				},
    42  				Files: []pkg.AlpmFileRecord{
    43  					{
    44  						Path: "/etc/",
    45  					},
    46  					{
    47  						Path: "/etc/makepkg.conf",
    48  					},
    49  					{
    50  						Path: "/etc/pacman.conf",
    51  					},
    52  					{
    53  						Path: "/usr/",
    54  					},
    55  					{
    56  						Path: "/usr/bin/",
    57  					},
    58  					{
    59  						Path: "/usr/bin/makepkg",
    60  					},
    61  					{
    62  						Path: "/usr/bin/makepkg-template",
    63  					},
    64  					{
    65  						Path: "/usr/bin/pacman",
    66  					},
    67  					{
    68  						Path: "/usr/bin/pacman-conf",
    69  					},
    70  					{
    71  						Path: "/var/",
    72  					},
    73  					{
    74  						Path: "/var/cache/",
    75  					},
    76  					{
    77  						Path: "/var/cache/pacman/",
    78  					},
    79  					{
    80  						Path: "/var/cache/pacman/pkg/",
    81  					},
    82  					{
    83  						Path: "/var/lib/",
    84  					},
    85  					{
    86  						Path: "/var/lib/pacman/",
    87  					},
    88  				},
    89  			},
    90  		},
    91  	}
    92  
    93  	for _, test := range tests {
    94  		t.Run(test.name, func(t *testing.T) {
    95  			f, err := os.Open(test.fixture)
    96  			require.NoError(t, err)
    97  			t.Cleanup(func() { require.NoError(t, f.Close()) })
    98  
    99  			reader := bufio.NewReader(f)
   100  
   101  			entry, err := parseAlpmDBEntry(reader)
   102  			require.NoError(t, err)
   103  
   104  			if diff := cmp.Diff(entry.Files, test.expected.Files); diff != "" {
   105  				t.Errorf("Files mismatch (-want +got):\n%s", diff)
   106  			}
   107  
   108  			if diff := cmp.Diff(entry.Backup, test.expected.Backup); diff != "" {
   109  				t.Errorf("Backup mismatch (-want +got):\n%s", diff)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func parseTime(stime string) time.Time {
   116  	t, _ := time.Parse(time.RFC3339, stime)
   117  	return t
   118  }
   119  
   120  func TestMtreeParse(t *testing.T) {
   121  	tests := []struct {
   122  		name     string
   123  		expected []pkg.AlpmFileRecord
   124  	}{
   125  		{
   126  			name: "test mtree parsing",
   127  			expected: []pkg.AlpmFileRecord{
   128  				{
   129  					Path:    "/etc",
   130  					Type:    "dir",
   131  					Time:    parseTime("2022-04-10T14:59:52+02:00"),
   132  					Digests: make([]file.Digest, 0),
   133  				},
   134  				{
   135  					Path:    "/etc/pacman.d",
   136  					Type:    "dir",
   137  					Time:    parseTime("2022-04-10T14:59:52+02:00"),
   138  					Digests: make([]file.Digest, 0),
   139  				},
   140  				{
   141  					Path: "/etc/pacman.d/mirrorlist",
   142  					Size: "44683",
   143  					Time: parseTime("2022-04-10T14:59:52+02:00"),
   144  					Digests: []file.Digest{
   145  						{
   146  							Algorithm: "md5",
   147  							Value:     "81c39827e38c759d7e847f05db62c233",
   148  						},
   149  						{
   150  							Algorithm: "sha256",
   151  							Value:     "fc135ab26f2a227b9599b66a2f1ba325c445acb914d60e7ecf6e5997a87abe1e",
   152  						},
   153  					},
   154  				},
   155  			},
   156  		},
   157  	}
   158  
   159  	for _, test := range tests {
   160  		t.Run(test.name, func(t *testing.T) {
   161  			f, err := os.Open("test-fixtures/mtree")
   162  			require.NoError(t, err)
   163  			t.Cleanup(func() { require.NoError(t, f.Close()) })
   164  
   165  			reader := bufio.NewReader(f)
   166  
   167  			entry, err := parseMtree(reader)
   168  			require.NoError(t, err)
   169  
   170  			if diff := cmp.Diff(entry, test.expected); diff != "" {
   171  				t.Errorf("Files mismatch (-want +got):\n%s", diff)
   172  			}
   173  		})
   174  	}
   175  
   176  }