File
Blob: acme/solver_test.go
| 1 | package acme |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "testing" |
| 7 | |
| 8 | acmeSpec "go.miragespace.co/specter/spec/acme" |
| 9 | "go.miragespace.co/specter/spec/chord" |
| 10 | "go.miragespace.co/specter/spec/mocks" |
| 11 | "go.miragespace.co/specter/spec/protocol" |
| 12 | "go.miragespace.co/specter/spec/tun" |
| 13 | |
| 14 | "github.com/mholt/acmez/v3/acme" |
| 15 | "github.com/stretchr/testify/mock" |
| 16 | "github.com/stretchr/testify/require" |
| 17 | ) |
| 18 | |
| 19 | const ( |
| 20 | testManagedDomain = "acme.example.com" |
| 21 | testDynamicDomain = "dynamic.example.com" |
| 22 | ) |
| 23 | |
| 24 | func TestSolverManaged(t *testing.T) { |
| 25 | as := require.New(t) |
| 26 | mockKV := new(mocks.VNode) |
| 27 | |
| 28 | solver := &ChordSolver{ |
| 29 | KV: mockKV, |
| 30 | ManagedDomains: []string{testManagedDomain}, |
| 31 | } |
| 32 | |
| 33 | append := mockKV.On("PrefixAppend", mock.Anything, mock.MatchedBy(func(key []byte) bool { |
| 34 | return bytes.Equal([]byte(dnsKeyName(acmeSpec.ManagedDelegation)), key) |
| 35 | }), mock.Anything).Return(nil) |
| 36 | |
| 37 | mockKV.On("PrefixRemove", mock.Anything, mock.MatchedBy(func(key []byte) bool { |
| 38 | return bytes.Equal([]byte(dnsKeyName(acmeSpec.ManagedDelegation)), key) |
| 39 | }), mock.Anything).Return(nil).NotBefore(append) |
| 40 | |
| 41 | err := solver.Present(context.Background(), acme.Challenge{ |
| 42 | Identifier: acme.Identifier{ |
| 43 | Type: "dns", |
| 44 | Value: testManagedDomain, |
| 45 | }, |
| 46 | }) |
| 47 | as.NoError(err) |
| 48 | |
| 49 | err = solver.CleanUp(context.Background(), acme.Challenge{ |
| 50 | Identifier: acme.Identifier{ |
| 51 | Type: "dns", |
| 52 | Value: testManagedDomain, |
| 53 | }, |
| 54 | }) |
| 55 | as.NoError(err) |
| 56 | |
| 57 | mockKV.AssertExpectations(t) |
| 58 | } |
| 59 | |
| 60 | func TestSolverDynamic(t *testing.T) { |
| 61 | as := require.New(t) |
| 62 | mockKV := new(mocks.VNode) |
| 63 | |
| 64 | solver := &ChordSolver{ |
| 65 | KV: mockKV, |
| 66 | ManagedDomains: []string{testManagedDomain}, |
| 67 | } |
| 68 | |
| 69 | testClient := &protocol.Node{ |
| 70 | Id: chord.Random(), |
| 71 | Address: "addr", |
| 72 | } |
| 73 | testToken := &protocol.ClientToken{ |
| 74 | Token: []byte("token"), |
| 75 | } |
| 76 | testBundle := &protocol.CustomHostname{ |
| 77 | ClientIdentity: testClient, |
| 78 | ClientToken: testToken, |
| 79 | } |
| 80 | bundleBuf, err := testBundle.MarshalVT() |
| 81 | as.NoError(err) |
| 82 | |
| 83 | get := mockKV.On("Get", mock.Anything, mock.MatchedBy(func(key []byte) bool { |
| 84 | return bytes.Equal([]byte(tun.CustomHostnameKey(testDynamicDomain)), key) |
| 85 | })).Return(bundleBuf, nil) |
| 86 | |
| 87 | append := mockKV.On("PrefixAppend", mock.Anything, mock.MatchedBy(func(key []byte) bool { |
| 88 | return bytes.Equal([]byte(dnsKeyName(acmeSpec.EncodeClientToken(testToken.GetToken()))), key) |
| 89 | }), mock.Anything).Return(nil).NotBefore(get) |
| 90 | |
| 91 | mockKV.On("PrefixRemove", mock.Anything, mock.MatchedBy(func(key []byte) bool { |
| 92 | return bytes.Equal([]byte(dnsKeyName(acmeSpec.EncodeClientToken(testToken.GetToken()))), key) |
| 93 | }), mock.Anything).Return(nil).NotBefore(append) |
| 94 | |
| 95 | err = solver.Present(context.Background(), acme.Challenge{ |
| 96 | Identifier: acme.Identifier{ |
| 97 | Type: "dns", |
| 98 | Value: testDynamicDomain, |
| 99 | }, |
| 100 | }) |
| 101 | as.NoError(err) |
| 102 | |
| 103 | err = solver.CleanUp(context.Background(), acme.Challenge{ |
| 104 | Identifier: acme.Identifier{ |
| 105 | Type: "dns", |
| 106 | Value: testDynamicDomain, |
| 107 | }, |
| 108 | }) |
| 109 | as.NoError(err) |
| 110 | |
| 111 | mockKV.AssertExpectations(t) |
| 112 | } |