]> git.ipfire.org Git - thirdparty/suricata-verify.git/commitdiff
test: add lua test for suricata.file lib 2505/head
authorJason Ish <jason.ish@oisf.net>
Thu, 8 May 2025 21:51:24 +0000 (15:51 -0600)
committerVictor Julien <victor@inliniac.net>
Sat, 10 May 2025 23:09:40 +0000 (01:09 +0200)
Ticket: #7491

tests/lua-scfileinfo-pre8/expected/scfileinfo.log [new file with mode: 0644]
tests/lua-scfileinfo-pre8/scfileinfo.lua [new file with mode: 0644]
tests/lua-scfileinfo-pre8/suricata.yaml [new file with mode: 0644]
tests/lua-scfileinfo-pre8/test.yaml [new file with mode: 0644]
tests/lua-scfileinfo/expected/scfileinfo.log
tests/lua-scfileinfo/scfileinfo.lua
tests/lua-scfileinfo/suricata.yaml
tests/lua-scfileinfo/test.yaml

diff --git a/tests/lua-scfileinfo-pre8/expected/scfileinfo.log b/tests/lua-scfileinfo-pre8/expected/scfileinfo.log
new file mode 100644 (file)
index 0000000..637546b
--- /dev/null
@@ -0,0 +1,12 @@
+** SCFileInfo is: [**] fileid: 1 [**] txid: 0
+name: /en-US/static/@95063/css/print.css
+size: 1054 [**] magic: nomagic
+md5: c0b818de79d068db0a989ed35044cd62
+sha1: afb4fb1ad18223825cfb7ce5bf506e0f9dc47d5c
+sha256: 57b43ee07432cf8a8b8a17d9d712138194e4564e4b36963a34c495b576b404fe
+** SCFileInfo is: [**] fileid: 2 [**] txid: 1
+name: /en-US/static/@95063/js/contrib/lowpro_for_jquery.js
+size: 4096 [**] magic: nomagic
+md5: 545507473518ecdb45600cd50252a3ae
+sha1: 8d4c76eb9ebab67a3c5a8234f02bcece0a021019
+sha256: 20af0596a3ca657849ae0be5136fae338e5d0df57630890509a8d9ecccd855ac
diff --git a/tests/lua-scfileinfo-pre8/scfileinfo.lua b/tests/lua-scfileinfo-pre8/scfileinfo.lua
new file mode 100644 (file)
index 0000000..a8d84ca
--- /dev/null
@@ -0,0 +1,29 @@
+-- Output test for SCFileInfo
+file_name = "scfileinfo.log"
+
+function init (args)
+    local needs = {}
+    needs['type'] = 'file'
+    return needs
+end
+
+function setup(args)
+    filename = SCLogPath() .. "/" .. file_name
+    file = assert(io.open(filename, "w"))
+    SCLogInfo("lua SCFileInfo Log Filename " .. filename)
+end
+
+function log(args)
+    fileid, txid, name, size, magic, md5, sha1, sha256 = SCFileInfo()
+    if magic == nil then
+        magic = "nomagic"
+    end
+
+    file:write ("** SCFileInfo is: [**] fileid: " .. fileid .. " [**] txid: " .. txid .. "\nname: " .. name .. "\nsize: " .. size .. " [**] magic: " .. magic .. "\nmd5: " .. md5 .. "\nsha1: " .. sha1 .. "\nsha256: " .. sha256 .. "\n")
+    file:flush()
+end
+
+function deinit(args)
+    SCLogInfo ("SCFileInfo logging finished");
+    file:close(file)
+end
diff --git a/tests/lua-scfileinfo-pre8/suricata.yaml b/tests/lua-scfileinfo-pre8/suricata.yaml
new file mode 100644 (file)
index 0000000..c66334a
--- /dev/null
@@ -0,0 +1,15 @@
+%YAML 1.1
+---
+
+outputs:
+  - lua:
+      enabled: yes
+      scripts-dir: .
+      scripts:
+          - scfileinfo.lua
+
+  - file-store:
+      version: 2
+      enabled: yes
+      force-filestore: yes
+      force-hash: [sha256, sha1, md5]
\ No newline at end of file
diff --git a/tests/lua-scfileinfo-pre8/test.yaml b/tests/lua-scfileinfo-pre8/test.yaml
new file mode 100644 (file)
index 0000000..3178215
--- /dev/null
@@ -0,0 +1,12 @@
+requires:
+  features:
+    - HAVE_LUA
+  lt-version: 8
+
+pcap: ../lua-scfileinfo/filecontainer-http-slice.pcap
+
+checks:
+  - file-compare:
+      # Two files should be identical
+      filename: scfileinfo.log
+      expected: expected/scfileinfo.log
index 637546b403e6ba58f260936f336d4f9dc54ebd71..3b1389e8bfafb7e1a77b66274ddcc16dd3e4ecd1 100644 (file)
@@ -4,9 +4,13 @@ size: 1054 [**] magic: nomagic
 md5: c0b818de79d068db0a989ed35044cd62
 sha1: afb4fb1ad18223825cfb7ce5bf506e0f9dc47d5c
 sha256: 57b43ee07432cf8a8b8a17d9d712138194e4564e4b36963a34c495b576b404fe
