Add tests for ODK overflow functions

Merged from http://go/wvgerrit/175855

This includes testing overflow functions that are only used in the Widevine client repository.

This patch also includes the following fix for the previous commit:

Add type casting for ODK overflow function unit tests

Merged from http://go/wvgerrit/175893

The new ODK overflow unit tests are causing some unit test failures in oemcrypto-v18 due to mismatched type comparisons.

PiperOrigin-RevId: 535308670
PiperOrigin-RevId: 534890798
Change-Id: I8bb67e47193a92191a91c83bf8a0de61e1b87793
This commit is contained in:
John Bruce
2023-05-24 10:18:38 -07:00
committed by Robert Shih
parent 8f6dbdb94e
commit db670e7bcc
2 changed files with 34 additions and 1 deletions

View File

@@ -19,7 +19,7 @@ extern "C" {
#define ODK_MINOR_VERSION 2
/* ODK Version string. Date changed automatically on each release. */
#define ODK_RELEASE_DATE "ODK v18.2 2023-05-05"
#define ODK_RELEASE_DATE "ODK v18.2 2023-05-25"
/* The lowest version number for an ODK message. */
#define ODK_FIRST_VERSION 16

View File

@@ -4,6 +4,7 @@
#include "odk.h"
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ostream>
@@ -17,6 +18,7 @@
#include "core_message_serialize_proto.h"
#include "core_message_types.h"
#include "gtest/gtest.h"
#include "odk_overflow.h"
#include "odk_structs.h"
#include "odk_structs_priv.h"
#include "odk_test_helper.h"
@@ -1222,6 +1224,37 @@ TEST(OdkTest, CheckReleaseVersion) {
<< "Version mismatch in odk_structs.h";
}
TEST(OdkOverflowTest, SubtractU64) {
uint64_t result = 0;
EXPECT_FALSE(odk_sub_overflow_u64(10, 5, &result));
EXPECT_EQ(result, static_cast<uint64_t>(10 - 5));
EXPECT_TRUE(odk_sub_overflow_u64(5, 10, &result));
}
TEST(OdkOverflowTest, AddU64) {
uint64_t result = 0;
EXPECT_FALSE(odk_add_overflow_u64(2, 2, &result));
EXPECT_EQ(result, static_cast<uint64_t>(2 + 2));
EXPECT_TRUE(odk_add_overflow_u64(0xffffffffffffffff, 1, &result));
EXPECT_TRUE(odk_add_overflow_u64(1, 0xffffffffffffffff, &result));
}
TEST(OdkOverflowTest, AddUX) {
size_t result = 0;
EXPECT_FALSE(odk_add_overflow_ux(2, 2, &result));
EXPECT_EQ(result, static_cast<uint64_t>(2 + 2));
EXPECT_TRUE(odk_add_overflow_ux(SIZE_MAX, 1, &result));
EXPECT_TRUE(odk_add_overflow_ux(1, SIZE_MAX, &result));
}
TEST(OdkOverflowTest, MultiplyUX) {
size_t result = 0;
EXPECT_FALSE(odk_mul_overflow_ux(2, 7, &result));
EXPECT_EQ(result, static_cast<uint64_t>(2 * 7));
EXPECT_TRUE(odk_mul_overflow_ux(SIZE_MAX >> 1, 4, &result));
EXPECT_TRUE(odk_mul_overflow_ux(4, SIZE_MAX >> 1, &result));
}
} // namespace
} // namespace wvodk_test