diff options
| author | Apollon Oikonomopoulos <apoikos@debian.org> | 2016-01-14 00:10:06 +0200 |
|---|---|---|
| committer | Apollon Oikonomopoulos <apollon@skroutz.gr> | 2016-01-14 00:10:06 +0200 |
| commit | 374e1947abcd3e127a2a613aff73ecffdb9199ea (patch) | |
| tree | d83973c3c9802450acd5b5e86fe0d4e8e60a3a1b /src/mongo/client/examples/authTest.cpp | |
| parent | 65585c90b12d6523bea75a2aebaae2a2fdf9e641 (diff) | |
Imported Upstream version 2.6.11upstream/2.6.11
Diffstat (limited to 'src/mongo/client/examples/authTest.cpp')
| -rw-r--r-- | src/mongo/client/examples/authTest.cpp | 46 |
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; } |
