-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOptOutEntryTest.java
More file actions
201 lines (167 loc) · 9.97 KB
/
OptOutEntryTest.java
File metadata and controls
201 lines (167 loc) · 9.97 KB
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
package com.uid2.shared.optout;
import org.junit.Assert;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class OptOutEntryTest {
@Test
public void parseLegacyEntry() {
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final byte[] timestamp = OptOutUtils.hexToByteArray("5051525354555600");
final byte[] record = new byte[OptOutConst.EntrySize];
System.arraycopy(idHash, 0, record, 0, OptOutConst.Sha256Bytes);
System.arraycopy(adsId, 0, record, OptOutConst.Sha256Bytes, OptOutConst.Sha256Bytes);
System.arraycopy(timestamp, 0, record, OptOutConst.Sha256Bytes * 2, Long.BYTES);
final OptOutEntry entry = OptOutEntry.parse(record, 0);
Assert.assertArrayEquals(idHash, entry.identityHash);
Assert.assertArrayEquals(adsId, entry.advertisingId);
Assert.assertEquals(0x56555453525150l, entry.timestamp);
Assert.assertFalse(entry.isTombstone);
}
@Test
public void parseEntry() {
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final byte[] timestamp = OptOutUtils.hexToByteArray("50515253545556");
final byte metadata = 0x54;
final byte[] record = new byte[OptOutConst.EntrySize];
System.arraycopy(idHash, 0, record, 0, OptOutConst.Sha256Bytes);
System.arraycopy(adsId, 0, record, OptOutConst.Sha256Bytes, OptOutConst.Sha256Bytes);
System.arraycopy(timestamp, 0, record, OptOutConst.Sha256Bytes * 2, Long.BYTES - 1);
record[OptOutConst.EntrySize - 1] = metadata;
final byte[] expectedAdsId = new byte[33];
expectedAdsId[0] = 0x14;
System.arraycopy(adsId, 0, expectedAdsId, 1, OptOutConst.Sha256Bytes);
final OptOutEntry entry = OptOutEntry.parse(record, 0);
Assert.assertArrayEquals(idHash, entry.identityHash);
Assert.assertArrayEquals(expectedAdsId, entry.advertisingId);
Assert.assertEquals(0x56555453525150l, entry.timestamp);
Assert.assertFalse(entry.isTombstone);
}
@Test
public void parseEntryAtOffset() {
final int offset = 12;
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final byte[] timestamp = OptOutUtils.hexToByteArray("50515253545556");
final byte metadata = 0x54;
final byte[] records = new byte[offset + OptOutConst.EntrySize];
System.arraycopy(idHash, 0, records, offset, OptOutConst.Sha256Bytes);
System.arraycopy(adsId, 0, records, offset + OptOutConst.Sha256Bytes, OptOutConst.Sha256Bytes);
System.arraycopy(timestamp, 0, records, offset + OptOutConst.Sha256Bytes * 2, Long.BYTES - 1);
records[offset + OptOutConst.EntrySize - 1] = metadata;
final byte[] expectedAdsId = new byte[33];
expectedAdsId[0] = 0x14;
System.arraycopy(adsId, 0, expectedAdsId, 1, OptOutConst.Sha256Bytes);
final OptOutEntry entry = OptOutEntry.parse(records, offset);
Assert.assertArrayEquals(idHash, entry.identityHash);
Assert.assertArrayEquals(expectedAdsId, entry.advertisingId);
Assert.assertEquals(0x56555453525150l, entry.timestamp);
Assert.assertFalse(entry.isTombstone);
}
@Test
public void parseTimestampAtOffset() {
final int offset = 12;
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final byte[] timestamp = OptOutUtils.hexToByteArray("50515253545556");
final byte metadata = 0x54;
final byte[] records = new byte[offset + OptOutConst.EntrySize];
System.arraycopy(idHash, 0, records, offset, OptOutConst.Sha256Bytes);
System.arraycopy(adsId, 0, records, offset + OptOutConst.Sha256Bytes, OptOutConst.Sha256Bytes);
System.arraycopy(timestamp, 0, records, offset + OptOutConst.Sha256Bytes * 2, Long.BYTES - 1);
records[offset + OptOutConst.EntrySize - 1] = metadata;
Assert.assertEquals(0x56555453525150l, OptOutEntry.parseTimestamp(records, offset));
}
@Test
public void setTimestampAtOffset() {
final int offset = 12;
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final byte[] timestamp = OptOutUtils.hexToByteArray("50515253545556");
final byte metadata = 0x54;
final byte[] records = new byte[offset + OptOutConst.EntrySize];
System.arraycopy(idHash, 0, records, offset, OptOutConst.Sha256Bytes);
System.arraycopy(adsId, 0, records, offset + OptOutConst.Sha256Bytes, OptOutConst.Sha256Bytes);
System.arraycopy(timestamp, 0, records, offset + OptOutConst.Sha256Bytes * 2, Long.BYTES - 1);
records[offset + OptOutConst.EntrySize - 1] = metadata;
final long newTimestamp = 0x60616263646566l;
OptOutEntry.setTimestamp(records, offset, newTimestamp);
final byte[] expectedAdsId = new byte[33];
expectedAdsId[0] = 0x14;
System.arraycopy(adsId, 0, expectedAdsId, 1, OptOutConst.Sha256Bytes);
final OptOutEntry entry = OptOutEntry.parse(records, offset);
Assert.assertArrayEquals(idHash, entry.identityHash);
Assert.assertArrayEquals(expectedAdsId, entry.advertisingId);
Assert.assertEquals(newTimestamp, entry.timestamp);
Assert.assertFalse(entry.isTombstone);
}
@Test
public void copyToByteArrayLegacy()
{
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final long timestamp = 0x56555453525150l;
final int offset = 12;
final byte[] records = new byte[offset + OptOutConst.EntrySize];
final OptOutEntry input = new OptOutEntry(idHash, adsId, timestamp, false);
input.copyToByteArray(records, offset);
final OptOutEntry entry = OptOutEntry.parse(records, 12);
Assert.assertArrayEquals(idHash, entry.identityHash);
Assert.assertArrayEquals(adsId, entry.advertisingId);
Assert.assertEquals(0x56555453525150l, entry.timestamp);
Assert.assertFalse(entry.isTombstone);
}
@Test
public void copyToByteArray()
{
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("04303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final long timestamp = 0x56555453525150l;
final int offset = 12;
final byte[] records = new byte[offset + OptOutConst.EntrySize];
final OptOutEntry input = new OptOutEntry(idHash, adsId, timestamp, false);
input.copyToByteArray(records, offset);
final OptOutEntry entry = OptOutEntry.parse(records, 12);
Assert.assertArrayEquals(idHash, entry.identityHash);
Assert.assertArrayEquals(adsId, entry.advertisingId);
Assert.assertEquals(0x56555453525150l, entry.timestamp);
Assert.assertFalse(entry.isTombstone);
}
@Test
public void writeToByteBuffer()
{
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("04303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final long timestamp = 0x56555453525150l;
ByteBuffer buffer = ByteBuffer.allocate(OptOutConst.EntrySize);
OptOutEntry.writeTo(buffer, idHash, adsId, timestamp, false);
final OptOutEntry entry = OptOutEntry.parse(buffer.array(), 0);
Assert.assertArrayEquals(idHash, entry.identityHash);
Assert.assertArrayEquals(adsId, entry.advertisingId);
Assert.assertEquals(0x56555453525150l, entry.timestamp);
Assert.assertFalse(entry.isTombstone);
}
@Test
public void checkTombstone() {
final int offset = 12;
final byte[] idHash = OptOutUtils.hexToByteArray("101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f");
final byte[] adsId = OptOutUtils.hexToByteArray("303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f");
final byte[] timestamp = OptOutUtils.hexToByteArray("50515253545556");
final byte metadata = 0x74;
final byte[] records = new byte[offset + OptOutConst.EntrySize];
System.arraycopy(idHash, 0, records, offset, OptOutConst.Sha256Bytes);
System.arraycopy(adsId, 0, records, offset + OptOutConst.Sha256Bytes, OptOutConst.Sha256Bytes);
System.arraycopy(timestamp, 0, records, offset + OptOutConst.Sha256Bytes * 2, Long.BYTES - 1);
records[offset + OptOutConst.EntrySize - 1] = metadata;
final byte[] expectedAdsId = new byte[33];
expectedAdsId[0] = 0x14;
System.arraycopy(adsId, 0, expectedAdsId, 1, OptOutConst.Sha256Bytes);
final OptOutEntry entry = OptOutEntry.parse(records, offset);
Assert.assertArrayEquals(idHash, entry.identityHash);
Assert.assertArrayEquals(expectedAdsId, entry.advertisingId);
Assert.assertEquals(0x56555453525150l, entry.timestamp);
Assert.assertTrue(entry.isTombstone);
}
}