summaryrefslogtreecommitdiff
path: root/rel/overlay/share/www/script/test/auth_cache.js
blob: 75827dbd3a6a865c54bf341dbcf0209c8b271981 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
// the License.

couchTests.auth_cache = function(debug) {

  if (debug) debugger;

  // Simple secret key generator
  function generateSecret(length) {
    var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
              "0123456789+/";
    var secret = '';
    for (var i = 0; i < length; i++) {
      secret += tab.charAt(Math.floor(Math.random() * 64));
    }
    return secret;
  }

  var authDb = new CouchDB("test_suite_users", {"X-Couch-Full-Commit":"false"});
  var server_config = [
    {
      section: "couch_httpd_auth",
      key: "authentication_db",
      value: authDb.name
    },
    {
      section: "couch_httpd_auth",
      key: "auth_cache_size",
      value: "3"
    },
    {
      section: "httpd",
      key: "authentication_handlers",
      value: "{couch_httpd_auth, default_authentication_handler}"
    },
    {
      section: "couch_httpd_auth",
      key: "secret",
      value: generateSecret(64)
    }
  ];


  function hits() {
    var hits = CouchDB.requestStats("couchdb", "auth_cache_hits", true);
    return hits.current || 0;
  }


  function misses() {
    var misses = CouchDB.requestStats("couchdb", "auth_cache_misses", true);
    return misses.current || 0;
  }


  function testFun() {
    var hits_before,
        misses_before,
        hits_after,
        misses_after;

    var fdmanana = CouchDB.prepareUserDoc({
      name: "fdmanana",
      roles: ["dev"]
    }, "qwerty");

    T(authDb.save(fdmanana).ok);

    var chris = CouchDB.prepareUserDoc({
      name: "chris",
      roles: ["dev", "mafia", "white_costume"]
    }, "the_god_father");

    T(authDb.save(chris).ok);

    var joe = CouchDB.prepareUserDoc({
      name: "joe",
      roles: ["erlnager"]
    }, "functional");

    T(authDb.save(joe).ok);

    var johndoe = CouchDB.prepareUserDoc({
      name: "johndoe",
      roles: ["user"]
    }, "123456");

    T(authDb.save(johndoe).ok);

    hits_before = hits();
    misses_before = misses();

    T(CouchDB.login("fdmanana", "qwerty").ok);

    hits_after = hits();
    misses_after = misses();

    T(misses_after === (misses_before + 1));
    T(hits_after === hits_before);

    hits_before = hits_after;
    misses_before = misses_after;

    T(CouchDB.logout().ok);
    T(CouchDB.login("fdmanana", "qwerty").ok);

    hits_after = hits();
    misses_after = misses();

    T(misses_after === misses_before);
    T(hits_after === (hits_before + 1));

    hits_before = hits_after;
    misses_before = misses_after;

    T(CouchDB.logout().ok);
    T(CouchDB.login("chris", "the_god_father").ok);

    hits_after = hits();
    misses_after = misses();

    T(misses_after === (misses_before + 1));
    T(hits_after === hits_before);

    hits_before = hits_after;
    misses_before = misses_after;

    T(CouchDB.logout().ok);
    T(CouchDB.login("joe", "functional").ok);

    hits_after = hits();
    misses_after = misses();

    T(misses_after === (misses_before + 1));
    T(hits_after === hits_before);

    hits_before = hits_after;
    misses_before = misses_after;

    T(CouchDB.logout().ok);
    T(CouchDB.login("johndoe", "123456").ok);

    hits_after = hits();
    misses_after = misses();

    T(misses_after === (misses_before + 1));
    T(hits_after === hits_before);

    hits_before = hits_after;
    misses_before = misses_after;

    T(CouchDB.logout().ok);
    T(CouchDB.login("joe", "functional").ok);

    hits_after = hits();
    misses_after = misses();

    // it's an MRU cache, joe was removed from cache to add johndoe
    T(misses_after === (misses_before + 1));
    T(hits_after === hits_before);

    hits_before = hits_after;
    misses_before = misses_after;

    T(CouchDB.logout().ok);
    T(CouchDB.login("fdmanana", "qwerty").ok);

    hits_after = hits();
    misses_after = misses();

    T(misses_after === misses_before);
    T(hits_after === (hits_before + 1));

    hits_before = hits_after;
    misses_before = misses_after;

    var new_salt = CouchDB.newUuids(1)[0];
    var new_passwd = hex_sha1("foobar" + new_salt);
    fdmanana.salt = new_salt;
    fdmanana.password_sha = new_passwd;

    T(authDb.save(fdmanana).ok);
    T(CouchDB.logout().ok);

    // cache was refreshed
    T(CouchDB.login("fdmanana", "qwerty").error === "unauthorized");
    T(CouchDB.login("fdmanana", "foobar").ok);

    hits_after = hits();
    misses_after = misses();

    T(misses_after === misses_before);
    T(hits_after === (hits_before + 2));

    T(CouchDB.logout().ok);

    hits_before = hits_after;
    misses_before = misses_after;

    // and yet another update
    new_salt = CouchDB.newUuids(1)[0];
    new_passwd = hex_sha1("javascript" + new_salt);
    fdmanana.salt = new_salt;
    fdmanana.password_sha = new_passwd;

    T(authDb.save(fdmanana).ok);
    T(CouchDB.logout().ok);

    // cache was refreshed
    T(CouchDB.login("fdmanana", "foobar").error === "unauthorized");
    T(CouchDB.login("fdmanana", "javascript").ok);

    hits_after = hits();
    misses_after = misses();

    T(misses_after === misses_before);
    T(hits_after === (hits_before + 2));

    T(authDb.deleteDoc(fdmanana).ok);
    T(CouchDB.logout().ok);

    hits_before = hits_after;
    misses_before = misses_after;

    T(CouchDB.login("fdmanana", "javascript").error === "unauthorized");

    hits_after = hits();
    misses_after = misses();

    T(misses_after === misses_before);
    T(hits_after === (hits_before + 1));
  }


  authDb.deleteDb();
  run_on_modified_server(server_config, testFun);

  // cleanup
  authDb.deleteDb();
}