void ConnectionDiagnosticThread::run()
    {
        boost::scoped_ptr<mongo::DBClientConnection> connection;

        try {
            connection.reset(new mongo::DBClientConnection);
            connection->connect(_connection->info());
            emit connectionStatus("", true);
        }
        catch(const mongo::UserException &ex) {
            const char *what = ex.what();
            emit connectionStatus(QString(what), false);
            emit completed();
            return;
        }

        try {
            if (_connection->hasEnabledPrimaryCredential())
            {
                CredentialSettings *credential = _connection->primaryCredential();
                std::string database = credential->databaseName();
                std::string username = credential->userName();
                std::string password = credential->userPassword();

                std::string errmsg;
                bool ok = connection->auth(database, username, password, errmsg);
                emit authStatus("", ok);
            }
        } catch (const mongo::UserException &) {
            emit authStatus("", false);
        }

        emit completed();
    }
//
// Test HTTP Basic with valid user name and password
//
void testBasicAuthSuccess()
{
    String authHeader;

    AuthenticationManager  authManager;

    AuthenticationInfo* authInfo = new AuthenticationInfo(true);

    String userPass = guestUser;
    userPass.append(":");
    userPass.append(guestPassword);

    authHeader.append(basicHeader);
    authHeader.append(encodeUserPass(userPass));

    Boolean authenticated;
    // test case looks for success, initialize with failure
    AuthenticationStatus authStatus(AUTHSC_UNAUTHORIZED);

    authStatus = authManager.performHttpAuthentication(authHeader, authInfo);
    authenticated = authStatus.isSuccess();

    if (verbose)
    {
        cout << "Authentication of user " + guestUser + " returned with: ";
        cout << authenticated << endl;
    }

    delete authInfo;

    //PEGASUS_TEST_ASSERT(authenticated);
}
//
// Perform http authentication
//
AuthenticationStatus AuthenticationManager::performHttpAuthentication(
    const String& authHeader,
    AuthenticationInfo* authInfo)
{
    PEG_METHOD_ENTER(TRC_AUTHENTICATION,
        "AuthenticationManager::performHttpAuthentication()");

    String authType;
    String cookie;

    //
    // Parse the HTTP authentication header for authentication information
    //
    if ( !HTTPMessage::parseHttpAuthHeader(authHeader, authType, cookie) )
    {
        PEG_TRACE((
            TRC_DISCARDED_DATA,
            Tracer::LEVEL1,
            "HTTPAuthentication failed. "
                "Malformed HTTP authentication header: %s",
            (const char*)authHeader.getCString()));
        PEG_METHOD_EXIT();
        return AuthenticationStatus(AUTHSC_UNAUTHORIZED);
    }

    AuthenticationStatus authStatus(AUTHSC_UNAUTHORIZED);

    //
    // Check the authenticationinformation and do the authentication
    //
    if ( String::equalNoCase(authType, "Basic") &&
         String::equal(_httpAuthType, "Basic") )
    {
        authStatus = _httpAuthHandler->authenticate(cookie, authInfo);
    }
#ifdef PEGASUS_KERBEROS_AUTHENTICATION
    else if ( String::equalNoCase(authType, "Negotiate") &&
              String::equal(_httpAuthType, "Kerberos") )
    {
        authStatus = _httpAuthHandler->authenticate(cookie, authInfo);
    }
#endif
    // FUTURE: Add code to check for "Digest" when digest
    // authentication is implemented.

    if ( authStatus.isSuccess() )
    {
        authInfo->setAuthType(authType);
    }

    PEG_METHOD_EXIT();

    return authStatus;
}
    void ConnectionDiagnosticDialog::completed()
    {
        if (!_connectionStatusReceived)
            connectionStatus("", false);

        if (!_authStatusReceived)
            authStatus("", false);

        // seems that it is wrong to call any method on thread,
        // because thread already can be disposed.
        // QThread *thread = static_cast<QThread *>(sender());
        // thread->quit();
    }
//
// Perform pegasus sepcific local authentication
//
AuthenticationStatus AuthenticationManager::performPegasusAuthentication(
    const String& authHeader,
    AuthenticationInfo* authInfo)
{
    PEG_METHOD_ENTER(TRC_AUTHENTICATION,
        "AuthenticationManager::performPegasusAuthentication()");

    AuthenticationStatus authStatus(AUTHSC_UNAUTHORIZED);

    String authType;
    String userName;
    String cookie;

    //
    // Parse the pegasus authentication header authentication information
    //
    if ( !HTTPMessage::parseLocalAuthHeader(authHeader,
              authType, userName, cookie) )
    {
        PEG_TRACE((
            TRC_DISCARDED_DATA,
            Tracer::LEVEL1,
            "PegasusAuthentication failed. "
                "Malformed Pegasus authentication header: %s",
            (const char*)authHeader.getCString()));
        PEG_METHOD_EXIT();
        return AuthenticationStatus(AUTHSC_UNAUTHORIZED);
    }

    // The HTTPAuthenticatorDelegator ensures only local authentication
    // requests get here.
    PEGASUS_ASSERT(authType == "Local");

    authStatus = _localAuthHandler->authenticate(cookie, authInfo);

    if ( authStatus.isSuccess() )
    {
        authInfo->setAuthType(authType);
    }

    PEG_METHOD_EXIT();

    return authStatus;
}
//
// Test local authentication
//
void testLocalAuthSuccess()
{
    String authHeader;

    AuthenticationManager  authManager;

    AuthenticationInfo* authInfo = new AuthenticationInfo(true);

    // Test valid header
    authHeader.append(localHeader);
    authHeader.append("\"");
    authHeader.append(testUser);
    authHeader.append("\"");

    String respHeader =
        authManager.getPegasusAuthResponseHeader(authHeader, authInfo);

    if (verbose) cout << "RespHeader: " << respHeader << endl;

    Uint32 startQuote = respHeader.find(0, '"');
    PEGASUS_TEST_ASSERT(startQuote != PEG_NOT_FOUND);

    Uint32 endQuote = respHeader.find(startQuote + 1, '"');
    PEGASUS_TEST_ASSERT(endQuote != PEG_NOT_FOUND);

    String filePath = respHeader.subString(
        startQuote + 1, (endQuote - startQuote - 1));
    PEGASUS_TEST_ASSERT(filePath.size() != 0);

    authHeader.clear();
    authHeader.append(localHeader);
    authHeader.append("\"");
    authHeader.append(testUser);
    authHeader.append(":");
    authHeader.append(filePath);
    authHeader.append(":");
    authHeader.append(authInfo->getLocalAuthSecret());
    authHeader.append("\"");

    if (verbose) cout << "Local Resp AuthHeader: " << authHeader << endl;

    Boolean authenticated;
    // test case looks for success, initialize with failure
    AuthenticationStatus authStatus(AUTHSC_UNAUTHORIZED);


    authStatus = authManager.performPegasusAuthentication(authHeader, authInfo);
    authenticated = authStatus.isSuccess();

    //
    // remove the auth file created for this user request
    //
    if (FileSystem::exists(filePath))
    {
        FileSystem::removeFile(filePath);
    }
    if (verbose)
    {
        cout << "Local Authentication of User " + testUser + " returned with: ";
        cout << authenticated << endl;
    }

    delete authInfo;

    PEGASUS_TEST_ASSERT(authenticated);
}