key_gen_parse_test.go 799 B

123456789101112131415161718192021222324252627282930313233
  1. package service
  2. import "testing"
  3. func TestParseXrayKeyPairOutput(t *testing.T) {
  4. a, b, err := parseXrayKeyPairOutput("Private key: abc123\nPublic key: def456\n")
  5. if err != nil {
  6. t.Fatalf("well-formed output errored: %v", err)
  7. }
  8. if a != "abc123" || b != "def456" {
  9. t.Fatalf("got (%q, %q), want (abc123, def456)", a, b)
  10. }
  11. malformed := []string{
  12. "",
  13. "only one line: value",
  14. "Private key: abc\n",
  15. "no colon here\nno colon two",
  16. "Private key\nPublic key",
  17. }
  18. for _, out := range malformed {
  19. func() {
  20. defer func() {
  21. if r := recover(); r != nil {
  22. t.Errorf("parseXrayKeyPairOutput panicked on %q: %v", out, r)
  23. }
  24. }()
  25. if _, _, err := parseXrayKeyPairOutput(out); err == nil {
  26. t.Errorf("expected error for malformed output %q, got nil", out)
  27. }
  28. }()
  29. }
  30. }