github.com/quay/claircore@v1.5.28/java/jar/manifest_test.go (about)

     1  package jar
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"errors"
     7  	"io"
     8  	"io/fs"
     9  	"os"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/google/go-cmp/cmp"
    14  	"github.com/quay/zlog"
    15  )
    16  
    17  func TestParseManifest(t *testing.T) {
    18  	t.Parallel()
    19  	ctx := zlog.Test(context.Background(), t)
    20  
    21  	md := os.DirFS("testdata/manifest")
    22  	fs, err := fs.ReadDir(md, ".")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	// Tee the manifests for easier diagnosing.
    27  	var buf bytes.Buffer
    28  	for _, d := range fs {
    29  		buf.Reset()
    30  		f, err := md.Open(d.Name())
    31  		if err != nil {
    32  			t.Error(err)
    33  			continue
    34  		}
    35  		tee := io.TeeReader(f, &buf)
    36  		var i Info
    37  		err = i.parseManifest(ctx, tee)
    38  		f.Close()
    39  		switch {
    40  		case errors.Is(err, nil):
    41  			t.Logf("%s: %+v", d.Name(), i)
    42  		case errors.Is(err, errUnpopulated):
    43  		default:
    44  			t.Error(err)
    45  		}
    46  		t.Logf("%s: %+q", d.Name(), buf.String())
    47  	}
    48  }
    49  
    50  func TestParseManifest_JenkinsPlugins(t *testing.T) {
    51  	var i Info
    52  	ctx := zlog.Test(context.Background(), t)
    53  	for _, tc := range jenkinsPlugins {
    54  		t.Run(tc.Name, func(t *testing.T) {
    55  			err := i.parseManifest(ctx, strings.NewReader(tc.Contents))
    56  			if err != nil {
    57  				t.Fatal(err)
    58  			}
    59  
    60  			if !cmp.Equal(tc.ExpectedName, i.Name) {
    61  				t.Error(cmp.Diff(tc.ExpectedName, i.Name))
    62  			}
    63  			if !cmp.Equal(tc.ExpectedVersion, i.Version) {
    64  				t.Error(cmp.Diff(tc.ExpectedVersion, i.Version))
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  type manifestTestCase struct {
    71  	Name            string
    72  	Contents        string
    73  	ExpectedName    string
    74  	ExpectedVersion string
    75  }
    76  
    77  var jenkinsPlugins = []manifestTestCase{
    78  	{
    79  		Name: "ghprb",
    80  		Contents: `Manifest-Version: 1.0
    81  Archiver-Version: Plexus Archiver
    82  Created-By: Apache Maven
    83  Built-By: ogondza
    84  Build-Jdk: 1.8.0_275
    85  Extension-Name: ghprb
    86  Specification-Title: The Jenkins Plugins Parent POM Project
    87  Implementation-Title: ghprb
    88  Implementation-Version: 1.42.2
    89  Group-Id: org.jenkins-ci.plugins
    90  Short-Name: ghprb
    91  Long-Name: GitHub Pull Request Builder
    92  Url: https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+b
    93   uilder+plugin
    94  Plugin-Version: 1.42.2
    95  Hudson-Version: 2.7
    96  Jenkins-Version: 2.7
    97  Plugin-Dependencies: build-flow-plugin:0.20;resolution:=optional,githu
    98   b:1.27.0,bouncycastle-api:2.16.1,credentials:2.1.14,git:3.3.1,github-
    99   api:1.92,job-dsl:1.63;resolution:=optional,matrix-project:1.11,plain-
   100   credentials:1.4,scm-api:2.1.0,script-security:1.25,structs:1.9,token-
   101   macro:2.1;resolution:=optional
   102  Plugin-Developers: Sam Gleske:sag47:sam.mxracer@gmail.com
   103  `,
   104  		ExpectedName:    "org.jenkins-ci.plugins:ghprb",
   105  		ExpectedVersion: "1.42.2",
   106  	},
   107  	{
   108  		Name: "stackrox-container-image-scanner",
   109  		Contents: `Manifest-Version: 1.0
   110  Archiver-Version: Plexus Archiver
   111  Created-By: Apache Maven
   112  Built-By: runner
   113  Build-Jdk: 1.8.0_362
   114  Extension-Name: stackrox-container-image-scanner
   115  Specification-Title: This plugin provides vulnerability scanning of co
   116   ntainer images for OS packages and language
   117           vulnerabilities using the StackRox Kubernetes Security Platform
   118  Specification-Vendor: Red Hat
   119  Implementation-Vendor: Red Hat
   120  Implementation-Title: stackrox-container-image-scanner
   121  Implementation-Version: 1.3.4
   122  Group-Id: org.jenkins-ci.plugins
   123  Short-Name: stackrox-container-image-scanner
   124  Long-Name: StackRox Container Image Scanner
   125  Url: https://github.com/stackrox/jenkins-plugin
   126  Minimum-Java-Version: 1.7
   127  Plugin-Version: 1.3.4
   128  Hudson-Version: 2.164.1
   129  Jenkins-Version: 2.164.1
   130  Plugin-Developers: Red Hat:stackrox-k8s-security-platform:jenkins-plug
   131   in@stackrox.com
   132  Plugin-License-Name: Apache 2 License
   133  Plugin-License-Url: http://opensource.org/licenses/Apache-2.0
   134  Plugin-ScmUrl: https://github.com/stackrox/jenkins-plugin
   135  `,
   136  		ExpectedName:    "org.jenkins-ci.plugins:stackrox-container-image-scanner",
   137  		ExpectedVersion: "1.3.4",
   138  	},
   139  	{
   140  		Name: "m2release",
   141  		Contents: `Manifest-Version: 1.0
   142  Archiver-Version: Plexus Archiver
   143  Created-By: Apache Maven
   144  Built-By: Kevin
   145  Build-Jdk: 1.8.0_202
   146  Extension-Name: m2release
   147  Specification-Title: A plug-in that enables you to perform releases us
   148   ing the maven-release-plugin from Jenkins.
   149  Implementation-Title: m2release
   150  Implementation-Version: 0.16.3
   151  Plugin-Class: org.jvnet.hudson.plugins.m2release.PluginImpl
   152  Group-Id: org.jenkins-ci.plugins.m2release
   153  Short-Name: m2release
   154  Long-Name: Jenkins Maven Release Plug-in Plug-in
   155  Url: http://wiki.jenkins-ci.org/display/JENKINS/M2+Release+Plugin
   156  Compatible-Since-Version: 0.15
   157  Minimum-Java-Version: 1.8
   158  Plugin-Version: 0.16.3
   159  Hudson-Version: 2.138.4
   160  Jenkins-Version: 2.138.4
   161  Plugin-Dependencies: maven-plugin:3.4,dashboard-view:2.0;resolution:=o
   162   ptional
   163  Plugin-Developers: James Nord:teilo:,Christian Slama:m68k:,Dominik Bar
   164   tholdi:imod:
   165  Plugin-License-Name: MIT License
   166  Plugin-License-Url: http://www.opensource.org/licenses/mit-license.php
   167  Plugin-ScmUrl: https://github.com/jenkinsci/m2release-plugin/
   168  `,
   169  		ExpectedName:    "org.jenkins-ci.plugins.m2release:m2release",
   170  		ExpectedVersion: "0.16.3",
   171  	},
   172  	{
   173  		Name: "ssh-credentials",
   174  		Contents: `Manifest-Version: 1.0
   175  Created-By: Maven Archiver 3.6.0
   176  Build-Jdk-Spec: 11
   177  Specification-Title: SSH Credentials Plugin
   178  Specification-Version: 0.0
   179  Implementation-Title: SSH Credentials Plugin
   180  Implementation-Version: 305.v8f4381501156
   181  Group-Id: org.jenkins-ci.plugins
   182  Short-Name: ssh-credentials
   183  Long-Name: SSH Credentials Plugin
   184  Url: https://github.com/jenkinsci/ssh-credentials-plugin
   185  Plugin-Version: 305.v8f4381501156
   186  Hudson-Version: 2.346.1
   187  Jenkins-Version: 2.346.1
   188  Plugin-Dependencies: credentials:1139.veb_9579fca_33b_,trilead-api:1.67.
   189   vc3938a_35172f
   190  Plugin-Developers: Stephen Connolly:stephenconnolly:,Oleg Nenashev:oleg_
   191   nenashev:,Matt Sicker:jvz:
   192  Plugin-License-Name: MIT License
   193  Plugin-License-Url: https://opensource.org/licenses/MIT
   194  Plugin-ScmUrl: https://github.com/jenkinsci/ssh-credentials-plugin
   195  `,
   196  		ExpectedName:    "org.jenkins-ci.plugins:ssh-credentials",
   197  		ExpectedVersion: "305.v8f4381501156",
   198  	},
   199  }