Hello,

I've started working through the test suite trying to expand it and fix
where commands are failing. Here's what I've got so far for basename,
blkid, cat, and cp.

The cp test rewrite was done a few months ago, and looking at it now it
seems a bit dense. I was trying to minimize the number of temporary
files, but I may have overdone it. I can redo it if you wish. Note that
'cp -P' is still broken.

Also, I'm not sure how to add test cases for blkid with no arguments,
since it will depend on the local configuration.

--
Regards,
Samuel Holland <[email protected]>
>From dc585eec2d57a7acf04ab89d170426696af5ce1b Mon Sep 17 00:00:00 2001
From: Samuel Holland <[email protected]>
Date: Wed, 11 Jun 2014 12:58:33 -0500
Subject: [PATCH 1/7] basename: fix segfault on null input; add tests

When passed an empty string, glibc's basename() returns a pointer to the
string "." in its private memory. If an empty suffix is given, it fits
the condition of being shorter than the path, so we try to overwrite the
null byte and crash. We fix this by just ignoring empty suffixes; they
don't do anything anyway.

Also, updated the help text.
---
 tests/basename.test   | 7 ++++++-
 toys/posix/basename.c | 6 +++---
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/tests/basename.test b/tests/basename.test
index 2f7a2ec..2789e29 100755
--- a/tests/basename.test
+++ b/tests/basename.test
@@ -4,6 +4,11 @@
 
 #testing "name" "command" "result" "infile" "stdin"
 
+# Null Input
+testing "basename null path" "basename ''" ".\n" "" ""
+testing "basename null path and suffix" "basename '' ''" ".\n" "" ""
+testing "basename path with null suffix" "basename a/b/c ''" "c\n" "" ""
+
 # Removal of extra /'s
 testing "basename /-only" "basename ///////" "/\n" "" ""
 testing "basename trailing /" "basename a//////" "a\n" "" ""
@@ -19,5 +24,5 @@ testing "basename suffix=result" "basename .txt .txt" ".txt\n" "" ""
 testing "basename reappearing suffix 1" "basename a.txt.txt .txt" "a.txt\n" "" ""
 testing "basename reappearing suffix 2" "basename a.txt.old .txt" "a.txt.old\n" "" ""
 
-# A suffix should be a real suffix, only a the end.
+# A suffix should be a real suffix, only at the end.
 testing "basename invalid suffix" "basename isthisasuffix? suffix" "isthisasuffix?\n" "" ""
diff --git a/toys/posix/basename.c b/toys/posix/basename.c
index c49a5f3..917f62f 100644
--- a/toys/posix/basename.c
+++ b/toys/posix/basename.c
@@ -11,9 +11,9 @@ config BASENAME
   bool "basename"
   default y
   help
-    usage: basename string [suffix]
+    usage: basename PATH [SUFFIX]
 
-    Return non-directory portion of a pathname removing suffix
+    Return non-directory portion of PATH, optionally removing SUFFIX.
 */
 
 #include "toys.h"
@@ -23,7 +23,7 @@ void basename_main(void)
   char *base = basename(*toys.optargs), *suffix = toys.optargs[1];
 
   // chop off the suffix if provided
