1 krisbash 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 krisbash 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 // every test needs to start with clean credcache in the test body
36 // reason is once fault injection runs with nits, it runs the same
37 // body multiple times and that causes the credcache not to start clean
38 // from the fault sim iteration start
39 NitsTest(TestCredUserAllowed)
40 {
41 CredCache_Clean();
42
43 krisbash 1.1 // First check that user is not allowed (not added yet)
44 UT_ASSERT(CredCache_CheckUser("user", "abc") < 0);
45
46 CredCache_PutUser("user", "abc");
47
48 // Verify that user is in cache now
49 UT_ASSERT(CredCache_CheckUser("user", "abc") == 0);
50 }
51 NitsEndTest
52
53 NitsTest(TestCredInvalidPassword)
54 {
55 CredCache_Clean();
56
57 // First check that user is not allowed (not added yet)
58 UT_ASSERT(CredCache_CheckUser("user", "abc") < 0);
59
60 CredCache_PutUser("user", "abc");
61
62 // Verify invalid passwords
63 UT_ASSERT(CredCache_CheckUser("user", "") < 0);
64 krisbash 1.1 UT_ASSERT(CredCache_CheckUser("user", "Abc") < 0);
65 UT_ASSERT(CredCache_CheckUser("user", "abc ") < 0);
66 UT_ASSERT(CredCache_CheckUser("user", "-abc") < 0);
67 }
68 NitsEndTest
69
70 NitsTest(TestCredSeveralUsers)
71 {
72 CredCache_Clean();
73
74 CredCache_PutUser("user1", "abc1");
75 CredCache_PutUser("user2", "abc2");
76 CredCache_PutUser("user3", "abc3");
77 CredCache_PutUser("user4", "abc4");
78
79 // Verify that users are in cache now
80 UT_ASSERT(CredCache_CheckUser("user1", "abc1") == 0);
81 UT_ASSERT(CredCache_CheckUser("user2", "abc2") == 0);
82 UT_ASSERT(CredCache_CheckUser("user3", "abc3") == 0);
83 UT_ASSERT(CredCache_CheckUser("user4", "abc4") == 0);
84 }
85 krisbash 1.1 NitsEndTest
86
87 NitsTest(TestCredExpiration)
88 {
89 CredCache_Clean();
90
91 CredCache_PutUser("user1", "abc1");
92
93 CredCache_SetExpirationTimeout(1);
94
95 sleep_ms(1);
96
97 // Verify that users expired
98 UT_ASSERT(CredCache_CheckUser("user1", "abc1") < 0);
99
100 /* Restore default one */
101 CredCache_SetExpirationTimeout(0);
102 }
103 NitsEndTest
104
105 NitsTest(TestCredOldestOverwritten)
106 krisbash 1.1 {
107 CredCache_Clean();
108
109 /* create oldest item */
110 CredCache_PutUser("oldest", "oldest");
111
112 UT_ASSERT(CredCache_CheckUser("oldest", "oldest") == 0);
113
114 /* make it older than the rest */
115 sleep_ms(1);
116
117 for (int i = 0; i < 64; i++)
118 {
119 char user[64];
120
121 sprintf(user, "user%d", i);
122 // Add new user and verify it's added
123 CredCache_PutUser(user, user);
124 UT_ASSERT(CredCache_CheckUser(user, user) == 0);
125 }
126
127 krisbash 1.1 // Verify that users expired
128 UT_ASSERT(CredCache_CheckUser("oldest", "oldest") < 0);
129 }
130 NitsEndTest
131
|