This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapi.go
More file actions
85 lines (71 loc) · 2.02 KB
/
Copy pathapi.go
File metadata and controls
85 lines (71 loc) · 2.02 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Package protocols is where the protocol-testers live.
//
// Tests are dynamically instantiated at run-time, via a class-factory
// pattern, and due to their plugin nature they are simple to implement
// as they require only implementing a single method.
//
package protocols
import (
"sync"
"github.com/skx/overseer/test"
)
// ProtocolTest interface is the core of our code, it
// defines the implementation methods which must be
// implemented to add a new protocol-test.
type ProtocolTest interface {
//
// Arguments return the arguments which this protocol-test accepts, along
// with a regular expression which will be used to validate a non-empty
// argument.
//
Arguments() map[string]string
// Example should return a string describing how your protocol-test
// works and is invoked.
//
// Optional arguments will automatically be documented.
Example() string
//
// RunTest actually invokes the protocol-handler to run its
// tests.
//
// Return a suitable error if the test fails, or nil to indicate
// it passed.
//
RunTest(tst test.Test, target string, opts test.Options) error
}
// This is a map of known-tests.
var handlers = struct {
m map[string]TestCtor
sync.RWMutex
}{m: make(map[string]TestCtor)}
// TestCtor is the signature of a constructor-function.
type TestCtor func() ProtocolTest
// Register a test-type with a constructor.
func Register(id string, newfunc TestCtor) {
handlers.Lock()
handlers.m[id] = newfunc
handlers.Unlock()
}
// ProtocolHandler is the factory-method which looks up and returns
// an object of the given type - if possible.
func ProtocolHandler(id string) (a ProtocolTest) {
handlers.RLock()
ctor, ok := handlers.m[id]
handlers.RUnlock()
if ok {
a = ctor()
}
return
}
// Handlers returns the names of all the registered protocol-handlers.
func Handlers() []string {
var result []string
// For each handler save the name
handlers.RLock()
for index := range handlers.m {
result = append(result, index)
}
handlers.RUnlock()
// And return the result
return result
}