1
0

config_mutation_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. // copyFile is the workhorse invoked by init()'s Windows-only DB migration
  8. // (config.go:214), the branch guarded by the platform check on config.go:196.
  9. // The init() guard itself cannot be re-driven from an in-process test (init runs
  10. // once at package load, the OS check is a compile-time constant, and the old-DB
  11. // source path is hardcoded to a system location), so these tests pin down the
  12. // migration payload's contract instead.
  13. func TestCopyFileCopiesContents(t *testing.T) {
  14. dir := t.TempDir()
  15. src := filepath.Join(dir, "src.db")
  16. dst := filepath.Join(dir, "dst.db")
  17. want := []byte("3x-ui sqlite payload\x00\x01\x02")
  18. if err := os.WriteFile(src, want, 0o600); err != nil {
  19. t.Fatalf("write src: %v", err)
  20. }
  21. if err := copyFile(src, dst); err != nil {
  22. t.Fatalf("copyFile returned error: %v", err)
  23. }
  24. got, err := os.ReadFile(dst)
  25. if err != nil {
  26. t.Fatalf("read dst: %v", err)
  27. }
  28. if string(got) != string(want) {
  29. t.Errorf("dst contents = %q, want %q", got, want)
  30. }
  31. }
  32. func TestCopyFileMissingSourceReturnsError(t *testing.T) {
  33. dir := t.TempDir()
  34. src := filepath.Join(dir, "does-not-exist.db")
  35. dst := filepath.Join(dir, "dst.db")
  36. if err := copyFile(src, dst); err == nil {
  37. t.Fatal("copyFile with missing source returned nil error, want error")
  38. }
  39. if _, err := os.Stat(dst); !os.IsNotExist(err) {
  40. t.Errorf("dst should not be created when source is missing, stat err = %v", err)
  41. }
  42. }
  43. func TestCopyFileOverwritesDestination(t *testing.T) {
  44. dir := t.TempDir()
  45. src := filepath.Join(dir, "src.db")
  46. dst := filepath.Join(dir, "dst.db")
  47. if err := os.WriteFile(src, []byte("new"), 0o600); err != nil {
  48. t.Fatalf("write src: %v", err)
  49. }
  50. if err := os.WriteFile(dst, []byte("stale-and-longer"), 0o600); err != nil {
  51. t.Fatalf("write dst: %v", err)
  52. }
  53. if err := copyFile(src, dst); err != nil {
  54. t.Fatalf("copyFile returned error: %v", err)
  55. }
  56. got, err := os.ReadFile(dst)
  57. if err != nil {
  58. t.Fatalf("read dst: %v", err)
  59. }
  60. if string(got) != "new" {
  61. t.Errorf("dst contents = %q, want %q (truncated overwrite)", got, "new")
  62. }
  63. }