-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
165 lines (133 loc) · 3.81 KB
/
main_test.go
File metadata and controls
165 lines (133 loc) · 3.81 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"io/ioutil"
"log"
"randAPI/helpers"
"randAPI/proto/grpcService"
"testing"
"time"
)
var kacp = keepalive.ClientParameters{
Time: 10 * time.Second, // send pings every 10 seconds if there is no activity
Timeout: time.Second, // wait 1 second for ping back
PermitWithoutStream: true, // send pings even without active streams
}
func loadClientTLSCredentials() (credentials.TransportCredentials, error) {
// Load certificate of the CA who signed server's certificate
pemServerCA, err := ioutil.ReadFile("./x509/ca-cert.pem")
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(pemServerCA) {
return nil, fmt.Errorf("failed to add server CA's certificate")
}
// Load client's certificate and private key
clientCert, err := tls.LoadX509KeyPair("./x509/client-cert.pem", "./x509/client-key.pem")
if err != nil {
return nil, err
}
// Create the credentials and return it
config := &tls.Config{
Certificates: []tls.Certificate{clientCert},
RootCAs: certPool,
}
return credentials.NewTLS(config), nil
}
func connectMain() grpcService.RandAPIClient {
tlsCredentials, err := loadClientTLSCredentials()
if err != nil {
log.Fatal("cannot load TLS credentials: ", err)
}
cc1, err := grpc.Dial("0.0.0.0:10001", grpc.WithTransportCredentials(tlsCredentials),
grpc.WithKeepaliveParams(kacp))
if err != nil {
log.Fatal("cannot dial server: ", err)
}
return grpcService.NewRandAPIClient(cc1)
}
func MainConnectionT(t *testing.T) {
tlsCredentials, err := loadClientTLSCredentials()
if err != nil {
log.Fatal("cannot load TLS credentials: ", err)
}
cc1, err := grpc.Dial("localhost:10001", grpc.WithTransportCredentials(tlsCredentials))
if err != nil {
log.Fatal("cannot dial server: ", err)
}
if cc1.GetState() != connectivity.Idle {
t.Errorf("Expected connection to be in state %s, got %s", connectivity.Idle, cc1.GetState())
}
}
func MainReqBoolT(t *testing.T) {
client := connectMain()
req := empty.Empty{}
var _, err = client.Boolean(context.Background(), &req)
if err != nil {
t.Errorf("Error calling Boolean: %s", err)
}
}
func MainReqWeightedBoolT(t *testing.T) {
client := connectMain()
req := grpcService.WeightedBooleanRequest{
FalseWeight: 9,
TrueWeight: 1,
}
var _, err = client.WeightedBoolean(context.Background(), &req)
if err != nil {
t.Errorf("Error calling Boolean: %s", err)
}
}
func MainReqIntT(t *testing.T) {
client := connectMain()
req := empty.Empty{}
var _, err = client.Integer(context.Background(), &req)
if err != nil {
t.Errorf("Error calling Boolean: %s", err)
}
}
func MainReqIntNT(t *testing.T) {
client := connectMain()
req := grpcService.IntegerRequest{Max: 10}
var _, err = client.IntegerN(context.Background(), &req)
if err != nil {
t.Errorf("Error calling Boolean: %s", err)
}
}
func MainReqBoolLoadT(t *testing.T) {
client := connectMain()
req := empty.Empty{}
var rounds = 10000
var a, b int
helpers.RotateSeed()
for i := 0; i < rounds; i++ {
var res, _ = client.Boolean(context.Background(), &req)
if res.Result {
a++
} else {
b++
}
}
result := float64(a) / float64(a+b)
if !(0.48 < result && result < 0.52) {
t.Errorf("Expected bias to be in range 0.49-0.51, got %f", result)
}
}
func TestMainFunction(t *testing.T) {
go main()
t.Run("Connect", MainConnectionT)
t.Run("RequestBoolean", MainReqBoolT)
t.Run("RequestInteger", MainReqIntT)
t.Run("RequestWeightedBoolean", MainReqWeightedBoolT)
t.Run("RequestIntegerN", MainReqIntNT)
t.Run("RequestBooleanLoad", MainReqBoolLoadT)
}