Debian Codegraph @

test/lua/unit/archive.lua

raw
1 -- archive module unit tests
2
3 context("Lua archive bindings", function()
4 local archive = require "archive"
5 local rspamd_text = require "rspamd_text"
6
7 test("pack/unpack zip roundtrip (text payload)", function()
8 local files = {
9 { name = "a.txt", content = "Hello" },
10 { name = "b/readme.md", content = "# Readme" },
11 }
12 local blob = archive.pack("zip", files)
13 assert_equal(type(blob), "userdata")
14 local out = archive.unpack(blob)
15 assert_equal(#out, 2)
16 local names = {}
17 for _, f in ipairs(out) do names[f.name] = f.content end
18 assert_rspamd_eq({ actual = names["a.txt"], expect = rspamd_text.fromstring("Hello") })
19 assert_equal(type(names["b/readme.md"]), "userdata")
20 end)
21
22 test("zip/unzip helpers roundtrip (binary payload)", function()
23 local rnd = rspamd_text.randombytes(1024)
24 local files = {
25 { name = "bin.dat", content = rnd },
26 }
27 local blob = archive.zip(files)
28 assert_equal(type(blob), "userdata")
29 local out = archive.unzip(blob)
30 assert_equal(#out, 1)
31 assert_equal(out[1].name, "bin.dat")
32 assert_rspamd_eq({ actual = out[1].content, expect = rnd })
33 end)
34
35 test("zip_encrypt without password == plain zip", function()
36 local files = {
37 { name = "a.txt", content = "Hello" },
38 }
39 local blob = archive.zip_encrypt(files) -- no password
40 assert_equal(type(blob), "userdata")
41 local out = archive.unzip(blob)
42 assert_equal(#out, 1)
43 assert_equal(out[1].name, "a.txt")
44 assert_rspamd_eq({ actual = out[1].content, expect = rspamd_text.fromstring("Hello") })
45 end)
46
47 test("zip_encrypt with password (ZipCrypto) roundtrip via libarchive", function()
48 local files = {
49 { name = "dir/x.txt", content = "secret" },
50 { name = "y.bin", content = rspamd_text.fromstring("\001\002\003") },
51 }
52 local pwd = "testpass123"
53 local blob = archive.zip_encrypt(files, pwd)
54 assert_equal(type(blob), "userdata")
55 -- libarchive can read ZipCrypto, so unpack should succeed and yield the same files
56 local out = archive.unpack(blob, "zip", pwd)
57 assert_equal(#out, 2)
58 local names = {}
59 for _, f in ipairs(out) do names[f.name] = f.content end
60 assert_rspamd_eq({ actual = names["dir/x.txt"], expect = rspamd_text.fromstring("secret") })
61 assert_rspamd_eq({ actual = names["y.bin"], expect = rspamd_text.fromstring("\001\002\003") })
62 end)
63
64 test("zip_encrypt with wrong password fails to unpack", function()
65 local files = {
66 { name = "secret.txt", content = "topsecret" },
67 }
68 local pwd = "goodpass"
69 local blob = archive.zip_encrypt(files, pwd)
70 assert_equal(type(blob), "userdata")
71 local ok, err = pcall(function()
72 archive.unpack(blob, "zip", "badpass")
73 end)
74 assert_equal(ok, false)
75 end)
76
77 test("pack zip with AES-128 via libarchive roundtrip", function()
78 local files = {
79 { name = "dir/x.txt", content = "secret" },
80 { name = "y.bin", content = rspamd_text.fromstring("\001\002\003") },
81 }
82 local opts = { password = "testpass123", format_options = { encryption = "aes128" } }
83 local ok_pack, blob_or_err = pcall(function()
84 return archive.pack("zip", files, opts)
85 end)
86 -- If libarchive lacks AES write support, skip quietly
87 if not ok_pack then return end
88 local blob = blob_or_err
89 assert_equal(type(blob), "userdata")
90 local out = archive.unpack(blob, "zip", opts.password)
91 assert_equal(#out, 2)
92 local names = {}
93 for _, f in ipairs(out) do names[f.name] = f.content end
94 assert_rspamd_eq({ actual = names["dir/x.txt"], expect = rspamd_text.fromstring("secret") })
95 assert_rspamd_eq({ actual = names["y.bin"], expect = rspamd_text.fromstring("\001\002\003") })
96 end)
97
98 test("pack zip with AES-256 via libarchive wrong password fails", function()
99 local files = {
100 { name = "a.txt", content = "Hello" },
101 }
102 local opts = { password = "goodpass", zip = { encryption = "aes256" } }
103 local ok_pack, blob_or_err = pcall(function()
104 return archive.pack("zip", files, opts)
105 end)
106 if not ok_pack then return end
107 local blob = blob_or_err
108 assert_equal(type(blob), "userdata")
109 local ok, err = pcall(function()
110 archive.unpack(blob, "zip", "badpass")
111 end)
112 assert_equal(ok, false)
113 end)
114
115 test("tar/untar helpers roundtrip (no compression)", function()
116 local files = {
117 { name = "x.txt", content = "X" },
118 { name = "y.txt", content = "YY" },
119 }
120 local blob = archive.tar(files)
121 assert_equal(type(blob), "userdata")
122 local out = archive.untar(blob)
123 assert_equal(#out, 2)
124 local names = {}
125 for _, f in ipairs(out) do names[f.name] = f.content end
126 assert_rspamd_eq({ actual = names["x.txt"], expect = rspamd_text.fromstring("X") })
127 assert_rspamd_eq({ actual = names["y.txt"], expect = rspamd_text.fromstring("YY") })
128 end)
129
130 test("tar/untar helpers roundtrip (gzip)", function()
131 local files = {
132 { name = "z.txt", content = "Z" },
133 }
134 local blob = archive.tar(files, "gz")
135 assert_equal(type(blob), "userdata")
136 local out = archive.untar(blob)
137 assert_equal(#out, 1)
138 assert_equal(out[1].name, "z.txt")
139 assert_rspamd_eq({ actual = out[1].content, expect = rspamd_text.fromstring("Z") })
140 end)
141
142 test("supported_formats contains some read/write entries", function()
143 local caps = archive.supported_formats()
144 assert_equal(type(caps), "table")
145 assert_equal(type(caps.formats), "table")
146 assert_equal(type(caps.filters), "table")
147 -- We don't hard-require specific formats, but lists should exist (possibly empty)
148 assert_equal(type(caps.formats.read), "table")
149 assert_equal(type(caps.formats.write), "table")
150 assert_equal(type(caps.filters.read), "table")
151 assert_equal(type(caps.filters.write), "table")
152 end)
153 end)