File: plugin_test.go

package info (click to toggle)
golang-github-notaryproject-notation-go 1.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,652 kB
  • sloc: makefile: 21
file content (268 lines) | stat: -rw-r--r-- 9,378 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plugin

import (
	"context"
	"encoding/json"
	"errors"
	"os"
	"reflect"
	"strconv"
	"strings"
	"testing"

	"github.com/notaryproject/notation-go/plugin/proto"
)

func TestGetMetadata(t *testing.T) {
	t.Run("plugin error is in invalid json format", func(t *testing.T) {
		exitErr := errors.New("unknown error")
		stderr := []byte("sad")
		expectedErrMsg := "invalid character 's' looking for beginning of value"
		plugin := CLIPlugin{name: "test-plugin"}
		executor = testCommander{stdout: nil, stderr: stderr, err: exitErr}
		_, err := plugin.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
		if err.Error() != expectedErrMsg {
			t.Fatalf("should error. got err = %v, want %v", err, expectedErrMsg)
		}
	})

	t.Run("plugin error is a valid json", func(t *testing.T) {
		stderr := []byte("{\"errorCode\":\"ACCESS_DENIED\"}")
		pluginErr := errors.New("plugin errors")
		wantErr := proto.RequestError{Code: proto.ErrorCodeAccessDenied}

		plugin := CLIPlugin{}
		executor = testCommander{stdout: nil, stderr: stderr, err: pluginErr}
		_, err := plugin.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
		if !errors.Is(err, wantErr) {
			t.Fatalf("should error. got err = %v, want %v", err, wantErr)
		}
	})

	t.Run("plugin cause system error", func(t *testing.T) {
		exitErr := errors.New("system error")
		stderr := []byte("")
		expectedErrMsg := "system error"
		plugin := CLIPlugin{name: "test-plugin"}
		executor = testCommander{stdout: nil, stderr: stderr, err: exitErr}
		_, err := plugin.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
		if err.Error() != expectedErrMsg {
			t.Fatalf("should error. got err = %v, want %v", err, expectedErrMsg)
		}
	})

}

func TestDescribeKey(t *testing.T) {
	t.Run("DescribeKey test", func(t *testing.T) {
		keyResp := proto.DescribeKeyResponse{KeyID: "1", KeySpec: "RSA-4096"}
		output, err := json.Marshal(keyResp)
		if err != nil {
			t.Fatal("should not error.")
		}
		executor = testCommander{stdout: output, err: nil}

		plugin := CLIPlugin{}
		resp, err := plugin.DescribeKey(context.Background(), &proto.DescribeKeyRequest{})
		if err != nil {
			t.Fatalf("should not error. got err = %v", err)
		}
		if reflect.DeepEqual(resp, keyResp) {
			t.Fatalf("DescribeKey() error. got: %+v, want: %+v", resp, keyResp)
		}
	})
}

func TestGenerateSignature(t *testing.T) {
	t.Run("GenerateSignature test", func(t *testing.T) {
		keyResp := proto.GenerateSignatureResponse{
			KeyID:            "1",
			Signature:        []byte("xxxxx"),
			SigningAlgorithm: "ECDSA-SHA-256",
			CertificateChain: [][]byte{{121, 132, 30, 42}},
		}
		output, err := json.Marshal(keyResp)
		if err != nil {
			t.Fatal("should not error.")
		}
		executor = testCommander{stdout: output, err: nil}

		plugin := CLIPlugin{}
		resp, err := plugin.GenerateSignature(context.Background(), &proto.GenerateSignatureRequest{})
		if err != nil {
			t.Fatalf("should not error. got err = %v", err)
		}
		if reflect.DeepEqual(resp, keyResp) {
			t.Fatalf("GenerateSignature() error. got: %+v, want: %+v", resp, keyResp)
		}
	})
}

func TestGenerateEnvelope(t *testing.T) {
	t.Run("GenerateEnvelope test", func(t *testing.T) {
		keyResp := proto.GenerateEnvelopeResponse{
			SignatureEnvelope:     []byte("{}"),
			SignatureEnvelopeType: "jws",
			Annotations:           map[string]string{},
		}
		output, err := json.Marshal(keyResp)
		if err != nil {
			t.Fatal("should not error.")
		}
		executor = testCommander{stdout: output, err: nil}

		plugin := CLIPlugin{}
		resp, err := plugin.GenerateEnvelope(context.Background(), &proto.GenerateEnvelopeRequest{})
		if err != nil {
			t.Fatalf("should not error. got err = %v", err)
		}
		if reflect.DeepEqual(resp, keyResp) {
			t.Fatalf("GenerateEnvelope() error. got: %+v, want: %+v", resp, keyResp)
		}
	})
}

func TestVerifySignature(t *testing.T) {
	t.Run("VerifySignature test", func(t *testing.T) {
		keyResp := proto.VerifySignatureResponse{
			VerificationResults: map[proto.Capability]*proto.VerificationResult{},
			ProcessedAttributes: []interface{}{"attr1"},
		}
		output, err := json.Marshal(keyResp)
		if err != nil {
			t.Fatal("should not error.")
		}
		executor = testCommander{stdout: output, err: nil}

		plugin := CLIPlugin{}
		resp, err := plugin.VerifySignature(context.Background(), &proto.VerifySignatureRequest{})
		if err != nil {
			t.Fatalf("should not error. got err = %v", err)
		}
		if reflect.DeepEqual(resp, keyResp) {
			t.Fatalf("VerifySignature() error. got: %+v, want: %+v", resp, keyResp)
		}
	})
}

