1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "entry_name_utils-inl.h"
18 
19 #include <gtest/gtest.h>
20 
TEST(entry_name_utils,NullChars)21 TEST(entry_name_utils, NullChars) {
22   // 'A', 'R', '\0', 'S', 'E'
23   const uint8_t zeroes[] = {0x41, 0x52, 0x00, 0x53, 0x45};
24   ASSERT_FALSE(IsValidEntryName(zeroes, sizeof(zeroes)));
25 
26   const uint8_t zeroes_continuation_chars[] = {0xc2, 0xa1, 0xc2, 0x00};
27   ASSERT_FALSE(IsValidEntryName(zeroes_continuation_chars, sizeof(zeroes_continuation_chars)));
28 }
29 
TEST(entry_name_utils,InvalidSequence)30 TEST(entry_name_utils, InvalidSequence) {
31   // 0xfe is an invalid start byte
32   const uint8_t invalid[] = {0x41, 0xfe};
33   ASSERT_FALSE(IsValidEntryName(invalid, sizeof(invalid)));
34 
35   // 0x91 is an invalid start byte (it's a valid continuation byte).
36   const uint8_t invalid2[] = {0x41, 0x91};
37   ASSERT_FALSE(IsValidEntryName(invalid2, sizeof(invalid2)));
38 }
39 
TEST(entry_name_utils,TruncatedContinuation)40 TEST(entry_name_utils, TruncatedContinuation) {
41   // Malayalam script with truncated bytes. There should be 2 bytes
42   // after 0xe0
43   const uint8_t truncated[] = {0xe0, 0xb4, 0x85, 0xe0, 0xb4};
44   ASSERT_FALSE(IsValidEntryName(truncated, sizeof(truncated)));
45 
46   // 0xc2 is the start of a 2 byte sequence that we've subsequently
47   // dropped.
48   const uint8_t truncated2[] = {0xc2, 0xc2, 0xa1};
49   ASSERT_FALSE(IsValidEntryName(truncated2, sizeof(truncated2)));
50 }
51 
TEST(entry_name_utils,BadContinuation)52 TEST(entry_name_utils, BadContinuation) {
53   // 0x41 is an invalid continuation char, since it's MSBs
54   // aren't "10..." (are 01).
55   const uint8_t bad[] = {0xc2, 0xa1, 0xc2, 0x41};
56   ASSERT_FALSE(IsValidEntryName(bad, sizeof(bad)));
57 
58   // 0x41 is an invalid continuation char, since it's MSBs
59   // aren't "10..." (are 11).
60   const uint8_t bad2[] = {0xc2, 0xa1, 0xc2, 0xfe};
61   ASSERT_FALSE(IsValidEntryName(bad2, sizeof(bad2)));
62 }
63