1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #include <vector>
26 #include <algorithm>
27 #include <ut/ut.h>
28 #include <base/credcache.h>
29
30 using namespace std;
31 using namespace ut;
32
33 //#define ENABLE_PRINT
34
35
36 static void setUp()
37 {
38 CredCache_Clean();
39 }
40
41 static void cleanup()
42 {
43 mike 1.1 }
44
45 static void TestCredUserAllowed()
46 {
47 // First check that user is not allowed (not added yet)
48 UT_ASSERT(CredCache_CheckUser("user", "abc") < 0);
49
50 CredCache_PutUser("user", "abc");
51
52 // Verify that user is in cache now
53 UT_ASSERT(CredCache_CheckUser("user", "abc") == 0);
54 }
55
56 static void TestCredInvalidPassword()
57 {
58 // First check that user is not allowed (not added yet)
59 UT_ASSERT(CredCache_CheckUser("user", "abc") < 0);
60
61 CredCache_PutUser("user", "abc");
62
63 // Verify invalid passwords
64 mike 1.1 UT_ASSERT(CredCache_CheckUser("user", "") < 0);
65 UT_ASSERT(CredCache_CheckUser("user", "Abc") < 0);
66 UT_ASSERT(CredCache_CheckUser("user", "abc ") < 0);
67 UT_ASSERT(CredCache_CheckUser("user", "-abc") < 0);
68 }
69
70 static void TestCredSeveralUsers()
71 {
72
73 CredCache_PutUser("user1", "abc1");
74 CredCache_PutUser("user2", "abc2");
75 CredCache_PutUser("user3", "abc3");
76 CredCache_PutUser("user4", "abc4");
77
78 // Verify that users are in cache now
79 UT_ASSERT(CredCache_CheckUser("user1", "abc1") == 0);
80 UT_ASSERT(CredCache_CheckUser("user2", "abc2") == 0);
81 UT_ASSERT(CredCache_CheckUser("user3", "abc3") == 0);
82 UT_ASSERT(CredCache_CheckUser("user4", "abc4") == 0);
83 }
84
85 mike 1.1 static void TestCredExpiration()
86 {
87
88 CredCache_PutUser("user1", "abc1");
89
90 CredCache_SetExpirationTimeout(1);
91
92 sleep_ms(1);
93
94 // Verify that users expired
95 UT_ASSERT(CredCache_CheckUser("user1", "abc1") < 0);
96
97 /* Restore default one */
98 CredCache_SetExpirationTimeout(0);
99 }
100
101 static void TestCredOldestOverwritten()
102 {
103
104 /* create oldest item */
105 CredCache_PutUser("oldest", "oldest");
106 mike 1.1
107 UT_ASSERT(CredCache_CheckUser("oldest", "oldest") == 0);
108
109 /* make it older than the rest */
110 sleep_ms(1);
111
112 for (int i = 0; i < 64; i++)
113 {
114 char user[64];
115
116 sprintf(user, "user%d", i);
117 // Add new user and verify it's added
118 CredCache_PutUser(user, user);
119 UT_ASSERT(CredCache_CheckUser(user, user) == 0);
120 }
121
122 // Verify that users expired
123 UT_ASSERT(CredCache_CheckUser("oldest", "oldest") < 0);
124 }
125
126
127 mike 1.1 static void RunTests()
128 {
129 UT_TEST(TestCredUserAllowed);
130 UT_TEST(TestCredInvalidPassword);
131 UT_TEST(TestCredSeveralUsers);
132 UT_TEST(TestCredExpiration);
133 UT_TEST(TestCredOldestOverwritten);
134 }
135
136 UT_ENTRY_POINT(RunTests);
|