...
1 package identity
2
3 import (
4 "context"
5 "testing"
6
7 pb "github.com/linkerd/linkerd2-proxy-api/go/identity"
8 "github.com/linkerd/linkerd2/pkg/tls"
9 )
10
11 func TestServiceNotReady(t *testing.T) {
12
13 svc := NewService(&fakeValidator{"successful-result", nil}, nil, nil, nil, "", "", "")
14 req := &pb.CertifyRequest{
15 Identity: "some-identity",
16 Token: []byte{},
17 CertificateSigningRequest: []byte{},
18 }
19
20 _, err := svc.Certify(context.TODO(), req)
21
22 expectedError := "rpc error: code = Unavailable desc = cert issuer not ready yet"
23
24 if err != nil {
25 if err.Error() != expectedError {
26 t.Fatalf("Expected error string\"%s\", got \"%s\"", expectedError, err)
27 }
28 } else {
29 t.Fatalf("Expected error but got got nothing")
30 }
31 }
32
33 func TestInvalidRequestArguments(t *testing.T) {
34 svc := NewService(&fakeValidator{"successful-result", nil}, nil, nil, nil, "", "", "")
35 svc.updateIssuer(&fakeIssuer{tls.Crt{}, nil})
36 fakeData := "fake-data"
37 invalidCsr := func() *pb.CertifyRequest {
38 return &pb.CertifyRequest{
39 Identity: fakeData,
40 Token: []byte(fakeData),
41 CertificateSigningRequest: []byte(fakeData),
42 }
43 }
44
45 reqNoIdentity := invalidCsr()
46 reqNoIdentity.Identity = ""
47
48 reqNoToken := invalidCsr()
49 reqNoToken.Token = []byte{}
50
51 reqNoCsr := invalidCsr()
52 reqNoCsr.CertificateSigningRequest = []byte{}
53
54 testCases := []struct {
55 input *pb.CertifyRequest
56 expectedError string
57 }{
58 {reqNoIdentity, "rpc error: code = InvalidArgument desc = missing identity"},
59 {reqNoToken, "rpc error: code = InvalidArgument desc = missing token"},
60 {reqNoCsr, "rpc error: code = InvalidArgument desc = missing certificate signing request"},
61 {invalidCsr(), "rpc error: code = InvalidArgument desc = asn1: structure error: tags don't match " +
62 "(16 vs {class:1 tag:6 length:97 isCompound:true}) " +
63 "{optional:false explicit:false application:false private:false defaultValue:<nil> " +
64 "tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} certificateRequest @2"},
65 }
66
67 for _, tc := range testCases {
68
69 _, err := svc.Certify(context.TODO(), tc.input)
70 if tc.expectedError != "" {
71 if err == nil {
72 t.Fatal("Expected error, got nothing")
73 }
74 if err.Error() != tc.expectedError {
75 t.Fatalf("Expected error string\"%s\", got \"%s\"", tc.expectedError, err)
76 }
77 }
78 }
79
80 }
81
View as plain text