1+ package org .thoughtcrime .securesms .database ;
2+
3+ import android .test .InstrumentationTestCase ;
4+
5+ import static org .fest .assertions .api .Assertions .assertThat ;
6+
7+ public class CanonicalAddressDatabaseTest extends InstrumentationTestCase {
8+ private static final String AMBIGUOUS_NUMBER = "222-3333" ;
9+ private static final String SPECIFIC_NUMBER = "+49 444 222 3333" ;
10+ private static final String EMAIL =
"[email protected] " ;
11+ private static final String SIMILAR_EMAIL =
"[email protected] " ;
12+ private static final String GROUP = "__textsecure_group__!000111222333" ;
13+ private static final String SIMILAR_GROUP = "__textsecure_group__!100111222333" ;
14+ private static final String ALPHA = "T-Mobile" ;
15+ private static final String SIMILAR_ALPHA = "T-Mobila" ;
16+
17+ private CanonicalAddressDatabase db ;
18+
19+ public void setUp () throws Exception {
20+ super .setUp ();
21+ this .db = CanonicalAddressDatabase .getInstance (getInstrumentation ().getTargetContext ());
22+ }
23+
24+ public void tearDown () throws Exception {
25+
26+ }
27+
28+ /**
29+ * Throw two equivalent numbers (one without locale info, one with full info) at the canonical
30+ * address db and see that the caching and DB operations work properly in revealing the right
31+ * addresses. This is run twice to ensure cache logic is hit.
32+ *
33+ * @throws Exception
34+ */
35+ public void testNumberAddressUpdates () throws Exception {
36+ final long id = db .getCanonicalAddressId (AMBIGUOUS_NUMBER );
37+
38+ assertThat (db .getAddressFromId (id )).isEqualTo (AMBIGUOUS_NUMBER );
39+ assertThat (db .getCanonicalAddressId (SPECIFIC_NUMBER )).isEqualTo (id );
40+ assertThat (db .getAddressFromId (id )).isEqualTo (SPECIFIC_NUMBER );
41+ assertThat (db .getCanonicalAddressId (AMBIGUOUS_NUMBER )).isEqualTo (id );
42+
43+ assertThat (db .getCanonicalAddressId (AMBIGUOUS_NUMBER )).isEqualTo (id );
44+ assertThat (db .getAddressFromId (id )).isEqualTo (AMBIGUOUS_NUMBER );
45+ assertThat (db .getCanonicalAddressId (SPECIFIC_NUMBER )).isEqualTo (id );
46+ assertThat (db .getAddressFromId (id )).isEqualTo (SPECIFIC_NUMBER );
47+ assertThat (db .getCanonicalAddressId (AMBIGUOUS_NUMBER )).isEqualTo (id );
48+ }
49+
50+ public void testSimilarNumbers () throws Exception {
51+ assertThat (db .getCanonicalAddressId ("This is a phone number 222-333-444" ))
52+ .isNotEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
53+ assertThat (db .getCanonicalAddressId ("222-333-444" ))
54+ .isNotEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
55+ assertThat (db .getCanonicalAddressId ("222-333-44" ))
56+ .isNotEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
57+ assertThat (db .getCanonicalAddressId ("222-333-4" ))
58+ .isNotEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
59+ assertThat (db .getCanonicalAddressId ("+49 222-333-4444" ))
60+ .isNotEqualTo (db .getCanonicalAddressId ("+1 222-333-4444" ));
61+
62+ assertThat (db .getCanonicalAddressId ("1 222-333-4444" ))
63+ .isEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
64+ assertThat (db .getCanonicalAddressId ("1 (222) 333-4444" ))
65+ .isEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
66+ assertThat (db .getCanonicalAddressId ("+12223334444" ))
67+ .isEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
68+ assertThat (db .getCanonicalAddressId ("+1 (222) 333.4444" ))
69+ .isEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
70+ assertThat (db .getCanonicalAddressId ("+49 (222) 333.4444" ))
71+ .isEqualTo (db .getCanonicalAddressId ("222-333-4444" ));
72+
73+ }
74+
75+ public void testEmailAddresses () throws Exception {
76+ final long emailId = db .getCanonicalAddressId (EMAIL );
77+ final long similarEmailId = db .getCanonicalAddressId (SIMILAR_EMAIL );
78+
79+ assertThat (emailId ).isNotEqualTo (similarEmailId );
80+
81+ assertThat (db .getAddressFromId (emailId )).isEqualTo (EMAIL );
82+ assertThat (db .getAddressFromId (similarEmailId )).isEqualTo (SIMILAR_EMAIL );
83+ }
84+
85+ public void testGroups () throws Exception {
86+ final long groupId = db .getCanonicalAddressId (GROUP );
87+ final long similarGroupId = db .getCanonicalAddressId (SIMILAR_GROUP );
88+
89+ assertThat (groupId ).isNotEqualTo (similarGroupId );
90+
91+ assertThat (db .getAddressFromId (groupId )).isEqualTo (GROUP );
92+ assertThat (db .getAddressFromId (similarGroupId )).isEqualTo (SIMILAR_GROUP );
93+ }
94+
95+ public void testAlpha () throws Exception {
96+ final long id = db .getCanonicalAddressId (ALPHA );
97+ final long similarId = db .getCanonicalAddressId (SIMILAR_ALPHA );
98+
99+ assertThat (id ).isNotEqualTo (similarId );
100+
101+ assertThat (db .getAddressFromId (id )).isEqualTo (ALPHA );
102+ assertThat (db .getAddressFromId (similarId )).isEqualTo (SIMILAR_ALPHA );
103+ }
104+
105+ public void testIsNumber () throws Exception {
106+ assertThat (CanonicalAddressDatabase .isNumberAddress ("+495556666777" )).isTrue ();
107+ assertThat (CanonicalAddressDatabase .isNumberAddress ("(222) 333-4444" )).isTrue ();
108+ assertThat (CanonicalAddressDatabase .isNumberAddress ("1 (222) 333-4444" )).isTrue ();
109+ assertThat (CanonicalAddressDatabase .isNumberAddress ("T-Mobile123" )).isTrue ();
110+ assertThat (CanonicalAddressDatabase .isNumberAddress ("333-4444" )).isTrue ();
111+ assertThat (CanonicalAddressDatabase .isNumberAddress ("12345" )).isTrue ();
112+ assertThat (CanonicalAddressDatabase .isNumberAddress ("T-Mobile" )).isFalse ();
113+ assertThat (CanonicalAddressDatabase .isNumberAddress ("T-Mobile1" )).isFalse ();
114+ assertThat (CanonicalAddressDatabase .isNumberAddress ("Wherever bank" )).isFalse ();
115+ assertThat (CanonicalAddressDatabase .isNumberAddress ("__textsecure_group__!afafafafafaf" )).isFalse ();
116+ assertThat (
CanonicalAddressDatabase .
isNumberAddress (
"[email protected] " )).
isFalse ();
117+ }
118+ }
0 commit comments