149 lines
3.3 KiB
Go
149 lines
3.3 KiB
Go
package cluster
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadOrCreateGeneratesAndReloads(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "cluster.key")
|
|
|
|
first, err := LoadOrCreate(path, true)
|
|
if err != nil {
|
|
t.Fatalf("LoadOrCreate (création) : %v", err)
|
|
}
|
|
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
t.Fatalf("Stat : %v", err)
|
|
}
|
|
if got := info.Mode().Perm(); got != 0o600 {
|
|
t.Fatalf("permissions = %04o, attendu 0600", got)
|
|
}
|
|
|
|
second, err := LoadOrCreate(path, true)
|
|
if err != nil {
|
|
t.Fatalf("LoadOrCreate (relecture) : %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(first.PublicKey, second.PublicKey) {
|
|
t.Fatal("clés publiques différentes après relecture")
|
|
}
|
|
if !bytes.Equal(first.PrivateKey, second.PrivateKey) {
|
|
t.Fatal("clés privées différentes après relecture")
|
|
}
|
|
}
|
|
|
|
func TestLoadOrCreateNoCreate(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "missing.key")
|
|
|
|
if _, err := LoadOrCreate(path, false); err == nil {
|
|
t.Fatal("LoadOrCreate aurait dû échouer sans création autorisée")
|
|
}
|
|
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Fatalf("le fichier ne devrait pas exister : %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoadOrCreateCorrupt(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "cluster.key")
|
|
if err := os.WriteFile(path, []byte("pas une clé"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := LoadOrCreate(path, true); err == nil {
|
|
t.Fatal("LoadOrCreate aurait dû refuser un fichier corrompu")
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(data) != "pas une clé" {
|
|
t.Fatal("le fichier corrompu a été modifié")
|
|
}
|
|
}
|
|
|
|
func TestLoadOrCreateRejectsLoosePermissions(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "cluster.key")
|
|
|
|
if _, err := LoadOrCreate(path, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(path, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := LoadOrCreate(path, true); err == nil {
|
|
t.Fatal("LoadOrCreate aurait dû refuser des permissions 0644")
|
|
}
|
|
}
|
|
|
|
func TestLoadOrCreateRejectsSymlink(t *testing.T) {
|
|
dir := t.TempDir()
|
|
target := filepath.Join(dir, "target.key")
|
|
link := filepath.Join(dir, "cluster.key")
|
|
|
|
if _, err := LoadOrCreate(target, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(target, link); err != nil {
|
|
t.Skipf("symlink non supporté : %v", err)
|
|
}
|
|
|
|
if _, err := LoadOrCreate(link, true); err == nil {
|
|
t.Fatal("LoadOrCreate aurait dû refuser un lien symbolique")
|
|
}
|
|
}
|
|
|
|
func TestLoadOrCreateConcurrent(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "cluster.key")
|
|
|
|
const goroutines = 16
|
|
|
|
type result struct {
|
|
identity Identity
|
|
err error
|
|
}
|
|
|
|
results := make(chan result, goroutines)
|
|
var waitGroup sync.WaitGroup
|
|
|
|
for range goroutines {
|
|
waitGroup.Add(1)
|
|
|
|
go func() {
|
|
defer waitGroup.Done()
|
|
|
|
identity, err := LoadOrCreate(path, true)
|
|
results <- result{identity: identity, err: err}
|
|
}()
|
|
}
|
|
|
|
waitGroup.Wait()
|
|
close(results)
|
|
|
|
var expected []byte
|
|
|
|
for current := range results {
|
|
if current.err != nil {
|
|
t.Fatalf("LoadOrCreate concurrent : %v", current.err)
|
|
}
|
|
|
|
publicKey := current.identity.PublicKeyBytes()
|
|
|
|
if expected == nil {
|
|
expected = publicKey
|
|
continue
|
|
}
|
|
|
|
if !bytes.Equal(expected, publicKey) {
|
|
t.Fatal("deux processus concurrents ont obtenu des clés différentes")
|
|
}
|
|
}
|
|
}
|