• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pact-foundation / pact-go / 12931992344

23 Jan 2025 03:03PM UTC coverage: 28.209% (-1.0%) from 29.225%
12931992344

push

github

web-flow
Merge pull request #454 from YOU54F/feat/linux-musl

feat: support linux musl

9 of 13 new or added lines in 1 file covered. (69.23%)

68 existing lines in 2 files now uncovered.

1802 of 6388 relevant lines covered (28.21%)

8.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/internal/native/verifier.go
1
package native
2

3
/*
4
#include "pact.h"
5
*/
6
import "C"
7

8
import (
9
        "fmt"
10
        "log"
11
        "strings"
12
        "unsafe"
13
)
14

15
type Verifier struct {
16
        handle *C.VerifierHandle
17
}
18

UNCOV
19
func (v *Verifier) Verify(args []string) error {
×
UNCOV
20
        log.Println("[DEBUG] executing verifier FFI with args", args)
×
UNCOV
21
        cargs := C.CString(strings.Join(args, "\n"))
×
UNCOV
22
        defer free(cargs)
×
UNCOV
23
        result := C.pactffi_verify(cargs)
×
UNCOV
24

×
UNCOV
25
        /// | Error | Description |
×
UNCOV
26
        /// |-------|-------------|
×
UNCOV
27
        /// | 1 | The verification process failed, see output for errors |
×
UNCOV
28
        /// | 2 | A null pointer was received |
×
UNCOV
29
        /// | 3 | The method panicked |
×
UNCOV
30
        switch int(result) {
×
31
        case 0:
×
32
                return nil
×
33
        case 1:
×
34
                return ErrVerifierFailed
×
35
        case 2:
×
36
                return ErrInvalidVerifierConfig
×
37
        case 3:
×
38
                return ErrVerifierPanic
×
UNCOV
39
        default:
×
UNCOV
40
                return fmt.Errorf("an unknown error (%d) ocurred when verifying the provider (this indicates a defect in the framework)", int(result))
×
41
        }
42
}
43

44
// Version returns the current semver FFI interface version
45
func (v *Verifier) Version() string {
×
46
        return Version()
×
47
}
×
48

49
var (
50
        // ErrVerifierPanic indicates a panic ocurred when invoking the verifier.
51
        ErrVerifierPanic = fmt.Errorf("a general panic occured when starting/invoking verifier (this indicates a defect in the framework)")
52

53
        // ErrInvalidVerifierConfig indicates an issue configuring the verifier
54
        ErrInvalidVerifierConfig = fmt.Errorf("configuration for the verifier was invalid and an unknown error occurred (this is most likely a defect in the framework)")
55

56
        //ErrVerifierFailed is the standard error if a verification failed (e.g. beacause the pact verification was not successful)
57
        ErrVerifierFailed = fmt.Errorf("the verifier failed to successfully verify the pacts, this indicates an issue with the provider API")
58
        //ErrVerifierFailedToRun indicates the verification process was unable to run
59
        ErrVerifierFailedToRun = fmt.Errorf("the verifier failed to execute (this is most likely a defect in the framework)")
60
)
61

UNCOV
62
func NewVerifier(name string, version string) *Verifier {
×
UNCOV
63
        cName := C.CString(name)
×
UNCOV
64
        cVersion := C.CString(version)
×
UNCOV
65
        defer free(cName)
×
UNCOV
66
        defer free(cVersion)
×
UNCOV
67

×
UNCOV
68
        h := C.pactffi_verifier_new_for_application(cName, cVersion)
×
UNCOV
69

×
UNCOV
70
        return &Verifier{
×
UNCOV
71
                handle: h,
×
UNCOV
72
        }
×
UNCOV
73
}
×
74

UNCOV
75
func (v *Verifier) Shutdown() {
×
UNCOV
76
        C.pactffi_verifier_shutdown(v.handle)
×
UNCOV
77
}
×
78

