summaryrefslogtreecommitdiffstats
path: root/b2g/components/test/unit/head_identity.js
blob: 604a7728486ad14f729b685bc130bcefb6b161f3 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var Ci = Components.interfaces;
var Cu = Components.utils;

// The following boilerplate makes sure that XPCOM calls
// that use the profile directory work.

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "MinimalIDService",
                                  "resource://gre/modules/identity/MinimalIdentity.jsm",
                                  "IdentityService");

XPCOMUtils.defineLazyModuleGetter(this,
                                  "Logger",
                                  "resource://gre/modules/identity/LogUtils.jsm");

XPCOMUtils.defineLazyServiceGetter(this,
                                   "uuidGenerator",
                                   "@mozilla.org/uuid-generator;1",
                                   "nsIUUIDGenerator");

const TEST_URL = "https://myfavoriteflan.com";
const TEST_USER = "uumellmahaye1969@hotmail.com";
const TEST_PRIVKEY = "i-am-a-secret";
const TEST_CERT = "i~like~pie";

// The following are utility functions for Identity testing

function log(...aMessageArgs) {
  Logger.log.apply(Logger, ["test"].concat(aMessageArgs));
}

function partial(fn) {
  let args = Array.prototype.slice.call(arguments, 1);
  return function() {
    return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
  };
}

function uuid() {
  return uuidGenerator.generateUUID().toString();
}

// create a mock "doc" object, which the Identity Service
// uses as a pointer back into the doc object
function mockDoc(aParams, aDoFunc) {
  let mockedDoc = {};
  mockedDoc.id = uuid();

  // Properties of aParams may include loggedInUser
  Object.keys(aParams).forEach(function(param) {
    mockedDoc[param] = aParams[param];
  });

  // the origin is set inside nsDOMIdentity by looking at the
  // document.nodePrincipal.origin.  Here we, we must satisfy
  // ourselves with pretending.
  mockedDoc.origin = "https://jedp.gov";

  mockedDoc['do'] = aDoFunc;
  mockedDoc.doReady = partial(aDoFunc, 'ready');
  mockedDoc.doLogin = partial(aDoFunc, 'login');
  mockedDoc.doLogout = partial(aDoFunc, 'logout');
  mockedDoc.doError = partial(aDoFunc, 'error');
  mockedDoc.doCancel = partial(aDoFunc, 'cancel');
  mockedDoc.doCoffee = partial(aDoFunc, 'coffee');

  return mockedDoc;
}

// create a mock "pipe" object that would normally communicate
// messages up to gaia (either the trusty ui or the hidden iframe),
// and convey messages back down from gaia to the controller through
// the message callback.

// The mock receiving pipe simulates gaia which, after receiving messages
// through the pipe, will call back with instructions to invoke
// certain methods.  It mocks what comes back from the other end of
// the pipe.
function mockReceivingPipe() {
  let MockedPipe = {
    communicate: function(aRpOptions, aGaiaOptions, aMessageCallback) {
      switch (aGaiaOptions.message) {
        case "identity-delegate-watch":
          aMessageCallback({json: {method: "ready"}});
          break;
        case "identity-delegate-request":
          aMessageCallback({json: {method: "login", assertion: TEST_CERT}});
          break;
        case "identity-delegate-logout":
          aMessageCallback({json: {method: "logout"}});
          break;
        default:
          throw("what the what?? " + aGaiaOptions.message);
          break;
      }
    }
  };
  return MockedPipe;
}

// The mock sending pipe lets us test what's actually getting put in the
// pipe.
function mockSendingPipe(aMessageCallback) {
  let MockedPipe = {
    communicate: function(aRpOptions, aGaiaOptions, aDummyCallback) {
      aMessageCallback(aRpOptions, aGaiaOptions);
    }
  };
  return MockedPipe;
}

// mimicking callback funtionality for ease of testing
// this observer auto-removes itself after the observe function
// is called, so this is meant to observe only ONE event.
function makeObserver(aObserveTopic, aObserveFunc) {
  let observer = {
    // nsISupports provides type management in C++
    // nsIObserver is to be an observer
    QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]),

    observe: function (aSubject, aTopic, aData) {
      if (aTopic == aObserveTopic) {
        Services.obs.removeObserver(observer, aObserveTopic);
        aObserveFunc(aSubject, aTopic, aData);
      }
    }
  };

  Services.obs.addObserver(observer, aObserveTopic, false);
}

// a hook to set up the ID service with an identity with keypair and all
// when ready, invoke callback with the identity.  It's there if we need it.
function setup_test_identity(identity, cert, cb) {
  cb();
}

// takes a list of functions and returns a function that
// when called the first time, calls the first func,
// then the next time the second, etc.
function call_sequentially() {
  let numCalls = 0;
  let funcs = arguments;

  return function() {
    if (!funcs[numCalls]) {
      let argString = Array.prototype.slice.call(arguments).join(",");
      do_throw("Too many calls: " + argString);
      return;
    }
    funcs[numCalls].apply(funcs[numCalls],arguments);
    numCalls += 1;
  };
}