diff mbox series

[Semihosting,Tests,v2,3/3] add SYS_GET_CMDLINE test

Message ID 20240530112332.1439238-4-alex.bennee@linaro.org (mailing list archive)
State New, archived
Headers show
Series add SYS_GET_CMDLINE test | expand

Commit Message

Alex Bennée May 30, 2024, 11:23 a.m. UTC
Adds a new test case for the semihosting SYS_GET_CMDLINE call. We can
use the existing previously unused semi_get_cmdline helper and we then
verify:

 - the call succeeds
 - the call returns the expected length string
 - the call returns the expected binary name as arg0

The most fiddly bit was exposing the binary name into the program so
we can validate the result.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

---
v2
  - add return leg on failing test
  - check returned length is sane
  - s/could/couldn't/
  - just let array reference decay to the pointer
  - rewrite the commit message to be less terse
---
 Makefile   | 22 +++++++++++-----------
 usertest.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 59fd831..f77665f 100644
--- a/Makefile
+++ b/Makefile
@@ -85,37 +85,37 @@  systest-srcs = start.S string.c $(usertest-srcs)
 microbit-systest-srcs = start-microbit.S string.c $(usertest-srcs)
 
 usertest-a32: $(usertest-srcs)
-	$(A32GCC) --static -o $@ $^
+	$(A32GCC) -DBINARY_NAME="\"$@\"" --static -o $@ $^
 
 usertest-t32: $(usertest-srcs)
-	$(T32GCC) --static -o $@ $^
+	$(T32GCC) -DBINARY_NAME="\"$@\"" --static -o $@ $^
 
 usertest-a32-hlt: $(usertest-srcs)
-	$(A32GCC) -DUSE_HLT --static -o $@ $^
+	$(A32GCC) -DBINARY_NAME="\"$@\"" -DUSE_HLT --static -o $@ $^
 
 usertest-t32-hlt: $(usertest-srcs)
-	$(T32GCC) -DUSE_HLT --static -o $@ $^
+	$(T32GCC) -DBINARY_NAME="\"$@\"" -DUSE_HLT --static -o $@ $^
 
 usertest-a64: $(usertest-srcs)
-	$(A64GCC) --static -o $@ $^
+	$(A64GCC) -DBINARY_NAME="\"$@\"" --static -o $@ $^
 
 systest-a32.axf: $(systest-srcs)
-	$(A32GCC) -o $@ $^ $(A32LINKOPTS)
+	$(A32GCC) -DBINARY_NAME="\"$@\"" -o $@ $^ $(A32LINKOPTS)
 
 systest-t32.axf: $(systest-srcs)
-	$(T32GCC) -o $@ $^ $(A32LINKOPTS)
+	$(T32GCC) -DBINARY_NAME="\"$@\"" -o $@ $^ $(A32LINKOPTS)
 
 systest-a32-hlt.axf: $(systest-srcs)
-	$(A32GCC) -DUSE_HLT -o $@ $^ $(A32LINKOPTS)
+	$(A32GCC) -DBINARY_NAME="\"$@\"" -DUSE_HLT -o $@ $^ $(A32LINKOPTS)
 
 systest-t32-hlt.axf: $(systest-srcs)
-	$(T32GCC) -DUSE_HLT -o $@ $^ $(A32LINKOPTS)
+	$(T32GCC) -DBINARY_NAME="\"$@\"" -DUSE_HLT -o $@ $^ $(A32LINKOPTS)
 
 systest-t32-bkpt.axf: $(microbit-systest-srcs)
-	$(V7MGCC) -DUSE_BKPT -o $@ $^ $(AV7MLINKOPTS)
+	$(V7MGCC) -DBINARY_NAME="\"$@\"" -DUSE_BKPT -o $@ $^ $(AV7MLINKOPTS)
 
 systest-a64.axf: $(systest-srcs)
-	$(A64GCC) -nostdlib -o $@ $^ $(A64LINKOPTS)
+	$(A64GCC) -DBINARY_NAME="\"$@\"" -nostdlib -o $@ $^ $(A64LINKOPTS)
 
 run-usertest-a32: usertest-a32
 	$(QEMU_ARM) usertest-a32
diff --git a/usertest.c b/usertest.c
index 5df95f3..7a12896 100644
--- a/usertest.c
+++ b/usertest.c
@@ -315,6 +315,33 @@  static int test_feature_detect(void)
     return 0;
 }
 
+static int test_cmdline(void)
+{
+    char cmdline[256];
+    int actual;
+    const char *s, *c;
+
+    if (semi_get_cmdline(cmdline, sizeof(cmdline), &actual)) {
+        semi_write0("FAIL couldn't recover command line\n");
+        return 1;
+    }
+
+    if (strlen(BINARY_NAME) != actual) {
+        semi_write0("FAIL cmdline length not what expected: ");
+        semi_write0(cmdline);
+        return 1;
+    }
+
+    if (strcmp(cmdline, BINARY_NAME) != 0) {
+        semi_write0("FAIL unexpected command line: ");
+        semi_write0(cmdline);
+        return 1;
+    }
+
+    semi_write0("PASS command line test\n");
+    return 0;
+}
+
 int main(void)
 {
     void *bufp;
@@ -366,6 +393,10 @@  int main(void)
         return 1;
     }
 
+    if (test_cmdline()) {
+        return 1;
+    }
+
     semi_write0("ALL TESTS PASSED\n");
 
     /* If we have EXIT_EXTENDED then use it */