]> git.ipfire.org Git - pakfire.git/commitdiff
tests: execute: Generate some random output for tests
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jul 2022 09:57:42 +0000 (09:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jul 2022 09:57:42 +0000 (09:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/python/execute.py
tests/stub/command.c

index 22dfe898aed09dcb665029fdbeedbe405ea33224..900d87aae6c87294d78120052446e588cfed2661 100755 (executable)
@@ -66,20 +66,20 @@ class Test(unittest.TestCase):
        def test_execute_output(self):
                self.pakfire.execute(["/command", "echo", "123"])
 
+               # Multiple newlines in one read
+               self.pakfire.execute(["/command", "echo", "1\n2\n3"])
+
                # Run a command with a lot of output which exceeds the buffer size
-               self.pakfire.execute(["/usr/bin/openssl", "rand", "-hex", "65536"])
+               self.pakfire.execute(["/command", "lines", "1", "65536"])
 
                # Run a command that generates lots of lines
-               self.pakfire.execute(["/usr/bin/openssl", "rand", "-base64", "4096"])
-
-               # Multiple newlines in one read
-               self.pakfire.execute(["/command", "echo", "1\n2\n3"])
+               self.pakfire.execute(["/command", "lines", "100", "40"])
 
        def test_execute_logger(self):
                def log(priority, message):
                        print("LOG (%s): %s" % (priority, message.rstrip()))
 
-               self.pakfire.execute(["/bin/bash", "--help"], logging_callback=log)
+               self.pakfire.execute(["/command", "lines", "10"], logging_callback=log)
 
        # This is an interactive test which cannot be performed automatically
        #def test_shell(self):
index 9b4990467de37b3b6bc0ae7ca5abd715cc082453..74b1384642301f5e37f3f9b9d2c0800172a15f02 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
+
+static const char* characters =
+       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+       "abcdefghijklmnopqrstuvwxyz"
+       "0123456789";
+
+static int random_string(char* s, size_t length) {
+       // Fill the buffer with random characters
+       for (unsigned int i = 0; i < length - 1; i++) {
+               // Select a random character
+               int c = rand() % (strlen(characters) - 1);
+
+               s[i] = characters[c];
+       }
+
+       // Terminate the buffer
+       s[length - 1] = '\0';
+
+       return 0;
+}
 
 static int echo(int argc, char* argv[]) {
        for (int i = 0; i < argc; i++) {
@@ -54,6 +75,43 @@ static int exit_with_code(int argc, char* argv[]) {
        return strtoul(argv[0], NULL, 10);
 }
 
+static int lines(int argc, char* argv[]) {
+       size_t length = 0;
+
+       if (argc < 1) {
+               fprintf(stderr, "lines requires a number of lines to print\n");
+               return EXIT_FAILURE;
+       }
+
+       // How many lines to print?
+       const unsigned int lines = strtoul(argv[0], NULL, 10);
+
+       // Optionally take the length of the line
+       if (argc > 1)
+               length = strtoul(argv[1], NULL, 10);
+
+       // Set a default length if none set
+       if (!length)
+               length = 40;
+
+       // Add space for NULL
+       length++;
+
+       char* buffer = alloca(length);
+
+       for (unsigned int line = 0; line < lines; line++) {
+               // Generate a random line
+               int r = random_string(buffer, length);
+               if (r)
+                       return r;
+
+               // Print the line
+               printf("%s\n", buffer);
+       }
+
+       return 0;
+}
+
 int main(int argc, char* argv[]) {
        if (argc < 2) {
                fprintf(stderr, "No command given\n");
@@ -75,6 +133,10 @@ int main(int argc, char* argv[]) {
        else if (strcmp(command, "exit-with-code") == 0)
                callback = exit_with_code;
 
+       // Print random lines
+       else if (strcmp(command, "lines") == 0)
+               callback = lines;
+
        // Exit if no callback has been set
        if (!callback) {
                fprintf(stderr, "Unknown command: %s\n", command);