-  if (suffix) {
+  if (suffix && *suffix) {
     char *s = base + strlen(base) - strlen(suffix);
     if (s > base && !strcmp(s, suffix)) *s = 0;
   }
-- 
2.2.1

>From 3cd796c710b144e0900689e8c666e47ad620e3be Mon Sep 17 00:00:00 2001
From: Samuel Holland <[email protected]>
Date: Mon, 1 Dec 2014 11:56:05 -0600
Subject: [PATCH 3/7] tests/cat: Compare the correct file. /proc/self/exe means
 different things to different processes.

---
 tests/cat.test | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/cat.test b/tests/cat.test
index 3d5842a..1ca3dc1 100755
--- a/tests/cat.test
+++ b/tests/cat.test
@@ -17,7 +17,7 @@ testing "cat file1 notfound file2" \
         "one\ntwo\ncat: notfound: No such file or directory\n" "" ""
 
 testing "cat file1" \
-        "cat /proc/self/exe > file1 && cmp /proc/self/exe file1 && echo yes" \
+        "cat `which cmp` > file1 && cmp /proc/self/exe file1 && echo yes" \
         "yes\n" "" ""
 
 testing "cat - file1" \
@@ -28,4 +28,4 @@ testing "cat > /dev/full" \
         "cat - > /dev/full 2>stderr && echo ok; cat stderr; rm stderr" \
         "cat: xwrite: No space left on device\n" "" "zero\n"
 
-rm file1 file2
\ No newline at end of file
+rm file1 file2
-- 
2.2.1

>From 3824036e6fba66d6b9f1536cef883a067c60c6a9 Mon Sep 17 00:00:00 2001
From: Samuel Holland <[email protected]>
Date: Tue, 1 Jul 2014 22:48:53 -0500
Subject: [PATCH 4/7] cp: greatly expand test suite

---
 tests/cp.test | 186 ++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 102 insertions(+), 84 deletions(-)
 mode change 100755 => 100644 tests/cp.test

diff --git a/tests/cp.test b/tests/cp.test
old mode 100755
new mode 100644
index 4327256..8eaf982
--- a/tests/cp.test
+++ b/tests/cp.test
@@ -3,99 +3,117 @@
 [ -f testing.sh ] && . testing.sh
 
 # Create test file
-dd if=/dev/urandom of=random bs=64 count=1 2> /dev/null
+dd if=/dev/urandom of=rand1 bs=1 count=64 2> /dev/null
+dd if=/dev/urandom of=rand2 bs=1 count=64 2> /dev/null
+dd if=/dev/urandom of=rand3 bs=1 count=64 2> /dev/null
+ln -s rand1 symrand
 
-#testing "name" "command" "result" "infile" "stdin"
+mkdir four five six eight eleven
+mkdir -p nine/two/three/four
+touch zero
+touch nine/two/three/five
+touch nine/{six,seven,eight}
 
-testing "cp not enough arguments [fail]" "cp one 2>/dev/null || echo yes" \
-	"yes\n" "" ""
-testing "cp -missing source [fail]" "cp missing two 2>/dev/null || echo yes" \
-	"yes\n" "" ""
-testing "cp file->file" "cp random two && cmp random two && echo yes" \
-	"yes\n" "" ""
-rm two
+chmod 000 zero six
+chmod 707 rand1
+chmod 646 rand2
+chmod 776 nine/seven
 
-mkdir two
-testing "cp file->dir" "cp random two && cmp random two/random && echo yes" \
-	"yes\n" "" ""
-rm two/random
-testing "cp file->dir/file" \
-	"cp random two/random && cmp random two/random && echo yes" \
-	"yes\n" "" ""
-testing "cp -r dir->missing" \
-	"cp -r two three && cmp random three/random && echo yes" \
-	"yes\n" "" ""
-touch walrus
-testing "cp -r dir->file [fail]" \
-	"cp -r two walrus 2>/dev/null || echo yes" "yes\n" "" ""
-touch two/three
-testing "cp -r dir hits file." \
-	"cp -r three two 2>/dev/null || echo yes" "yes\n" "" ""
-rm -rf two three walrus
+#testing "name" "command" "result" "infile" "stdin"
 
-touch two
-chmod 000 two
-testing "cp file->inaccessable [fail]" \
-	"cp random two 2>/dev/null || echo yes" "yes\n" "" ""
-rm -f two
+# First synopsis form
+testing "cp not enough arguments [fail]" "cp one 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp missing source [fail]" "cp missing two 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp file -> missing" "cp rand1 two && cmp rand1 two && echo yes" \
+  "yes\n" "" ""
+testing "cp symlink -> missing" "cp symrand three && cmp rand1 three && echo yes" \
+  "yes\n" "" ""
+testing "cp symlink -> file" "cp symrand three && cmp rand1 three && echo yes" \
+  "yes\n" "" ""
+testing "cp -P symlink -> missing" "cp -P symrand one && readlink one" \
+  "rand1\n" "" ""
+testing "cp -P symlink -> symlink" "cp -P symrand one && readlink one" \
+  "rand1\n" "" ""
+testing "cp file -> file" "cp rand2 three && cmp rand2 three && echo yes" \
+  "yes\n" "" ""
+testing "cp file -> dir/file" "cp rand1 four/extra && cmp rand1 four/extra && echo yes" \
+  "yes\n" "" ""
+testing "cp dir/file -> missing" "cp four/extra extra2 && cmp four/extra extra2 && echo yes" \
+  "yes\n" "" ""
+testing "cp file -> itself [fail]" "cp rand1 rand1 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp file -> symlink to itself [fail]" "cp rand1 symrand 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp file -> inaccessable [fail]" "cp rand1 zero 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp -f file -> inaccessable" "cp -f rand2 zero && cmp rand2 zero && echo yes" \
+  "yes\n" "" ""
+testing "cp -p file -> missing" "cp -p rand1 twelve && find -path ./twelve -printf '%m' 2>/dev/null " \
+  "707" "" ""
+testing "cp -p file -> file" "cp -p rand2 twelve && find -path ./twelve -printf '%m' 2>/dev/null " \
+  "646" "" ""
 
-touch two
-chmod 000 two
-testing "cp -f file->inaccessable" \
-	"cp -f random two && cmp random two && echo yes" "yes\n" "" ""
-mkdir sub
-chmod 000 sub
-testing "cp file->inaccessable_dir [fail]" \
-	"cp random sub 2>/dev/null || echo yes" "yes\n" "" ""
-rm two
-rmdir sub
+# Second synopsis form
+testing "cp file -> dir" "cp rand1 four && cmp rand1 four/rand1 && echo yes" \
+  "yes\n" "" ""
+testing "cp ../file -> dir" "cd nine/two && cp ../six three && cmp ../six three/six && echo yes" \
+  "yes\n" "" ""
+testing "cp file1 file2 -> dir" "cp rand2 rand3 four && cmp rand2 four/rand2 && echo yes" \
+  "yes\n" "" ""
+testing "cp file1 file2 -> file [fail]" "cp rand1 rand2 rand3 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp file1 file2 -> missing [fail]" "cp rand1 rand2 missing 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp dir -> dir [fail]" "cp four five 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp file dir -> dir [fail]" "cp rand2 four five 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp symlink -> dir" "cp symrand five && cmp rand1 five/symrand && echo yes" \
+  "yes\n" "" ""
+testing "cp file -> inaccessable_dir [fail]" "cp rand3 six 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp dir/file -> dir (itself) [fail]" "cp nine/two/three/five nine/two/three 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp -p file -> dir (overwrite)" "cp -p rand2 four && find -path ./four/rand2 -printf '%m' 2>/dev/null " \
+  "646" "" ""
 
+# Third synopsis form
+testing "cp -r dir -> missing" "cp -r five seven && cmp five/symrand seven/symrand && echo yes" \
+  "yes\n" "" ""
+testing "cp -r dir -> dir" "cp -r four seven && cmp four/rand3 seven/four/rand3 && echo yes" \
+  "yes\n" "" ""
+testing "cp -r dir -> dir/name" "cp -r five seven/extra && cmp five/symrand seven/extra/symrand && echo yes" \
+  "yes\n" "" ""
+testing "cp -r dir -> itself [fail]" "cp -r five five 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp -r dir -> inaccessible dir [fail]" "cp -r five six 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp -r dir -> file [fail]" "cp -r five rand2 2>/dev/null || echo yes" \
+  "yes\n" "" ""
 # This test fails because our -rf deletes existing target files without
 # regard to what we'd be copying over it. Posix says to only do that if
 # we'd be copying a file over the file, but does not say _why_.
+#testing "cp -rf dir file [fail]" "cp -rf file rand1 2>/dev/null || echo yes" \
+#  "yes\n" "" ""
+testing "cp -r dir -> dir (file exists) [fail]" "cp -r seven/extra four 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp -r dir1 dir2 -> dir" "cp -r four five eight && cmp eight/four/extra eight/five/symrand && echo yes" \
+  "yes\n" "" ""
+testing "cp -r dir1 dir2 -> missing [fail]" "cp -r four five missing 2>/dev/null || echo yes" \
+  "yes\n" "" ""
+testing "cp -r /abspath dir" "cp -r \"$(readlink -f nine)\" ten && diff -r nine ten && echo yes" \
+  "yes\n" "" ""
+testing "cp -r dir again" "cp -r nine/. ten && diff -r nine ten && echo yes" \
+  "yes\n" "" ""
+testing "cp -r ten/* eleven" "cp -r ten/* eleven && diff -r nine eleven && echo yes" \
+  "yes\n" "" ""
+testing "cp -rp dir -> missing" "cp -rp nine thirteen && find -path ./thirteen/seven -printf '%m' 2>/dev/null " \
+  "776" "" ""
 
-#mkdir dir
-#touch file
-#testing "cp -rf dir file [fail]" "cp -rf dir file 2>/dev/null || echo yes" \
-#	"yes\n" "" ""
-#rm -rf dir file
-
-touch one two
-testing "cp file1 file2 missing [fail]" \
-	"cp one two missing 2>/dev/null || echo yes" "yes\n" "" ""
-mkdir dir
-testing "cp dir file missing [fail]" \
-	"cp dir two missing 2>/dev/null || echo yes" "yes\n" "" ""
-testing "cp -rf dir file missing [fail]" \
-	"cp dir two missing 2>/dev/null || echo yes" "yes\n" "" ""
-testing "cp file1 file2 file [fail]" \
-	"cp random one two 2>/dev/null || echo yes" "yes\n" "" ""
-testing "cp file1 file2 dir" \
-	"cp random one dir && cmp random dir/random && cmp one dir/one && echo yes" \
-	"yes\n" "" ""
-rm one two random
-rm -rf dir
-
-mkdir -p one/two/three/four
-touch one/two/three/five
-touch one/{six,seven,eight}
-testing "cp -r /abspath dest" \
-	"cp -r \"$(readlink -f one)\" dir && diff -r one dir && echo yes" \
-	"yes\n" "" ""
-testing "cp -r dir again" "cp -r one/. dir && diff -r one dir && echo yes" \
-	"yes\n" "" ""
-mkdir dir2
-testing "cp -r dir1/* dir2" \
-	"cp -r one/* dir2 && diff -r one dir2 && echo yes" "yes\n" "" ""
-rm -rf one dir dir2
-
-# cp -r ../source destdir
-# cp -r one/two/three missing
-# cp -r one/two/three two
-# mkdir one; touch one/two; ln -s two one/three
-# cp file1 file2 dir
-# cp file1 missing file2 -> dir
-
-# Make sure it's truncating existing file
 # copy with -d at top level, with -d in directory, without -d at top level,
 #      without -d in directory
+
+rm rand1 rand2 rand3 symrand extra2
+rm -rf zero one two three four five six seven eight nine ten eleven twelve thirteen
-- 
2.2.1

>From c42aa899b9bcc8aed8a17e7de61133a541fbb4c9 Mon Sep 17 00:00:00 2001
From: Samuel Holland <[email protected]>
Date: Tue, 30 Dec 2014 03:19:33 +0000
Subject: [PATCH 5/7] tests/blkid: Fix ext3 and ext4 testcases

They were comparing the output to 'ext2' instead of the appropriate type.
---
 tests/blkid.test | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/blkid.test b/tests/blkid.test
index 3f676df..25ba9d1 100755
--- a/tests/blkid.test
+++ b/tests/blkid.test
@@ -16,10 +16,10 @@ testing "blkid ext2" 'bzcat "$BDIR"/ext2.bz2 | blkid -' \
   '-: LABEL="myext2" UUID="e59093ba-4135-4fdb-bcc4-f20beae4dfaf" TYPE="ext2"\n' \
   "" "" 
 testing "blkid ext3" 'bzcat "$BDIR"/ext3.bz2 | blkid -' \
-  '-: LABEL="myext3" UUID="79d1c877-1a0f-4e7d-b21d-fc32ae3ef101" TYPE="ext2"\n' \
+  '-: LABEL="myext3" UUID="79d1c877-1a0f-4e7d-b21d-fc32ae3ef101" TYPE="ext3"\n' \
   "" "" 
 testing "blkid ext4" 'bzcat "$BDIR"/ext4.bz2 | blkid -' \
-  '-: LABEL="myext4" UUID="dc4b7c00-c0c0-4600-af7e-0335f09770fa" TYPE="ext2"\n' \
+  '-: LABEL="myext4" UUID="dc4b7c00-c0c0-4600-af7e-0335f09770fa" TYPE="ext4"\n' \
   "" ""
 testing "blkid f2fs" 'bzcat "$BDIR"/f2fs.bz2 | blkid -' \
   '-: LABEL="" UUID="b53d3619-c204-4c0b-8504-36363578491c" TYPE="f2fs"\n' \
-- 
2.2.1

>From b4b1d006902ff4e08148c49fc2f1a30a5dd4daa8 Mon Sep 17 00:00:00 2001
From: Samuel Holland <[email protected]>
Date: Tue, 30 Dec 2014 04:59:36 +0000
Subject: [PATCH 6/7] blkid: Implement no-argument case

---
 toys/other/blkid.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/toys/other/blkid.c b/toys/other/blkid.c
index 725f163..8a4a53c 100644
--- a/toys/other/blkid.c
+++ b/toys/other/blkid.c
@@ -4,16 +4,17 @@
  *
  * See ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.24/libblkid-docs/api-index-full.html
 
-USE_BLKID(NEWTOY(blkid, "<1", TOYFLAG_BIN))
+USE_BLKID(NEWTOY(blkid, "", TOYFLAG_BIN))
 USE_FSTYPE(NEWTOY(fstype, "<1", TOYFLAG_BIN))
 
 config BLKID
   bool "blkid"
   default y
   help
-    usage: blkid DEV...
+    usage: blkid [DEV...]
 
     Prints type, label and UUID of filesystem on a block device or image.
+    If no devices are specified, prints information for all partitions.
 
 config FSTYPE
   bool "fstype"
@@ -56,7 +57,6 @@ static const struct fstype fstypes[] = {
   {"vfat", 0x31544146, 4, 54, 39+(4<<24), 11, 43}     // fat1
 };
 
-/* TODO if no args use proc/partitions */
 void do_blkid(int fd, char *name)
 {
   int off, i, j;
@@ -136,7 +136,23 @@ void do_blkid(int fd, char *name)
 
 void blkid_main(void)
 {
-  loopfiles(toys.optargs, do_blkid);
+  if (*toys.optargs) {
+    loopfiles(toys.optargs, do_blkid);
+  } else {
+    char *name = xmalloc(NAME_MAX + 5);
+    FILE * fp;
+    int fd;
+
+    strcpy(name, "/dev/");
+    fp = xfopen("/proc/partitions", "r");
+    fscanf(fp, "major minor #blocks name \n \n");
+    while (fscanf(fp, " %*d %*d %*d %s\n", name+5) > 0) {
+      do_blkid(fd = xopen(name, O_RDONLY), name);
+      xclose(fd);
+    }
+    fclose(fp);
+    if (CFG_TOYBOX_FREE) free(name);
+  }
 }
 
 void fstype_main(void)
-- 
2.2.1

>From 9b700d7887ef95b4c5d0dd6bc8c0c065df95aebf Mon Sep 17 00:00:00 2001
From: Samuel Holland <[email protected]>
Date: Tue, 30 Dec 2014 05:42:33 +0000
Subject: [PATCH 7/7] blkid: Fix handling of vfat labels; update tests to match

---
 tests/blkid.test   |  4 ++--
 toys/other/blkid.c | 14 +++++++++++---
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/tests/blkid.test b/tests/blkid.test
index 25ba9d1..db42e0f 100755
--- a/tests/blkid.test
+++ b/tests/blkid.test
@@ -25,7 +25,7 @@ testing "blkid f2fs" 'bzcat "$BDIR"/f2fs.bz2 | blkid -' \
   '-: LABEL="" UUID="b53d3619-c204-4c0b-8504-36363578491c" TYPE="f2fs"\n' \
   "" ""
 testing "blkid msdos" 'bzcat "$BDIR"/msdos.bz2 | blkid -' \
-  '-: LABEL="mymsdos    " UUID="5108-1e6e" TYPE="vfat"\n' "" ""
+  '-: LABEL="mymsdos" UUID="5108-1e6e" TYPE="vfat"\n' "" ""
 testing "blkid ntfs" 'bzcat "$BDIR"/ntfs.bz2 | blkid -' \
   '-: UUID="8585600838bfe16e" TYPE="ntfs"\n' "" ""
 testing "blkid reiserfs" 'bzcat "$BDIR"/reiser3.bz2 | blkid -' \
@@ -34,7 +34,7 @@ testing "blkid reiserfs" 'bzcat "$BDIR"/reiser3.bz2 | blkid -' \
 testing "blkid squashfs" 'bzcat "$BDIR"/squashfs.bz2 | blkid -' \
   '-: TYPE="squashfs"\n' "" ""
 testing "blkid vfat" 'bzcat "$BDIR"/vfat.bz2 | blkid -' \
-  '-: LABEL="myvfat     " UUID="1db9-5673" TYPE="vfat"\n' "" ""
+  '-: LABEL="myvfat" UUID="1db9-5673" TYPE="vfat"\n' "" ""
 testing "blkid xfs" 'bzcat "$BDIR"/xfs.bz2 | blkid -' \
   '-: LABEL="XFS_test" UUID="d63a1dc3-27d5-4dd4-8b38-f4f97f495c6f" TYPE="xfs"\n' \
   "" ""
diff --git a/toys/other/blkid.c b/toys/other/blkid.c
index 8a4a53c..51b5da0 100644
--- a/toys/other/blkid.c
+++ b/toys/other/blkid.c
@@ -115,9 +115,17 @@ void do_blkid(int fd, char *name)
   // output for blkid
   printf("%s:",name);
 
-  if (fstypes[i].label_len)
-    printf(" LABEL=\"%.*s\"", fstypes[i].label_len,
-           toybuf+fstypes[i].label_off-off);
+  if (fstypes[i].label_len) {
+    int label_len = fstypes[i].label_len;
+    if (!strcmp(fstypes[i].name, "vfat")) {
+      if (!strncmp(toybuf+fstypes[i].label_off-off, "NO NAME    ", label_len))
+        label_len=0;
+      else while (*(toybuf+fstypes[i].label_off-off+label_len-1) == ' ')
+        label_len--;
+    }
+    if (label_len)
+      printf(" LABEL=\"%.*s\"", label_len, toybuf+fstypes[i].label_off-off);
+  }
 
   if (fstypes[i].uuid_off) {
     int bits = 0x550, size = fstypes[i].uuid_off >> 24,
-- 
2.2.1

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to