| // Copyright 2023 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "context" |
| "fmt" |
| "os" |
| "strings" |
| "testing" |
| "time" |
| ) |
| |
| func noWait() error { |
| return nil |
| } |
| |
| func TestRunInterruptOnCancel(t *testing.T) { |
| t.Run("kill cat", func(t *testing.T) { |
| ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Millisecond*50)) |
| defer cancel() |
| // 'cat' hangs indefinitely waiting for stdin. Kill the cat. |
| err := runInterruptOnCancel(ctx, "[cat] ", noWait, "cat") |
| if err != nil { |
| t.Errorf("test KillCat: expected no error. Got %v", err) |
| } |
| }) |
| |
| t.Run("normal exit", func(t *testing.T) { |
| ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*5)) |
| defer cancel() |
| // 'echo' will complete right away. |
| err := runInterruptOnCancel(ctx, "[echo] ", noWait, "echo", "hello") |
| if err != nil { |
| t.Errorf("test NormalExit: expected no error. Got %v", err) |
| } |
| }) |
| |
| t.Run("nonzero exit", func(t *testing.T) { |
| ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*5)) |
| defer cancel() |
| // 'false' will fail right away with code 1. |
| err := runInterruptOnCancel(ctx, "[false] ", noWait, "false") |
| if err == nil { |
| t.Errorf("test NonzeroExit: expected error. Got nil") |
| } |
| }) |
| } |
| |
| const ( |
| fakeSocat = "./socket_sink.py" |
| ) |
| |
| func TestMultiRelayWrap(t *testing.T) { |
| t.Run("wrapped command exit zero", func(t *testing.T) { |
| fakeRelay := socketRelay{ |
| Name: "foo", |
| SocketFileName: "foo.sock", |
| SocketPathEnvVar: "FOO_socket_path", |
| ServerAddr: "foo-service.googleapis.com:443", |
| } |
| |
| ctx := context.Background() |
| tempDir, dirErr := os.MkdirTemp("", "test_wrap_cmd_zero") |
| if dirErr != nil { |
| t.Errorf("TestMultiRelayWrap (zero): expected mkdir success. Got %v", dirErr) |
| } |
| defer os.RemoveAll(tempDir) |
| |
| // We don't really need a real 'socat' binary to test. |
| err := multiRelayWrap(ctx, []*socketRelay{&fakeRelay}, tempDir, fakeSocat, func(env []string) error { |
| e := env[0] |
| if !strings.HasPrefix(e, "FOO_socket_path=") { |
| t.Errorf("TestMultiRelayWrap (zero): expected \"%q\" to start with \"%s\"", e, "FOO_socket_path=") |
| } |
| if !strings.HasSuffix(e, "foo.sock") { |
| t.Errorf("TestMultiRelayWrap (zero): expected \"%q\" to end with \"%s\"", e, "foo.sock") |
| } |
| return nil |
| }) |
| if err != nil { |
| t.Errorf("TestMultiRelayWrap (zero): expected no error. Got %v", err) |
| } |
| }) |
| |
| t.Run("wrapped command exit nonzero", func(t *testing.T) { |
| fakeRelay := socketRelay{ |
| Name: "bar", |
| SocketFileName: "bar.sock", |
| SocketPathEnvVar: "BAR_socket_path", |
| ServerAddr: "bar-service.googleapis.com:443", |
| } |
| |
| ctx := context.Background() |
| tempDir, dirErr := os.MkdirTemp("", "test_wrap_cmd_nonzero") |
| if dirErr != nil { |
| t.Errorf("TestMultiRelayWrap (nonzero): expected mkdir success. Got %v", dirErr) |
| } |
| defer os.RemoveAll(tempDir) |
| |
| // 'cat' will run until interrupted |
| err := multiRelayWrap(ctx, []*socketRelay{&fakeRelay}, tempDir, fakeSocat, func(env []string) error { |
| e := env[0] |
| if !strings.HasPrefix(e, "BAR_socket_path=") { |
| t.Errorf("TestMultiRelayWrap (nonzero): expected \"%q\" to start with \"%s\"", e, "BAR_socket_path=") |
| } |
| if !strings.HasSuffix(e, "bar.sock") { |
| t.Errorf("TestMultiRelayWrap (nonzero): expected \"%q\" to end with \"%s\"", e, "foo.sock") |
| } |
| // mimic nonzero exit |
| return fmt.Errorf("exit 1") |
| }) |
| if err == nil { |
| t.Errorf("TestMultiRelayWrap (nonzero): expected error. Got nil") |
| } |
| }) |
| } |