| 1 | package xlsx_test |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/sheets/internal/sheets/xlsx" |
| 9 | ) |
| 10 | |
| 11 | // canary is content no refusal may repeat. |
| 12 | const canary = "SECRET-LEDGER-7731" |
| 13 | |
| 14 | // Each workbook here must refuse the whole import. Where the cell is |
| 15 | // known, the refusal names it as data (xlsx.CellError) and its text |
| 16 | // carries the coordinate and nothing the cell held. |
| 17 | func TestImportRefusesWhatItCannotTrust(t *testing.T) { |
| 18 | long := strings.Repeat("x", 9000) |
| 19 | cases := []struct { |
| 20 | name string |
| 21 | book rawBook |
| 22 | ref string // "" when the refusal precedes any one cell |
| 23 | }{ |
| 24 | {"unsupported formula without a saved value", |
| 25 | rawBook{sheets: []string{row(1, formula("A1", "", `FOO("`+canary+`")`, nil))}}, "A1"}, |
| 26 | {"unsupported formula that saved an error", |
| 27 | rawBook{sheets: []string{row(1, formula("A1", "e", `FOO("`+canary+`")`, ptr("#N/A")))}}, "A1"}, |
| 28 | {"unsupported formula that saved a shared-string index", |
| 29 | rawBook{sheets: []string{row(1, formula("A1", "s", "FOO(1)", ptr("0")))}}, "A1"}, |
| 30 | {"unsupported formula that saved a non-number", |
| 31 | rawBook{sheets: []string{row(1, formula("A1", "", "FOO(1)", ptr(canary)))}}, "A1"}, |
| 32 | {"unsupported formula that saved an infinite number", |
| 33 | rawBook{sheets: []string{row(1, formula("A1", "", "FOO(1)", ptr("1e999")))}}, "A1"}, |
| 34 | {"unsupported formula that saved a boolean other than 0 or 1", |
| 35 | rawBook{sheets: []string{row(1, formula("A1", "b", "FOO(1)", ptr("2")))}}, "A1"}, |
| 36 | {"unsupported formula that saved a date", |
| 37 | rawBook{sheets: []string{row(1, formula("A1", "d", "FOO(1)", ptr("2026-11-01")))}}, "A1"}, |
| 38 | {"a literal error value", |
| 39 | rawBook{sheets: []string{row(1, `<c r="A1" t="e"><v>#N/A</v></c>`)}}, "A1"}, |
| 40 | {"a number cell that is not a number", |
| 41 | rawBook{sheets: []string{row(1, num("A1", canary))}}, "A1"}, |
| 42 | {"ambiguous text past the input limit once wrapped", |
| 43 | rawBook{sheets: []string{row(1, inline("A1", "="+long))}}, "A1"}, |
| 44 | {"a frozen saved string past the input limit once wrapped", |
| 45 | rawBook{sheets: []string{row(1, formula("A1", "str", "FOO(1)", ptr("="+long)))}}, "A1"}, |
| 46 | {"a shared formula follower whose master is missing", |
| 47 | rawBook{sheets: []string{row(1, `<c r="A1"><f t="shared" si="5"/><v>1</v></c>`)}}, "A1"}, |
| 48 | {"a cell written twice", |
| 49 | rawBook{sheets: []string{row(1, num("A1", "1"), formula("A1", "", "FOO(1)", ptr("2")))}}, ""}, |
| 50 | {"a cell written twice in different case", |
| 51 | rawBook{sheets: []string{row(1, num("A1", "1"), num("a1", "2"))}}, ""}, |
| 52 | {"a cell with two formulas", |
| 53 | rawBook{sheets: []string{row(1, `<c r="A1"><f>1+1</f><f>FOO(1)</f><v>2</v></c>`)}}, ""}, |
| 54 | {"a cell with two saved values", |
| 55 | rawBook{sheets: []string{row(1, `<c r="A1"><f>FOO(1)</f><v>2</v><v>3</v></c>`)}}, ""}, |
| 56 | {"a formula of a type the standard does not define", |
| 57 | rawBook{sheets: []string{row(1, `<c r="A1"><f t="bogus">1+1</f><v>2</v></c>`)}}, ""}, |
| 58 | {"a formula cell with no reference", |
| 59 | rawBook{sheets: []string{`<row r="1"><c><f>FOO(1)</f><v>2</v></c></row>`}}, ""}, |
| 60 | {"a formula cell filed under another row", |
| 61 | rawBook{sheets: []string{row(1, formula("A5", "", "FOO(1)", ptr("2")))}}, ""}, |
| 62 | {"nesting past the depth bound", |
| 63 | rawBook{sheets: []string{row(1, num("A1", "1"))}, |
| 64 | extra: `<extLst>` + strings.Repeat("<x>", 80) + strings.Repeat("</x>", 80) + `</extLst>`}, ""}, |
| 65 | {"a chartsheet first", |
| 66 | rawBook{sheets: []string{row(1, num("A1", "1"))}, parts: map[string]string{ |
| 67 | "xl/_rels/workbook.xml.rels": `<?xml version="1.0" encoding="UTF-8"?> |
| 68 | <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> |
| 69 | <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="worksheets/sheet1.xml"/> |
| 70 | <Relationship Id="rIdS" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/> |
| 71 | </Relationships>`}}, ""}, |
| 72 | } |
| 73 | for _, c := range cases { |
| 74 | t.Run(c.name, func(t *testing.T) { |
| 75 | _, _, err := importRaw(t, c.book) |
| 76 | if err == nil { |
| 77 | t.Fatal("Import succeeded, want a refusal") |
| 78 | } |
| 79 | if strings.Contains(err.Error(), canary) || strings.Contains(err.Error(), "xxxx") { |
| 80 | t.Fatalf("refusal carries cell content: %q", err.Error()) |
| 81 | } |
| 82 | var cell *xlsx.CellError |
| 83 | if c.ref != "" && (!errors.As(err, &cell) || cell.Ref != c.ref) { |
| 84 | t.Fatalf("refusal = %v, want a CellError at %s", err, c.ref) |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // A <c> or <v> anywhere but its own place in sheetData is not a cell |
| 91 | // and not a saved value, for this reader as for excelize; a decoy |
| 92 | // there must neither add a cell nor replace the real saved value. |
| 93 | func TestImportIgnoresDecoysOutsideTheCellPath(t *testing.T) { |
| 94 | book := rawBook{ |
| 95 | sheets: []string{row(1, |
| 96 | `<c r="A1"><f>FOO(1)</f><extLst><ext><v>9</v><c r="B1"><f>FOO(2)</f></c></ext></extLst><v>7</v></c>`)}, |
| 97 | extra: `<extLst><ext><sheetData><row r="2"><c r="A2"><f>FOO(3)</f></c></row></sheetData></ext></extLst>`, |
| 98 | } |
| 99 | h, rep, err := importRaw(t, book) |
| 100 | if err != nil { |
| 101 | t.Fatalf("Import: %v", err) |
| 102 | } |
| 103 | if raw, v := cellByRef(t, h, "A1"); raw != "7" || v.Num != 7 { |
| 104 | t.Errorf("A1 = %q %+v, want the saved 7", raw, v) |
| 105 | } |
| 106 | for _, ref := range []string{"B1", "A2"} { |
| 107 | if raw, _ := cellByRef(t, h, ref); raw != "" { |
| 108 | t.Errorf("%s = %q, want nothing: it was a decoy", ref, raw) |
| 109 | } |
| 110 | } |
| 111 | if got := entry(rep, xlsx.FormulaUnsupported); got.Count != 1 { |
| 112 | t.Errorf("unsupported = %+v, want A1 alone", got) |
| 113 | } |
| 114 | } |
| 115 | |