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):
#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++) {
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");
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);