UNCOV
79
func (v *Verifier) SetProviderInfo(name string, scheme string, host string, port uint16, path string) {
×
UNCOV
80
        cName := C.CString(name)
×
UNCOV
81
        defer free(cName)
×
UNCOV
82
        cScheme := C.CString(scheme)
×
UNCOV
83
        defer free(cScheme)
×
UNCOV
84
        cHost := C.CString(host)
×
UNCOV
85
        defer free(cHost)
×
UNCOV
86
        cPort := C.ushort(port)
×
UNCOV
87
        cPath := C.CString(path)
×
UNCOV
88
        defer free(cPath)
×
UNCOV
89

×
UNCOV
90
        C.pactffi_verifier_set_provider_info(v.handle, cName, cScheme, cHost, cPort, cPath)
×
UNCOV
91
}
×
92

93
func (v *Verifier) AddTransport(protocol string, port uint16, path string, scheme string) {
×
94
        log.Println("[DEBUG] Adding transport with protocol:", protocol, "port:", port, "path:", path, "scheme:", scheme)
×
95
        cProtocol := C.CString(protocol)
×
96
        defer free(cProtocol)
×
97
        cPort := C.ushort(port)
×
98
        cPath := C.CString(path)
×
99
        defer free(cPath)
×
100
        cScheme := C.CString(scheme)
×
101
        defer free(cScheme)
×
102

×
103
        C.pactffi_verifier_add_provider_transport(v.handle, cProtocol, cPort, cPath, cScheme)
×
104
}
×
105

106
func (v *Verifier) SetFilterInfo(description string, state string, noState bool) {
×
107
        cFilterDescription := C.CString(description)
×
108
        defer free(cFilterDescription)
×
109
        cFilterState := C.CString(state)
×
110
        defer free(cFilterState)
×
111

×
112
        C.pactffi_verifier_set_filter_info(v.handle, cFilterDescription, cFilterState, boolToCUchar(noState))
×
113
}
×
114

115
func (v *Verifier) SetProviderState(url string, teardown bool, body bool) {
×
116
        cURL := C.CString(url)
×
117
        defer free(cURL)
×
118

×
119
        C.pactffi_verifier_set_provider_state(v.handle, cURL, boolToCUchar(teardown), boolToCUchar(body))
×
120
}
×
121

122
func (v *Verifier) SetVerificationOptions(disableSSLVerification bool, requestTimeout int64) {
×
123
        // TODO: this returns an int and therefore can error. We should have all of these functions return values??
×
124
        C.pactffi_verifier_set_verification_options(v.handle, boolToCUchar(disableSSLVerification), C.ulong(requestTimeout))
×
125
}
×
126

UNCOV
127
func (v *Verifier) SetConsumerFilters(consumers []string) {
×
UNCOV
128
        // TODO: check if this actually works!
×
UNCOV
129
        C.pactffi_verifier_set_consumer_filters(v.handle, stringArrayToCStringArray(consumers), C.ushort(len(consumers)))
×
UNCOV
130
}
×
131

132
func (v *Verifier) AddCustomHeader(name string, value string) {
×
133
        cHeaderName := C.CString(name)
×
134
        defer free(cHeaderName)
×
135
        cHeaderValue := C.CString(value)
×
136
        defer free(cHeaderValue)
×
137

×
138
        C.pactffi_verifier_add_custom_header(v.handle, cHeaderName, cHeaderValue)
×
139
}
×
140

141
func (v *Verifier) AddFileSource(file string) {
×
142
        cFile := C.CString(file)
×
143
        defer free(cFile)
×
144

×
145
        C.pactffi_verifier_add_file_source(v.handle, cFile)
×
146
}
×
147

148
func (v *Verifier) AddDirectorySource(directory string) {
×
149
        cDirectory := C.CString(directory)
×
150
        defer free(cDirectory)
×
151

×
152
        C.pactffi_verifier_add_directory_source(v.handle, cDirectory)
×
153
}
×
154

155
func (v *Verifier) AddURLSource(url string, username string, password string, token string) {
×
156
        cUrl := C.CString(url)
×
157
        defer free(cUrl)
×
158
        cUsername := C.CString(username)
×
159
        defer free(cUsername)
×
160
        cPassword := C.CString(password)
×
161
        defer free(cPassword)
×
162
        cToken := C.CString(token)
×
163
        defer free(cToken)
×
164

×
165
        C.pactffi_verifier_url_source(v.handle, cUrl, cUsername, cPassword, cToken)
×
166
}
×
167

