summaryrefslogtreecommitdiff
path: root/adservices/tests/unittest/service-core/src/jni/java/com/android/adservices/HpkeJniTest.java
blob: e7a8171984956412fd0fd7c02fd0767d695e1196 (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
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.adservices;

import org.junit.Assert;
import org.junit.Test;

import java.util.Base64;

public class HpkeJniTest {

    private static final byte[] sAssociatedData = "associated_data".getBytes();
    private static final byte[] sPlaintext = "plaintext".getBytes();
    private static final byte[] sCiphertext =
            decode("0Ie+jDZ/Hznx1IrIkS06V+kAHuD5RsybXWwrKRIbGEL5TJT4/HYny2SHfWbeXxMydwvS0FEZqvzs");
    private static final byte[] sPublicKey = decode("rSJBSUYG0ebvfW1AXCWO0CMGMJhDzpfQm3eLyw1uxX8=");
    private static final byte[] sPrivateKey =
            decode("f86EzLmGaVmc+PwjJk5ADPE4ijQvliWf0CQyY/Zyy7I=");

    @Test
    public void testHpkeEncrypt_Success() {
        final byte[] result = HpkeJni.encrypt(sPublicKey, sPlaintext, sAssociatedData);
        Assert.assertNotNull(result);
        Assert.assertTrue(result.length > 0);
    }

    @Test
    public void testHpkeDecrypt_Success() {
        final byte[] result = HpkeJni.decrypt(sPrivateKey, sCiphertext, sAssociatedData);
        Assert.assertNotNull(result);
        Assert.assertTrue(result.length > 0);
        Assert.assertTrue(new String(sPlaintext).equals(new String(result)));
    }

    @Test
    public void testHpkeEncryptDecrypt_Success() {
        final byte[] ciphertext = HpkeJni.encrypt(sPublicKey, sPlaintext, sAssociatedData);
        Assert.assertNotNull(ciphertext);
        Assert.assertTrue(ciphertext.length > 0);
        Assert.assertFalse(new String(sPlaintext).equals(new String(ciphertext)));

        final byte[] plaintext = HpkeJni.decrypt(sPrivateKey, ciphertext, sAssociatedData);
        Assert.assertNotNull(plaintext);
        Assert.assertTrue(plaintext.length > 0);
        Assert.assertTrue(new String(sPlaintext).equals(new String(plaintext)));
    }

    @Test
    public void testHpkeEncrypt_publicKeyNull_fail() {
        final byte[] result = HpkeJni.encrypt(/* publicKey= */ null, sPlaintext, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeEncrypt_publicKeyShorterThan32_fail() {
        final byte[] shortPublicKey = new byte[31];
        final byte[] result = HpkeJni.encrypt(shortPublicKey, sPlaintext, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeEncrypt_publicKeyLongerThan32_fail() {
        final byte[] longPublicKey = new byte[33];
        final byte[] result = HpkeJni.encrypt(longPublicKey, sPlaintext, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeEncrypt_plainTextNull_fail() {
        final byte[] result = HpkeJni.encrypt(sPublicKey, /* plainText = */ null, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeEncrypt_plainTextEmpty_success() {
        final byte[] emptyPlainText = new byte[] {};
        final byte[] result = HpkeJni.encrypt(sPublicKey, emptyPlainText, sAssociatedData);
        Assert.assertNotNull(result);
        Assert.assertTrue(result.length > 0);
    }

    @Test
    public void testHpkeEncrypt_associatedDataNull_fail() {
        final byte[] result = HpkeJni.encrypt(sPublicKey, sPlaintext, /* associatedData = */ null);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeEncrypt_associatedDataEmpty_success() {
        final byte[] emptyAssociatedData = new byte[] {};
        final byte[] result = HpkeJni.encrypt(sPublicKey, sPlaintext, emptyAssociatedData);
        Assert.assertNotNull(result);
        Assert.assertTrue(result.length > 0);
    }

    @Test
    public void testHpkeDecrypt_privateKeyNull_fail() {
        final byte[] result = HpkeJni.decrypt(/* privateKey= */ null, sCiphertext, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkDecrypt_privateKeyShorterThan32_fail() {
        final byte[] shortPrivateKey = new byte[31];
        final byte[] result = HpkeJni.decrypt(shortPrivateKey, sCiphertext, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeDecrypt_privateKeyLargerThan32_fail() {
        final byte[] longPrivateKey = new byte[33];
        final byte[] result = HpkeJni.decrypt(longPrivateKey, sCiphertext, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeDecrypt_privateKeyInvalid_fail() {
        final byte[] privateKey = new byte[32];
        final byte[] result = HpkeJni.decrypt(privateKey, sCiphertext, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeDecrypt_ciphertextNull_fail() {
        final byte[] result =
                HpkeJni.encrypt(sPrivateKey, /* ciphertext = */ null, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeDecrypt_ciphertextInvalid_fail() {
        final byte[] emptyCiphertext = new byte[] {};
        final byte[] result = HpkeJni.decrypt(sPrivateKey, emptyCiphertext, sAssociatedData);
        Assert.assertNull(result);
    }

    @Test
    public void testHpkeDecrypt_associatedDataNull_fail() {
        final byte[] result =
                HpkeJni.decrypt(sPrivateKey, sCiphertext, /* associatedData = */ null);
        Assert.assertNull(result);
    }

    private static byte[] decode(String value) {
        return Base64.getDecoder().decode(value.getBytes());
    }
}