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