Skip to content

Commit 7632c9e

Browse files
committed
C++: Add test cases involving strings and comparisons.
1 parent 2e236dd commit 7632c9e

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-327/BrokenCryptoAlgorithm.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
| test2.cpp:49:4:49:24 | call to my_des_implementation | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test2.cpp:304:5:304:19 | call to doDesEncryption | call to doDesEncryption |
1414
| test2.cpp:49:4:49:24 | call to my_des_implementation | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test2.cpp:305:9:305:23 | call to doDesEncryption | call to doDesEncryption |
1515
| test2.cpp:49:4:49:24 | call to my_des_implementation | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test2.cpp:321:2:321:57 | ALGO_DES | invocation of macro ALGO_DES |
16+
| test2.cpp:49:4:49:24 | call to my_des_implementation | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test2.cpp:336:24:336:42 | ENCRYPTION_DES_NAME | invocation of macro ENCRYPTION_DES_NAME |
17+
| test2.cpp:49:4:49:24 | call to my_des_implementation | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test2.cpp:358:24:358:43 | call to getEncryptionNameDES | call to getEncryptionNameDES |
18+
| test2.cpp:49:4:49:24 | call to my_des_implementation | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test2.cpp:373:10:373:29 | call to getEncryptionNameDES | call to getEncryptionNameDES |
19+
| test2.cpp:49:4:49:24 | call to my_des_implementation | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test2.cpp:383:42:383:49 | ALGO_DES | invocation of macro ALGO_DES |
1620
| test.cpp:38:2:38:31 | ENCRYPT_WITH_DES(data,amount) | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test.cpp:38:2:38:31 | ENCRYPT_WITH_DES(data,amount) | invocation of macro ENCRYPT_WITH_DES |
1721
| test.cpp:38:2:38:31 | ENCRYPT_WITH_DES(data,amount) | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test.cpp:39:2:39:31 | ENCRYPT_WITH_RC2(data,amount) | invocation of macro ENCRYPT_WITH_RC2 |
1822
| test.cpp:38:2:38:31 | ENCRYPT_WITH_DES(data,amount) | This file makes use of a broken or weak cryptographic algorithm (specified by $@). | test.cpp:41:2:41:32 | ENCRYPT_WITH_3DES(data,amount) | invocation of macro ENCRYPT_WITH_3DES |

cpp/ql/test/query-tests/Security/CWE/CWE-327/test2.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,69 @@ void test_assert(int algo, algorithmInfo *algoInfo)
323323
// ...
324324
}
325325

326+
// --- string comparisons ---
327+
328+
int strcmp(const char *s1, const char *s2);
329+
void abort(void);
330+
331+
#define ENCRYPTION_DES_NAME "DES"
332+
#define ENCRYPTION_AES_NAME "AES"
333+
334+
void test_string_comparisons1(const char *algo_name)
335+
{
336+
if (strcmp(algo_name, ENCRYPTION_DES_NAME) == 0) // GOOD [FALSE POSITIVE]
337+
{
338+
abort();
339+
}
340+
if (strcmp(algo_name, ENCRYPTION_AES_NAME) == 0) // GOOD
341+
{
342+
// ...
343+
}
344+
}
345+
346+
const char *getEncryptionNameDES()
347+
{
348+
return "DES";
349+
}
350+
351+
const char *getEncryptionNameAES()
352+
{
353+
return "AES";
354+
}
355+
356+
void test_string_comparisons2(const char *algo_name)
357+
{
358+
if (strcmp(algo_name, getEncryptionNameDES()) == 0) // GOOD [FALSE POSITIVE]
359+
{
360+
abort();
361+
}
362+
if (strcmp(algo_name, getEncryptionNameAES()) == 0) // GOOD
363+
{
364+
// ...
365+
}
366+
}
367+
368+
const char *getEncryptionName(int algo)
369+
{
370+
switch (algo)
371+
{
372+
case ALGO_DES:
373+
return getEncryptionNameDES(); // GOOD [FALSE POSITIVE]
374+
case ALGO_AES:
375+
return getEncryptionNameAES(); // GOOD
376+
default:
377+
abort();
378+
}
379+
}
380+
381+
void test_string_comparisons3(const char *algo_name)
382+
{
383+
if (strcmp(algo_name, getEncryptionName(ALGO_DES)) == 0) // GOOD [FALSE POSITIVE]
384+
{
385+
abort();
386+
}
387+
if (strcmp(algo_name, getEncryptionName(ALGO_AES)) == 0) // GOOD
388+
{
389+
// ...
390+
}
391+
}

0 commit comments

Comments
 (0)