Add MTD test script

So the quick & dirty hack is to just modify the cgpt command to be
cgpt -N=<MTD magic> <rest of the commands>

There are a couple limitations of the MTD command versions that will cause
them to always fail, so they're skipped if the second argument is empty - boot,
adding unknown GUIDs and more than 15 partitions.

BUG=chromium:221745
TEST=MTD version of run_cgpt_tests.sh passes
BRANCH=none

Original-Change-Id: Ida0debdefdc736f38e616801f6a40e67d393f405
Reviewed-on: https://gerrit.chromium.org/gerrit/47177
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Queue: Albert Chaulk <achaulk@chromium.org>
Tested-by: Albert Chaulk <achaulk@chromium.org>
(cherry picked from commit 53fd488fc772e2ed964331fe98eaa21d2a1e471b)

Change-Id: Id7af245cc0e8c2dc00fe9ceab9ce0be0e47882ec
Reviewed-on: https://gerrit.chromium.org/gerrit/49796
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Queue: Albert Chaulk <achaulk@chromium.org>
Tested-by: Albert Chaulk <achaulk@chromium.org>
diff --git a/Makefile b/Makefile
index 1aad0ae..403b4d6 100644
--- a/Makefile
+++ b/Makefile
@@ -997,6 +997,7 @@
 .PHONY: runtestscripts
 runtestscripts: test_setup genfuzztestcases
 	tests/run_cgpt_tests.sh ${BUILD_RUN}/cgpt/cgpt
+	tests/run_cgpt_tests.sh ${BUILD_RUN}/cgpt/cgpt -N=512,32,1,3
 	tests/run_preamble_tests.sh
 	tests/run_rsa_tests.sh
 	tests/run_vbutil_kernel_arg_tests.sh
diff --git a/tests/run_cgpt_tests.sh b/tests/run_cgpt_tests.sh
index 445070a..847c081 100755
--- a/tests/run_cgpt_tests.sh
+++ b/tests/run_cgpt_tests.sh
@@ -12,6 +12,9 @@
 CGPT=$(readlink -f "$1")
 [ -x "$CGPT" ] || error "Can't execute $CGPT"
 
+MTD="${2:-}"
+CGPT="${CGPT} ${MTD}"
+
 # Run tests in a dedicated directory for easy cleanup or debugging.
 DIR="${TEST_DIR}/cgpt_test_dir"
 [ -d "$DIR" ] || mkdir -p "$DIR"
@@ -122,22 +125,25 @@
 X=$($CGPT show -t -i 1 ${DEV} | tr 'A-Z' 'a-z')
 [ "$X" = "$FUTURE_GUID" ] || error
 # arbitrary value
-$CGPT add -i 1 -t 610a563a-a55c-4ae0-ab07-86e5bb9db67f ${DEV} || error
-X=$($CGPT show -t -i 1 ${DEV})
-[ "$X" = "610A563A-A55C-4AE0-AB07-86E5BB9DB67F" ] || error
+if [ -z "$MTD" ]; then
+  $CGPT add -i 1 -t 610a563a-a55c-4ae0-ab07-86e5bb9db67f ${DEV} || error
+  X=$($CGPT show -t -i 1 ${DEV})
+  [ "$X" = "610A563A-A55C-4AE0-AB07-86E5BB9DB67F" ] || error
+fi
 $CGPT add -i 1 -t data ${DEV} || error
 X=$($CGPT show -t -i 1 ${DEV} | tr 'A-Z' 'a-z')
 [ "$X" = "$DATA_GUID" ] || error
 
 
-echo "Set the boot partition.."
-$CGPT boot -i ${KERN_NUM} ${DEV} >/dev/null
+if [ -z "$MTD" ]; then
+  echo "Set the boot partition.."
+  $CGPT boot -i ${KERN_NUM} ${DEV} >/dev/null
 
-echo "Check the PMBR's idea of the boot partition..."
-X=$($CGPT boot ${DEV})
-Y=$($CGPT show -u -i $KERN_NUM $DEV)
-[ "$X" = "$Y" ] || error
-
+  echo "Check the PMBR's idea of the boot partition..."
+  X=$($CGPT boot ${DEV})
+  Y=$($CGPT show -u -i $KERN_NUM $DEV)
+  [ "$X" = "$Y" ] || error
+fi
 
 echo "Test the cgpt prioritize command..."
 
@@ -170,7 +176,6 @@
     error 1 "expected priority \"$expected\", actual priority \"$actual\""
 }
 
-
 # no kernels at all. This should do nothing.
 $CGPT create ${DEV}
 $CGPT add -t rootfs -b 100 -s 1 ${DEV}
@@ -248,15 +253,17 @@
 $CGPT prioritize -P 1 -i 3 ${DEV}
 assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 1 1
 
-# squish if we try to go too high
-make_pri   15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0
-$CGPT prioritize -i 3 ${DEV}
-assert_pri 14 14 15 13 12 12 11 11 10 10  9  9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 0
-$CGPT prioritize -i 5 ${DEV}
-assert_pri 13 13 14 12 15 11 10 10  9  9  8  8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
-# but if I bring friends I don't have to squish
-$CGPT prioritize -i 1 -f ${DEV}
-assert_pri 15 15 13 12 14 11 10 10  9  9  8  8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
+if [ -z "$MTD" ]; then # MTD doesn't support this many partitions
+  # squish if we try to go too high
+  make_pri   15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0
+  $CGPT prioritize -i 3 ${DEV}
+  assert_pri 14 14 15 13 12 12 11 11 10 10  9  9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 0
+  $CGPT prioritize -i 5 ${DEV}
+  assert_pri 13 13 14 12 15 11 10 10  9  9  8  8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
+  # but if I bring friends I don't have to squish
+  $CGPT prioritize -i 1 -f ${DEV}
+  assert_pri 15 15 13 12 14 11 10 10  9  9  8  8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
+fi
 
 
 # Now make sure that we don't need write access if we're just looking.
@@ -276,7 +283,9 @@
 $CGPT boot -i 2 ${DEV} 2>/dev/null && error
 
 # These should pass
-$CGPT boot ${DEV} >/dev/null
+if [ -z "$MTD" ]; then
+  $CGPT boot ${DEV} >/dev/null
+fi
 $CGPT show ${DEV} >/dev/null
 $CGPT find -t kernel ${DEV} >/dev/null