luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

test_ramdisk2.lua (3499B)


      1 local ramdisk = require("ramdisk2")
      2 
      3 -- Create a new root filesystem
      4 local root = ramdisk.createRoot()
      5 
      6 -- Create directories
      7 local osDir = root:newDir("os")
      8 local appsDir = root:newDir("apps")
      9 local binDir = root:newDir("bin")
     10 
     11 -- Add text files to /os/ (with special characters to test escaping)
     12 osDir:newFile("init.lua", "-- OS Initialization\nprint('LuajitOS Starting...')\nreturn true")
     13 osDir:newFile("config.txt", "version=1.0\nauthor=LuajitOS Team\ndebug=true\npath=C:\\Windows\\System32")
     14 osDir:newFile("readme.md", "# LuajitOS\n\nA lightweight operating system written in Lua.\n\tIndented line\r\nWindows newline")
     15 
     16 -- Add a binary file to /os/ (simulated binary data with non-printable characters)
     17 local binaryData = string.char(0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A) .. "PNG_HEADER_DATA"
     18 osDir:newFile("logo.png", binaryData)
     19 
     20 -- Add a file to /bin/
     21 binDir:newFile("shell.lua", "-- Shell binary\nfunction run()\n  print('Shell running')\nend")
     22 
     23 -- Add a file to /apps/
     24 appsDir:newFile("calculator.lua", "-- Calculator app\nlocal calc = {}\nreturn calc")
     25 
     26 -- Create a subdirectory in /os/
     27 local libDir = osDir:newDir("lib")
     28 libDir:newFile("utils.lua", "local utils = {}\nfunction utils.log(msg)\n  print('[LOG] ' .. msg)\nend\nreturn utils")
     29 
     30 -- Another binary file in lib
     31 local elfData = string.char(0x7F, 0x45, 0x4C, 0x46, 0x02, 0x01, 0x01, 0x00) .. "ELF_BINARY"
     32 libDir:newFile("native.so", elfData)
     33 
     34 -- Save and print the filesystem
     35 print("=== Filesystem Structure ===")
     36 print("\nDirectories:")
     37 print("  /")
     38 print("  /os/")
     39 print("  /apps/")
     40 print("  /bin/")
     41 print("  /os/lib/")
     42 
     43 print("\nFiles:")
     44 print("  /os/init.lua (text)")
     45 print("  /os/config.txt (text)")
     46 print("  /os/readme.md (text)")
     47 print("  /os/logo.png (binary)")
     48 print("  /os/lib/utils.lua (text)")
     49 print("  /os/lib/native.so (binary)")
     50 print("  /bin/shell.lua (text)")
     51 print("  /apps/calculator.lua (text)")
     52 
     53 print("\n=== Serialized Filesystem ===")
     54 local serialized = ramdisk.saveFS(root)
     55 print(serialized)
     56 
     57 print("\n=== Serialized Length ===")
     58 print("Total bytes: " .. #serialized)
     59 
     60 print("\n=== Testing Load/Save Round Trip ===")
     61 -- Load the serialized filesystem
     62 local loadedRoot = ramdisk.loadFS(serialized)
     63 
     64 -- Read a few files to verify
     65 local loadedConfigFile = loadedRoot:traverse("os/config.txt")
     66 local loadedLibUtilsFile = loadedRoot:traverse("os/lib/utils.lua")
     67 local loadedBinaryFile = loadedRoot:traverse("os/logo.png")
     68 
     69 if not loadedConfigFile then
     70     print("ERROR: Could not load config.txt")
     71     os.exit(1)
     72 end
     73 
     74 print("\n--- Loaded config.txt content: ---")
     75 print(loadedConfigFile:read())
     76 
     77 print("\n--- Loaded os/lib/utils.lua content: ---")
     78 print(loadedLibUtilsFile:read())
     79 
     80 print("\n--- Binary file verification: ---")
     81 local originalBinary = binaryData
     82 local loadedBinary = loadedBinaryFile:read()
     83 print("Original binary length: " .. #originalBinary)
     84 print("Loaded binary length: " .. #loadedBinary)
     85 print("Binary match: " .. tostring(originalBinary == loadedBinary))
     86 
     87 print("\n--- Verifying special characters: ---")
     88 local loadedReadme = loadedRoot:traverse("os/readme.md")
     89 local readmeContent = loadedReadme:read()
     90 print("Contains newline (\\n): " .. tostring(readmeContent:find("\n") ~= nil))
     91 print("Contains tab (\\t): " .. tostring(readmeContent:find("\t") ~= nil))
     92 print("Contains carriage return (\\r): " .. tostring(readmeContent:find("\r") ~= nil))
     93 print("Contains backslash (\\\\): " .. tostring(loadedConfigFile:read():find("\\") ~= nil))
     94 
     95 print("\n=== Round Trip Successful! ===")