Skip to content

Commit 1f21c4b

Browse files
authored
Add environment variable check utility function (#290)
1 parent 15b96b5 commit 1f21c4b

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

src/utils/include/com/amazonaws/kinesis/video/utils/Include.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,15 @@ PUBLIC_API STATUS threadpoolTryAdd(PThreadpool, startRoutine, PVOID);
20052005
*/
20062006
PUBLIC_API STATUS threadpoolPush(PThreadpool, startRoutine, PVOID);
20072007

2008+
/**
2009+
* @brief Checks if an environment variable is enabled.
2010+
*
2011+
* @param - PCHAR - IN - The label of the environment variable to check for.
2012+
*
2013+
* @return - BOOL - TRUE if the environment variable is enabled, FALSE otherwise.
2014+
*/
2015+
PUBLIC_API BOOL isEnvVarEnabled(PCHAR);
2016+
20082017
#ifdef __cplusplus
20092018
}
20102019
#endif

src/utils/src/Environment.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "Include_i.h"
2+
3+
BOOL isEnvVarEnabled(PCHAR envVarName)
4+
{
5+
ENTERS();
6+
STATUS retStatus = STATUS_SUCCESS;
7+
BOOL retBool = FALSE;
8+
9+
// Null or empty envVarName to GETENV is undefined behavior.
10+
CHK_ERR(envVarName != NULL && envVarName[0] != '\0', STATUS_NULL_ARG, "Environment variable name is NULL or empty.");
11+
12+
PCHAR envVarVal = GETENV(envVarName);
13+
14+
// Case-insensitive comparisons.
15+
retBool = envVarVal != NULL && (STRCMPI(envVarVal, "1") == 0 || STRCMPI(envVarVal, "true") == 0 || STRCMPI(envVarVal, "on") == 0);
16+
17+
CleanUp:
18+
CHK_LOG_ERR(retStatus);
19+
LEAVES();
20+
return retBool;
21+
}

src/utils/src/FileIo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ STATUS readFile(PCHAR filePath, BOOL binMode, PBYTE pBuffer, PUINT64 pSize)
1919

2020
CHK(filePath != NULL && pSize != NULL, STATUS_NULL_ARG);
2121

22-
DLOGD("Opening file: %s", filePath);
22+
DLOGV("Opening file: %s", filePath);
2323
fp = FOPEN(filePath, binMode ? "rb" : "r");
2424

