|
| 1 | +package ctpolicy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/letsencrypt/boulder/cmd" |
| 10 | + "github.com/letsencrypt/boulder/core" |
| 11 | + blog "github.com/letsencrypt/boulder/log" |
| 12 | + pubpb "github.com/letsencrypt/boulder/publisher/proto" |
| 13 | + "github.com/letsencrypt/boulder/test" |
| 14 | +) |
| 15 | + |
| 16 | +type mockPub struct { |
| 17 | +} |
| 18 | + |
| 19 | +func (mp *mockPub) SubmitToCT(ctx context.Context, der []byte) error { |
| 20 | + return nil |
| 21 | +} |
| 22 | +func (mp *mockPub) SubmitToSingleCT(ctx context.Context, logURL, logPublicKey string, der []byte) error { |
| 23 | + return nil |
| 24 | +} |
| 25 | +func (mp *mockPub) SubmitToSingleCTWithResult(_ context.Context, _ *pubpb.Request) (*pubpb.Result, error) { |
| 26 | + return &pubpb.Result{Sct: []byte{0}}, nil |
| 27 | +} |
| 28 | + |
| 29 | +type alwaysFail struct { |
| 30 | + mockPub |
| 31 | +} |
| 32 | + |
| 33 | +func (mp *alwaysFail) SubmitToSingleCTWithResult(_ context.Context, _ *pubpb.Request) (*pubpb.Result, error) { |
| 34 | + return nil, errors.New("BAD") |
| 35 | +} |
| 36 | + |
| 37 | +func TestGetSCTs(t *testing.T) { |
| 38 | + expired, cancel := context.WithDeadline(context.Background(), time.Now()) |
| 39 | + defer cancel() |
| 40 | + testCases := []struct { |
| 41 | + name string |
| 42 | + mock core.Publisher |
| 43 | + groups [][]cmd.LogDescription |
| 44 | + ctx context.Context |
| 45 | + result []core.SCTDER |
| 46 | + err error |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "basic success case", |
| 50 | + mock: &mockPub{}, |
| 51 | + groups: [][]cmd.LogDescription{ |
| 52 | + { |
| 53 | + {URI: "abc", Key: "def"}, |
| 54 | + {URI: "ghi", Key: "jkl"}, |
| 55 | + }, |
| 56 | + { |
| 57 | + {URI: "abc", Key: "def"}, |
| 58 | + {URI: "ghi", Key: "jkl"}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + ctx: context.Background(), |
| 62 | + result: []core.SCTDER{[]byte{0}, []byte{0}}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "basic failure case", |
| 66 | + mock: &alwaysFail{}, |
| 67 | + groups: [][]cmd.LogDescription{ |
| 68 | + { |
| 69 | + {URI: "abc", Key: "def"}, |
| 70 | + {URI: "ghi", Key: "jkl"}, |
| 71 | + }, |
| 72 | + { |
| 73 | + {URI: "abc", Key: "def"}, |
| 74 | + {URI: "ghi", Key: "jkl"}, |
| 75 | + }, |
| 76 | + }, |
| 77 | + ctx: context.Background(), |
| 78 | + err: errors.New("all submissions for group failed"), |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "parent context timeout failure case", |
| 82 | + mock: &alwaysFail{}, |
| 83 | + groups: [][]cmd.LogDescription{ |
| 84 | + { |
| 85 | + {URI: "abc", Key: "def"}, |
| 86 | + {URI: "ghi", Key: "jkl"}, |
| 87 | + }, |
| 88 | + { |
| 89 | + {URI: "abc", Key: "def"}, |
| 90 | + {URI: "ghi", Key: "jkl"}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + ctx: expired, |
| 94 | + err: context.DeadlineExceeded, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for _, tc := range testCases { |
| 99 | + t.Run(tc.name, func(t *testing.T) { |
| 100 | + ctp := New(tc.mock, tc.groups, blog.NewMock()) |
| 101 | + ret, err := ctp.GetSCTs(tc.ctx, []byte{0}) |
| 102 | + if tc.result != nil { |
| 103 | + test.AssertDeepEquals(t, ret, tc.result) |
| 104 | + } else if tc.err != nil { |
| 105 | + test.AssertDeepEquals(t, err, tc.err) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments