1 // NOTE: This tests code outside of Necko. The test still lives here because
2 // the contract is part of Necko.
10 const nsIAuthInformation
= Ci
.nsIAuthInformation
;
11 const nsIAuthPromptAdapterFactory
= Ci
.nsIAuthPromptAdapterFactory
;
14 const contractID
= "@mozilla.org/network/authprompt-adapter-factory;1";
15 if (!(contractID
in Cc
)) {
16 print("No adapter factory found, skipping testing");
19 var adapter
= Cc
[contractID
].getService();
20 Assert
.equal(adapter
instanceof nsIAuthPromptAdapterFactory
, true);
22 // NOTE: xpconnect lets us get away with passing an empty object here
23 // For this part of the test, we only care that this function returns
25 Assert
.notEqual(adapter
.createAdapter({}), null);
27 const host
= "www.mozilla.org";
34 flags
: nsIAuthInformation
.AUTH_HOST
,
35 authenticationScheme
: "basic",
39 const CALLED_PROMPT
= 1 << 0;
40 const CALLED_PROMPTUP
= 1 << 1;
41 const CALLED_PROMPTP
= 1 << 2;
52 QueryInterface
: ChromeUtils
.generateQI(["nsIAuthPrompt"]),
54 prompt
: function ap1_prompt(title
, text
, realm
) {
55 this.called
|= CALLED_PROMPT
;
56 this.doChecks(text
, realm
);
60 promptUsernameAndPassword
: function ap1_promptUP(
68 this.called
|= CALLED_PROMPTUP
;
69 this.doChecks(text
, realm
);
70 user
.value
= this.user
;
75 promptPassword
: function ap1_promptPW(title
, text
, realm
, save
, pwd
) {
76 this.called
|= CALLED_PROMPTP
;
77 this.doChecks(text
, realm
);
82 doChecks
: function ap1_check(text
, realm
) {
83 Assert
.equal(this.scheme
+ "://" + host
+ " (" + info
.realm
+ ")", realm
);
85 Assert
.notEqual(text
.indexOf(host
), -1);
86 if (info
.flags
& nsIAuthInformation
.ONLY_PASSWORD
) {
87 // Should have the username in the text
88 Assert
.notEqual(text
.indexOf(info
.username
), -1);
90 // Make sure that we show the realm if we have one and that we don't
92 if (info
.realm
!= "") {
93 Assert
.notEqual(text
.indexOf(info
.realm
), -1);
95 Assert
.equal(text
.indexOf('""'), -1);
97 // No explicit port in the URL; message should not contain -1
99 Assert
.equal(text
.indexOf("-1"), -1);
104 // Also have to make up a channel
105 var uri
= NetUtil
.newURI("http://" + host
);
106 var chan
= NetUtil
.newChannel({
108 loadUsingSystemPrincipal
: true,
111 function do_tests(expectedRV
) {
115 // 1: The simple case
116 prompt1
= new Prompt1();
117 prompt1
.rv
= expectedRV
;
118 wrapper
= adapter
.createAdapter(prompt1
);
120 var rv
= wrapper
.promptAuth(chan
, 0, info
);
121 Assert
.equal(rv
, prompt1
.rv
);
122 Assert
.equal(prompt1
.called
, CALLED_PROMPTUP
);
125 Assert
.equal(info
.domain
, "");
126 Assert
.equal(info
.username
, prompt1
.user
);
127 Assert
.equal(info
.password
, prompt1
.pw
);
134 // 2: Only ask for a PW
135 prompt1
= new Prompt1();
136 prompt1
.rv
= expectedRV
;
137 info
.flags
|= nsIAuthInformation
.ONLY_PASSWORD
;
139 // Initialize the username so that the prompt can show it
140 info
.username
= prompt1
.user
;
142 wrapper
= adapter
.createAdapter(prompt1
);
143 rv
= wrapper
.promptAuth(chan
, 0, info
);
144 Assert
.equal(rv
, prompt1
.rv
);
145 Assert
.equal(prompt1
.called
, CALLED_PROMPTP
);
148 Assert
.equal(info
.domain
, "");
149 Assert
.equal(info
.username
, prompt1
.user
); // we initialized this
150 Assert
.equal(info
.password
, prompt1
.pw
);
153 info
.flags
&= ~nsIAuthInformation
.ONLY_PASSWORD
;
159 // 3: user, pw and domain
160 prompt1
= new Prompt1();
161 prompt1
.rv
= expectedRV
;
162 info
.flags
|= nsIAuthInformation
.NEED_DOMAIN
;
164 wrapper
= adapter
.createAdapter(prompt1
);
165 rv
= wrapper
.promptAuth(chan
, 0, info
);
166 Assert
.equal(rv
, prompt1
.rv
);
167 Assert
.equal(prompt1
.called
, CALLED_PROMPTUP
);
170 Assert
.equal(info
.domain
, "foo");
171 Assert
.equal(info
.username
, "bar");
172 Assert
.equal(info
.password
, prompt1
.pw
);
175 info
.flags
&= ~nsIAuthInformation
.NEED_DOMAIN
;
181 // 4: username that doesn't contain a domain
182 prompt1
= new Prompt1();
183 prompt1
.rv
= expectedRV
;
184 info
.flags
|= nsIAuthInformation
.NEED_DOMAIN
;
186 prompt1
.user
= "foo";
188 wrapper
= adapter
.createAdapter(prompt1
);
189 rv
= wrapper
.promptAuth(chan
, 0, info
);
190 Assert
.equal(rv
, prompt1
.rv
);
191 Assert
.equal(prompt1
.called
, CALLED_PROMPTUP
);
194 Assert
.equal(info
.domain
, "");
195 Assert
.equal(info
.username
, prompt1
.user
);
196 Assert
.equal(info
.password
, prompt1
.pw
);
199 info
.flags
&= ~nsIAuthInformation
.NEED_DOMAIN
;