feat(cybersecurity): auto-commit con 48 cambios
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPTYCaptureIdle(t *testing.T) {
|
||||
t.Run("captura output de echo hola", func(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip en modo corto")
|
||||
}
|
||||
ctx := context.Background()
|
||||
out, err := PTYCaptureIdle(ctx, "echo", []string{"hola"}, 100*time.Millisecond, nil, 0, 300*time.Millisecond, 5*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("error inesperado: %v", err)
|
||||
}
|
||||
if !strings.Contains(out, "hola") {
|
||||
t.Errorf("se esperaba 'hola' en el output, got: %q", out)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("input interactivo con cat", func(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip en modo corto: timing sensible en CI")
|
||||
}
|
||||
ctx := context.Background()
|
||||
// cat repite stdin a stdout via PTY; el PTY hace echo del input ademas.
|
||||
// "ping\r" simula Enter; la palabra "ping" debe aparecer en el output.
|
||||
out, err := PTYCaptureIdle(
|
||||
ctx,
|
||||
"cat", nil,
|
||||
200*time.Millisecond,
|
||||
[]string{"ping\r"},
|
||||
100*time.Millisecond,
|
||||
500*time.Millisecond,
|
||||
5*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("error inesperado: %v", err)
|
||||
}
|
||||
if !strings.Contains(out, "ping") {
|
||||
t.Errorf("se esperaba 'ping' en el output, got: %q", out)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("timeout duro con sleep 10", func(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip en modo corto: espera ~1s de timeout")
|
||||
}
|
||||
ctx := context.Background()
|
||||
start := time.Now()
|
||||
_, err := PTYCaptureIdle(
|
||||
ctx,
|
||||
"sleep", []string{"10"},
|
||||
50*time.Millisecond,
|
||||
nil,
|
||||
0,
|
||||
600*time.Millisecond,
|
||||
1*time.Second,
|
||||
)
|
||||
elapsed := time.Since(start)
|
||||
if err != nil {
|
||||
// Un error de señal/exit es esperado; no falla el test.
|
||||
t.Logf("error (esperado al matar sleep): %v", err)
|
||||
}
|
||||
// La función debe retornar en menos de 3s, no esperar los 10s del sleep.
|
||||
if elapsed >= 3*time.Second {
|
||||
t.Errorf("la función tardó %v, se esperaba < 3s", elapsed)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user