File: util_test.go

package info (click to toggle)
golang-github-docker-libtrust 0.0~git20150526.0.9cbd2a1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, buster-proposed-updates, sid
  • size: 288 kB
  • ctags: 297
  • sloc: makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,420 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
package libtrust

import (
	"encoding/pem"
	"reflect"
	"testing"
)

func TestAddPEMHeadersToKey(t *testing.T) {
	pk := &rsaPublicKey{nil, map[string]interface{}{}}
	blk := &pem.Block{Headers: map[string]string{"hosts": "localhost,127.0.0.1"}}
	addPEMHeadersToKey(blk, pk)

	val := pk.GetExtendedField("hosts")
	hosts, ok := val.([]string)
	if !ok {
		t.Fatalf("hosts type(%v), expected []string", reflect.TypeOf(val))
	}
	expected := []string{"localhost", "127.0.0.1"}
	if !reflect.DeepEqual(hosts, expected) {
		t.Errorf("hosts(%v), expected %v", hosts, expected)
	}
}

func TestBase64URL(t *testing.T) {
	clean := "eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJwMnMiOiIyV0NUY0paMVJ2ZF9DSnVKcmlwUTF3IiwicDJjIjo0MDk2LCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiY3R5IjoiandrK2pzb24ifQ"

	tests := []string{
		clean, // clean roundtrip
		"eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJwMnMiOiIyV0NUY0paMVJ2\nZF9DSnVKcmlwUTF3IiwicDJjIjo0MDk2LCJlbmMiOiJBMTI4Q0JDLUhTMjU2\nIiwiY3R5IjoiandrK2pzb24ifQ",     // with newlines
		"eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJwMnMiOiIyV0NUY0paMVJ2 \n ZF9DSnVKcmlwUTF3IiwicDJjIjo0MDk2LCJlbmMiOiJBMTI4Q0JDLUhTMjU2 \n IiwiY3R5IjoiandrK2pzb24ifQ", // with newlines and spaces
	}

	for i, test := range tests {
		b, err := joseBase64UrlDecode(test)
		if err != nil {
			t.Fatalf("on test %d: %s", i, err)
		}
		got := joseBase64UrlEncode(b)

		if got != clean {
			t.Errorf("expected %q, got %q", clean, got)
		}
	}
}