Skip to content

Commit

Permalink
Add script tests and minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fhunleth committed May 9, 2023
1 parent 74b2810 commit 4229474
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Supported boards/methods:
atecc508a Read an ATECC508A (I2C device '-f', I2C address '-a')
nerves_key Read a NervesKey (I2C device '-f', I2C address '-a')
dmi Read the system ID out of the SMBIOS/DMI
script Run a script to get the ID (Specify script with '-f')
force Force the ID (Specify ID with '-f')
Without the `-b` option, `boardid` will try a few methods of determining an ID.
Expand Down
3 changes: 2 additions & 1 deletion src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool script_id(const struct boardid_options *options, char *buffer)

if (pid == 0) {
// child
int devnull = open("/dev/null", O_RDWR);
int devnull = open("/dev/null", O_RDONLY);
if (devnull < 0)
exit(EXIT_FAILURE);

Expand All @@ -40,6 +40,7 @@ bool script_id(const struct boardid_options *options, char *buffer)
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);
close(devnull);
close(pipefd[1]);

char* argv[] = {
(char*)options->filename,
Expand Down
22 changes: 22 additions & 0 deletions tests/047_script_all
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

# SPDX-FileCopyrightText: 2023 Nerves Project Developers
#
# SPDX-License-Identifier: Apache-2.0

#
# Script
#

cat >$WORK/etc/my_serial <<EOF
#!/bin/sh
printf "12345678"
EOF
chmod +x $WORK/etc/my_serial

CMDLINE="-b script -f $WORK/etc/my_serial"

cat >$EXPECTED <<EOF
12345678
EOF
29 changes: 29 additions & 0 deletions tests/048_script_parts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

# SPDX-FileCopyrightText: 2023 Nerves Project Developers
#
# SPDX-License-Identifier: Apache-2.0

#
# Script that outputs the serial number in pieces
#

cat >$WORK/etc/my_serial <<EOF
#!/bin/sh
printf "1"
printf "2"
printf "3"
printf "4"
printf "5"
printf "6"
printf "7"
printf "8"
EOF
chmod +x $WORK/etc/my_serial

CMDLINE="-b script -f $WORK/etc/my_serial"

cat >$EXPECTED <<EOF
12345678
EOF
23 changes: 23 additions & 0 deletions tests/049_script_fail
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

# SPDX-FileCopyrightText: 2023 Nerves Project Developers
#
# SPDX-License-Identifier: Apache-2.0

#
# Script that prints something but returns error should fail
#

cat >$WORK/etc/my_serial <<EOF
#!/bin/sh
printf "12345678"
exit 1
EOF
chmod +x $WORK/etc/my_serial

CMDLINE="-b script -f $WORK/etc/my_serial"

cat >$EXPECTED <<EOF
00000000000000000000000000000000
EOF
25 changes: 25 additions & 0 deletions tests/050_script_read
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

# SPDX-FileCopyrightText: 2023 Nerves Project Developers
#
# SPDX-License-Identifier: Apache-2.0

#
# Test that reading from stdin in the script doesn't hang
#

cat >$WORK/etc/my_serial <<EOF
#!/bin/sh
read bad_script
cat >$WORK/testing
printf "23456781"
EOF
chmod +x $WORK/etc/my_serial

CMDLINE="-b script -f $WORK/etc/my_serial"

cat >$EXPECTED <<EOF
23456781
EOF
29 changes: 29 additions & 0 deletions tests/051_script_too_long
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

# SPDX-FileCopyrightText: 2023 Nerves Project Developers
#
# SPDX-License-Identifier: Apache-2.0

#
# Test script outputing too much. Result should max out at 31 characters.
#

cat >$WORK/etc/my_serial <<EOF
#!/bin/sh
printf "0123456789"
printf "0123456789"
printf "0123456789"
printf "0123456789"
printf "0123456789"
printf "0123456789"
printf "0123456789"
printf "0123456789"
EOF
chmod +x $WORK/etc/my_serial

CMDLINE="-b script -f $WORK/etc/my_serial"

cat >$EXPECTED <<EOF
01234567890123456789012345678901
EOF

0 comments on commit 4229474

Please sign in to comment.