statfs_darwin_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package statfs_test
  15. import (
  16. "fmt"
  17. "math"
  18. "regexp"
  19. "syscall"
  20. "github.com/jacobsa/fuse/fuseops"
  21. . "github.com/jacobsa/ogletest"
  22. )
  23. // Sample output:
  24. //
  25. // Filesystem 1024-blocks Used Available Capacity iused ifree %iused Mounted on
  26. // fake@bucket 32 16 16 50% 0 0 100% /Users/jacobsa/tmp/mp
  27. //
  28. var gDfOutputRegexp = regexp.MustCompile(`^\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+\d+%\s+\d+\s+\d+\s+\d+%.*$`)
  29. ////////////////////////////////////////////////////////////////////////
  30. // Helpers
  31. ////////////////////////////////////////////////////////////////////////
  32. func convertName(in []int8) (s string) {
  33. var tmp []byte
  34. for _, v := range in {
  35. if v == 0 {
  36. break
  37. }
  38. tmp = append(tmp, byte(v))
  39. }
  40. s = string(tmp)
  41. return
  42. }
  43. ////////////////////////////////////////////////////////////////////////
  44. // Tests
  45. ////////////////////////////////////////////////////////////////////////
  46. func (t *StatFSTest) Syscall_ZeroValues() {
  47. var err error
  48. var stat syscall.Statfs_t
  49. // Call without configuring a canned response, meaning the OS will see the
  50. // zero value for each field. The assertions below act as documentation for
  51. // the OS's behavior in this case.
  52. err = syscall.Statfs(t.Dir, &stat)
  53. AssertEq(nil, err)
  54. ExpectEq(4096, stat.Bsize)
  55. ExpectEq(65536, stat.Iosize)
  56. ExpectEq(0, stat.Blocks)
  57. ExpectEq(0, stat.Bfree)
  58. ExpectEq(0, stat.Bavail)
  59. ExpectEq(0, stat.Files)
  60. ExpectEq(0, stat.Ffree)
  61. ExpectEq("osxfuse", convertName(stat.Fstypename[:]))
  62. ExpectEq(t.canonicalDir, convertName(stat.Mntonname[:]))
  63. ExpectEq(fsName, convertName(stat.Mntfromname[:]))
  64. }
  65. func (t *StatFSTest) Syscall_NonZeroValues() {
  66. var err error
  67. var stat syscall.Statfs_t
  68. // Set up the canned response.
  69. canned := fuseops.StatFSOp{
  70. BlockSize: 1 << 15,
  71. IoSize: 1 << 16,
  72. Blocks: 1<<51 + 3,
  73. BlocksFree: 1<<43 + 5,
  74. BlocksAvailable: 1<<41 + 7,
  75. Inodes: 1<<59 + 11,
  76. InodesFree: 1<<58 + 13,
  77. }
  78. t.fs.SetStatFSResponse(canned)
  79. // Stat.
  80. err = syscall.Statfs(t.Dir, &stat)
  81. AssertEq(nil, err)
  82. ExpectEq(canned.BlockSize, stat.Bsize)
  83. ExpectEq(canned.IoSize, stat.Iosize)
  84. ExpectEq(canned.Blocks, stat.Blocks)
  85. ExpectEq(canned.BlocksFree, stat.Bfree)
  86. ExpectEq(canned.BlocksAvailable, stat.Bavail)
  87. ExpectEq(canned.Inodes, stat.Files)
  88. ExpectEq(canned.InodesFree, stat.Ffree)
  89. ExpectEq("osxfuse", convertName(stat.Fstypename[:]))
  90. ExpectEq(t.canonicalDir, convertName(stat.Mntonname[:]))
  91. ExpectEq(fsName, convertName(stat.Mntfromname[:]))
  92. }
  93. func (t *StatFSTest) BlockSizes() {
  94. var err error
  95. // Test a bunch of block sizes that the OS does or doesn't support
  96. // faithfully, checking what it transforms them too.
  97. testCases := []struct {
  98. fsBlockSize uint32
  99. expectedBsize uint32
  100. }{
  101. 0: {0, 4096},
  102. 1: {1, 128},
  103. 2: {3, 128},
  104. 3: {511, 512},
  105. 4: {512, 512},
  106. 5: {513, 1024},
  107. 6: {1023, 1024},
  108. 7: {1024, 1024},
  109. 8: {4095, 4096},
  110. 9: {1 << 16, 1 << 16},
  111. 10: {1 << 17, 1 << 17},
  112. 11: {1 << 18, 1 << 18},
  113. 12: {1 << 19, 1 << 19},
  114. 13: {1<<20 - 1, 1 << 20},
  115. 14: {1 << 20, 1 << 20},
  116. 15: {1<<20 + 1, 1 << 20},
  117. 16: {1 << 21, 1 << 20},
  118. 17: {1 << 22, 1 << 20},
  119. 18: {math.MaxInt32 - 1, 1 << 20},
  120. 19: {math.MaxInt32, 1 << 20},
  121. 20: {math.MaxInt32 + 1, 128},
  122. 21: {math.MaxInt32 + 1<<15, 1 << 15},
  123. 22: {math.MaxUint32, 1 << 20},
  124. }
  125. for i, tc := range testCases {
  126. desc := fmt.Sprintf("Case %d: block size %d", i, tc.fsBlockSize)
  127. // Set up.
  128. canned := fuseops.StatFSOp{
  129. BlockSize: tc.fsBlockSize,
  130. Blocks: 10,
  131. }
  132. t.fs.SetStatFSResponse(canned)
  133. // Check.
  134. var stat syscall.Statfs_t
  135. err = syscall.Statfs(t.Dir, &stat)
  136. AssertEq(nil, err)
  137. ExpectEq(tc.expectedBsize, stat.Bsize, "%s", desc)
  138. }
  139. }
  140. func (t *StatFSTest) IoSizes() {
  141. var err error
  142. // Test a bunch of io sizes that the OS does or doesn't support faithfully,
  143. // checking what it transforms them too.
  144. testCases := []struct {
  145. fsIoSize uint32
  146. expectedIosize uint32
  147. }{
  148. 0: {0, 65536},
  149. 1: {1, 4096},
  150. 2: {3, 4096},
  151. 3: {4095, 4096},
  152. 4: {4096, 4096},
  153. 5: {4097, 8192},
  154. 6: {8191, 8192},
  155. 7: {8192, 8192},
  156. 8: {8193, 16384},
  157. 9: {1 << 18, 1 << 18},
  158. 10: {1 << 20, 1 << 20},
  159. 11: {1 << 23, 1 << 23},
  160. 12: {1<<25 - 1, 1 << 25},
  161. 13: {1 << 25, 1 << 25},
  162. 14: {1<<25 + 1, 1 << 25},
  163. 15: {math.MaxInt32 - 1, 1 << 25},
  164. 16: {math.MaxInt32, 1 << 25},
  165. 17: {math.MaxInt32 + 1, 4096},
  166. 18: {math.MaxInt32 + 1<<15, 1 << 15},
  167. 19: {math.MaxUint32, 1 << 25},
  168. }
  169. for i, tc := range testCases {
  170. desc := fmt.Sprintf("Case %d: IO size %d", i, tc.fsIoSize)
  171. // Set up.
  172. canned := fuseops.StatFSOp{
  173. IoSize: tc.fsIoSize,
  174. Blocks: 10,
  175. }
  176. t.fs.SetStatFSResponse(canned)
  177. // Check.
  178. var stat syscall.Statfs_t
  179. err = syscall.Statfs(t.Dir, &stat)
  180. AssertEq(nil, err)
  181. ExpectEq(tc.expectedIosize, stat.Iosize, "%s", desc)
  182. }
  183. }