size_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (C) 2017 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "testing"
  9. "github.com/syncthing/syncthing/lib/fs"
  10. "github.com/syncthing/syncthing/lib/structutil"
  11. )
  12. type TestStruct struct {
  13. Size Size `default:"10%"`
  14. }
  15. func TestSizeDefaults(t *testing.T) {
  16. x := &TestStruct{}
  17. structutil.SetDefaults(x)
  18. if !x.Size.Percentage() {
  19. t.Error("not percentage")
  20. }
  21. if x.Size.Value != 10 {
  22. t.Error("not ten")
  23. }
  24. }
  25. func TestParseSize(t *testing.T) {
  26. cases := []struct {
  27. in string
  28. ok bool
  29. val float64
  30. pct bool
  31. }{
  32. // We accept upper case SI units
  33. {"5K", true, 5e3, false}, // even when they should be lower case
  34. {"4 M", true, 4e6, false},
  35. {"3G", true, 3e9, false},
  36. {"2 T", true, 2e12, false},
  37. // We accept lower case SI units out of user friendliness
  38. {"1 k", true, 1e3, false},
  39. {"2m", true, 2e6, false},
  40. {"3 g", true, 3e9, false},
  41. {"4t", true, 4e12, false},
  42. // Fractions are OK
  43. {"123.456 k", true, 123.456e3, false},
  44. {"0.1234 m", true, 0.1234e6, false},
  45. {"3.45 g", true, 3.45e9, false},
  46. // We don't parse negative numbers
  47. {"-1", false, 0, false},
  48. {"-1k", false, 0, false},
  49. {"-0.45g", false, 0, false},
  50. // We accept various unit suffixes on the unit prefix
  51. {"100 KBytes", true, 100e3, false},
  52. {"100 Kbps", true, 100e3, false},
  53. {"100 MAU", true, 100e6, false},
  54. // Percentages are OK
  55. {"1%", true, 1, true},
  56. {"200%", true, 200, true}, // even large ones
  57. {"200K%", true, 200e3, true}, // even with prefixes, although this makes no sense
  58. {"2.34%", true, 2.34, true}, // fractions are A-ok
  59. // The empty string is a valid zero
  60. {"", true, 0, false},
  61. {" ", true, 0, false},
  62. // Just numbers are fine too
  63. {"0", true, 0, false},
  64. {"3", true, 3, false},
  65. {"34.3", true, 34.3, false},
  66. }
  67. for _, tc := range cases {
  68. size, err := ParseSize(tc.in)
  69. if !tc.ok {
  70. if err == nil {
  71. t.Errorf("Unexpected nil error in UnmarshalText(%q)", tc.in)
  72. }
  73. continue
  74. }
  75. if err != nil {
  76. t.Errorf("Unexpected error in UnmarshalText(%q): %v", tc.in, err)
  77. continue
  78. }
  79. if size.BaseValue() > tc.val*1.001 || size.BaseValue() < tc.val*0.999 {
  80. // Allow 0.1% slop due to floating point multiplication
  81. t.Errorf("Incorrect value in UnmarshalText(%q): %v, wanted %v", tc.in, size.BaseValue(), tc.val)
  82. }
  83. if size.Percentage() != tc.pct {
  84. t.Errorf("Incorrect percentage bool in UnmarshalText(%q): %v, wanted %v", tc.in, size.Percentage(), tc.pct)
  85. }
  86. }
  87. }
  88. func TestFormatSI(t *testing.T) {
  89. cases := []struct {
  90. bytes uint64
  91. result string
  92. }{
  93. {
  94. bytes: 0,
  95. result: "0 ", // space for unit
  96. },
  97. {
  98. bytes: 999,
  99. result: "999 ",
  100. },
  101. {
  102. bytes: 1000,
  103. result: "1.0 K",
  104. },
  105. {
  106. bytes: 1023 * 1000,
  107. result: "1.0 M",
  108. },
  109. {
  110. bytes: 5 * 1000 * 1000 * 1000,
  111. result: "5.0 G",
  112. },
  113. {
  114. bytes: 50000 * 1000 * 1000 * 1000 * 1000,
  115. result: "50000.0 T",
  116. },
  117. }
  118. for _, tc := range cases {
  119. res := formatSI(tc.bytes)
  120. if res != tc.result {
  121. t.Errorf("formatSI(%d) => %q, expected %q", tc.bytes, res, tc.result)
  122. }
  123. }
  124. }
  125. func TestCheckAvailableSize(t *testing.T) {
  126. cases := []struct {
  127. req, free, total uint64
  128. minFree string
  129. ok bool
  130. }{
  131. {10, 1e8, 1e9, "1%", true},
  132. {1e4, 1e3, 1e9, "1%", false},
  133. {1e2, 1e3, 1e9, "1%", false},
  134. {1e9, 1 << 62, 1 << 63, "1%", true},
  135. {10, 1e8, 1e9, "1M", true},
  136. {1e4, 1e3, 1e9, "1M", false},
  137. {1e2, 1e3, 1e9, "1M", false},
  138. {1e9, 1 << 62, 1 << 63, "1M", true},
  139. }
  140. for _, tc := range cases {
  141. minFree, err := ParseSize(tc.minFree)
  142. if err != nil {
  143. t.Errorf("Failed to parse %v: %v", tc.minFree, err)
  144. continue
  145. }
  146. usage := fs.Usage{Free: tc.free, Total: tc.total}
  147. err = checkAvailableSpace(tc.req, minFree, usage)
  148. t.Log(err)
  149. if (err == nil) != tc.ok {
  150. t.Errorf("checkAvailableSpace(%v, %v, %v) == %v, expected %v", tc.req, minFree, usage, err, tc.ok)
  151. }
  152. }
  153. }