+state: CLOSED
+is_stored: true
 ** SCFileInfo is: [**] fileid: 2 [**] txid: 1
 name: /en-US/static/@95063/js/contrib/lowpro_for_jquery.js
 size: 4096 [**] magic: nomagic
 md5: 545507473518ecdb45600cd50252a3ae
 sha1: 8d4c76eb9ebab67a3c5a8234f02bcece0a021019
 sha256: 20af0596a3ca657849ae0be5136fae338e5d0df57630890509a8d9ecccd855ac
+state: CLOSED
+is_stored: true
index a8d84ca4843fca1dd59a2dc78d67f1bb20578f71..f143e5a91d8f419cb184e6236d0f566e890891e4 100644 (file)
@@ -1,29 +1,40 @@
+local filelib = require("suricata.file")
+
 -- Output test for SCFileInfo
 file_name = "scfileinfo.log"
 
 function init (args)
-    local needs = {}
-    needs['type'] = 'file'
-    return needs
+    return {type = "file"}
 end
 
 function setup(args)
     filename = SCLogPath() .. "/" .. file_name
-    file = assert(io.open(filename, "w"))
+    output = assert(io.open(filename, "w"))
     SCLogInfo("lua SCFileInfo Log Filename " .. filename)
 end
 
 function log(args)
-    fileid, txid, name, size, magic, md5, sha1, sha256 = SCFileInfo()
+    local file = filelib.get_file()
+
+    local fileid = file:file_id()
+    local txid = file:tx_id()
+    local name = file:name()
+    local size = file:size()
+    local magic = file:magic()
     if magic == nil then
         magic = "nomagic"
     end
+    local md5 = file:md5()
+    local sha1 = file:sha1()
+    local sha256 = file:sha256()
 
-    file:write ("** SCFileInfo is: [**] fileid: " .. fileid .. " [**] txid: " .. txid .. "\nname: " .. name .. "\nsize: " .. size .. " [**] magic: " .. magic .. "\nmd5: " .. md5 .. "\nsha1: " .. sha1 .. "\nsha256: " .. sha256 .. "\n")
-    file:flush()
+    output:write("** SCFileInfo is: [**] fileid: " .. fileid .. " [**] txid: " .. txid .. "\nname: " .. name .. "\nsize: " .. size .. " [**] magic: " .. magic .. "\nmd5: " .. md5 .. "\nsha1: " .. sha1 .. "\nsha256: " .. sha256 .. "\n")
+    output:write("state: " .. file:get_state() .. "\n")
+    output:write("is_stored: " .. tostring(file:is_stored()) .. "\n")
+    output:flush()
 end
 
 function deinit(args)
     SCLogInfo ("SCFileInfo logging finished");
-    file:close(file)
+    output:close()
 end
index 042fb068b40b50986a88cd07515007f3de5d37e6..6b60b3021490072a59ad8228697b67c311d9add6 100644 (file)
@@ -2,6 +2,12 @@
 ---
 
 outputs:
+  - eve-log:
+      enabled: yes
+      types:
+        - alert:
+        - files:
+
   - lua:
       enabled: yes
       scripts-dir: .
index 874017ea975e7414cf8fd4186ac7a4e98613001c..8796abeedc6a1ac25286214508ccab1365302318 100644 (file)
@@ -1,6 +1,8 @@
 requires:
-  features:
-      - HAVE_LUA
+  min-version: 8.0.0
+
+args:
+ - -k none
 
 checks:
   - file-compare: