Skip to content
Snippets Groups Projects
Commit e5c6acdb authored by Elemer Lelik's avatar Elemer Lelik
Browse files

Source files added

parent 9f0fdec5
No related branches found
No related tags found
No related merge requests found
// ==================================================================================
// Copyright (c) 2017 Ericsson AB
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
// ==================================================================================
// Contributors:
// Krisztian Gulyas - initial implementation and initial documentation
//
// File: MongoDBTest.ttcn
// Rev: R1A
// Prodnr: CNL 0
// ==================================================================================
module MongoDBTest
{
// ==============================================================================
//
// Import(s)
//
// ==============================================================================
// mongoDB prototol messages and helper functions
import from MongoDB_Types all;
import from MongoDB_Functions all;
// import basic defitions for TCP/IP communication
import from IPL4asp_Types all;
import from IPL4asp_PortType all;
import from SimpleTCP all;
// ==============================================================================
//
// Module parameter(s)
//
// ==============================================================================
// local and remote TCP host/port
modulepar { SimpleTCP.ConnectionData LocalTCPConnection, RemoteTCPConnection }
// ==============================================================================
//
// Component(s)
//
// ==============================================================================
// test component type definition
type component testComponent
{
port IPL4asp_PT testPort;
}
// ==============================================================================
//
// Additional types and functions
//
// ==============================================================================
// Wrapper function for MongoDB message length function
function IPL4asp_MsgLen(in octetstring stream, inout ro_integer args) return integer {
var integer l := MsgLen(stream)
log (" [::] TCP (mongoDB) message length: ", l);
return l;
}
// ------------------------------------------------------------------------------
// Generic function testing mongoDB wire protocol (using testComponent):
//
// 1) encoding the given mongoDB wire message
// 2) sending message (via TCP)
// 3) if necessary:
// - catching reply message
// - decoding and evaluating it
//
// parameters:
// - Msg mongoDB message template
// - expectResponse expect response message [true/false]
// ------------------------------------------------------------------------------
function testMongoDB (
in template Msg mongo_msg, // mongoDB test message template
boolean expectResponse) // expect response message [true/false]
runs on testComponent
{
var octetstring pduOut, pduIn;
@try {
// trying to encode the outgoing message
pduOut := encMsg(valueof(mongo_msg));
}
@catch(err) {
log("[!!] Unable to encode the outgoing message | error: ", err);
setverdict(fail);
}
// logging the encoded message (encoder updates some fields)
log("[=>] MongoDB message encoded and sent: ", decMsg(pduOut));
if (SimpleTCP.sendReceiveMsg (
testPort,
refers(IPL4asp_MsgLen),
LocalTCPConnection,
RemoteTCPConnection,
pduOut,
pduIn,
expectResponse)) {
if (expectResponse) {
// initalize a mongoDB query message
var Msg mongo_reply_msg;
// trying to decode the incomming message
@try {
mongo_reply_msg := decMsg(pduIn);
var JSONRecords json;
var integer error_code := bsonStream2json(mongo_reply_msg.reply_.documents.octets, json);
// check return value of bson stream conversion
if (error_code != 0) {
setverdict(fail);
}
else {
mongo_reply_msg.reply_.documents.json := json;
}
}
@catch(err) {
log("[!!] Unable to decode incomming message | error: ", err);
setverdict(fail);
}
// compare received message with reply template
if (match(mongo_reply_msg, replyMsgTemplate)) {
log("[<=] MongoDB reply message received: ", mongo_reply_msg);
setverdict(pass);
}
else {
log("[<=] Response does not match.");
setverdict(fail);
}
}
else {
setverdict(pass);
}
}
else {
// TCP communication error
setverdict(fail);
}
}
// ==============================================================================
//
// mongoDB message template definitions (based on the message definitions)
//
// ==============================================================================
// ------------------------------------------------------------------------------
// mongoDB insert template
// ------------------------------------------------------------------------------
template Msg insertMsgTemplate (
universal charstring fullConnectionName_,
integer flag_bytes_,
octetstring documents_
) :=
{
insert := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED },
flags := { bytes := flag_bytes_ },
fullCollectionName := fullConnectionName_,
documents := documents_
}
}
// ------------------------------------------------------------------------------
// mongoDB update template
// ------------------------------------------------------------------------------
template Msg updateMsgTemplate (
universal charstring fullConnectionName_,
integer flag_bytes_,
octetstring selector_,
octetstring update_
) :=
{
update := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED },
ZERO := 0,
fullCollectionName := fullConnectionName_,
flags := { bytes := flag_bytes_ },
selector := selector_,
update := update_
}
}
// ------------------------------------------------------------------------------
// mongoDB query template
// ------------------------------------------------------------------------------
template Msg queryMsgTemplate (
universal charstring fullConnectionName_,
integer flag_bytes_,
integer numberToSkip_,
integer numberToReturn_,
octetstring query_
) :=
{
query := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED},
flags := { bytes := flag_bytes_ },
fullCollectionName := fullConnectionName_,
numberToSkip := numberToSkip_,
numberToReturn := numberToReturn_,
query := query_,
returnFieldsSelector := omit
}
}
// ------------------------------------------------------------------------------
// mongoDB get more template
// ------------------------------------------------------------------------------
template Msg getMoreMsgTemplate (
universal charstring fullConnectionName_,
integer numberToReturn_,
integer cursorID_
) :=
{
getMore := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED},
ZERO := 0,
fullCollectionName := fullConnectionName_,
numberToReturn := numberToReturn_,
cursorID := cursorID_
}
}
// ------------------------------------------------------------------------------
// mongoDB delete template
// ------------------------------------------------------------------------------
template Msg deleteMsgTemplate (
universal charstring fullConnectionName_,
integer flag_bytes_,
octetstring selector_
) :=
{
delete := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED },
ZERO := 0,
fullCollectionName := fullConnectionName_,
flags := { bytes := flag_bytes_ },
selector := selector_
}
}
// ------------------------------------------------------------------------------
// mongoDB kill cursor template
// ------------------------------------------------------------------------------
// record of integer
type record of integer roInteger;
template Msg killCursorMsgTemplate (
integer numberOfCursorIDs_,
roInteger cursorID_
) :=
{
killCursor := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED },
ZERO := 0,
numberOfCursorIDs := numberOfCursorIDs_,
cursorIDs := cursorID_
}
}
// ------------------------------------------------------------------------------
// mongoDB reply message template
// ------------------------------------------------------------------------------
template Msg replyMsgTemplate :=
{
reply_ := {
header := { messageLength := ?, requestId := ?, responseTo := ?, opCode := OP_REPLY },
responseFlags := ?,
cursorID := ?,
startingFrom := ?,
numberReturned := ?,
documents := ?
}
}
// ==============================================================================
//
// Test cases
//
// ==============================================================================
// ------------------------------------------------------------------------------
// Testing MongoDB insert message (no reply message)
// ------------------------------------------------------------------------------
testcase TC_Insert(in universal charstring data) runs on testComponent
{
log("===================================================================================");
log("[::] TC: Insert message test");
map(mtc:testPort, system:testPort);
testMongoDB(
insertMsgTemplate(
"test.ttcn", // dbname.collectionname
1, // insert flags as byte
// one or more BSON documents to insert into the collection
json2bson("{ \"command\": \"insert\", \"data\": " & data & "}")
),
false);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB update message (no reply message)
// ------------------------------------------------------------------------------
testcase TC_Update() runs on testComponent
{
log("===================================================================================");
log("[::] TC Update message test");
map(mtc:testPort, system:testPort);
testMongoDB(
updateMsgTemplate(
"test.ttcn", // dbname.collectionname
1, // update flags as byte
// BSON document that specifies the query for selection of the document to update
json2bson("{}"),
// BSON document that specifies the update to be performed
json2bson("{ \"command\": \"update\", \"data\": \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \"}")
),
false);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB query message
// ------------------------------------------------------------------------------
testcase TC_Query() runs on testComponent
{
log("===================================================================================");
log("[::] TC: Query message test");
map(mtc:testPort, system:testPort);
testMongoDB(
queryMsgTemplate(
"test.ttcn", // dbname.collectionname
0, // query flags as byte
0, // number of documents to skip
0, // number of documents to return in the first OP_REPLY batch
// BSON document that represents the query
json2bson("{}")
),
true);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB get more message
// ------------------------------------------------------------------------------
testcase TC_GetMore() runs on testComponent
{
log("===================================================================================");
log("[::] TC: Get More message test");
map(mtc:testPort, system:testPort);
testMongoDB(
getMoreMsgTemplate(
"test.ttcn", // dbname.collectionname
0, // number of documents to return
0 // cursor id from OP_REPLY
),
true);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB delete message (no reply message)
// ------------------------------------------------------------------------------
testcase TC_Delete() runs on testComponent
{
log("===================================================================================");
log("[::] TC: Delete message test");
map(mtc:testPort, system:testPort);
testMongoDB(
deleteMsgTemplate(
"test.ttcn", // dbname.collectionname
0, // query flags as byte
// BSON document that represent the query used to select the documents to be removed.
json2bson("{\"data\": 17}")
),
false);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB delete message (no reply message)
// ------------------------------------------------------------------------------
testcase TC_KillCursor() runs on testComponent
{
log("===================================================================================");
log("[::] TC: KillCursor message test");
map(mtc:testPort, system:testPort);
testMongoDB(valueof(
killCursorMsgTemplate(
1, // number of cursorIDs in message
{1, 2, 3} // sequence of cursorIDs
)),
false);
log("");
}
// ==============================================================================
//
// Run the following testcases
//
// ==============================================================================
control
{
// insert 20 records
for (var integer i := 0; i < 20; i := i + 1) {
execute(TC_Insert(int2str(i)));
}
execute(TC_Update());
execute(TC_Query());
execute(TC_GetMore());
execute(TC_Delete());
execute(TC_KillCursor());
execute(TC_Query());
}
}
mongoDB protocol module test info
________________________________________________________________________
1) Prerequisites
________________________________________________________________________
-mongoDB access (local or remote)
-wireshark (https://www.wireshark.org/) [optional]
-https://github.com/eclipse/titan.TestPorts.Common_Components.Socket-API
-https://github.com/eclipse/titan.TestPorts.IPL4asp
________________________________________________________________________
2) Prepare your TTCN mongoDB test
________________________________________________________________________
- unpack mongoDBTest.zip file, then create symbolic links with:
> cd MongoDBTest/bin
> ../src/install.script
- set up your mongoDB connection parameters (host/port) in
cfg/mongoDB.cfg file:
...
// Local TCP connection address
LocalTCPConnection := { "127.0.0.1", 3000 };
// Remote TCP connection address (mongoDB database)
RemoteTCPConnection := { "127.0.0.1", 27017 };
...
________________________________________________________________________
3) Build and run test(s)
________________________________________________________________________
> make
> ./mongoDBTest ../cfg/mongoDB.cfg
________________________________________________________________________
Additional info/help
________________________________________________________________________
- The Makefile was generated with:
> makefilegen -s -e mongoDBTest *.ttcn *.cc *.hh
- Installing mongoDB:
https://docs.mongodb.com/manual/administration/install-community/
// ==================================================================================
// Copyright (c) 2017 Ericsson AB
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
// ==================================================================================
// Contributors:
// Krisztian Gulyas - initial implementation and initial documentation
//
// File: SimpleTCP.ttcn
// Rev:
// Prodnr:
// ==================================================================================
// ==============================================================================
//
// Helper module for simple TCP/IP communication using the IPL4asp library
//
// ==============================================================================
module SimpleTCP
{
// import basic definitions of TCP/IP communication
import from IPL4asp_Types all;
import from IPL4asp_PortType all;
// basic TCP connection data type
type record ConnectionData {
charstring host, // host name [string]
integer portNumber // port number [integer]
}
// ------------------------------------------------------------------------------
// Helper function for simple TCP message send and receive
//
// 0) Register message lenght callback (function)
// 1) Initalize TCP connection
// 2) Sending out TCP message with given PDU
// 3) if necessary: catching reply message, get PDU
//
// parameters:
// - testPort // IPL4asp_PT type TCP/IP testport [IPL4asp_PT]
// - msgLenCallback // message length function callback
// - local // local host/port [ConnectionData]
// - remote // remote host/port [ConnectionData]
// - pduOut // these octets will be sent out as the part of TCP message [octets]
// - pduIn // these octets are received from incomming TCP message [octets]
// - expectResponse // expected response message from host/port [true/false]
// return:
// - TCP communication was success [true/false]
//
// ------------------------------------------------------------------------------
function sendReceiveMsg (
IPL4asp_PT testPort, // IPL4asp_PT type TCP/IP testport [IPL4asp_PT]
f_IPL4_getMsgLen msgLenCallback,
ConnectionData local,
ConnectionData remote,
in octetstring pduOut,
inout octetstring pduIn,
boolean expectResponse
) return boolean // TCP commution result
{
var boolean res := false; // send/receive result, default = false
var Result connectionResult, closingResult;
// register message lenght callback
f_IPL4_setGetMsgLen(testPort, -1, msgLenCallback, {});
// connect to the socket and check result
connectionResult := f_IPL4_connect(
testPort,
remote.host, remote.portNumber,
local.host, local.portNumber,
-1,
{ tcp := {}},
{
{ reuseAddress := {enable := true} }
});
if (ispresent(connectionResult.errorCode)) {
log(" [!!] TCP port connection error: ", connectionResult);
}
else {
// successfully connected to the given host/port
log(" [::] TCP port [", remote.host, "/", remote.portNumber, "] connected.");
// send and receive TCP message
var ASP_SendTo tcp_msg_send;
var ASP_RecvFrom tcp_msg_received;
// Timer with 0.1 s duration
timer T := 0.1;
// set up TCP message
tcp_msg_send := { connectionResult.connId, remote.host, remote.portNumber, { tcp := {} }, pduOut }
// send out TCP message to the given port
testPort.send(tcp_msg_send);
log(" [=>] TCP message sent...");
// process received message if needed
if (expectResponse) {
// start timer with 0.1 s
T.start;
alt
{
[] testPort.receive(ASP_RecvFrom : ?) -> value tcp_msg_received
{
log(" [<=] TCP message received ");
pduIn := tcp_msg_received.msg;
res := true;
}
[] T.timeout {
log(" [!!] Timeout (no response) ");
}
}
}
else {
log(" [::] Expecting no response");
res := true;
}
// close the connection and check result
closingResult := f_IPL4_close(testPort, connectionResult.connId, { unspecified := {} });
if (ispresent(closingResult.errorCode)) {
log(" [!!] TCP port closing error: ", connectionResult.errorCode);
}
else {
log(" [::] TCP port [", remote.host, "/", remote.portNumber, "] closed");
}
// 0.1 second delay (proper closing of TCP operations)
T.start; T.timeout;
}
return res;
}
}
// Module parameters
[MODULE_PARAMETERS]
// Local TCP connection address
LocalTCPConnection := { "127.0.0.1", 3000 };
// Remote TCP connection address (mongoDB database)
RemoteTCPConnection := { "127.0.0.1", 27017 };
// Logging settings
[LOGGING]
LogFile := "logs/%e.%h-%r.%s"
FileMask := LOG_ALL | DEBUG | MATCHING
ConsoleMask := ERROR | WARNING | TESTCASE | STATISTICS | PORTEVENT
LogSourceInfo := No
AppendFile := No
TimeStampFormat := DateTime
LogEventTypes := Yes
SourceInfoFormat := None
LogEntityName := Yes
MongoDB protocol module
Based on: MongoDB Wire Protocol — MongoDB Manual 3.4
https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/
// ==================================================================================
// Copyright (c) 2017 Ericsson AB
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
// ==================================================================================
// Contributors:
// Krisztian Gulyas - initial implementation and initial documentation
//
// File: MongoDBProtocolHelper.ttcn
// Rev: R1A
// Prodnr: CNL 0
// ==================================================================================
module MongoDB_Functions
{
// ==============================================================================
//
// mongoDB wire protocol helper definitions
// please refer to MongoDBProtocol.ttcn for MongoDBProtocol defitions
//
// ==============================================================================
import from MongoDB_Types all; // mongoDB protocol description
// ------------------------------------------------------------------------------
// Helper function for TCP protocol, returns with the length of message
//
// - first 4 octets (32 bits) (of any MongoDB message) shows the length of
// the message (please refer to the MsgHeader definition in MongoDBProtocol)
//
// - definition of int32 (raw encoding/decoding) provides a simple decoding
// function (please refer to the definition of int32 in MongoDBProtocol)
//
// parameters:
// - stream octets
// return:
// - length of the message (read it from the message header)
//
// ------------------------------------------------------------------------------
function MsgLen(in octetstring stream) return integer {
return decInt32(substr(stream, 0, 4));
}
// ------------------------------------------------------------------------------
// Helper function to serialize a BSON stream.
//
// Finds and converts each BSON documents (of the given stream) to a JSON string.
// parameters:
// - json array of the converted JSON string
// return:
// - 0 no error
// - 1 BSON to JSON conversion error (error type logged)
// - 2 buffer cut error (error type logged)
//
// ------------------------------------------------------------------------------
function bsonStream2json(in octetstring stream, inout JSONRecords json) return integer {
var integer streamLength := lengthof(stream),
noDocs := 0,
docLength;
// initalize records
json := {""};
while (streamLength > 0) {
// length of BSON document
docLength := decInt32(substr(stream, 0, 4));
// convert bson octects to json string
@try {
json[noDocs] := bson2json(substr(stream, 0, docLength));
}
@catch(err) {
log("[!!] Unable to encode bson message | error: ", err);
return 1;
}
// cut current octects, update number of docs and stream length
@try {
stream := substr(stream, docLength, streamLength - docLength);
}
@catch(err) {
log("[!!] Unable to cut buffer properly | error: ", err);
return 2;
}
noDocs := noDocs + 1;
streamLength := lengthof(stream);
}
log ("[::] " & int2str(noDocs) & " documents serialized from the incomming BSON stream");
return 0;
}
} with { encode "RAW" }
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment