Skip to content

Commit 9043ec7

Browse files
chore(ci): add ci
1 parent 1a2d03d commit 9043ec7

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

cmd/signing-server/main_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/pem"
6+
"fmt"
7+
"io"
8+
"log"
9+
"net/http"
10+
"os"
11+
"os/exec"
12+
"strings"
13+
"sync"
14+
"testing"
15+
"time"
16+
17+
"go.uber.org/zap"
18+
)
19+
20+
func TestSoftHSMConcurrentSignRequests(t *testing.T) {
21+
hsmModule, err := exec.Command("softhsm2-util", "--show-config", "default-pkcs11-lib").CombinedOutput()
22+
if err != nil {
23+
hsmModule = []byte("/usr/lib/softhsm/libsofthsm2.so")
24+
}
25+
tokenLabel := os.Getenv("TOKEN_LABEL")
26+
if tokenLabel == "" {
27+
t.Skip("TOKEN_LABEL environment variable is not set")
28+
}
29+
keyLabel := os.Getenv("KEY_LABEL")
30+
if keyLabel == "" {
31+
t.Skip("KEY_LABEL environment variable is not set")
32+
}
33+
hsmPin := os.Getenv("HSM_PIN")
34+
if hsmPin == "" {
35+
t.Skip("HSM_PIN environment variable is not set")
36+
}
37+
38+
const (
39+
base = "http://localhost:8080"
40+
healthURL = base + "/healthz"
41+
url = base + "/sign/rsassa-pss?hashAlgorithm=sha256"
42+
bodyHex = "aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f"
43+
)
44+
45+
var (
46+
headers = map[string]string{
47+
"Content-Type": "text/plain",
48+
"Content-Encoding": "hex",
49+
"Accept": "application/x-pem-file",
50+
}
51+
)
52+
53+
l, err := zap.NewDevelopment()
54+
if err != nil {
55+
t.Fatalf("Failed to create logger: %v", err)
56+
}
57+
58+
go func() {
59+
60+
if err := run(&Config{
61+
HSMModule: strings.TrimSpace(string(hsmModule)),
62+
HSMTokenLabel: tokenLabel,
63+
HSMSlot: -1,
64+
HSMKeyLabel: keyLabel,
65+
HSMPass: hsmPin,
66+
Port: "8080",
67+
DisableHTTPS: true,
68+
DisableAuth: true,
69+
MaxBodySizeBytes: 2048,
70+
Logger: l,
71+
RunServer: true,
72+
}); err != nil {
73+
t.Fatalf("Failed to start signing server: %v", err)
74+
}
75+
}()
76+
77+
var wg sync.WaitGroup
78+
client := &http.Client{Timeout: 10 * time.Second}
79+
80+
outer:
81+
for {
82+
select {
83+
case <-t.Context().Done():
84+
t.Fatal("Test context was cancelled before health check passed")
85+
default:
86+
healthReq, err := http.NewRequest(http.MethodGet, healthURL, nil)
87+
if err != nil {
88+
t.Fatalf("Health request creation failed: %v", err)
89+
}
90+
healthResp, _ := client.Do(healthReq)
91+
if healthResp != nil && healthResp.StatusCode == http.StatusOK {
92+
log.Println("Health check passed")
93+
break outer
94+
}
95+
}
96+
}
97+
98+
for i := 0; i < 10; i++ {
99+
wg.Add(1)
100+
101+
go func(index int) {
102+
defer wg.Done()
103+
104+
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBufferString(bodyHex))
105+
if err != nil {
106+
t.Errorf("Request %d creation failed: %v", index, err)
107+
return
108+
}
109+
110+
for k, v := range headers {
111+
req.Header.Set(k, v)
112+
}
113+
114+
resp, err := client.Do(req)
115+
if err != nil {
116+
t.Errorf("Request %d failed: %v", index, err)
117+
return
118+
}
119+
defer resp.Body.Close()
120+
121+
body, err := io.ReadAll(resp.Body)
122+
if err != nil {
123+
t.Errorf("Request %d read body failed: %v", index, err)
124+
return
125+
}
126+
if resp.StatusCode != http.StatusOK {
127+
t.Errorf("Request %d failed with status %d: %s", index, resp.StatusCode, body)
128+
return
129+
}
130+
131+
block, _ := pem.Decode(body)
132+
if block == nil || block.Type != "SIGNATURE" {
133+
log.Fatal("Failed to parse PEM block")
134+
}
135+
fmt.Printf("Decoded signature: %x\n", block.Bytes)
136+
137+
log.Printf("Response %d: %d - %.100q\n", index, resp.StatusCode, body)
138+
}(i)
139+
}
140+
141+
wg.Wait()
142+
}

0 commit comments

Comments
 (0)