This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
/
TestCredentialsEndToEnd.java
68 lines (52 loc) · 2.53 KB
/
TestCredentialsEndToEnd.java
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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
package org.mozilla.android.sync.net.test;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.json.simple.parser.ParseException;
import org.junit.Test;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.NonObjectJSONException;
import org.mozilla.gecko.sync.Utils;
import org.mozilla.gecko.sync.net.BaseResource;
import ch.boye.httpclientandroidlib.Header;
/**
* Test the transfer of a UTF-8 string from desktop, and ensure that it results in the
* correct hashed Basic Auth header.
*/
public class TestCredentialsEndToEnd {
public static final String REAL_PASSWORD = "pïgéons1";
public static final String USERNAME = "utvm3mk6hnngiir2sp4jsxf2uvoycrv6";
public static final String DESKTOP_PASSWORD_JSON = "{\"password\":\"pïgéons1\"}";
public static final String BTOA_PASSWORD = "cMOvZ8Opb25zMQ==";
public static final int DESKTOP_ASSERTED_SIZE = 10;
public static final String DESKTOP_BASIC_AUTH = "Basic dXR2bTNtazZobm5naWlyMnNwNGpzeGYydXZveWNydjY6cMOvZ8Opb25zMQ==";
private String getCreds(String password) {
Header authenticate = BaseResource.getBasicAuthHeader(USERNAME + ":" + password);
return authenticate.getValue();
}
@Test
public void testUTF8() throws UnsupportedEncodingException {
final String in = "pïgéons1";
final String out = "pïgéons1";
assertEquals(out, Utils.decodeUTF8(in));
}
@Test
public void testAuthHeaderFromPassword() throws NonObjectJSONException, IOException, ParseException {
final ExtendedJSONObject parsed = new ExtendedJSONObject(DESKTOP_PASSWORD_JSON);
final String password = parsed.getString("password");
final String decoded = Utils.decodeUTF8(password);
final byte[] expectedBytes = Utils.decodeBase64(BTOA_PASSWORD);
final String expected = new String(expectedBytes, "UTF-8");
assertEquals(DESKTOP_ASSERTED_SIZE, password.length());
assertEquals(expected, decoded);
System.out.println("Retrieved password: " + password);
System.out.println("Expected password: " + expected);
System.out.println("Rescued password: " + decoded);
assertEquals(getCreds(expected), getCreds(decoded));
}
// Note that we do *not* have a test for the J-PAKE setup process
// (SetupSyncActivity) that actually stores credentials and requires
// decodeUTF8. This will have to suffice.
}