summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/news/test/unit/test_uriParser.js
blob: a152076333cbd73e7ab5a6a1f18f2da9739c304f (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
// Tests nsINntpUrl parsing.

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var localserver;
var tests = [
  // news://host/-based URIs
  {
    uri: "news://localhost/?newgroups",
    get server() {
      return localserver;
    },
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionListNewGroups,
  },
  // news://host/group-based
  {
    uri: "news://news.server.example/example.group.this",
    server: null,
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionGetNewNews,
    group: "example.group.this",
  },
  {
    uri: "news://news.server.example/*",
    server: null,
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionListGroups,
  },
  {
    uri: "news://news.server.example/news.*",
    server: null,
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionListGroups,
  },
  {
    uri: "news://localhost/test.filter?list-ids",
    get server() {
      return localserver;
    },
    get folder() {
      return localserver.rootFolder.getChildNamed("test.filter");
    },
    newsAction: Ci.nsINntpUrl.ActionListIds,
    group: "test.filter",
  },
  {
    uri: "news://localhost/some.group?search/XPAT From 1-5 [Ww][Hh][Oo]",
    get server() {
      return localserver;
    },
    newsAction: Ci.nsINntpUrl.ActionSearch,
    group: "some.group",
  },

  // news://host/message-based URIs
  {
    uri: "news://localhost/message-id@some-host.invalid",
    get server() {
      return localserver;
    },
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionFetchArticle,
    messageID: "message-id@some-host.invalid",
    group: "",
    key: 0xffffffff,
  },
  {
    uri: "news://localhost/message-id@some-host.invalid?part=1.4",
    get server() {
      return localserver;
    },
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionFetchPart,
    messageID: "message-id@some-host.invalid",
  },
  {
    uri: "news://localhost/message-id@some-host.invalid?cancel",
    get server() {
      return localserver;
    },
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionCancelArticle,
    messageID: "message-id@some-host.invalid",
  },
  {
    uri: "news://localhost/message-id@some-host.invalid?group=foo&key=123",
    get server() {
      return localserver;
    },
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionFetchArticle,
    messageID: "message-id@some-host.invalid",
    group: "foo",
    key: 123,
  },

  // No-authority uris
  {
    uri: "news:rec.games.pinball",
    server: null,
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionGetNewNews,
    group: "rec.games.pinball",
    host: "",
  },
  {
    uri: "news:message-id@some-host.invalid",
    server: null,
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionFetchArticle,
    messageID: "message-id@some-host.invalid",
    group: "",
    key: 0xffffffff,
  },

  // news-message://host/group#key
  {
    uri: "news-message://localhost/test.simple.subscribe#1",
    newsAction: Ci.nsINntpUrl.ActionFetchArticle,
    group: "test.simple.subscribe",
    key: 1,
  },

  // nntp://host/group
  {
    uri: "nntp://localhost/test.filter",
    get server() {
      return localserver;
    },
    get folder() {
      return localserver.rootFolder.getChildNamed("test.filter");
    },
    newsAction: Ci.nsINntpUrl.ActionGetNewNews,
    group: "test.filter",
  },
  {
    uri: "nntp://localhost/i.dont.exist",
    get server() {
      return localserver;
    },
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionGetNewNews,
    group: "i.dont.exist",
  },
  {
    uri: "nntp://news.example.invalid/i.dont.exist",
    server: null,
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionGetNewNews,
    group: "i.dont.exist",
  },

  // nntp://host/group/key
  {
    uri: "nntp://localhost/test.filter/123",
    get server() {
      return localserver;
    },
    get folder() {
      return localserver.rootFolder.getChildNamed("test.filter");
    },
    newsAction: Ci.nsINntpUrl.ActionFetchArticle,
    group: "test.filter",
    key: 123,
  },
  {
    uri: "nntp://localhost/i.dont.exist/123",
    get server() {
      return localserver;
    },
    folder: null,
    newsAction: Ci.nsINntpUrl.ActionFetchArticle,
    group: "i.dont.exist",
    key: 123,
  },
];

var invalid_uris = [
  "news-message://localhost/test.simple.subscribe#hello",
  "nntp://localhost/",
  "nntp://localhost/a.group/hello",
  "nntp://localhost/a.group/0",
  "nntp:a.group",
];

function run_test() {
  // We're not running the server, just setting it up
  localserver = setupLocalServer(119);
  for (let test of tests) {
    dump("Checking URL " + test.uri + "\n");
    let url = Services.io.newURI(test.uri);
    url.QueryInterface(Ci.nsIMsgMailNewsUrl);
    url.QueryInterface(Ci.nsINntpUrl);
    for (let prop in test) {
      if (prop == "uri") {
        continue;
      }
      Assert.equal(url[prop], test[prop]);
    }
  }

  for (let fail of invalid_uris) {
    try {
      dump("Checking URL " + fail + " for failure\n");
      Services.io.newURI(fail);
      Assert.ok(false);
    } catch (e) {
      Assert.equal(e.result, Cr.NS_ERROR_MALFORMED_URI);
    }
  }

  // The password migration is async, so trigger an event to prevent the logon
  // manager from trying to migrate after shutdown has started.
  let thread = Services.tm.currentThread;
  while (thread.hasPendingEvents()) {
    thread.processNextEvent(true);
  }
}