diff --git a/.gitignore b/.gitignore
index 250273f..bc0b97f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -40,3 +40,4 @@
 
 # Ignore auto-generated docs
 /docs
+scripts/man/dub*.1.gz
diff --git a/test/4-describe-data-3-zero-delim.sh b/test/4-describe-data-3-zero-delim.sh
index aee4d08..a6924ca 100755
--- a/test/4-describe-data-3-zero-delim.sh
+++ b/test/4-describe-data-3-zero-delim.sh
@@ -71,7 +71,7 @@
     die 'Printing null-delimited list-style project data failed!'
 fi
 
-if ! diff -Z "$temp_file_normal" "$temp_file_zero_delim"; then
+if ! diff -b -B "$temp_file_normal" "$temp_file_zero_delim"; then
     die 'The null-delimited list-style project data did not match the expected output!'
 fi
 
@@ -86,7 +86,7 @@
     die 'Printing null-delimited --import-paths failed!'
 fi
 
-if ! diff -Z -B "$temp_file_normal" "$temp_file_zero_delim"; then
+if ! diff -b -B "$temp_file_normal" "$temp_file_zero_delim"; then
     die 'The null-delimited --import-paths data did not match the expected output!'
 fi
 
@@ -107,7 +107,7 @@
     die 'Printing null-delimited dmd-style --data=versions failed!'
 fi
 
-if ! diff -Z "$temp_file_normal" "$temp_file_zero_delim"; then
+if ! diff -b -B "$temp_file_normal" "$temp_file_zero_delim"; then
     die 'The null-delimited dmd-style --data=versions did not match the expected output!'
 fi
 
@@ -122,6 +122,6 @@
     die 'Printing null-delimited dmd-style --data=source-files failed!'
 fi
 
-if ! diff -Z "$temp_file_normal" "$temp_file_zero_delim"; then
+if ! diff -b -B "$temp_file_normal" "$temp_file_zero_delim"; then
     die 'The null-delimited dmd-style --data=source-files did not match the expected output!'
 fi
diff --git a/test/common.sh b/test/common.sh
index eba424d..054f007 100644
--- a/test/common.sh
+++ b/test/common.sh
@@ -19,3 +19,40 @@
     exit 1
 }
 trap 'die $LINENO' ERR
+
+# Get a random port for the test to use
+# This isn't foolproof but should fail less than handcrafted approaches
+function getRandomPort() {
+    # Get the PID of this script as a way to get a random port,
+    # and make sure the value is > 1024, as ports < 1024 are priviledged
+    # and require root priviledges.
+    # We also need to make sure the value is not > ushort.max
+    PORT=$(($$ % 65536))
+    if [ $PORT -le 1024 ]; then
+        PORT=$(($PORT + 1025))
+    fi
+    echo $PORT
+}
+
+# Emulate GNU readlink's behavior on non-GNU readlink (e.g. MacOSX / BSD's)
+# Credit to https://stackoverflow.com/a/1116890
+function gnureadlink() {
+    TARGET_FILE=$1
+
+    cd `dirname $TARGET_FILE`
+    TARGET_FILE=`basename $TARGET_FILE`
+
+    # Iterate down a (possible) chain of symlinks
+    while [ -L "$TARGET_FILE" ]
+    do
+        TARGET_FILE=`readlink $TARGET_FILE`
+        cd `dirname $TARGET_FILE`
+        TARGET_FILE=`basename $TARGET_FILE`
+    done
+
+    # Compute the canonicalized name by finding the physical path
+    # for the directory we're in and appending the target file.
+    PHYS_DIR=`pwd -P`
+    RESULT=$PHYS_DIR/$TARGET_FILE
+    echo $RESULT
+}
diff --git a/test/fetchzip.sh b/test/fetchzip.sh
index 723e698..15b906e 100755
--- a/test/fetchzip.sh
+++ b/test/fetchzip.sh
@@ -3,7 +3,7 @@
 
 . "$DIR"/common.sh
 
-PORT=$(($$ + 1024)) # PID + 1024
+PORT=$(getRandomPort)
 
 dub remove gitcompatibledubpackage --non-interactive --version=* 2>/dev/null || true
 
diff --git a/test/issue1180-local-cache-broken.sh b/test/issue1180-local-cache-broken.sh
index e46970a..0a83177 100755
--- a/test/issue1180-local-cache-broken.sh
+++ b/test/issue1180-local-cache-broken.sh
@@ -3,7 +3,8 @@
 
 . "$DIR"/common.sh
 