168
func (v *Verifier) BrokerSourceWithSelectors(url string, username string, password string, token string, enablePending bool, includeWipPactsSince string, providerTags []string, providerBranch string, selectors []string, consumerVersionTags []string) {
×
169
        cUrl := C.CString(url)
×
170
        defer free(cUrl)
×
171
        cUsername := C.CString(username)
×
172
        defer free(cUsername)
×
173
        cPassword := C.CString(password)
×
174
        defer free(cPassword)
×
175
        cToken := C.CString(token)
×
176
        defer free(cToken)
×
177
        cIncludeWipPactsSince := C.CString(includeWipPactsSince)
×
178
        defer free(cIncludeWipPactsSince)
×
179
        cProviderBranch := C.CString(providerBranch)
×
180
        defer free(cProviderBranch)
×
181

×
182
        C.pactffi_verifier_broker_source_with_selectors(v.handle, cUrl, cUsername, cPassword, cToken, boolToCUchar(enablePending), cIncludeWipPactsSince, stringArrayToCStringArray(providerTags), C.ushort(len(providerTags)), cProviderBranch, stringArrayToCStringArray(selectors), C.ushort(len(selectors)), stringArrayToCStringArray(consumerVersionTags), C.ushort(len(consumerVersionTags)))
×
183
}
×
184

185
func (v *Verifier) SetPublishOptions(providerVersion string, buildUrl string, providerTags []string, providerBranch string) {
×
186
        cProviderVersion := C.CString(providerVersion)
×
187
        defer free(cProviderVersion)
×
188
        cBuildUrl := C.CString(buildUrl)
×
189
        defer free(cBuildUrl)
×
190
        cProviderBranch := C.CString(providerBranch)
×
191
        defer free(cProviderBranch)
×
192

×
193
        C.pactffi_verifier_set_publish_options(v.handle, cProviderVersion, cBuildUrl, stringArrayToCStringArray(providerTags), C.ushort(len(providerTags)), cProviderBranch)
×
194
}
×
195

UNCOV
196
func (v *Verifier) Execute() error {
×
UNCOV
197
        // TODO: Validate
×
UNCOV
198
        result := C.pactffi_verifier_execute(v.handle)
×
UNCOV
199

×
UNCOV
200
        /// | Error | Description |
×
UNCOV
201
        /// |-------|-------------|
×
UNCOV
202
        /// | 1     | The verification process failed, see output for errors |
×
UNCOV
203
        switch int(result) {
×
UNCOV
204
        case 0:
×
UNCOV
205
                return nil
×
206
        case 1:
×
207
                return ErrVerifierFailed
×
208
        case 2:
×
209
                return ErrVerifierFailedToRun
×
210
        default:
×
211
                return fmt.Errorf("an unknown error (%d) ocurred when verifying the provider (this indicates a defect in the framework)", int(result))
×
212
        }
213
}
214

215
func (v *Verifier) SetNoPactsIsError(isError bool) {
×
216
        C.pactffi_verifier_set_no_pacts_is_error(v.handle, boolToCUchar(isError))
×
217
}
×
218

219
func (v *Verifier) SetColoredOutput(isColoredOutput bool) {
×
220
        C.pactffi_verifier_set_coloured_output(v.handle, boolToCUchar(isColoredOutput))
×
221
}
×
222

UNCOV
223
func stringArrayToCStringArray(inputs []string) **C.char {
×
UNCOV
224
        if len(inputs) == 0 {
×
225
                return nil
×
226
        }
×
227

UNCOV
228
        output := make([]*C.char, len(inputs))
×
UNCOV
229

×
UNCOV
230
        for i, consumer := range inputs {
×
UNCOV
231
                output[i] = C.CString(consumer)
×
UNCOV
232
        }
×
233

UNCOV
234
        return (**C.char)(unsafe.Pointer(&output[0]))
×
235
}
236

237
func boolToCUchar(val bool) C.uchar {
×
238
        if val {
×
239
                return C.uchar(1)
×
240
        }
×
241
        return C.uchar(0)
×
242
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc