Skip to content

Commit

Permalink
dbconn: fix the connection failure in #52
Browse files Browse the repository at this point in the history
  • Loading branch information
dmabupt committed Jan 14, 2019
2 parents 59439d2 + 16edf3d commit c20f37c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# idb-connector changelog

## 1.1.7

- Fixed the connection failure(#52) with user/password provided
- Updated some test cases

## 1.1.6

- Added alias shorthands for use during bindParams()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "idb-connector",
"version": "1.1.6",
"version": "1.1.7",
"description": "A Node.js DB2 driver for IBM i",
"main": "lib/db2a.js",
"directories": {
Expand Down
26 changes: 8 additions & 18 deletions src/db2ia/dbconn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,26 +222,12 @@ void DbConn::Conn(const Napi::CallbackInfo& info) {
Napi::Error::New(env, "conn() takes either 1,2,3, or 4 parameters").ThrowAsJavaScriptException();
return;
}
std::string arg0 = Napi::String(env, info[0]).Utf8Value();
// std::vector<char> arg0Vec(arg0.begin(), arg0.end());
// arg0Vec.push_back('\0'); // may lead to "Authorization failure on distributed database connection attempt"

SQLCHAR* datasource = &arg0[0u];
SQLCHAR* datasource = strdup(Napi::String(env, info[0]).Utf8Value().c_str());
SQLCHAR* loginuser = NULL;
SQLCHAR* password = NULL;

if(length >= 3) {

std::string arg1 = Napi::String(env, info[1]).Utf8Value();
// std::vector<char> arg1Vec(arg1.begin(), arg1.end());
// arg1Vec.push_back('\0');

std::string arg2 = Napi::String(env, info[2]).Utf8Value();
// std::vector<char> arg2Vec(arg2.begin(), arg2.end());
// arg2Vec.push_back('\0');

loginuser = &arg1[0u];
password = &arg2[0u];
loginuser = strdup(Napi::String(env, info[1]).Utf8Value().c_str());
password = strdup(Napi::String(env, info[2]).Utf8Value().c_str());
}
// Doc https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/cli/rzadpfnconn.htm
sqlReturnCode = SQLConnect(this->connh, //SQLHDBC Connection Handle
Expand All @@ -253,7 +239,11 @@ void DbConn::Conn(const Napi::CallbackInfo& info) {
SQL_NTS );//SQLSMALLINT cbAuthStr Length of Contents of szAuthStr

DEBUG(this, "SQLConnect(%d): conn obj [%p] handler [%d]\n", sqlReturnCode, this, this->connh);

free(datasource);
if(length >= 3) {
free(loginuser);
free(password);
}
if(sqlReturnCode != SQL_SUCCESS) {
this->throwErrMsg(SQL_HANDLE_DBC, env);
SQLFreeConnect(this->connh);
Expand Down
3 changes: 2 additions & 1 deletion test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

Specific Tests can be run by `npm test path_to_test`.
For example when inside the test directory you can run `npm test connectionTest`.
All Tests can be run by `npm test`.
All Tests can be run by `npm test`.
Test connection with user name & password by `DBNAME=yourname DBPWD=password npm test`.
31 changes: 30 additions & 1 deletion test/connectionTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const expect = require('chai').expect;
const db2a = require('../lib/db2a');
const {dbconn} = db2a;
const username = process.env.DBNAME;
const password = process.env.DBPWD;

// Test connection Class
describe('Connection Test', () => {
Expand Down Expand Up @@ -31,6 +33,33 @@ describe('Connection Test', () => {
expect(result).to.be.false;
});
});
if(username && password) {
it('disconnects an exsisting connection to the datbase with user/pwd.', () => {
// Test with username/password
let connection = new dbconn(),
result = connection.conn("*LOCAL", username, password);
result = connection.isConnected();
expect(result).to.be.true;

result = connection.disconn();
expect(result).to.be.true;

result = connection.isConnected();
expect(result).to.be.false;

// Test the callback style
connection.conn("*LOCAL", username, password, () => {
result = connection.isConnected();
expect(result).to.be.true;

result = connection.disconn();
expect(result).to.be.true;

result = connection.isConnected();
expect(result).to.be.false;
});
});
}
});

//if successful returns String or Int depending on attribute
Expand Down Expand Up @@ -116,7 +145,7 @@ describe('Connection Test', () => {
result = connection.validStmt(sql);

expect(result).to.equal(null);
} catch (e) { console.error(e); }
} catch (e) { }
});
});

Expand Down

0 comments on commit c20f37c

Please sign in to comment.