~bigbes/shroud

ref: 999bf1cb8045e0b245640d8f284d92f41b31b8a8 shroud/internal/vless/keygen_test.go -rw-r--r-- 1.5 KiB
999bf1cb — Eugene Blikh feat(reality): add autodetect command for decoy server 2 months ago
                                                                                
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
package vless

import (
	"encoding/base64"
	"encoding/hex"
	"testing"
)

func TestGenerateX25519Keypair(t *testing.T) {
	priv, pub, err := GenerateX25519Keypair()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	privBytes, err := base64.StdEncoding.DecodeString(priv)
	if err != nil {
		t.Fatalf("invalid base64 private key: %v", err)
	}
	if len(privBytes) != 32 {
		t.Errorf("private key length: got %d, want 32", len(privBytes))
	}

	pubBytes, err := base64.StdEncoding.DecodeString(pub)
	if err != nil {
		t.Fatalf("invalid base64 public key: %v", err)
	}
	if len(pubBytes) != 32 {
		t.Errorf("public key length: got %d, want 32", len(pubBytes))
	}

	// Keys should be different.
	if priv == pub {
		t.Error("private and public keys should differ")
	}
}

func TestGenerateX25519Keypair_Unique(t *testing.T) {
	priv1, _, _ := GenerateX25519Keypair()
	priv2, _, _ := GenerateX25519Keypair()
	if priv1 == priv2 {
		t.Error("two generated keypairs should not be identical")
	}
}

func TestGenerateShortID(t *testing.T) {
	id, err := GenerateShortID()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(id) != 16 {
		t.Errorf("short ID hex length: got %d, want 16", len(id))
	}
	b, err := hex.DecodeString(id)
	if err != nil {
		t.Fatalf("invalid hex short ID: %v", err)
	}
	if len(b) != 8 {
		t.Errorf("short ID byte length: got %d, want 8", len(b))
	}
}

func TestGenerateShortID_Unique(t *testing.T) {
	id1, _ := GenerateShortID()
	id2, _ := GenerateShortID()
	if id1 == id2 {
		t.Error("two generated short IDs should not be identical")
	}
}