Skip to content

Commit 7481a8a

Browse files
committed
Add test harness, write first tests for WP_LDAP::API.
1 parent 8014f6e commit 7481a8a

File tree

6 files changed

+263
-2
lines changed

6 files changed

+263
-2
lines changed

.travis.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
language: php
2+
3+
notifications:
4+
email:
5+
on_success: never
6+
on_failure: change
7+
8+
branches:
9+
only:
10+
- develop
11+
- master
12+
13+
php:
14+
- 5.3
15+
- 5.4
16+
- 5.5
17+
- 5.6
18+
- 7.0
19+
- 7.1
20+
21+
env:
22+
- WP_VERSION=latest WP_MULTISITE=1
23+
24+
before_script:
25+
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
26+
- apt install -y php-ldap slapd
27+
28+
script: phpunit

bin/install-wp-tests.sh

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env bash
2+
3+
if [ $# -lt 3 ]; then
4+
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
5+
exit 1
6+
fi
7+
8+
DB_NAME=$1
9+
DB_USER=$2
10+
DB_PASS=$3
11+
DB_HOST=${4-localhost}
12+
WP_VERSION=${5-latest}
13+
14+
WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
15+
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
16+
17+
download() {
18+
if [ `which curl` ]; then
19+
curl -s "$1" > "$2";
20+
elif [ `which wget` ]; then
21+
wget -nv -O "$2" "$1"
22+
fi
23+
}
24+
25+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
26+
WP_TESTS_TAG="tags/$WP_VERSION"
27+
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
28+
WP_TESTS_TAG="trunk"
29+
else
30+
# http serves a single offer, whereas https serves multiple. we only want one
31+
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
32+
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
33+
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
34+
if [[ -z "$LATEST_VERSION" ]]; then
35+
echo "Latest WordPress version could not be found"
36+
exit 1
37+
fi
38+
WP_TESTS_TAG="tags/$LATEST_VERSION"
39+
fi
40+
41+
set -ex
42+
43+
install_wp() {
44+
45+
if [ -d $WP_CORE_DIR ]; then
46+
return;
47+
fi
48+
49+
mkdir -p $WP_CORE_DIR
50+
51+
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
52+
mkdir -p /tmp/wordpress-nightly
53+
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
54+
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
55+
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
56+
else
57+
if [ $WP_VERSION == 'latest' ]; then
58+
local ARCHIVE_NAME='latest'
59+
else
60+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
61+
fi
62+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
63+
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
64+
fi
65+
66+
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
67+
}
68+
69+
install_test_suite() {
70+
# portable in-place argument for both GNU sed and Mac OSX sed
71+
if [[ $(uname -s) == 'Darwin' ]]; then
72+
local ioption='-i .bak'
73+
else
74+
local ioption='-i'
75+
fi
76+
77+
# set up testing suite if it doesn't yet exist
78+
if [ ! -d $WP_TESTS_DIR ]; then
79+
# set up testing suite
80+
mkdir -p $WP_TESTS_DIR
81+
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
82+
fi
83+
84+
cd $WP_TESTS_DIR
85+
86+
if [ ! -f wp-tests-config.php ]; then
87+
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
88+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" "$WP_TESTS_DIR"/wp-tests-config.php
89+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
90+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
91+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
92+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
93+
fi
94+
95+
}
96+
97+
install_db() {
98+
# parse DB_HOST for port or socket references
99+
local PARTS=(${DB_HOST//\:/ })
100+
local DB_HOSTNAME=${PARTS[0]};
101+
local DB_SOCK_OR_PORT=${PARTS[1]};
102+
local EXTRA=""
103+
104+
if ! [ -z $DB_HOSTNAME ] ; then
105+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
106+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
107+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
108+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
109+
elif ! [ -z $DB_HOSTNAME ] ; then
110+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
111+
fi
112+
fi
113+
114+
# create database
115+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
116+
}
117+
118+
install_wp
119+
install_test_suite
120+
install_db

includes/class-wp-ldap-api.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ public static function sanitize_dn( $dn ) {
145145
* mechanism that OpenLDAP 2.4 supports.
146146
*
147147
* @param string $plain
148+
* @param string $salt
148149
*
149150
* @return string
150151
*/
151-
public static function hashPassword ( $plain ) {
152-
$salt = random_bytes( 8 ); // arbitrary, but 8 seems fine
152+
public static function hashPassword ( $plain, $salt = '' ) {
153+
// Generate 8 bytes of salt if not given any.
154+
$salt = ( empty( $salt ) ) ? random_bytes( 8 ): $salt;
153155
return '{SSHA}' . base64_encode(
154156
hash( 'sha1', $plain . $salt , true ) . $salt
155157
);

phpunit.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<phpunit
2+
bootstrap="tests/bootstrap.php"
3+
backupGlobals="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
>
9+
<testsuites>
10+
<testsuite>
11+
<directory prefix="test-" suffix=".php">./tests/</directory>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

tests/bootstrap.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
$_tests_dir = getenv( 'WP_TESTS_DIR' );
3+
if ( ! $_tests_dir ) {
4+
$_tests_dir = '/tmp/wordpress-tests-lib';
5+
}
6+
7+
require_once $_tests_dir . '/includes/functions.php';
8+
9+
function _manually_load_plugin() {
10+
require dirname( dirname( __FILE__ ) ) . '/wp-ldap.php';
11+
}
12+
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
13+
14+
require $_tests_dir . '/includes/bootstrap.php';
15+
16+
/**
17+
* Custom test case class.
18+
*
19+
* We use PHPUnit's `@ticket` annotation to refer to GitHub issues,
20+
* but still want to use `WP_UnitTestCase` for its various factory
21+
* methods, so we subclass it and stub out the Trac-checking part. :)
22+
*/
23+
class Plugin_UnitTestCase extends WP_UnitTestCase {
24+
25+
/**
26+
* Empty method to disable integration with WP Core Trac.
27+
*
28+
* Overrides the parent's method to disable fetching information
29+
* from the WordPress Core Trac ticket tracker which causes tests
30+
* with the `@ticket` annotation in our own test cases to fail.
31+
*
32+
* @link https://core.trac.wordpress.org/browser/tags/4.4/tests/phpunit/includes/testcase.php#L434
33+
*
34+
* @return void
35+
*/
36+
protected function checkRequirements () {
37+
// do nothing!
38+
}
39+
40+
}

tests/test-wp-ldap-api.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Test cases for the WP_LDAP::API class.
4+
*/
5+
6+
use WP_LDAP\API;
7+
8+
/**
9+
* @covers API
10+
*/
11+
final class APITest extends Plugin_UnitTestCase {
12+
13+
/**
14+
* Tests the API::hashPassword implementation against the
15+
* output of using OpenLDAP's `slappasswd(8)` to create a
16+
* password for the same purpose.
17+
*
18+
* Refer to RFC 2307 § 5.3 for implementation details.
19+
*
20+
* This raises a test failure if our password hashing
21+
* implementation differs in any way from OpenLDAP's.
22+
*
23+
* @link http://www.faqs.org/rfcs/rfc2307.html
24+
*/
25+
public function testCreateRfc2307UserPassword () {
26+
$plain = 'password';
27+
$cmd = "slappasswd -h {SSHA} -s $plain";
28+
$userPassword = trim( shell_exec( escapeshellcmd( $cmd ) ) );
29+
$saltedhash = base64_decode( substr( $userPassword, 6 ) );
30+
$salt = substr( $saltedhash, -4 );
31+
32+
$this->assertSame(
33+
$userPassword,
34+
API::hashPassword( $plain, $salt )
35+
);
36+
}
37+
38+
/**
39+
* Tests to ensure that various special characters in an
40+
* LDAP DN are sanitized correctly.
41+
*/
42+
public function testCanSanitizeDN () {
43+
$arr = array(
44+
// Input DN => Sanitized DN
45+
'employeeNumber=2+uid=someuser,dc=example,dc=com' => 'employeeNumber=2+uid=someuser,dc=example,dc=com',
46+
'somekey=with spaces' => 'somekey=with spaces',
47+
'somekey=withequals=init' => 'somekey=withequals\5c3Dinit',
48+
);
49+
foreach ( $arr as $dn => $cleaned ) {
50+
$this->assertSame(
51+
$cleaned,
52+
API::sanitize_dn( $dn )
53+
);
54+
}
55+
}
56+
57+
}

0 commit comments

Comments
 (0)