|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/binary" |
4 | 5 | "testing" |
| 6 | + "unicode/utf16" |
5 | 7 |
|
| 8 | + "github.com/LeanerCloud/cloud-commitments-go/pkg/common" |
6 | 9 | "github.com/stretchr/testify/assert" |
7 | 10 | "github.com/stretchr/testify/require" |
8 | 11 | ) |
9 | 12 |
|
10 | | -// Header validation (#1327): before this, a header the parser did not |
11 | | -// recognize silently decoded every row to an empty Service/Region/ |
12 | | -// ResourceType, and only the Count cell check (added in #1944) caught |
13 | | -// anything at all, with a misleading "missing Count column" message when |
14 | | -// the real problem was a wrong header. The run must fail loudly up front |
15 | | -// and name the missing columns. |
16 | 13 | func TestLoadRecommendationsFromCSV_HeaderValidation_1327(t *testing.T) { |
17 | 14 | tests := []struct { |
18 | 15 | name string |
19 | 16 | header string |
| 17 | + row string |
20 | 18 | errContains string |
21 | 19 | }{ |
22 | | - {"missing Service", "Region,ResourceType,Count\n", "Service"}, |
23 | | - {"missing Region", "Service,ResourceType,Count\n", "Region"}, |
24 | | - {"missing ResourceType", "Service,Region,Count\n", "ResourceType"}, |
25 | | - {"missing Count", "Service,Region,ResourceType\n", "Count"}, |
26 | | - {"multiple missing", "Service,Count\n", "Region, ResourceType"}, |
| 20 | + {"missing Service", "Region,ResourceType,Count\n", "us-east-1,db.t3.micro,2\n", "Service"}, |
| 21 | + {"missing Region", "Service,ResourceType,Count\n", "rds,db.t3.micro,2\n", "Region"}, |
| 22 | + {"missing ResourceType", "Service,Region,Count\n", "rds,us-east-1,2\n", "ResourceType"}, |
| 23 | + {"missing Count", "Service,Region,ResourceType\n", "rds,us-east-1,db.t3.micro\n", "Count"}, |
| 24 | + {"multiple missing", "Service,Count\n", "rds,2\n", "Region, ResourceType"}, |
27 | 25 | { |
28 | 26 | "legacy TEST-02 headers rejected", |
29 | 27 | "Service,Region,Instance Type,Instance Count\n", |
| 28 | + "rds,us-east-1,db.t3.micro,2\n", |
30 | 29 | "ResourceType, Count", |
31 | 30 | }, |
32 | 31 | } |
33 | 32 | for _, tt := range tests { |
34 | 33 | t.Run(tt.name, func(t *testing.T) { |
35 | | - err := loadCSVContent(t, tt.header+"rds,us-east-1,db.t3.micro,2\n") |
| 34 | + err := loadCSVContent(t, tt.header+tt.row) |
36 | 35 | require.Error(t, err) |
37 | 36 | assert.Contains(t, err.Error(), "CSV header missing required columns") |
38 | 37 | assert.Contains(t, err.Error(), tt.errContains) |
39 | 38 | }) |
40 | 39 | } |
41 | 40 | } |
42 | 41 |
|
43 | | -// A UTF-8 BOM is stripped before header matching so Excel-exported CSVs |
44 | | -// parse instead of failing the required-column check on the BOM-prefixed |
45 | | -// first header cell. |
46 | 42 | func TestLoadRecommendationsFromCSV_BOMPrefixed_1327(t *testing.T) { |
47 | 43 | recs, err := loadRecommendationsFromCSV(writeTestRecommendationsCSV(t, |
48 | 44 | "\ufeffService,Region,ResourceType,Count\nrds,us-east-1,db.t3.micro,2\n")) |
49 | 45 | require.NoError(t, err) |
50 | 46 | require.Len(t, recs, 1) |
| 47 | + assert.Equal(t, common.ServiceRDS, recs[0].Service) |
51 | 48 | assert.Equal(t, "us-east-1", recs[0].Region) |
52 | 49 | assert.Equal(t, 2, recs[0].Count) |
53 | 50 | } |
54 | 51 |
|
| 52 | +func TestLoadRecommendationsFromCSV_Encoding_1327(t *testing.T) { |
| 53 | + t.Run("UTF-16", func(t *testing.T) { |
| 54 | + units := utf16.Encode([]rune("Service,Region,ResourceType,Count\nrds,us-east-1,db.t3.micro,2\n")) |
| 55 | + for _, tt := range []struct { |
| 56 | + name string |
| 57 | + bom []byte |
| 58 | + order binary.ByteOrder |
| 59 | + }{ |
| 60 | + {"little endian", []byte{0xff, 0xfe}, binary.LittleEndian}, |
| 61 | + {"big endian", []byte{0xfe, 0xff}, binary.BigEndian}, |
| 62 | + } { |
| 63 | + t.Run(tt.name, func(t *testing.T) { |
| 64 | + encoded := make([]byte, 2+2*len(units)) |
| 65 | + copy(encoded, tt.bom) |
| 66 | + for i, unit := range units { |
| 67 | + tt.order.PutUint16(encoded[2+2*i:], unit) |
| 68 | + } |
| 69 | + err := loadCSVContent(t, string(encoded)) |
| 70 | + require.Error(t, err) |
| 71 | + assert.EqualError(t, err, "CSV header: column 1: invalid UTF-8 encoding") |
| 72 | + }) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("Latin-1 optional header", func(t *testing.T) { |
| 77 | + err := loadCSVContent(t, "Service,Region,ResourceType,Count,Caf\xe9\n"+ |
| 78 | + "rds,us-east-1,db.t3.micro,2,prod\n") |
| 79 | + require.Error(t, err) |
| 80 | + assert.EqualError(t, err, "CSV header: column 5: invalid UTF-8 encoding") |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("Latin-1 AccountName", func(t *testing.T) { |
| 84 | + err := loadCSVContent(t, "Service,Region,ResourceType,Count,AccountName\n"+ |
| 85 | + "rds,us-east-1,db.t3.micro,2,Caf\xe9\n") |
| 86 | + require.Error(t, err) |
| 87 | + assert.EqualError(t, err, "CSV line 2: column 5: invalid UTF-8 encoding") |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("invalid ignored field in TOTAL after multiline record", func(t *testing.T) { |
| 91 | + err := loadCSVContent(t, "Service,Region,ResourceType,Count,Ignored\n"+ |
| 92 | + "rds,us-east-1,db.t3.micro,2,\"prod\naccount\"\n"+ |
| 93 | + "TOTAL,,,2,\xff\n") |
| 94 | + require.Error(t, err) |
| 95 | + assert.EqualError(t, err, "CSV line 4: column 5: invalid UTF-8 encoding") |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("valid non-ASCII UTF-8", func(t *testing.T) { |
| 99 | + recs, err := loadRecommendationsFromCSV(writeTestRecommendationsCSV(t, |
| 100 | + "Service,Region,ResourceType,Count,AccountName\n"+ |
| 101 | + "rds,us-east-1,db.t3.micro,2,Café 東京\n")) |
| 102 | + require.NoError(t, err) |
| 103 | + require.Len(t, recs, 1) |
| 104 | + assert.Equal(t, "Café 東京", recs[0].AccountName) |
| 105 | + }) |
| 106 | +} |
| 107 | + |
55 | 108 | // Lock in encoding/csv behaviors the purchase path relies on. |
56 | 109 | func TestLoadRecommendationsFromCSV_EdgeCases_1327(t *testing.T) { |
57 | 110 | t.Run("CRLF line endings", func(t *testing.T) { |
|
0 commit comments