-PORT=$(($$ + 1024)) # PID + 1024
+PORT=$(getRandomPort)
+
 "$DUB" remove maven-dubpackage --root="$DIR/issue1180-local-cache-broken" --non-interactive --version=* 2>/dev/null || true
 
 "$DUB" build --single "$DIR"/test_registry.d
diff --git a/test/issue1416-maven-repo-pkg-supplier.sh b/test/issue1416-maven-repo-pkg-supplier.sh
index 636ef31..3aca25b 100755
--- a/test/issue1416-maven-repo-pkg-supplier.sh
+++ b/test/issue1416-maven-repo-pkg-supplier.sh
@@ -3,7 +3,7 @@
 
 . "$DIR"/common.sh
 
-PORT=$(($$ + 1024)) # PID + 1024
+PORT=$(getRandomPort)
 
 dub remove maven-dubpackage --non-interactive --version=* 2>/dev/null || true
 
diff --git a/test/issue1524-maven-upgrade-dependency-tree.sh b/test/issue1524-maven-upgrade-dependency-tree.sh
index 711cded..24f5f65 100755
--- a/test/issue1524-maven-upgrade-dependency-tree.sh
+++ b/test/issue1524-maven-upgrade-dependency-tree.sh
@@ -3,7 +3,7 @@
 
 . "$DIR"/common.sh
 
-PORT=$(($$ + 1024)) # PID + 1024
+PORT=$(getRandomPort)
 
 dub remove maven-dubpackage-a --non-interactive --version=* 2>/dev/null || true
 dub remove maven-dubpackage-b --non-interactive --version=* 2>/dev/null || true
diff --git a/test/issue1574-addcommand.sh b/test/issue1574-addcommand.sh
index dddedca..94d90f1 100755
--- a/test/issue1574-addcommand.sh
+++ b/test/issue1574-addcommand.sh
@@ -3,7 +3,7 @@
 
 . "$DIR"/common.sh
 
-PORT=$(($$ + 1024)) # PID + 1024
+PORT=$(getRandomPort)
 tempDir="issue1574-addcommand"
 
 "$DUB" build --single "$DIR"/test_registry.d
diff --git a/test/run-unittest.sh b/test/run-unittest.sh
index 5f27b93..b0f29a5 100755
--- a/test/run-unittest.sh
+++ b/test/run-unittest.sh
@@ -41,7 +41,7 @@
 
 for script in $(ls $CURR_DIR/*.sh); do
     if [[ ! "$script" =~ $FILTER ]]; then continue; fi
-    if [ "$script" = "$(readlink -f ${BASH_SOURCE[0]})" ] || [ "$(basename $script)" = "common.sh" ]; then continue; fi
+    if [ "$script" = "$(gnureadlink ${BASH_SOURCE[0]})" ] || [ "$(basename $script)" = "common.sh" ]; then continue; fi
     if [ -e $script.min_frontend ] && [ ! -z "$FRONTEND" ] && [ ${FRONTEND} \< $(cat $script.min_frontend) ]; then continue; fi
     log "Running $script..."
     DUB=$DUB DC=$DC CURR_DIR="$CURR_DIR" $script || logError "Script failure."
diff --git a/test/test_registry.d b/test/test_registry.d
index f5d14c1..7b6f67f 100755
--- a/test/test_registry.d
+++ b/test/test_registry.d
@@ -38,7 +38,7 @@
 {
 	import std.conv;
 	immutable folder = readRequiredOption!string("folder", "Folder to service files from.");
-	immutable port = readRequiredOption!uint("port", "Port to use");
+	immutable port = readRequiredOption!ushort("port", "Port to use");
 	auto router = new URLRouter;
 	router.get("stop", (HTTPServerRequest req, HTTPServerResponse res){
 		res.writeVoidBody;
diff --git a/test/timeout.sh b/test/timeout.sh
index 3e841c9..160e78b 100755
--- a/test/timeout.sh
+++ b/test/timeout.sh
@@ -2,7 +2,7 @@
 
 . $(dirname "${BASH_SOURCE[0]}")/common.sh
 
-PORT=$(($$ + 1024)) # PID + 1024
+PORT=$(getRandomPort)
 
 log '    Testing unconnectable registry'
 if timeout 1s $DUB fetch dub --skip-registry=all --registry=http://localhost:$PORT; then