File: jws_test.go

package info (click to toggle)
golang-github-notaryproject-notation-core-go 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,036 kB
  • sloc: makefile: 22
file content (68 lines) | stat: -rw-r--r-- 1,757 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
// 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 jws

import (
	"encoding/json"
	"math"
	"testing"
)

func Test_convertToMap(t *testing.T) {
	type S struct {
		A string
		B int
		C float64
	}
	t.Run("invalid value", func(t *testing.T) {
		_, err := convertToMap(math.Inf(1))
		if err == nil {
			t.Fatal("should cause error")
		}
	})

	t.Run("normal case", func(t *testing.T) {
		testStruct := S{
			A: "test string",
			B: 1,
			C: 1.1,
		}
		// generate map
		m, err := convertToMap(&testStruct)
		checkNoError(t, err)

		// convert map to struct
		bytes, err := json.Marshal(m)
		checkNoError(t, err)

		var newStruct S
		err = json.Unmarshal(bytes, &newStruct)
		checkNoError(t, err)

		// check new struct equal with original struct
		if newStruct != testStruct {
			t.Fatal("convertToMap error")
		}
	})
}

func Test_generateJWSError(t *testing.T) {
	_, err := generateJWS("", nil, nil)
	checkErrorEqual(t, "unexpected error occurred while generating a JWS-JSON serialization from compact serialization", err.Error())
}

func Test_getSignatureAlgorithmError(t *testing.T) {
	_, err := getSignatureAlgorithm("ES222")
	checkErrorEqual(t, `signature algorithm "ES222" is not supported`, err.Error())
}