func TestValidateMetadata(t *testing.T) {
	tests := []struct {
		m       *proto.GetMetadataResponse
		wantErr bool
	}{
		{&proto.GetMetadataResponse{}, true},
		{&proto.GetMetadataResponse{Name: "name"}, true},
		{&proto.GetMetadataResponse{Name: "name", Description: "friendly"}, true},
		{&proto.GetMetadataResponse{Name: "name", Description: "friendly", Version: "1"}, true},
		{&proto.GetMetadataResponse{Name: "name", Description: "friendly", Version: "1", URL: "example.com"}, true},
		{&proto.GetMetadataResponse{Name: "name", Description: "friendly", Version: "1", URL: "example.com", Capabilities: []proto.Capability{"cap"}}, true},
		{&proto.GetMetadataResponse{Name: "name", Description: "friendly", Version: "1", URL: "example.com", SupportedContractVersions: []string{"1.0"}}, true},
		{&proto.GetMetadataResponse{Name: "name", Description: "friendly", Version: "1", URL: "example.com", SupportedContractVersions: []string{"1.0"}, Capabilities: []proto.Capability{"cap"}}, false},
	}
	for i, tt := range tests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			if err := validate(tt.m); (err != nil) != tt.wantErr {
				t.Errorf("GetMetadataResponse.Validate() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestNewCLIPlugin_PathError(t *testing.T) {
	ctx := context.Background()
	t.Run("plugin directory exists without executable.", func(t *testing.T) {
		p, err := NewCLIPlugin(ctx, "emptyplugin", "./testdata/plugins/emptyplugin/notation-emptyplugin")
		if !errors.Is(err, os.ErrNotExist) {
			t.Errorf("NewCLIPlugin() error = %v, want %v", err, os.ErrNotExist)
		}
		if p != nil {
			t.Errorf("NewCLIPlugin() plugin = %v, want nil", p)
		}
	})

	t.Run("plugin is not a regular file", func(t *testing.T) {
		expectedErrMsg := "plugin executable file is not a regular file"
		p, err := NewCLIPlugin(ctx, "badplugin", "./testdata/plugins/badplugin/notation-badplugin")
		if err.Error() != expectedErrMsg {
			t.Errorf("NewCLIPlugin() error = %v, want %v", err, expectedErrMsg)
		}
		if p != nil {
			t.Errorf("NewCLIPlugin() plugin = %v, want nil", p)
		}
	})
}

func TestNewCLIPlugin_ValidError(t *testing.T) {
	ctx := context.Background()
	p, err := NewCLIPlugin(ctx, "foo", "./testdata/plugins/foo/notation-foo")
	if err != nil {
		t.Fatal("should no error.")
	}
	t.Run("command no response", func(t *testing.T) {
		executor = testCommander{}
		_, err := p.GetMetadata(ctx, &proto.GetMetadataRequest{})
		if _, ok := err.(*PluginMalformedError); !ok {
			t.Fatal("should return plugin validity error")
		}
	})

	t.Run("invalid json", func(t *testing.T) {
		executor = testCommander{stdout: []byte("content")}
		_, err := p.GetMetadata(ctx, &proto.GetMetadataRequest{})
		if _, ok := err.(*PluginMalformedError); !ok {
			t.Fatal("should return plugin validity error")
		}
	})

	t.Run("invalid metadata name", func(t *testing.T) {
		executor = testCommander{stdout: metadataJSON(invalidMetadataName)}
		_, err := p.GetMetadata(ctx, &proto.GetMetadataRequest{})
		if !strings.Contains(err.Error(), "executable file name must be") {
			t.Fatal("should fail the operation.")
		}
	})

	t.Run("invalid metadata content", func(t *testing.T) {
		executor = testCommander{stdout: metadataJSON(proto.GetMetadataResponse{Name: "foo"})}
		_, err := p.GetMetadata(ctx, &proto.GetMetadataRequest{})
		if _, ok := err.(*PluginMalformedError); !ok {
			t.Fatal("should be plugin validity error.")
		}
	})

	t.Run("valid", func(t *testing.T) {
		executor = testCommander{stdout: metadataJSON(validMetadata)}
		_, err := p.GetMetadata(ctx, &proto.GetMetadataRequest{})
		if err != nil {
			t.Fatalf("should valid. got err = %v", err)
		}
		metadata, err := p.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
		if err != nil {
			t.Fatalf("should valid. got err = %v", err)
		}
		if !reflect.DeepEqual(metadata, &validMetadata) {
			t.Fatalf("should be equal. got metadata = %+v, want %+v", metadata, validMetadata)
		}
	})

	t.Run("invalid contract version", func(t *testing.T) {
		executor = testCommander{stdout: metadataJSON(invalidContractVersionMetadata)}
		_, err := p.GetMetadata(ctx, &proto.GetMetadataRequest{})
		if err == nil {
			t.Fatal("should have an invalid contract version error")
		}
	})
}