Home | History | Annotate | Download | only in libziparchive
      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 
     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,
     28                                 sizeof(zeroes_continuation_chars)));
     29 }
     30 
     31 TEST(entry_name_utils, InvalidSequence) {
     32   // 0xfe is an invalid start byte
     33   const uint8_t invalid[] = { 0x41, 0xfe };
     34   ASSERT_FALSE(IsValidEntryName(invalid, sizeof(invalid)));
     35 
     36   // 0x91 is an invalid start byte (it's a valid continuation byte).
     37   const uint8_t invalid2[] = { 0x41, 0x91 };
     38   ASSERT_FALSE(IsValidEntryName(invalid2, sizeof(invalid2)));
     39 }
     40 
     41 TEST(entry_name_utils, TruncatedContinuation) {
     42   // Malayalam script with truncated bytes. There should be 2 bytes
     43   // after 0xe0
     44   const uint8_t truncated[] = { 0xe0, 0xb4, 0x85, 0xe0, 0xb4 };
     45   ASSERT_FALSE(IsValidEntryName(truncated, sizeof(truncated)));
     46 
     47   // 0xc2 is the start of a 2 byte sequence that we've subsequently
     48   // dropped.
     49   const uint8_t truncated2[] = { 0xc2, 0xc2, 0xa1 };
     50   ASSERT_FALSE(IsValidEntryName(truncated2, sizeof(truncated2)));
     51 }
     52 
     53 TEST(entry_name_utils, BadContinuation) {
     54   // 0x41 is an invalid continuation char, since it's MSBs
     55   // aren't "10..." (are 01).
     56   const uint8_t bad[] = { 0xc2, 0xa1, 0xc2, 0x41 };
     57   ASSERT_FALSE(IsValidEntryName(bad, sizeof(bad)));
     58 
     59   // 0x41 is an invalid continuation char, since it's MSBs
     60   // aren't "10..." (are 11).
     61   const uint8_t bad2[] = { 0xc2, 0xa1, 0xc2, 0xfe };
     62   ASSERT_FALSE(IsValidEntryName(bad2, sizeof(bad2)));
     63 }
     64