|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +//////////////////////////////////////////////////////////////////////////////// |
| 16 | +package com.google.crypto.tink.hybrid.internal; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | +import static org.junit.Assert.assertThrows; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.junit.runners.JUnit4; |
| 24 | + |
| 25 | +@RunWith(JUnit4.class) |
| 26 | +public final class HpkeUtilTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void intToByteArray_works() { |
| 30 | + assertThat(HpkeUtil.intToByteArray(0, 0)).isEqualTo(new byte[] {}); |
| 31 | + assertThat(HpkeUtil.intToByteArray(1, 42)).isEqualTo(new byte[] {(byte) 42}); |
| 32 | + assertThat(HpkeUtil.intToByteArray(2, 0x0102)).isEqualTo(new byte[] {(byte) 01, (byte) 0x02}); |
| 33 | + |
| 34 | + assertThat(HpkeUtil.intToByteArray(1, 0xaa)).isEqualTo(new byte[] {(byte) 0xaa}); |
| 35 | + assertThat(HpkeUtil.intToByteArray(1, 256 - 1)).isEqualTo(new byte[] {(byte) 0xff}); |
| 36 | + assertThat(HpkeUtil.intToByteArray(2, 0xaabb)).isEqualTo(new byte[] {(byte) 0xaa, (byte) 0xbb}); |
| 37 | + assertThat(HpkeUtil.intToByteArray(2, 256 * 256 - 1)) |
| 38 | + .isEqualTo(new byte[] {(byte) 0xff, (byte) 0xff}); |
| 39 | + assertThat(HpkeUtil.intToByteArray(3, 0xaabbcc)) |
| 40 | + .isEqualTo(new byte[] {(byte) 0xaa, (byte) 0xbb, (byte) 0xcc}); |
| 41 | + assertThat(HpkeUtil.intToByteArray(3, 256 * 256 * 256 - 1)) |
| 42 | + .isEqualTo(new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff}); |
| 43 | + assertThat(HpkeUtil.intToByteArray(4, 0x0abbccdd)) |
| 44 | + .isEqualTo(new byte[] {(byte) 0x0a, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd}); |
| 45 | + assertThat(HpkeUtil.intToByteArray(4, Integer.MAX_VALUE)) |
| 46 | + .isEqualTo(new byte[] {(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff}); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void intToByteArray_failsWithInvalidCapacity() { |
| 51 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(5, 0)); |
| 52 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(-1, 0)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void intToByteArray_valueTooLong_fails() { |
| 57 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(0, 1)); |
| 58 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(0, -1)); |
| 59 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(1, 256)); |
| 60 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(1, -1)); |
| 61 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(2, 256 * 256)); |
| 62 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(2, -1)); |
| 63 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(3, 256 * 256 * 256)); |
| 64 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(3, -1)); |
| 65 | + assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(4, -1)); |
| 66 | + assertThrows( |
| 67 | + IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(4, Integer.MIN_VALUE)); |
| 68 | + } |
| 69 | +} |
0 commit comments