|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/csv" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "unaware/pkg" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCSVProcessing_Hashed(t *testing.T) { |
| 16 | + salt := []byte("csv-salt") |
| 17 | + input := `id,name,email,ip_address,notes |
| 18 | +1,John Doe,john.doe@example.com,192.168.1.1,some notes |
| 19 | +2,Jane Smith,jane.smith@example.net,10.0.0.2,more data` |
| 20 | + |
| 21 | + var buf bytes.Buffer |
| 22 | + err := pkg.Start("csv", 2, strings.NewReader(input), &buf, pkg.Hashed(salt), nil, nil) |
| 23 | + require.NoError(t, err) |
| 24 | + |
| 25 | + output := buf.String() |
| 26 | + assert.NotEmpty(t, output) |
| 27 | + |
| 28 | + // Verify header is preserved |
| 29 | + assert.True(t, strings.HasPrefix(output, "id,name,email,ip_address,notes\n")) |
| 30 | + |
| 31 | + // Verify original sensitive data is gone |
| 32 | + assert.NotContains(t, output, "John Doe") |
| 33 | + assert.NotContains(t, output, "john.doe@example.com") |
| 34 | + assert.NotContains(t, output, "192.168.1.1") |
| 35 | + assert.NotContains(t, output, "Jane Smith") |
| 36 | + assert.NotContains(t, output, "jane.smith@example.net") |
| 37 | + assert.NotContains(t, output, "10.0.0.2") |
| 38 | + |
| 39 | + // Verify the structure is a valid CSV with the correct number of records |
| 40 | + r := csv.NewReader(strings.NewReader(output)) |
| 41 | + records, err := r.ReadAll() |
| 42 | + require.NoError(t, err) |
| 43 | + assert.Len(t, records, 3) // Header + 2 rows |
| 44 | + assert.Len(t, records[1], 5) |
| 45 | + assert.Len(t, records[2], 5) |
| 46 | +} |
| 47 | + |
| 48 | +func TestCSVProcessing_WithInclude(t *testing.T) { |
| 49 | + salt := []byte("csv-include-salt") |
| 50 | + input := `id,name,email,ip_address,notes |
| 51 | +1,John Doe,john.doe@example.com,192.168.1.1,some notes |
| 52 | +2,Jane Smith,jane.smith@example.net,10.0.0.2,more data` |
| 53 | + |
| 54 | + // Only include 'email' and 'ip_address' for masking |
| 55 | + include := []string{"email", "ip_address"} |
| 56 | + |
| 57 | + var buf bytes.Buffer |
| 58 | + err := pkg.Start("csv", 2, strings.NewReader(input), &buf, pkg.Hashed(salt), include, nil) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + output := buf.String() |
| 62 | + |
| 63 | + // Verify sensitive data that should be masked is gone |
| 64 | + assert.NotContains(t, output, "john.doe@example.com") |
| 65 | + assert.NotContains(t, output, "192.168.1.1") |
| 66 | + |
| 67 | + // Verify data that should be preserved is still there |
| 68 | + assert.Contains(t, output, "John Doe") |
| 69 | + assert.Contains(t, output, "Jane Smith") |
| 70 | + assert.Contains(t, output, "some notes") |
| 71 | + |
| 72 | + // Verify structure and read the output to check specific fields |
| 73 | + r := csv.NewReader(strings.NewReader(output)) |
| 74 | + records, err := r.ReadAll() |
| 75 | + require.NoError(t, err) |
| 76 | + require.Len(t, records, 3) |
| 77 | + |
| 78 | + // Check first data row |
| 79 | + assert.Equal(t, "1", records[1][0]) |
| 80 | + assert.Equal(t, "John Doe", records[1][1]) |
| 81 | + assert.NotEqual(t, "john.doe@example.com", records[1][2]) // Masked |
| 82 | + assert.NotEqual(t, "192.168.1.1", records[1][3]) // Masked |
| 83 | + assert.Equal(t, "some notes", records[1][4]) |
| 84 | +} |
| 85 | + |
| 86 | +func TestCSVProcessing_WithExclude(t *testing.T) { |
| 87 | + salt := []byte("csv-exclude-salt") |
| 88 | + input := `id,name,email,ip_address,notes |
| 89 | +1,John Doe,john.doe@example.com,192.168.1.1,some notes` |
| 90 | + |
| 91 | + // Exclude 'id' and 'notes' from masking |
| 92 | + exclude := []string{"id", "notes"} |
| 93 | + |
| 94 | + var buf bytes.Buffer |
| 95 | + err := pkg.Start("csv", 2, strings.NewReader(input), &buf, pkg.Hashed(salt), nil, exclude) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + output := buf.String() |
| 99 | + |
| 100 | + // Verify sensitive data that should be masked is gone |
| 101 | + assert.NotContains(t, output, "John Doe") |
| 102 | + assert.NotContains(t, output, "john.doe@example.com") |
| 103 | + |
| 104 | + // Verify data that should be preserved is still there |
| 105 | + assert.Contains(t, output, "1,") // id is preserved |
| 106 | + assert.Contains(t, output, ",some notes") // notes is preserved |
| 107 | + |
| 108 | + // Verify structure |
| 109 | + r := csv.NewReader(strings.NewReader(output)) |
| 110 | + records, err := r.ReadAll() |
| 111 | + require.NoError(t, err) |
| 112 | + require.Len(t, records, 2) |
| 113 | + assert.Equal(t, "1", records[1][0]) // Preserved |
| 114 | + assert.NotEqual(t, "John Doe", records[1][1]) // Masked |
| 115 | + assert.NotEqual(t, "john.doe@example.com", records[1][2]) // Masked |
| 116 | + assert.NotEqual(t, "192.168.1.1", records[1][3]) // Masked |
| 117 | + assert.Equal(t, "some notes", records[1][4]) // Preserved |
| 118 | +} |
| 119 | + |
| 120 | +func TestEmptyReader_CSV(t *testing.T) { |
| 121 | + var buf bytes.Buffer |
| 122 | + err := pkg.Start("csv", 1, strings.NewReader(""), &buf, pkg.Random(), nil, nil) |
| 123 | + require.NoError(t, err) |
| 124 | + assert.Equal(t, "", buf.String()) |
| 125 | +} |
| 126 | + |
| 127 | +func TestReaderError_CSV(t *testing.T) { |
| 128 | + errorReader := &errorReader{} |
| 129 | + var buf bytes.Buffer |
| 130 | + err := pkg.Start("csv", 1, errorReader, &buf, pkg.Random(), nil, nil) |
| 131 | + require.Error(t, err) |
| 132 | +} |
0 commit comments