xref: /aosp_15_r20/external/tink/go/subtle/subtle_test.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15////////////////////////////////////////////////////////////////////////////////
16
17package subtle_test
18
19import (
20	"encoding/hex"
21	"hash"
22	"testing"
23
24	"github.com/google/tink/go/subtle"
25)
26
27func TestConvertHashName(t *testing.T) {
28	if subtle.ConvertHashName("SHA-256") != "SHA256" ||
29		subtle.ConvertHashName("SHA-1") != "SHA1" ||
30		subtle.ConvertHashName("SHA-512") != "SHA512" ||
31		subtle.ConvertHashName("UNKNOWN_HASH") != "" {
32		t.Errorf("incorrect hash name conversion")
33	}
34}
35
36func TestConvertCurveName(t *testing.T) {
37	if subtle.ConvertCurveName("secp256r1") != "NIST_P256" ||
38		subtle.ConvertCurveName("secp384r1") != "NIST_P384" ||
39		subtle.ConvertCurveName("secp521r1") != "NIST_P521" ||
40		subtle.ConvertCurveName("UNKNOWN_CURVE") != "" {
41		t.Errorf("incorrect curve name conversion")
42	}
43}
44
45func TestComputeHash(t *testing.T) {
46	data := []byte("Hello")
47	var tests = []struct {
48		hashFunc     func() hash.Hash
49		expectedHash string
50	}{
51		{subtle.GetHashFunc("SHA1"), "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0"},
52		{subtle.GetHashFunc("SHA256"), "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969"},
53		{subtle.GetHashFunc("SHA512"), "3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315"},
54	}
55
56	for _, tt := range tests {
57		hashFunc := tt.hashFunc
58		if hashFunc == nil {
59			t.Fatal("got nil hash func")
60		}
61		hashed, err := subtle.ComputeHash(hashFunc, data)
62		if err != nil {
63			t.Fatalf("got error: %q", err)
64		}
65		if gotHash := hex.EncodeToString(hashed); gotHash != tt.expectedHash {
66			t.Fatalf("Expected: %s. Got: %s", tt.expectedHash, gotHash)
67		}
68	}
69
70	// unknown
71	if subtle.GetHashFunc("UNKNOWN_HASH") != nil {
72		t.Errorf("unexpected result for invalid hash types")
73	}
74}
75
76func TestGetCurve(t *testing.T) {
77	if subtle.GetCurve("NIST_P256").Params().Name != "P-256" {
78		t.Errorf("incorrect result for NIST_P256")
79	}
80	if subtle.GetCurve("NIST_P384").Params().Name != "P-384" {
81		t.Errorf("incorrect result for NIST_P384")
82	}
83	if subtle.GetCurve("NIST_P521").Params().Name != "P-521" {
84		t.Errorf("incorrect result for NIST_P521")
85	}
86	if subtle.GetCurve("UNKNOWN_CURVE") != nil {
87		t.Errorf("expect nil when curve is unknown")
88	}
89}
90