|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/bloodhoundad/azurehound/v2/client/query" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// fakeRestClient is a minimal test double for rest.RestClient that allows |
| 16 | +// controlling the Get response per test case. |
| 17 | +type fakeRestClient struct { |
| 18 | + getFunc func(ctx context.Context, path string, params query.Params, headers map[string]string) (*http.Response, error) |
| 19 | +} |
| 20 | + |
| 21 | +func (s *fakeRestClient) Get(ctx context.Context, path string, params query.Params, headers map[string]string) (*http.Response, error) { |
| 22 | + return s.getFunc(ctx, path, params, headers) |
| 23 | +} |
| 24 | + |
| 25 | +func (s *fakeRestClient) Delete(context.Context, string, interface{}, query.Params, map[string]string) (*http.Response, error) { |
| 26 | + return nil, nil |
| 27 | +} |
| 28 | +func (s *fakeRestClient) Patch(context.Context, string, interface{}, query.Params, map[string]string) (*http.Response, error) { |
| 29 | + return nil, nil |
| 30 | +} |
| 31 | +func (s *fakeRestClient) Post(context.Context, string, interface{}, query.Params, map[string]string) (*http.Response, error) { |
| 32 | + return nil, nil |
| 33 | +} |
| 34 | +func (s *fakeRestClient) Put(context.Context, string, interface{}, query.Params, map[string]string) (*http.Response, error) { |
| 35 | + return nil, nil |
| 36 | +} |
| 37 | +func (s *fakeRestClient) Send(req *http.Request) (*http.Response, error) { return nil, nil } |
| 38 | +func (s *fakeRestClient) AddAuthenticationToRequest(req *http.Request) (*http.Request, error) { |
| 39 | + return req, nil |
| 40 | +} |
| 41 | +func (s *fakeRestClient) CloseIdleConnections() {} |
| 42 | + |
| 43 | +func TestGetAzureObjectList_SuccessfulResponse(t *testing.T) { |
| 44 | + body := `{"value": [{"id": "1"}, {"id": "2"}]}` |
| 45 | + client := &fakeRestClient{ |
| 46 | + getFunc: func(ctx context.Context, path string, params query.Params, headers map[string]string) (*http.Response, error) { |
| 47 | + return &http.Response{ |
| 48 | + StatusCode: http.StatusOK, |
| 49 | + Body: io.NopCloser(strings.NewReader(body)), |
| 50 | + }, nil |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + out := make(chan AzureResult[map[string]string]) |
| 55 | + go getAzureObjectList(client, context.Background(), "/test/path", nil, out) |
| 56 | + |
| 57 | + var results []map[string]string |
| 58 | + for result := range out { |
| 59 | + require.NoError(t, result.Error) |
| 60 | + results = append(results, result.Ok) |
| 61 | + } |
| 62 | + |
| 63 | + require.Len(t, results, 2) |
| 64 | + require.Equal(t, "1", results[0]["id"]) |
| 65 | + require.Equal(t, "2", results[1]["id"]) |
| 66 | +} |
| 67 | + |
| 68 | +func TestGetAzureObjectList_HungResponseTimesOut(t *testing.T) { |
| 69 | + // Shorten the timeout so the test completes quickly |
| 70 | + original := pageRequestTimeout |
| 71 | + pageRequestTimeout = 500 * time.Millisecond |
| 72 | + defer func() { pageRequestTimeout = original }() |
| 73 | + |
| 74 | + client := &fakeRestClient{ |
| 75 | + getFunc: func(ctx context.Context, path string, params query.Params, headers map[string]string) (*http.Response, error) { |
| 76 | + // Verify the context has a deadline (set by pageRequestTimeout) |
| 77 | + _, hasDeadline := ctx.Deadline() |
| 78 | + require.True(t, hasDeadline, "expected context passed to Get to have a deadline from pageRequestTimeout") |
| 79 | + |
| 80 | + // Simulate a hung connection: block until the context expires |
| 81 | + <-ctx.Done() |
| 82 | + return nil, ctx.Err() |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + out := make(chan AzureResult[map[string]string]) |
| 87 | + go getAzureObjectList(client, context.Background(), "/test/path", nil, out) |
| 88 | + |
| 89 | + // The channel should produce an error and close well within a few seconds |
| 90 | + select { |
| 91 | + case result, ok := <-out: |
| 92 | + require.True(t, ok, "expected a value on out, channel closed") |
| 93 | + require.Error(t, result.Error, "expected an error result from timed-out request") |
| 94 | + require.ErrorIs(t, result.Error, context.DeadlineExceeded) |
| 95 | + case <-time.After(5 * time.Second): |
| 96 | + t.Fatal("getAzureObjectList did not return within expected timeout; pipeline is hung") |
| 97 | + } |
| 98 | + |
| 99 | + // drain and ensure channel closes |
| 100 | + for range out { |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestGetAzureObjectList_ParentContextCanceled(t *testing.T) { |
| 105 | + ctx, cancel := context.WithCancel(context.Background()) |
| 106 | + |
| 107 | + client := &fakeRestClient{ |
| 108 | + getFunc: func(ctx context.Context, path string, params query.Params, headers map[string]string) (*http.Response, error) { |
| 109 | + <-ctx.Done() |
| 110 | + return nil, ctx.Err() |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + out := make(chan AzureResult[map[string]string]) |
| 115 | + go getAzureObjectList(client, ctx, "/test/path", nil, out) |
| 116 | + |
| 117 | + // Cancel the parent context after a short delay |
| 118 | + time.AfterFunc(100*time.Millisecond, cancel) |
| 119 | + |
| 120 | + select { |
| 121 | + case result, ok := <-out: |
| 122 | + if ok { |
| 123 | + require.Error(t, result.Error) |
| 124 | + } |
| 125 | + case <-time.After(5 * time.Second): |
| 126 | + t.Fatal("getAzureObjectList did not respect parent context cancellation") |
| 127 | + } |
| 128 | + |
| 129 | + for range out { |
| 130 | + } |
| 131 | +} |
0 commit comments