| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/BurntSushi/toml" |
| 11 | ) |
| 12 | |
| 13 | func TestAttachmentStreamLimitDefaultsAndUserValues(t *testing.T) { |
| 14 | if DefaultAttachmentStreamBytes != 1<<30 { |
| 15 | t.Fatal("attachment disk safety default must be 1 GiB") |
| 16 | } |
| 17 | for _, tc := range []struct { |
| 18 | name string |
| 19 | value *int64 |
| 20 | bytes int64 |
| 21 | source string |
| 22 | }{ |
| 23 | {"unset", nil, 1 << 30, "default"}, |
| 24 | {"negative", attachmentBytes(-1), 1 << 30, "default"}, |
| 25 | {"unlimited", attachmentBytes(0), 0, "user"}, |
| 26 | {"custom", attachmentBytes(4096), 4096, "user"}, |
| 27 | } { |
| 28 | t.Run(tc.name, func(t *testing.T) { |
| 29 | cfg := Default() |
| 30 | cfg.Attachments.MaxStreamBytes = tc.value |
| 31 | if got := cfg.AttachmentStreamLimit(); got != (AttachmentLimit{Bytes: tc.bytes, Source: tc.source}) { |
| 32 | t.Fatalf("limit=%+v", got) |
| 33 | } |
| 34 | }) |
| 35 | } |
| 36 | var cfg *Config |
| 37 | if got := cfg.AttachmentStreamLimit(); got != (AttachmentLimit{Bytes: 1 << 30, Source: "default"}) { |
| 38 | t.Fatalf("nil config limit=%+v", got) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestAttachmentStreamLimitCannotBeSetByProject(t *testing.T) { |
| 43 | for _, user := range []string{"", "0", "8"} { |
| 44 | for _, project := range []string{"0", "1", "100"} { |
| 45 | t.Run(user+"/"+project, func(t *testing.T) { |
| 46 | body := "" |
| 47 | if user != "" { |
| 48 | body = "[attachments]\nmax_stream_bytes = " + user + "\n" |
| 49 | } |
| 50 | cfg, _ := loadScoped(t, body, "[attachments]\nmax_stream_bytes = "+project+"\n") |
| 51 | want := AttachmentLimit{Bytes: 1 << 30, Source: "default"} |
| 52 | if user != "" { |
| 53 | want = AttachmentLimit{Bytes: 8, Source: "user"} |
| 54 | if user == "0" { |
| 55 | want.Bytes = 0 |
| 56 | } |
| 57 | } |
| 58 | if cfg.AttachmentStreamLimit() != want { |
| 59 | t.Fatalf("project changed user limit: %+v", cfg.AttachmentStreamLimit()) |
| 60 | } |
| 61 | found := false |
| 62 | for _, ignored := range cfg.IgnoredProjectSettings() { |
| 63 | found = found || ignored.Key == "attachments.max_stream_bytes" && ignored.Reason == ProjectUserOnly |
| 64 | } |
| 65 | if user != project && !found { |
| 66 | t.Fatalf("missing user-only diagnostic: %+v", cfg.IgnoredProjectSettings()) |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestAttachmentStreamLimitRoundTripAndHeldPointerIsolation(t *testing.T) { |
| 74 | for _, value := range []int64{0, 98765} { |
| 75 | cfg := Default() |
| 76 | cfg.Attachments.MaxStreamBytes = attachmentBytes(value) |
| 77 | body := RenderTOMLForScope(cfg, RenderScopeUser) |
| 78 | var decoded Config |
| 79 | if _, err := toml.Decode(body, &decoded); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | if got := decoded.AttachmentStreamLimit(); got != (AttachmentLimit{Bytes: value, Source: "user"}) { |
| 83 | t.Fatalf("roundtrip=%+v", got) |
| 84 | } |
| 85 | if project := RenderTOMLForScope(cfg, RenderScopeProject); strings.Contains(project, "[attachments]") { |
| 86 | t.Fatal("project rendering persisted a user-only limit") |
| 87 | } |
| 88 | held := holdUserScope(cfg) |
| 89 | *cfg.Attachments.MaxStreamBytes = value + 1 |
| 90 | held.narrow(cfg, t.TempDir()) |
| 91 | if *cfg.Attachments.MaxStreamBytes != value { |
| 92 | t.Fatal("project merge mutated held user limit through shared pointer") |
| 93 | } |
| 94 | } |
| 95 | if body := RenderTOMLForScope(Default(), RenderScopeUser); strings.Contains(body, "[attachments]") { |
| 96 | t.Fatal("default limit was materialized as a user choice") |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func attachmentBytes(value int64) *int64 { return &value } |
| 101 | |
| 102 | func TestLoadAttachmentStreamLimitStrictUserRead(t *testing.T) { |
| 103 | home := t.TempDir() |
| 104 | t.Setenv("REASONIX_HOME", home) |
| 105 | path := filepath.Join(home, "config.toml") |
| 106 | for _, value := range []string{"0", "2048"} { |
| 107 | body := []byte("[attachments]\nmax_stream_bytes = " + value + "\n") |
| 108 | if err := os.WriteFile(path, body, 0600); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | limit, err := LoadAttachmentStreamLimit() |
| 112 | if err != nil || limit.Source != "user" || fmt.Sprint(limit.Bytes) != value { |
| 113 | t.Fatalf("limit=%+v err=%v", limit, err) |
| 114 | } |
| 115 | after, err := os.ReadFile(path) |
| 116 | if err != nil || string(after) != string(body) { |
| 117 | t.Fatal("strict upload read rewrote user configuration", err) |
| 118 | } |
| 119 | } |
| 120 | if err := os.WriteFile(path, []byte("[attachments]\nmax_stream_bytes = [broken"), 0600); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | if _, err := LoadAttachmentStreamLimit(); err == nil { |
| 124 | t.Fatal("strict upload read recovered a malformed config to wider defaults") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func ExampleConfig_AttachmentStreamLimit() { |
| 129 | cfg := Default() |
| 130 | fmt.Println(cfg.AttachmentStreamLimit()) |
| 131 | // Output: {1073741824 default} |
| 132 | } |
| 133 | |
| 134 | func TestAttachmentLimitSavePreservesUserChoice(t *testing.T) { |
| 135 | for _, value := range []int64{0, 19} { |
| 136 | home := t.TempDir() |
| 137 | t.Setenv("REASONIX_HOME", home) |
| 138 | path := filepath.Join(home, "config.toml") |
| 139 | cfg := Default() |
| 140 | cfg.Attachments.MaxStreamBytes = &value |
| 141 | cfg.UI.Theme = "dark" |
| 142 | if err := cfg.SaveTo(path); err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | loaded, err := LoadForEditReadOnlyStrict(path) |
| 146 | if err != nil || loaded.AttachmentStreamLimit() != cfg.AttachmentStreamLimit() { |
| 147 | t.Fatalf("saved limit=%+v err=%v", loaded.AttachmentStreamLimit(), err) |
| 148 | } |
| 149 | if loaded.UI.Theme != "dark" { |
| 150 | t.Fatal("existing user theme lost") |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 |