fileutil_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package fileutil
  10. import (
  11. "io/ioutil"
  12. "os"
  13. "path/filepath"
  14. "testing"
  15. )
  16. const TESTPATH = "fileutiltestpath"
  17. func TestDirectoryExists(t *testing.T) {
  18. os.Remove(TESTPATH)
  19. res, err := PathExists(TESTPATH)
  20. if err != nil {
  21. t.Error(err.Error())
  22. return
  23. }
  24. if res {
  25. t.Error("Path test should not exist")
  26. }
  27. os.Mkdir(TESTPATH, 0770)
  28. defer func() {
  29. os.RemoveAll(TESTPATH)
  30. }()
  31. res, err = PathExists(TESTPATH)
  32. if err != nil {
  33. t.Error(err.Error())
  34. return
  35. }
  36. if !res {
  37. t.Error("Path test should exist after it was created")
  38. return
  39. }
  40. _, err = PathExists("**\x00")
  41. if err == nil {
  42. t.Error("Incorrect paths should throw an error")
  43. return
  44. }
  45. }
  46. func TestIsDir(t *testing.T) {
  47. os.Remove(TESTPATH)
  48. res, err := IsDir(TESTPATH)
  49. if err != nil && !os.IsNotExist(err) {
  50. t.Error(err.Error())
  51. return
  52. }
  53. if res {
  54. t.Error("Path test should not exist")
  55. }
  56. os.Mkdir(TESTPATH, 0770)
  57. defer func() {
  58. os.RemoveAll(TESTPATH)
  59. }()
  60. res, err = IsDir(TESTPATH)
  61. if err != nil {
  62. t.Error(err.Error())
  63. return
  64. }
  65. if !res {
  66. t.Error("Dir test should exist after it was created")
  67. return
  68. }
  69. _, err = IsDir("**\x00")
  70. if err == nil {
  71. t.Error("Incorrect paths should throw an error")
  72. return
  73. }
  74. }
  75. func TestCheckSumFiles(t *testing.T) {
  76. os.Remove(TESTPATH)
  77. res, err := IsDir(TESTPATH)
  78. if err != nil && !os.IsNotExist(err) {
  79. t.Error(err.Error())
  80. return
  81. }
  82. if res {
  83. t.Error("Path test should not exist")
  84. }
  85. os.Mkdir(TESTPATH, 0770)
  86. defer func() {
  87. os.RemoveAll(TESTPATH)
  88. }()
  89. testfile := filepath.Join(TESTPATH, "testfile.txt")
  90. ioutil.WriteFile(testfile, []byte("Omnium enim rerum\nprincipia parva sunt"), 0660)
  91. if res, err := CheckSumFile(testfile); res != "90a258b01ceab4058906318bf0b34a31f2ff7ac2268c7bf3df9168f1f6ca5bc6" || err != nil {
  92. t.Error("Unexpected result:", res, err)
  93. return
  94. }
  95. // Test fast checksum
  96. if res, err := CheckSumFileFast(testfile); res != "6f05b934" || err != nil {
  97. t.Error("Unexpected result:", res, err)
  98. return
  99. }
  100. testfile = filepath.Join(TESTPATH, "testfile2.txt")
  101. buf := make([]byte, fastSumSampleSize*8)
  102. for i := 0; i < fastSumSampleSize*8; i++ {
  103. buf[i] = byte(i % 10)
  104. }
  105. ioutil.WriteFile(testfile, buf, 0660)
  106. if res, err := CheckSumFileFast(testfile); res != "14294b07" || err != nil {
  107. t.Error("Unexpected result:", res, err)
  108. return
  109. }
  110. }