fs_test_part2.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. require "fs_test_utils"
  2. --[[
  3. 用例0201 异常文件打开删除
  4. 前置条件:
  5. 测试步骤与期望:
  6. ]]--
  7. function tc0201_file_abnormal_operate(pathto)
  8. --打开一个不存在的文件
  9. local file, err0 = io.open(pathto .. "tc201_file.txt", "r")
  10. assert(file == nil, "Should not open non-existent file" .. tostring(err0))
  11. -- 若打开失败,err信息指示不准确,先不判断err信息
  12. --assert(err ~= nil, "Error message should indicate file not found")
  13. log.info("fsstat: " .. pathto, fs.fsstat(pathto))
  14. -- 删除不存在的文件,期望是第一个返回参数为nil
  15. local ret, err = os.remove(pathto .. "tc12_file.txt")
  16. assert(success == nil, "Should not remove non-exist file: " .. tostring(err))
  17. -- 重命名不存在的文件,,期望是第一个返回参数为nil
  18. ret, err = os.rename(pathto .. "tc12_file.txt", pathto .. "tc17_file_new.txt")
  19. assert(success == nil, "Should not rename non-exist file: " .. tostring(err))
  20. end
  21. --[[
  22. 用例0202 异常目录打开删除
  23. 前置条件:
  24. 测试步骤与期望:
  25. ]]--
  26. function tc0202_directory_abnormal_operate(pathto)
  27. --先创建一个目录
  28. local dirname = "tc0202_dir"
  29. generate_directory(pathto, dirname)
  30. --重复创建同名目录失败
  31. local ret, err = io.mkdir(pathto .. dirname .. "/")
  32. assert(ret == false, "Should be not create directory: " .. tostring(err))
  33. --在前面目录里在建立一个文件并写入数据
  34. local filename = "tc202_file.txt"
  35. local rc = io.writeFile(pathto .. dirname .. "/" .. filename, "123456test")
  36. assert(rc, "Cannot Create file and write data")
  37. --删除非空目录返回失败
  38. ret, err = io.rmdir(pathto .. dirname .. "/")
  39. assert(ret == false, "Should be not remove directory: " .. tostring(err))
  40. --删除文件后在删除目录
  41. os.remove(pathto .. dirname .. "/" .. filename)
  42. ret, err = io.rmdir(pathto .. dirname .. "/")
  43. assert(ret == true, "Can not remove directory: " .. tostring(err))
  44. end
  45. --[[
  46. 用例0203 不同文件模式读写操作异常
  47. ]]--
  48. function tc0203_file_abnormal_readwrite(pathto)
  49. --若"/luadb/"只测试下面两点
  50. if pathto == "/luadb/" then
  51. -- 只读文件系统的文件不能以写模式打开
  52. local fd, err = io.open(pathto .. "test_fewline.txt", "w")
  53. log.info("fs", "write fd: ", fd)
  54. assert(fd == nil, "Read-Only file open in write mode " .. tostring(err))
  55. --if fd then
  56. --local ret, err1 =fd:write("12")
  57. --log.info("fs", "write ret:", ret)
  58. --fd:close()
  59. --end
  60. -- 打开一个不存在的文件
  61. fd, err = io.open(pathto .. "tc12_file.txt", "r")
  62. assert(fd == nil, "Should not open non-existent file" .. tostring(err))
  63. return
  64. end
  65. --在目录里建立一个文件并写入数据
  66. local filename = "tc0203_file.txt"
  67. local rc = io.writeFile(pathto .. filename, "123456test")
  68. assert(rc, "Cannot Create file and write data")
  69. --只读方式打开文件
  70. local fd = io.open(pathto .. filename, "r")
  71. assert(fd ~= nil, "Failed to open file for read")
  72. if fd then
  73. -- 只读方式打开文件不能写入数据
  74. local ret, err = fd:write("Hello")
  75. log.info("fs", "write ret:", ret)
  76. --assert(ret == nil, "Cannot Write data in ro mode" .. tostring(err))
  77. fd:seek("set", 0)
  78. local data = fd:read("*a")
  79. log.info("fs", "read all:", data)
  80. fd:close()
  81. assert(data == "123456test", "123456test change to " .. data)
  82. end
  83. -- 清理工作
  84. if pathto == "/ram/" then
  85. -- 对于/ram/ 测试完删除文件,因为ram文件系统最大允许文件数为8个超过就无法新建文件
  86. os.remove(pathto .. filename)
  87. end
  88. end
  89. --[[
  90. 用例0204 文件名最大长度
  91. ]]--
  92. function tc0204_filename_max(pathto)
  93. -- 构造一个长度为length的文件名
  94. local length = 64 -- lfs:"/" 文件名长度最大63字节
  95. if pathto == "/ram/" then
  96. length = 32 -- ramfs:"/ram/" 文件名长度最大31字节
  97. end
  98. local filename = string.rep("A", length)
  99. local fd = io.open(pathto .. filename, "w")
  100. -- 最大文件名不超过63,否则返回失败
  101. assert(fd == nil, "Can create file with the length of name:" .. tostring(length))
  102. -- 测试结束删除文件
  103. if fd then
  104. fd:close()
  105. os.remove(pathto .. filename)
  106. end
  107. end
  108. WRITE_SIZE = 2048
  109. LAST_BLKCOUNT = 4
  110. chunk_dat = string.rep("F", WRITE_SIZE)
  111. tc205_chunk_dat_last = string.rep("A", WRITE_SIZE-468)
  112. tc206_chunk_dat_last = string.rep("B", WRITE_SIZE-4)
  113. --[[
  114. 用例0205 根目录单文件最大写入和删除
  115. ]]--
  116. function tc0205_file_writemax(pathto)
  117. -- 仅支持lfs根目录测试
  118. if pathto ~= "/" then return end
  119. -- 先格式化文件系统
  120. local rc, err = io.mkfs(pathto)
  121. assert(rc, "FS format error" .. tostring(err))
  122. log.info("fsstat: " .. pathto, fs.fsstat(pathto))
  123. local succ, total, used, blksize = fs.fsstat(pathto)
  124. -- 获取剩余容量大小,可写入块
  125. local free = total - used
  126. -- 按block size定义数据块
  127. --local chunk_dat = string.rep("F", blksize)
  128. local filename = "tc205_file.txt"
  129. -- 最后一块大小
  130. --local chunk_dat_last = string.rep("A", 4096-468)
  131. -- 创建打开一个文件
  132. local fd = io.open(pathto .. filename, "w+")
  133. -- 连续写入多块
  134. local chunk_cnt = (4096/WRITE_SIZE)*(free-1) + (4096/WRITE_SIZE) - 1
  135. local ret = file_chunkdata_write(fd, chunk_dat, chunk_cnt)
  136. log.info("fs","1st ret: " .. tostring(ret))
  137. assert(ret ~= nil, "File write chunk data failed1")
  138. -- 最后一块
  139. ret = file_chunkdata_write(fd, tc205_chunk_dat_last, 1)
  140. log.info("fs", "2nd write ret: ", ret)
  141. assert(ret ~= nil, "File write chunk data failed2")
  142. -- 注意这里如果继续写一个字节且因已满写失败了前面数据在文件close后会丢失filesize=0
  143. -- 所以前面写满后先关闭保存
  144. --ret = fd:write("1")
  145. --log.info("fs", "3rd write ret: ", ret)
  146. --assert(ret == nil, "Continue to write data success")
  147. fd:close()
  148. local succss, data = io.lsdir(pathto, 10, 0)
  149. if succss then
  150. log.info("fs", "ls " .. pathto, json.encode(data))
  151. end
  152. log.info("fs", "max file size: ", fs.fsize(pathto .. filename))
  153. -- 读取最后一包检查
  154. local readdata = io.readFile(pathto .. filename, "r", WRITE_SIZE *(chunk_cnt), WRITE_SIZE)
  155. --log.info("fs", readdata)
  156. assert(readdata == tc205_chunk_dat_last, "Data not same")
  157. -- 删除文件
  158. os.remove(pathto .. filename)
  159. assert(io.exists(pathto .. filename) == false, "Should not exit file")
  160. end
  161. --[[
  162. 用例0206 文件系统满容量写入读取测试
  163. ]]
  164. function tc0206_filesystem_full_readwrite(pathto)
  165. -- 仅支持lfs根目录测试
  166. if pathto ~= "/" then return end
  167. -- 先格式化文件系统
  168. local rc, err = io.mkfs(pathto)
  169. assert(rc, "FS format error")
  170. local succ, total, used, blksize = fs.fsstat(pathto)
  171. local free = total - used
  172. local dirname = "tc206_dir"
  173. local filename1 = "tc206_file01.txt"
  174. local filename2 = "tc206_file02.txt"
  175. -- 创建一个目录
  176. generate_directory(pathto, dirname);
  177. -- 重新获取剩余容量大小
  178. succ, total, used, blksize = fs.fsstat(pathto)
  179. free = total - used
  180. log.info("fsstat: " .. pathto, fs.fsstat(pathto))
  181. --local chunk_dat = string.rep("5", blksize)
  182. --local chunk_dat_last = string.rep("A", blksize - 4)
  183. -- 创建第一个文件
  184. local fd1 = io.open(pathto .. filename1, "w+")
  185. -- 连续写入多块
  186. local ret = file_chunkdata_write(fd1, chunk_dat, 2*(free-(LAST_BLKCOUNT-1)))
  187. --log.info("fs", "ret: " .. tostring(ret))
  188. fd1:close()
  189. assert(ret ~= nil, "Write chunk data failed1")
  190. -- 创建第二个文件
  191. local fd2 = io.open(pathto .. filename2, "w+")
  192. -- 连续写入多块
  193. local ret = file_chunkdata_write(fd2, chunk_dat, LAST_BLKCOUNT - 1)
  194. --log.info("fs", "ret: " .. tostring(ret))
  195. assert(ret ~= nil, "Write chunk data failed2")
  196. -- 最后一块
  197. ret = file_chunkdata_write(fd2, tc206_chunk_dat_last, 1)
  198. assert(ret ~= nil, "Write chunk data failed3")
  199. fd2:close()
  200. local succss, data = io.lsdir(pathto, 10, 0)
  201. if succss then
  202. log.info("fs", "ls " .. pathto, json.encode(data))
  203. end
  204. -- 通过luatos扩展io库接口io.readFile读取数据, 根据offset分块读取数据验证写正确
  205. for i=1, (LAST_BLKCOUNT-1) do
  206. local readdata = io.readFile(pathto .. filename2, "r", (i-1)*WRITE_SIZE, WRITE_SIZE)
  207. assert(readdata == chunk_dat, "Read data != Test data")
  208. end
  209. -- 最后一包
  210. local last_readdata = io.readFile(pathto .. filename2, "r", (LAST_BLKCOUNT-1)*WRITE_SIZE, WRITE_SIZE - 4)
  211. assert(last_readdata == tc206_chunk_dat_last, "Read data != Test data")
  212. -- 清理工作,删除文件
  213. os.remove(pathto .. filename1)
  214. --assert(io.exists(pathto .. filename1) == false, "Should not exit file")
  215. os.remove(pathto .. filename2)
  216. --assert(io.exists(pathto .. filename1) == false, "Should not exit file")
  217. end
  218. --[[
  219. 用例0207 不同文件同时写入读取数据
  220. ]]
  221. function tc0207_files_multiopen_readwrite(pathto)
  222. local data1 = string.rep("B", 128)
  223. local data2 = string.rep("C", 128)
  224. local binaryData = "\x12\x34\x56\78\90\xAB\xCD\xEF"
  225. local count = 0
  226. local filename0 = "tc208_data0.txt"
  227. local filename1 = "tc208_data1.txt"
  228. local filename2 = "tc208_data2.txt"
  229. -- 扩展接口写入二进制数据
  230. local rc = io.writeFile(pathto .. filename0, binaryData)
  231. assert(rc, "Write Bin File Error")
  232. -- 同时打开不同文件,两个以写模式打开,一个以只读模式打开
  233. local fd0 = io.open(pathto .. filename0, "rb")
  234. log.info("[R]", "open file:" .. pathto .. filename0)
  235. local fd1 = io.open(pathto .. filename1, "w+")
  236. log.info("[W1]", "open file:" .. pathto .. filename1)
  237. local fd2 = io.open(pathto .. filename2, "w+")
  238. log.info("[W2]", "open file:" .. pathto .. filename2)
  239. if fd0 and fd1 and fd2 then
  240. for i=1, 10 do
  241. -- 打开的文件,分别测试写入和读取数据
  242. fd1:write("[W1]:" .. data1 .. "\n")
  243. --log.info("fs", "[W1]", "write ")
  244. fd2:write("[W2]:" .. data2 .. "\n")
  245. --log.info("fs", "[W2]", "write ")
  246. fd0:seek("set", 0)
  247. local data = fd0:read("*a")
  248. if data == binaryData then count = count + 1 end
  249. end
  250. fd0:close()
  251. fd1:close()
  252. fd2:close()
  253. end
  254. local fsize1 = fs.fsize(pathto .. filename1)
  255. local fsize2 = fs.fsize(pathto .. filename2)
  256. assert(fsize1 == 1340, pathto .. filename1 .. " fsize != 1340")
  257. assert(fsize2 == 1340, pathto .. filename2 .. " fsize != 1340")
  258. assert(count == 10, pathto .. filename0 .. " read data fail")
  259. -- 清理工作
  260. if pathto == "/ram/" then
  261. -- 对于/ram/ 测试完删除文件,因为ram文件系统最大允许文件数为8个超过就无法新建文件
  262. os.remove(pathto .. filename0)
  263. os.remove(pathto .. filename1)
  264. os.remove(pathto .. filename2)
  265. end
  266. end