1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 // Regression test for WW8PLCF::IsValidLength, the length guard shared by
23 // WW8PLCF::ReadPLCF and the WW8PLCFspecial constructor (ww8scan.cxx).
24
25 #include "gtest/gtest.h"
26
27 #include "ww8scan.hxx" // WW8PLCF::IsValidLength (inline, no filter link)
28 #include "ww8struc.hxx" // WW8_CP_MAX
29 #include <sal/types.h>
30
31 // Non-positive count must be rejected.
TEST(WW8Plcf,IsValidLengthRejectsNegativeAndZero)32 TEST(WW8Plcf, IsValidLengthRejectsNegativeAndZero)
33 {
34 EXPECT_FALSE( WW8PLCF::IsValidLength( -1 ) ); // 0xFFFFFFFF narrowed
35 EXPECT_FALSE( WW8PLCF::IsValidLength( 0 ) );
36 EXPECT_FALSE( WW8PLCF::IsValidLength( -64 ) );
37 EXPECT_FALSE( WW8PLCF::IsValidLength( SAL_MIN_INT32 ) );
38 }
39
40 // Lengths close to the maximum must be rejected too
TEST(WW8Plcf,IsValidLengthRejectsOverflowingUpperRange)41 TEST(WW8Plcf, IsValidLengthRejectsOverflowingUpperRange)
42 {
43 EXPECT_FALSE( WW8PLCF::IsValidLength( WW8_CP_MAX ) );
44 EXPECT_FALSE( WW8PLCF::IsValidLength( WW8_CP_MAX - 1 ) );
45 EXPECT_FALSE( WW8PLCF::IsValidLength( WW8_CP_MAX - 3 ) );
46 }
47
48 // Genuine lengths are accepted
TEST(WW8Plcf,IsValidLengthAcceptsBenignLengths)49 TEST(WW8Plcf, IsValidLengthAcceptsBenignLengths)
50 {
51 EXPECT_TRUE( WW8PLCF::IsValidLength( 1 ) );
52 EXPECT_TRUE( WW8PLCF::IsValidLength( 12 ) );
53 EXPECT_TRUE( WW8PLCF::IsValidLength( 64 ) );
54 EXPECT_TRUE( WW8PLCF::IsValidLength( WW8_CP_MAX - 4 ) ); // largest accepted
55 }
56
57 // For every accepted length, the element count both readers allocate is
58 // large enough to hold the nPLCF bytes the subsequent Read copies in.
TEST(WW8Plcf,AcceptedLengthSizesBufferToHoldRead)59 TEST(WW8Plcf, AcceptedLengthSizesBufferToHoldRead)
60 {
61 const sal_Int32 aLens[] = { 1, 3, 4, 5, 12, 64, 4096, WW8_CP_MAX - 4 };
62 for ( size_t i = 0; i < sizeof(aLens) / sizeof(aLens[0]); ++i )
63 {
64 const sal_Int32 nPLCF = aLens[i];
65 ASSERT_TRUE( WW8PLCF::IsValidLength( nPLCF ) );
66 // Done in 64-bit to model the allocation without itself overflowing.
67 const sal_Int64 nElems = ( static_cast<sal_Int64>(nPLCF) + 3 ) / 4;
68 const sal_Int64 nCapacityBytes = nElems * 4; // sizeof(WW8_CP) == 4
69 EXPECT_GT( nElems, 0 );
70 EXPECT_GE( nCapacityBytes, static_cast<sal_Int64>(nPLCF) );
71 }
72 }
73
main(int argc,char ** argv)74 int main( int argc, char** argv )
75 {
76 ::testing::InitGoogleTest( &argc, argv );
77 return RUN_ALL_TESTS();
78 }
79