package main import ( "encoding/json" "reflect" "testing" ) // The Kubernetes client reads U+0085 as a line break in a plain scalar, so a // value carrying one adds a key. A parser that disagrees, as PyYAML does on // U+0085 and U+2028, would call the same render safe. func TestNELBreaksAPlainScalar(t *testing.T) { docs, err := decodeStream("data:\n v: " + c + "\u0085" + c + "x: y\n") if err != nil { t.Fatal(err) } // The break ends the indented value, so the key lands at the top. if _, ok := asMap(docs[0].obj)[c+"x"]; !ok { t.Fatalf("U+0085 did not start a key: %v", docs[0].obj) } } // helm reads --values as YAML. A raw U+0085 inside a quoted string is a line // break there and folds into a space, so the payload would not reach the // chart as written. func TestEncodeValuesEscapesNonASCII(t *testing.T) { in := map[string]any{"v": c + "\u0085\u2028\u00e9\U0001F600<&>", "n": json.Number("1.0"), "i": 2} blob, err := encodeValues(in) if err != nil { t.Fatal(err) } for _, r := range string(blob) { if r >= 0x80 { t.Fatalf("non-ASCII %U left raw in %s", r, blob) } } if want := `{"i":2,"n":1.0,"v":"` + c + `\u0085\u2028\u00e9\U0001f600<&>"}`; string(blob) != want { t.Fatalf("encodeValues = %s, want %s", blob, want) } back, _, err := decodeYAML(string(blob)) if err != nil { t.Fatal(err) } if got := asMap(back)["v"]; got != in["v"] { t.Fatalf("the values did not read back as written: %q", got) } raw, _, err := decodeYAML(`{"v": "a` + "\u0085" + `b"}`) if err != nil { t.Fatal(err) } if asMap(raw)["v"] == "a\u0085b" { t.Fatal("a raw U+0085 survived a quoted scalar, so escaping it guards nothing") } } func TestDecodeStreamSources(t *testing.T) { text := "---\n# Source: a/templates/one.yaml\nkind: A\n---\n# Source: a/templates/empty.yaml\n---\n# Source: a/templates/two.yaml\nkind: B\n" docs, err := decodeStream(text) if err != nil { t.Fatal(err) } var got []string for _, d := range docs { got = append(got, d.source+"="+asMap(d.obj)["kind"].(string)) } if want := []string{"a/templates/one.yaml=A", "a/templates/two.yaml=B"}; !reflect.DeepEqual(got, want) { t.Fatalf("documents = %v, want %v", got, want) } if _, err := decodeStream("kind: A\n---\nkind: [B\n"); err == nil { t.Fatal("a stream with a document that does not decode decoded") } } func TestStructured(t *testing.T) { for _, tc := range []struct { in string want bool }{ {"a: 1\nb: 2\n", true}, {"[1, 2]", true}, {` {"a": 1} `, true}, {`{"a": 1} trailing`, false}, {"plain text", false}, {"a: 1\n---\nb: 2\n", false}, {"", false}, {"\u0085", false}, } { if got := structured(tc.in) != nil; got != tc.want { t.Errorf("structured(%q) = %v, want %v", tc.in, got, tc.want) } } } func TestTypeName(t *testing.T) { for _, tc := range []struct { in any want string }{ {float64(1), "int"}, {1.5, "float"}, {1e21, "float"}, {json.Number("1"), "int"}, {json.Number("1.0"), "float"}, {"s", "str"}, {nil, "NoneType"}, {true, "bool"}, } { if got := typeName(tc.in); got != tc.want { t.Errorf("typeName(%#v) = %q, want %q", tc.in, got, tc.want) } } } func TestSplitLines(t *testing.T) { got := splitLines("a\r\nb\u0085c\u2028d\re\n") if want := []string{"a", "b", "c", "d", "e"}; !reflect.DeepEqual(got, want) { t.Fatalf("splitLines = %q, want %q", got, want) } } func TestRepr(t *testing.T) { for _, tc := range []struct{ in, want string }{ {"plain", `'plain'`}, {"it's", `"it's"`}, {`it's "x"`, `'it\'s "x"'`}, {"a\nb\u0085\u2028\u00e9", "'a\\nb\\x85\\u2028\u00e9'"}, } { if got := repr(tc.in); got != tc.want { t.Errorf("repr(%q) = %s, want %s", tc.in, got, tc.want) } } } func TestBounded(t *testing.T) { for _, tc := range []struct { text, word string want int }{ {"size: 1Gi", "1Gi", 1}, {"size: 11Gi 1Gi-x 1Gib", "1Gi", 1}, {"1Gi,1Gi 1Gi", "1Gi", 3}, {"x1Gi", "1Gi", 0}, } { if got := bounded(tc.text, tc.word); got != tc.want { t.Errorf("bounded(%q, %q) = %d, want %d", tc.text, tc.word, got, tc.want) } } }