| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package jws |
| 15 |
|
| 16 |
import ( |
| 17 |
"encoding/json" |
| 18 |
"math" |
| 19 |
"testing" |
| 20 |
) |
| 21 |
|
| 22 |
func Test_convertToMap(t *testing.T) { |
| 23 |
type S struct { |
| 24 |
A string |
| 25 |
B int |
| 26 |
C float64 |
| 27 |
} |
| 28 |
t.Run("invalid value", func(t *testing.T) { |
| 29 |
_, err := convertToMap(math.Inf(1)) |
| 30 |
if err == nil { |
| 31 |
t.Fatal("should cause error") |
| 32 |
} |
| 33 |
}) |
| 34 |
|
| 35 |
t.Run("normal case", func(t *testing.T) { |
| 36 |
testStruct := S{ |
| 37 |
A: "test string", |
| 38 |
B: 1, |
| 39 |
C: 1.1, |
| 40 |
} |
| 41 |
// generate map |
| 42 |
m, err := convertToMap(&testStruct) |
| 43 |
checkNoError(t, err) |
| 44 |
|
| 45 |
// convert map to struct |
| 46 |
bytes, err := json.Marshal(m) |
| 47 |
checkNoError(t, err) |
| 48 |
|
| 49 |
var newStruct S |
| 50 |
err = json.Unmarshal(bytes, &newStruct) |
| 51 |
checkNoError(t, err) |
| 52 |
|
| 53 |
// check new struct equal with original struct |
| 54 |
if newStruct != testStruct { |
| 55 |
t.Fatal("convertToMap error") |
| 56 |
} |
| 57 |
}) |
| 58 |
} |
| 59 |
|
| 60 |
func Test_generateJWSError(t *testing.T) { |
| 61 |
_, err := generateJWS("", nil, nil) |
| 62 |
checkErrorEqual(t, "unexpected error occurred while generating a JWS-JSON serialization from compact serialization", err.Error()) |
| 63 |
} |
| 64 |
|
| 65 |
func Test_getSignatureAlgorithmError(t *testing.T) { |
| 66 |
_, err := getSignatureAlgorithm("ES222") |
| 67 |
checkErrorEqual(t, `signature algorithm "ES222" is not supported`, err.Error()) |
| 68 |
} |