2525
CHK(fp != NULL, STATUS_OPEN_FILE_FAILED);
@@ -87,7 +87,7 @@ STATUS readFileSegment(PCHAR filePath, BOOL binMode, PBYTE pBuffer, UINT64 offse
8787

8888
CHK(filePath != NULL && pBuffer != NULL && readSize != 0, STATUS_NULL_ARG);
8989

90-
DLOGD("Opening file: %s", filePath);
90+
DLOGV("Opening file: %s", filePath);
9191
fp = FOPEN(filePath, binMode ? "rb" : "r");
9292

9393
CHK(fp != NULL, STATUS_OPEN_FILE_FAILED);
@@ -148,7 +148,7 @@ STATUS writeFileWithLogging(PCHAR filePath, BOOL binMode, BOOL append, PBYTE pBu
148148

149149
CHK(filePath != NULL && pBuffer != NULL, STATUS_NULL_ARG);
150150

151-
DLOGD("Opening file: %s", filePath);
151+
DLOGV("Opening file: %s", filePath);
152152
fp = FOPEN(filePath, binMode ? (append ? "ab" : "wb") : (append ? "a" : "w"));
153153

154154
CHK(fp != NULL, STATUS_OPEN_FILE_FAILED);
@@ -222,7 +222,7 @@ STATUS updateFile(PCHAR filePath, BOOL binMode, PBYTE pBuffer, UINT64 offset, UI
222222

223223
CHK(filePath != NULL && pBuffer != NULL, STATUS_NULL_ARG);
224224

225-
DLOGD("Opening file: %s", filePath);
225+
DLOGV("Opening file: %s", filePath);
226226
fp = FOPEN(filePath, binMode ? "rb+" : "r+");
227227

228228
CHK(fp != NULL, STATUS_OPEN_FILE_FAILED);

tst/utils/Environment.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "UtilTestFixture.h"
2+
3+
class EnvironmentFunctionalityTest : public UtilTestBase {};
4+
5+
TEST_F(EnvironmentFunctionalityTest, CheckTrueCases)
6+
{
7+
std::string envVar = "KVS_TEST_ENV_VAR_TRUE";
8+
9+
#ifdef _WIN32
10+
CHAR envBuf[256];
11+
#endif
12+
13+
// Common potential true values to test.
14+
std::vector<std::string> trueValues = {
15+
"1", "true", "True", "TRUE", "on", "On", "ON",
16+
};
17+
18+
for (std::string val : trueValues) {
19+
// Unset env var first.
20+
#ifdef _WIN32
21+
SNPRINTF(envBuf, sizeof(envBuf), "%s=", envVar.c_str());
22+
_putenv(envBuf);
23+
#else
24+
unsetenv(envVar.c_str());
25+
#endif
26+
27+
// Check unset case.
28+
EXPECT_FALSE(isEnvVarEnabled((PCHAR) envVar.c_str()));
29+
30+
// Set to the TRUE value.
31+
#ifdef _WIN32
32+
SNPRINTF(envBuf, sizeof(envBuf), "%s=%s", envVar.c_str(), val.c_str());
33+
_putenv(envBuf);
34+
#else
35+
setenv(envVar.c_str(), val.c_str(), 1);
36+
#endif
37+
38+
EXPECT_TRUE(isEnvVarEnabled((PCHAR) envVar.c_str()));
39+
}
40+
41+
// Cleanup the test environment variable.
42+
#ifdef _WIN32
43+
SNPRINTF(envBuf, sizeof(envBuf), "%s=", envVar.c_str());
44+
_putenv(envBuf);
45+
#else
46+
unsetenv(envVar.c_str());
47+
#endif
48+
}
49+
50+
TEST_F(EnvironmentFunctionalityTest, CheckFalseCases)
51+
{
52+
std::string envVar = "KVS_TEST_ENV_VAR_FALSE";
53+
54+
#ifdef _WIN32
55+
CHAR envBuf[256];
56+
#endif
57+
58+
// Common potential false values to test, including near-true ones.
59+
std::vector<std::string> falseValues = {
60+
"0", "false", "off", "random_string", "", " ", "tru", "rue", "onn", "yes", "no", "2", "-1",
61+
};
62+
63+
for (std::string val : falseValues) {
64+
// Unset env var first.
65+
#ifdef _WIN32
66+
SNPRINTF(envBuf, sizeof(envBuf), "%s=", envVar.c_str());
67+
_putenv(envBuf);
68+
#else
69+
unsetenv(envVar.c_str());
70+
#endif
71+
72+
// Check unset case.
73+
EXPECT_FALSE(isEnvVarEnabled((PCHAR) envVar.c_str()));
74+
75+
// Set to the false value
76+
#ifdef _WIN32
77+
SNPRINTF(envBuf, sizeof(envBuf), "%s=%s", envVar.c_str(), val.c_str());
78+
_putenv(envBuf);
79+
#else
80+
setenv(envVar.c_str(), val.c_str(), 1);
81+
#endif
82+
83+
EXPECT_FALSE(isEnvVarEnabled((PCHAR) envVar.c_str()));
84+
}
85+
86+
// Null variable name check.
87+
EXPECT_FALSE(isEnvVarEnabled((PCHAR) NULL));
88+
89+
// Empty variable name check.
90+
EXPECT_FALSE(isEnvVarEnabled((PCHAR) ""));
91+
92+
// Cleanup the test environment variable.
93+
#ifdef _WIN32
94+
SNPRINTF(envBuf, sizeof(envBuf), "%s=", envVar.c_str());
95+
_putenv(envBuf);
96+
#else
97+
unsetenv(envVar.c_str());
98+
#endif
99+
}

0 commit comments

Comments
 (0)