Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions source/jst_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ static duk_ret_t session_create(duk_context *ctx)
char* session_id = NULL;

session_id = (char*)malloc(SESSION_ID_BYTES_LENGTH+1);
if(!session_id)
{
CosaPhpExtLog("Failed to allocate session_id!\n");
RETURN_FALSE;
}

n = syscall(SYS_getrandom, bytes, SESSION_ID_BYTES_LENGTH, 0);
if(n != SESSION_ID_BYTES_LENGTH)
{
Expand All @@ -177,16 +183,27 @@ static duk_ret_t session_create(duk_context *ctx)
session_id[i] = BYTE_TO_PRINTABLE_HEX_CODE(bytes[i]);
}

if(session_identifier)
{
char filename[SESSION_FILE_MAX_PATH];
snprintf(filename, SESSION_FILE_MAX_PATH, "%s/%s", SESSION_TMP_DIR, session_identifier);
unlink(filename);
free(session_identifier);
session_identifier = NULL;
}
Comment thread
pavankumar464 marked this conversation as resolved.

session_identifier = (char*)malloc(SESSION_ID_LENGTH+1);
if(!session_identifier)
{
CosaPhpExtLog("Failed to allocate session_identifier!\n");
free(session_id);
RETURN_FALSE;
}
memset(session_identifier, 0, SESSION_ID_LENGTH+1);

session_id[SESSION_ID_BYTES_LENGTH] = '\0';
snprintf(session_identifier, SESSION_ID_LENGTH+1, "%s%s", SESSION_PREFIX, session_id);
free(session_id);

RETURN_TRUE;
return 1;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ add_executable(
parser_test
../tests/parser_test.cpp
../source/jst_parser.c
../source/jst_session.c
../source/jst_internal.c
../source/duktape/duktape.c)
target_link_libraries(parser_test libgtest libgmock -pthread)
Expand Down
84 changes: 84 additions & 0 deletions tests/parser_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
If not stated otherwise in this file or this component's Licenses.txt file the

Check failure on line 2 in tests/parser_test.cpp

View workflow job for this annotation

GitHub Actions / call-fossid-workflow / Fossid Annotate PR

FossID Detected License Issue

Snippet with 'Apache-2.0' file license found (103 lines) Location: tests/parser_test.cpp (link unavailable) Component: rdk/components/generic/jst/rdk/components/generic/jst@rdk-dev-2101 Download: https://code.rdkcentral.com/r/plugins/gitiles/rdk/components/generic/jst/+archive/rdk-dev-2101.tar.gz
following copyright and licenses apply:

Copyright 2018 RDK Management
Expand All @@ -20,12 +20,17 @@
#include <string>
#include <fstream>
#include <streambuf>
#include <cstring>
#include "jst.h"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

extern "C" {
duk_ret_t ccsp_session_module_open(duk_context *ctx);
}
Comment thread
pavankumar464 marked this conversation as resolved.

using namespace std;

class BufferFreer
Expand Down Expand Up @@ -106,6 +111,85 @@
}
}

TEST(general, session_create_multiple_calls_succeed)
{
duk_context* ctx = duk_create_heap_default();
ASSERT_NE(ctx, nullptr);

duk_push_c_function(ctx, ccsp_session_module_open, 0);
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
duk_put_global_string(ctx, "ccsp_session");
Comment thread
Copilot marked this conversation as resolved.

duk_get_global_string(ctx, "ccsp_session");
duk_get_prop_string(ctx, -1, "create");
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
EXPECT_TRUE(duk_get_boolean(ctx, -1));
duk_pop_2(ctx);

duk_get_global_string(ctx, "ccsp_session");
duk_get_prop_string(ctx, -1, "create");
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
EXPECT_TRUE(duk_get_boolean(ctx, -1));
duk_pop_2(ctx);

duk_get_global_string(ctx, "ccsp_session");
duk_get_prop_string(ctx, -1, "destroy");
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
EXPECT_TRUE(duk_get_boolean(ctx, -1));
duk_pop_2(ctx);

duk_destroy_heap(ctx);
}

TEST(general, session_create_destroy_cycle_and_id_format)
{
duk_context* ctx = duk_create_heap_default();
ASSERT_NE(ctx, nullptr);

duk_push_c_function(ctx, ccsp_session_module_open, 0);
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
duk_put_global_string(ctx, "ccsp_session");
Comment thread
pavankumar464 marked this conversation as resolved.

duk_get_global_string(ctx, "ccsp_session");
duk_get_prop_string(ctx, -1, "create");
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
EXPECT_TRUE(duk_get_boolean(ctx, -1));
duk_pop_2(ctx);

duk_get_global_string(ctx, "ccsp_session");
duk_get_prop_string(ctx, -1, "getId");
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
const char* first_id = duk_get_string(ctx, -1);
ASSERT_NE(first_id, nullptr);
EXPECT_EQ(strlen(first_id), 40u);
EXPECT_EQ(strncmp(first_id, "jst_sess", 8), 0);

char first_session_file[128] = {0};
snprintf(first_session_file, sizeof(first_session_file), "/tmp/%s", first_id);
duk_pop_2(ctx);

FILE* stale = fopen(first_session_file, "w");
ASSERT_NE(stale, nullptr);
fclose(stale);
ASSERT_EQ(access(first_session_file, F_OK), 0);

duk_get_global_string(ctx, "ccsp_session");
duk_get_prop_string(ctx, -1, "create");
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
EXPECT_TRUE(duk_get_boolean(ctx, -1));
duk_pop_2(ctx);

EXPECT_NE(access(first_session_file, F_OK), 0);

duk_get_global_string(ctx, "ccsp_session");
duk_get_prop_string(ctx, -1, "destroy");
ASSERT_EQ(duk_pcall(ctx, 0), DUK_EXEC_SUCCESS);
EXPECT_TRUE(duk_get_boolean(ctx, -1));
duk_pop_2(ctx);

duk_destroy_heap(ctx);
}

int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);
Expand Down
Loading