1 // Copyright 2020 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 //     https://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 #include "securegcm/java_util.h"
16 
17 #include <limits>
18 
19 #include "gtest/gtest.h"
20 
21 namespace securegcm {
22 
23 using securemessage::ByteBuffer;
24 
25 namespace {
26 
27 int32_t kMinInt32 = std::numeric_limits<int32_t>::min();
28 int32_t kMaxInt32 = std::numeric_limits<int32_t>::max();
29 
30 }  // namespace
31 
32 
TEST(JavaUtilTest,TestJavaAdd_InRange)33 TEST(JavaUtilTest, TestJavaAdd_InRange) {
34   EXPECT_EQ(2, java_util::JavaAdd(1, 1));
35 }
36 
TEST(JavaUtilTest,TestJavaAdd_Underflow)37 TEST(JavaUtilTest, TestJavaAdd_Underflow) {
38   EXPECT_EQ(kMaxInt32, java_util::JavaAdd(kMinInt32, -1));
39   EXPECT_EQ(kMaxInt32 - 1, java_util::JavaAdd(kMinInt32, -2));
40   EXPECT_EQ(1, java_util::JavaAdd(kMinInt32, -kMaxInt32));
41 }
42 
TEST(JavaUtilTest,TestJavaAdd_Overflow)43 TEST(JavaUtilTest, TestJavaAdd_Overflow) {
44   EXPECT_EQ(kMinInt32, java_util::JavaAdd(kMaxInt32, 1));
45   EXPECT_EQ(kMinInt32 + 1, java_util::JavaAdd(kMaxInt32, 2));
46   EXPECT_EQ(-2, java_util::JavaAdd(kMaxInt32, kMaxInt32));
47 }
48 
TEST(JavaUtilTest,TestJavaMultiply_InRange)49 TEST(JavaUtilTest, TestJavaMultiply_InRange) {
50   EXPECT_EQ(4, java_util::JavaAdd(2, 2));
51 }
52 
TEST(JavaUtilTest,TestJavaMultiply_Underflow)53 TEST(JavaUtilTest, TestJavaMultiply_Underflow) {
54   EXPECT_EQ(0, java_util::JavaMultiply(kMinInt32, 2));
55   EXPECT_EQ(-(kMinInt32 / 2), java_util::JavaMultiply(kMinInt32 / 2, 3));
56   EXPECT_EQ(kMinInt32, java_util::JavaMultiply(kMinInt32, kMaxInt32));
57 }
58 
TEST(JavaUtilTest,TestJavaMultiply_Overflow)59 TEST(JavaUtilTest, TestJavaMultiply_Overflow) {
60   EXPECT_EQ(-2, java_util::JavaMultiply(kMaxInt32, 2));
61   EXPECT_EQ(kMaxInt32 - 2, java_util::JavaMultiply(kMaxInt32, 3));
62   EXPECT_EQ(1, java_util::JavaMultiply(kMaxInt32, kMaxInt32));
63 }
64 
TEST(JavaUtilTest,TestJavaHashCode_EmptyBytes)65 TEST(JavaUtilTest, TestJavaHashCode_EmptyBytes) {
66   EXPECT_EQ(1, java_util::JavaHashCode(ByteBuffer()));
67 }
68 
TEST(JavaUtilTest,TestJavaHashCode_LongByteArray)69 TEST(JavaUtilTest, TestJavaHashCode_LongByteArray) {
70   const uint8_t kBytes[] = {
71       0x93, 0x75, 0xE1, 0x2E, 0x26, 0x28, 0x54, 0x8C, 0xD9, 0x5C, 0x48, 0x7A,
72       0x07, 0x53, 0x4E, 0xED, 0x28, 0x52, 0x5D, 0x41, 0xE3, 0x18, 0x84, 0x84,
73       0x5F, 0xF6, 0x89, 0x98, 0x25, 0x1E, 0xD9, 0x6C, 0x85, 0xF3, 0x5A, 0x83,
74       0x39, 0x37, 0x4E, 0x77, 0x95, 0xB5, 0x58, 0x7C, 0xD2, 0x55, 0xA0, 0x86,
75       0x13, 0x3F, 0xBF, 0x85, 0xD3, 0xE0, 0x28, 0x90, 0x17, 0x3D, 0x2E, 0xD4,
76       0x4D, 0x95, 0x9C, 0xAE, 0xAD, 0x8A, 0x05, 0x91, 0x5D, 0xC6, 0x4B, 0x09,
77       0xB2, 0xD9, 0x34, 0x64, 0x07, 0x7B, 0x07, 0x8C, 0xA6, 0xC7, 0x1C, 0x10,
78       0x34, 0xD4, 0x30, 0x80, 0x03, 0x4F, 0x2C, 0x70};
79   const int32_t kExpectedHashCode = 1983685004;
80   EXPECT_EQ(kExpectedHashCode,
81             java_util::JavaHashCode(ByteBuffer(kBytes, sizeof(kBytes))));
82 }
83 
84 }  // namespace securegcm
85