diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e9d16e..dbbe7ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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() diff --git a/package.json b/package.json index 6f63202..cd5c134 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/db2ia/dbconn.cc b/src/db2ia/dbconn.cc index e2e296f..e03ad3b 100644 --- a/src/db2ia/dbconn.cc +++ b/src/db2ia/dbconn.cc @@ -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 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 arg1Vec(arg1.begin(), arg1.end()); - // arg1Vec.push_back('\0'); - - std::string arg2 = Napi::String(env, info[2]).Utf8Value(); - // std::vector 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 @@ -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); diff --git a/test/README.md b/test/README.md index b0cf1b0..860c4e9 100644 --- a/test/README.md +++ b/test/README.md @@ -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`. \ No newline at end of file + All Tests can be run by `npm test`. + Test connection with user name & password by `DBNAME=yourname DBPWD=password npm test`. \ No newline at end of file diff --git a/test/connectionTest.js b/test/connectionTest.js index 173d2f0..a9adde0 100644 --- a/test/connectionTest.js +++ b/test/connectionTest.js @@ -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', () => { @@ -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 @@ -116,7 +145,7 @@ describe('Connection Test', () => { result = connection.validStmt(sql); expect(result).to.equal(null); - } catch (e) { console.error(e); } + } catch (e) { } }); });