|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "net/url" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +func TestSanitizeError_SimpleURLError(t *testing.T) { |
| 30 | + // Create a url.Error with sensitive info |
| 31 | + originalURL := "https://storage.blob.core.windows.net/container/blob?sig=SECRET&sv=2020" |
| 32 | + urlErr := &url.Error{ |
| 33 | + Op: "Get", |
| 34 | + URL: originalURL, |
| 35 | + Err: fmt.Errorf("connection timeout"), |
| 36 | + } |
| 37 | + |
| 38 | + // Sanitize |
| 39 | + sanitized := SanitizeError(urlErr) |
| 40 | + require.NotNil(t, sanitized) |
| 41 | + |
| 42 | + // Check it's a sanitizedError with correct properties |
| 43 | + sanitizedErr, ok := sanitized.(*sanitizedError) |
| 44 | + require.True(t, ok, "Should return *sanitizedError type") |
| 45 | + assert.Equal(t, urlErr, sanitizedErr.original) |
| 46 | + assert.Equal(t, urlErr, sanitizedErr.urlError) |
| 47 | + assert.Equal(t, "https://storage.blob.core.windows.net/container/blob?sig=%5BREDACTED%5D&sv=%5BREDACTED%5D", sanitizedErr.sanitizedURL) |
| 48 | + |
| 49 | + // Test Error() method - verifies ReplaceAll functionality |
| 50 | + expected := "Get \"https://storage.blob.core.windows.net/container/blob?sig=%5BREDACTED%5D&sv=%5BREDACTED%5D\": connection timeout" |
| 51 | + assert.Equal(t, expected, sanitized.Error()) |
| 52 | +} |
| 53 | + |
| 54 | +func TestSanitizeError_WrappedError(t *testing.T) { |
| 55 | + originalURL := "https://storage.blob.core.windows.net/blob?sig=SECRET&sv=2020" |
| 56 | + urlErr := &url.Error{ |
| 57 | + Op: "Get", |
| 58 | + URL: originalURL, |
| 59 | + Err: fmt.Errorf("timeout"), |
| 60 | + } |
| 61 | + |
| 62 | + wrappedErr := fmt.Errorf("image pull failed: %w", urlErr) |
| 63 | + |
| 64 | + // Sanitize |
| 65 | + sanitized := SanitizeError(wrappedErr) |
| 66 | + |
| 67 | + // Test Error() method with wrapped error - verifies ReplaceAll works in wrapped context |
| 68 | + sanitizedMsg := sanitized.Error() |
| 69 | + assert.NotContains(t, sanitizedMsg, "SECRET", "Secret should be sanitized") |
| 70 | + assert.Contains(t, sanitizedMsg, "image pull failed", "Wrapper message should be preserved") |
| 71 | + assert.Contains(t, sanitizedMsg, "%5BREDACTED%5D", "Should contain sanitized marker") |
| 72 | + |
| 73 | + // Should still be able to unwrap to url.Error |
| 74 | + var targetURLErr *url.Error |
| 75 | + assert.True(t, errors.As(sanitized, &targetURLErr), |
| 76 | + "Should be able to find *url.Error in sanitized error chain") |
| 77 | + |
| 78 | + // Verify url.Error properties are preserved |
| 79 | + assert.Equal(t, "Get", targetURLErr.Op) |
| 80 | + assert.Contains(t, targetURLErr.Err.Error(), "timeout") |
| 81 | +} |
| 82 | + |
| 83 | +func TestSanitizeError_NonURLError(t *testing.T) { |
| 84 | + // Regular error without url.Error |
| 85 | + regularErr := fmt.Errorf("some error occurred") |
| 86 | + |
| 87 | + sanitized := SanitizeError(regularErr) |
| 88 | + |
| 89 | + // Should return the exact same error object |
| 90 | + assert.Equal(t, regularErr, sanitized, |
| 91 | + "Non-URL errors should pass through unchanged") |
| 92 | +} |
| 93 | + |
| 94 | +func TestSanitizeError_NilError(t *testing.T) { |
| 95 | + sanitized := SanitizeError(nil) |
| 96 | + assert.Nil(t, sanitized, "nil error should return nil") |
| 97 | +} |
| 98 | + |
| 99 | +func TestSanitizeError_NoQueryParams(t *testing.T) { |
| 100 | + // URL without any query parameters |
| 101 | + urlErr := &url.Error{ |
| 102 | + Op: "Get", |
| 103 | + URL: "https://registry.example.com/v2/image/manifests/latest", |
| 104 | + Err: fmt.Errorf("not found"), |
| 105 | + } |
| 106 | + |
| 107 | + sanitized := SanitizeError(urlErr) |
| 108 | + |
| 109 | + // Should return the same error object (no sanitization needed) |
| 110 | + assert.Equal(t, urlErr, sanitized, |
| 111 | + "Errors without query params should pass through unchanged") |
| 112 | +} |
| 113 | + |
| 114 | +func TestSanitizedError_Unwrap(t *testing.T) { |
| 115 | + originalURL := "https://storage.blob.core.windows.net/blob?sig=SECRET" |
| 116 | + urlErr := &url.Error{ |
| 117 | + Op: "Get", |
| 118 | + URL: originalURL, |
| 119 | + Err: fmt.Errorf("timeout"), |
| 120 | + } |
| 121 | + |
| 122 | + sanitized := SanitizeError(urlErr) |
| 123 | + |
| 124 | + // Should be able to unwrap |
| 125 | + unwrapped := errors.Unwrap(sanitized) |
| 126 | + assert.NotNil(t, unwrapped, "Should be able to unwrap sanitized error") |
| 127 | + assert.Equal(t, urlErr, unwrapped, "Unwrapped should be the original error") |
| 128 | +} |
0 commit comments