1+ package e2e_test
2+
3+ import (
4+ "os"
5+ "testing"
6+
7+ chclient "github.com/jpillora/chisel/client"
8+ chserver "github.com/jpillora/chisel/server"
9+ )
10+
11+ func TestChiselKeyEnvironmentVariable (t * testing.T ) {
12+ // Set the CHISEL_KEY environment variable
13+ os .Setenv ("CHISEL_KEY" , "test-key-value" )
14+ defer os .Unsetenv ("CHISEL_KEY" )
15+
16+ tmpPort := availablePort ()
17+
18+ // Create server with empty config - should pick up CHISEL_KEY env var
19+ serverConfig := & chserver.Config {}
20+
21+ // Setup server and client
22+ teardown := simpleSetup (t ,
23+ serverConfig ,
24+ & chclient.Config {
25+ Remotes : []string {tmpPort + ":$FILEPORT" },
26+ })
27+ defer teardown ()
28+
29+ // Test that the connection works - if the key is properly set,
30+ // the server should start successfully and connections should work
31+ result , err := post ("http://localhost:" + tmpPort , "env-key-test" )
32+ if err != nil {
33+ t .Fatal (err )
34+ }
35+ if result != "env-key-test!" {
36+ t .Fatalf ("expected exclamation mark added, got: %s" , result )
37+ }
38+ }
39+
40+ func TestChiselKeyEnvironmentVariableConsistency (t * testing.T ) {
41+ // This test verifies that the same CHISEL_KEY value produces
42+ // consistent behavior (same fingerprint) by manually setting KeySeed
43+ keyValue := "consistency-test-key"
44+
45+ // Create two server instances with the same KeySeed (simulating what main.go does)
46+ server1 , err := chserver .NewServer (& chserver.Config {
47+ KeySeed : keyValue ,
48+ })
49+ if err != nil {
50+ t .Fatalf ("Failed to create first server: %v" , err )
51+ }
52+
53+ server2 , err := chserver .NewServer (& chserver.Config {
54+ KeySeed : keyValue ,
55+ })
56+ if err != nil {
57+ t .Fatalf ("Failed to create second server: %v" , err )
58+ }
59+
60+ // Both servers should have the same fingerprint since they use the same key
61+ if server1 .GetFingerprint () != server2 .GetFingerprint () {
62+ t .Fatalf ("Expected same fingerprint for same key, got %s and %s" ,
63+ server1 .GetFingerprint (), server2 .GetFingerprint ())
64+ }
65+ }
0 commit comments