summaryrefslogtreecommitdiff
path: root/src/mongo/client/examples/authTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/client/examples/authTest.cpp')
-rw-r--r--src/mongo/client/examples/authTest.cpp46
1 files changed, 37 insertions, 9 deletions
diff --git a/src/mongo/client/examples/authTest.cpp b/src/mongo/client/examples/authTest.cpp
index 9ecd764199f..81ce22e4502 100644
--- a/src/mongo/client/examples/authTest.cpp
+++ b/src/mongo/client/examples/authTest.cpp
@@ -15,6 +15,7 @@
* limitations under the License.
*/
+#include <boost/scoped_ptr.hpp>
#include <iostream>
#include <cstdlib>
#include <string>
@@ -33,24 +34,51 @@ int main( int argc, const char **argv ) {
port = argv[ 2 ];
}
- DBClientConnection conn;
+ Status status = client::initialize();
+ if (!status.isOK()) {
+ std::cout << "failed to initialize the client driver: " << status.toString() << endl;
+ return EXIT_FAILURE;
+ }
+
std::string errmsg;
- if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
+ ConnectionString cs = ConnectionString::parse(string("127.0.0.1:") + port, errmsg);
+ if (!cs.isValid()) {
+ cout << "error parsing url: " << errmsg << endl;
+ return EXIT_FAILURE;
+ }
+
+ boost::scoped_ptr<DBClientBase> conn(cs.connect(errmsg));
+ if (!conn) {
cout << "couldn't connect: " << errmsg << endl;
return EXIT_FAILURE;
}
+ BSONObj ret;
// clean up old data from any previous tests
- conn.remove( "test.system.users" , BSONObj() );
+ conn->runCommand( "test", BSON("removeUsersFromDatabase" << 1), ret );
- conn.insert( "test.system.users" , BSON( "user" << "eliot" << "pwd" << conn.createPasswordDigest( "eliot" , "bar" ) ) );
+ conn->runCommand( "test",
+ BSON( "createUser" << "eliot" <<
+ "pwd" << "bar" <<
+ "roles" << BSON_ARRAY("readWrite")),
+ ret);
errmsg.clear();
- bool ok = conn.auth( "test" , "eliot" , "bar" , errmsg );
- if ( ! ok )
- cout << errmsg << endl;
- MONGO_verify( ok );
+ conn->auth(BSON("user" << "eliot" <<
+ "db" << "test" <<
+ "pwd" << "bar" <<
+ "mechanism" << "MONGODB-CR"));
- MONGO_verify( ! conn.auth( "test" , "eliot" , "bars" , errmsg ) );
+ try {
+ conn->auth(BSON("user" << "eliot" <<
+ "db" << "test" <<
+ "pwd" << "bars" << // incorrect password
+ "mechanism" << "MONGODB-CR"));
+ // Shouldn't get here.
+ cout << "Authentication with invalid password should have failed but didn't" << endl;
+ return EXIT_FAILURE;
+ } catch (const DBException& e) {
+ // expected
+ }
return EXIT_SUCCESS;
}