From: Michael Tremer Date: Thu, 21 Jul 2022 09:57:42 +0000 (+0000) Subject: tests: execute: Generate some random output for tests X-Git-Tag: 0.9.28~658 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4d5b4f42adbf54f97bb2dd3abc3a86ef820a9c4;p=pakfire.git tests: execute: Generate some random output for tests Signed-off-by: Michael Tremer --- diff --git a/tests/python/execute.py b/tests/python/execute.py index 22dfe898a..900d87aae 100755 --- a/tests/python/execute.py +++ b/tests/python/execute.py @@ -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): diff --git a/tests/stub/command.c b/tests/stub/command.c index 9b4990467..74b138464 100644 --- a/tests/stub/command.c +++ b/tests/stub/command.c @@ -21,6 +21,27 @@ #include #include #include +#include + +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);