summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/test/TestCryptThreading.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/test/TestCryptThreading.cpp')
-rw-r--r--xbmc/utils/test/TestCryptThreading.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/xbmc/utils/test/TestCryptThreading.cpp b/xbmc/utils/test/TestCryptThreading.cpp
new file mode 100644
index 0000000..949bd6f
--- /dev/null
+++ b/xbmc/utils/test/TestCryptThreading.cpp
@@ -0,0 +1,79 @@
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#include "utils/CryptThreading.h"
10#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
11#include "threads/SingleLock.h"
12
13#include <atomic>
14#include <set>
15#include <thread>
16#include <vector>
17
18#include <gtest/gtest.h>
19
20TEST(TestCryptThreadingInitializer, General)
21{
22 std::cout << "g_cryptThreadingInitializer address: " <<
23 testing::PrintToString(&g_cryptThreadingInitializer) << "\n";
24}
25
26#define PVTID_NUM_THREADS 10
27
28TEST(TestCryptThreadingInitializer, ProducesValidThreadIds)
29{
30 std::thread testThreads[PVTID_NUM_THREADS];
31
32 std::vector<unsigned long> gatheredIds;
33 CCriticalSection gatheredIdsMutex;
34
35 std::atomic<unsigned long> threadsWaiting{0};
36 std::atomic<bool> gate{false};
37
38 for (int i = 0; i < PVTID_NUM_THREADS; i++)
39 {
40 testThreads[i] = std::thread([&gatheredIds, &gatheredIdsMutex, &threadsWaiting, &gate]() {
41 threadsWaiting++;
42
43 while (!gate);
44
45 unsigned long myTid = g_cryptThreadingInitializer.GetCurrentCryptThreadId();
46
47 {
48 CSingleLock gatheredIdsLock(gatheredIdsMutex);
49 gatheredIds.push_back(myTid);
50 }
51 });
52 }
53
54 gate = true;
55
56 for (int i = 0; i < PVTID_NUM_THREADS; i++)
57 // This is somewhat dangerous but C++ doesn't have a join with timeout or a way to check
58 // if a thread is still running.
59 testThreads[i].join();
60
61 // Verify that all of the thread id's are unique, and that there are 10 of them, and that none
62 // of them is zero
63 std::set<unsigned long> checkIds;
64 for (std::vector<unsigned long>::const_iterator i = gatheredIds.begin(); i != gatheredIds.end(); ++i)
65 {
66 unsigned long curId = *i;
67 // Thread ID isn't zero (since the sequence is pre-incremented and starts at 0)
68 ASSERT_TRUE(curId != 0);
69
70 // Make sure the ID isn't duplicated
71 ASSERT_TRUE(checkIds.find(curId) == checkIds.end());
72 checkIds.insert(curId);
73 }
74
75 // Make sure there's exactly PVTID_NUM_THREADS of them
76 ASSERT_EQ(PVTID_NUM_THREADS, gatheredIds.size());
77 ASSERT_EQ(PVTID_NUM_THREADS, checkIds.size());
78}
79#endif