summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2019-10-14 09:53:56 -0600
committermo khan <mo.khan@gmail.com>2019-10-14 09:53:56 -0600
commit64397c35fb21db7d47d0182eeaa44c2070fc6afa (patch)
treea640862b44042076dcc18095cee9792c425785fc
parent9c63abb0a154c526a480ec8b82672e3252e3f4c3 (diff)
Add unit tests
-rw-r--r--README.md12
-rw-r--r--Rakefile1
-rwxr-xr-xbin/duplicate-ids14
-rwxr-xr-xbin/no-password2
-rw-r--r--test/duplicate-ids_test.bats15
-rw-r--r--test/fixtures/passwd_with_duplicate_ids11
-rw-r--r--test/missing-expiration-date_test.bats (renamed from tmp/.keep)0
7 files changed, 46 insertions, 9 deletions
diff --git a/README.md b/README.md
index 28a1972..bbc7e94 100644
--- a/README.md
+++ b/README.md
@@ -1171,11 +1171,9 @@ Rootly Powers and Processes
```bash
#!/bin/bash
- if [[ "$OSTYPE" == "darwin"* ]]; then
- cat /etc/passwd | cut -d: -f1,3 | grep ':0'
- else
- cat /etc/passwd | grep -P '^\w+:[x]:\d+:0.*$'
- fi
+ FILE=${1:-'/etc/passwd'}
+
+ cat "$FILE" | cut -d: -f1,3 | grep ':0'
```
b. Find entries that have no password (needs /etc/shadow).
@@ -1183,7 +1181,9 @@ Rootly Powers and Processes
```bash
#!/bin/bash
- grep -E '^\w+::.*' /etc/passwd
+ FILE=${1:-'/etc/passwd'}
+
+ grep -E '^\w+::.*' $FILE
```
c. Find any sets of entries that have duplicate UIDs.
diff --git a/Rakefile b/Rakefile
index 4bff04e..685204f 100644
--- a/Rakefile
+++ b/Rakefile
@@ -8,7 +8,6 @@ task :tarball do
'README.pdf',
'bin/*',
'test/**/*',
- 'tmp/.keep',
'vendor/bats/**/*',
]
end
diff --git a/bin/duplicate-ids b/bin/duplicate-ids
index 2d04668..a1e2b39 100755
--- a/bin/duplicate-ids
+++ b/bin/duplicate-ids
@@ -1,3 +1,15 @@
#!/bin/bash
-cat /etc/passwd | grep -v -E '^#' | awk -F: '{ print $3 }' | sort -n | uniq -d
+FILE=${1:-'/etc/passwd'}
+
+ids=$(grep -v -E '^#' "$FILE" | awk -F: '{ print $3 }' | sort -n | uniq -d)
+
+for id in $ids; do
+ grep -v -E '^#' "$FILE" | awk -F: '{ print $3 " " $1 }' | grep -E "^$id "
+done
+
+if [ -z "$ids" ]; then
+ exit 0
+else
+ exit 1
+fi
diff --git a/bin/no-password b/bin/no-password
index bd32709..4705ddd 100755
--- a/bin/no-password
+++ b/bin/no-password
@@ -2,4 +2,4 @@
FILE=${1:-'/etc/passwd'}
-grep -E '^\w+::.*' $FILE
+grep -E '^\w+::.*' "$FILE"
diff --git a/test/duplicate-ids_test.bats b/test/duplicate-ids_test.bats
new file mode 100644
index 0000000..9bcb7a0
--- /dev/null
+++ b/test/duplicate-ids_test.bats
@@ -0,0 +1,15 @@
+#!/usr/bin/env bats
+
+load test_helper
+
+@test "it finds users with duplicate user ids" {
+ run ./bin/duplicate-ids test/fixtures/passwd_with_duplicate_ids
+
+ assert_failure $'1 bin\n1 daemon\n6 shutdown\n6 halt'
+}
+
+@test "it does not find any duplicate user ids" {
+ run ./bin/duplicate-ids test/fixtures/passwd
+
+ assert_success
+}
diff --git a/test/fixtures/passwd_with_duplicate_ids b/test/fixtures/passwd_with_duplicate_ids
new file mode 100644
index 0000000..f8bb796
--- /dev/null
+++ b/test/fixtures/passwd_with_duplicate_ids
@@ -0,0 +1,11 @@
+root:x:0:0:root:/root:/bin/bash
+bin:x:1:1:bin:/bin:/sbin/nologin
+daemon:x:1:2:daemon:/sbin:/sbin/nologin
+sync:x:5:0:sync:/sbin:/bin/sync
+shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
+halt:x:6:0:halt:/sbin:/sbin/halt
+mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
+operator:x:11:0:operator:/root:/sbin/nologin
+games:x:12:100:games:/usr/games:/sbin/nologin
+ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
+nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
diff --git a/tmp/.keep b/test/missing-expiration-date_test.bats
index e69de29..e69de29 100644
--- a/tmp/.keep
+++ b/test/missing-expiration-date_test.bats