runscript_1737_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package rearmer
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. // scriptPool implementa Run y RunScript: el rearmer tiene que elegir el
  7. // segundo para el script de reinstalación, que pasa del límite de argv de
  8. // dropbear (#1737).
  9. type scriptPool struct {
  10. cmd string
  11. script string
  12. }
  13. func (p *scriptPool) Run(_ string, cmd string, _ time.Duration) (string, error) {
  14. p.cmd = cmd
  15. return "", nil
  16. }
  17. func (p *scriptPool) RunScript(_ string, script string, _ time.Duration) (string, error) {
  18. p.script = script
  19. return "", nil
  20. }
  21. // argvOnlyPool es lo que implementan los fakes de test: solo Run.
  22. type argvOnlyPool struct {
  23. cmd string
  24. }
  25. func (p *argvOnlyPool) Run(_ string, cmd string, _ time.Duration) (string, error) {
  26. p.cmd = cmd
  27. return "", nil
  28. }
  29. func TestRunScriptSendsByStdinWhenThePoolCan(t *testing.T) {
  30. p := &scriptPool{}
  31. if _, err := runScript(p, "192.168.1.2", "echo hola", time.Second); err != nil {
  32. t.Fatalf("runScript: %v", err)
  33. }
  34. if p.script != "echo hola" {
  35. t.Fatalf("script by stdin = %q, want %q", p.script, "echo hola")
  36. }
  37. if p.cmd != "" {
  38. t.Fatalf("also went through argv: %q", p.cmd)
  39. }
  40. }
  41. func TestRunScriptFallsBackToArgvWithoutStdinSupport(t *testing.T) {
  42. p := &argvOnlyPool{}
  43. if _, err := runScript(p, "192.168.1.2", "echo hola", time.Second); err != nil {
  44. t.Fatalf("runScript: %v", err)
  45. }
  46. if p.cmd != "echo hola" {
  47. t.Fatalf("argv = %q, want %q", p.cmd, "echo hola")
  48. }
  49. }