Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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 package android.util.proto.cts;
     18 
     19 import android.util.proto.ProtoOutputStream;
     20 import android.util.proto.cts.nano.Test;
     21 
     22 import com.google.protobuf.nano.MessageNano;
     23 import junit.framework.TestCase;
     24 import org.junit.Assert;
     25 
     26 /**
     27  * Test tag related methods on ProtoOutputStream.
     28  */
     29 public class ProtoOutputStreamTagTest extends TestCase {
     30     /**
     31      * Test that check field ID matches exactly the field Id.
     32      */
     33     public void testCheckFieldIdTypes() throws Exception {
     34         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_TYPE_DOUBLE, ProtoOutputStream.FIELD_TYPE_INT32);
     35         ProtoOutputStream.checkFieldId(1 | ProtoOutputStream.FIELD_TYPE_ENUM, ProtoOutputStream.FIELD_TYPE_ENUM);
     36     }
     37 
     38     /**
     39      * Test that check field ID matches the table of count checks.
     40      */
     41     public void testCheckFieldIdCounts() throws Exception {
     42         // UNKNOWN provided
     43         ProtoOutputStream.checkFieldId(1 | ProtoOutputStream.FIELD_COUNT_UNKNOWN,
     44                 ProtoOutputStream.FIELD_COUNT_UNKNOWN);
     45         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_COUNT_UNKNOWN, ProtoOutputStream.FIELD_COUNT_SINGLE);
     46         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_COUNT_UNKNOWN, ProtoOutputStream.FIELD_COUNT_REPEATED);
     47         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_COUNT_UNKNOWN, ProtoOutputStream.FIELD_COUNT_PACKED);
     48 
     49         // SINGLE provided
     50         ProtoOutputStream.checkFieldId(1 | ProtoOutputStream.FIELD_COUNT_SINGLE,
     51                 ProtoOutputStream.FIELD_COUNT_SINGLE);
     52         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_COUNT_SINGLE, ProtoOutputStream.FIELD_COUNT_REPEATED);
     53         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_COUNT_SINGLE, ProtoOutputStream.FIELD_COUNT_PACKED);
     54 
     55         // REPEATED provided
     56         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_COUNT_REPEATED, ProtoOutputStream.FIELD_COUNT_SINGLE);
     57         ProtoOutputStream.checkFieldId(1 | ProtoOutputStream.FIELD_COUNT_REPEATED,
     58                 ProtoOutputStream.FIELD_COUNT_REPEATED);
     59         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_COUNT_REPEATED, ProtoOutputStream.FIELD_COUNT_PACKED);
     60 
     61         // PACKED provided
     62         assertCheckFieldIdThrows(ProtoOutputStream.FIELD_COUNT_PACKED, ProtoOutputStream.FIELD_COUNT_SINGLE);
     63         ProtoOutputStream.checkFieldId(1 | ProtoOutputStream.FIELD_COUNT_PACKED,
     64                 ProtoOutputStream.FIELD_COUNT_REPEATED);
     65         ProtoOutputStream.checkFieldId(1 | ProtoOutputStream.FIELD_COUNT_PACKED,
     66                 ProtoOutputStream.FIELD_COUNT_PACKED);
     67     }
     68 
     69     /**
     70      * Test that the error message for types works.
     71      */
     72     public void testCheckFieldIdErrorMessage() throws Exception {
     73         checkMessageForType(ProtoOutputStream.FIELD_TYPE_DOUBLE, "Double");
     74 
     75         checkMessageForCount(ProtoOutputStream.FIELD_COUNT_SINGLE, "");
     76         checkMessageForCount(ProtoOutputStream.FIELD_COUNT_REPEATED, "Repeated");
     77         checkMessageForCount(ProtoOutputStream.FIELD_COUNT_PACKED, "Packed");
     78     }
     79 
     80     /**
     81      * Check that the exception message properly gets the type.
     82      */
     83     private void checkMessageForType(long fieldType, String string) {
     84         final long badType = fieldType == ProtoOutputStream.FIELD_TYPE_DOUBLE
     85                 ? ProtoOutputStream.FIELD_TYPE_INT32
     86                 : ProtoOutputStream.FIELD_TYPE_DOUBLE;
     87         final String badTypeString = badType == ProtoOutputStream.FIELD_TYPE_DOUBLE
     88                 ? "Double"
     89                 : "Int32";
     90         final long goodCount = ProtoOutputStream.FIELD_COUNT_REPEATED;
     91 
     92         // Try it in the provided name
     93         try {
     94             ProtoOutputStream.checkFieldId(42 | goodCount | badType, goodCount | fieldType);
     95             fail("Should have thrown an exception.");
     96         } catch (IllegalArgumentException ex) {
     97             assertEquals("writeRepeated" + string
     98                         + " called for field 42 which should be used"
     99                         + " with writeRepeated" + badTypeString + ".",
    100                     ex.getMessage());
    101         }
    102 
    103         // Try it in the expected name
    104         try {
    105             ProtoOutputStream.checkFieldId(43 | goodCount | fieldType, goodCount | badType);
    106             fail("Should have thrown an exception.");
    107         } catch (IllegalArgumentException ex) {
    108             assertEquals("writeRepeated" + badTypeString
    109                         + " called for field 43 which should be used"
    110                         + " with writeRepeated" + string + ".",
    111                     ex.getMessage());
    112         }
    113     }
    114 
    115 
    116     /**
    117      * Check that the exception message properly gets the count.
    118      */
    119     private void checkMessageForCount(long fieldCount, String string) {
    120         final long badCount = fieldCount == ProtoOutputStream.FIELD_COUNT_SINGLE
    121                 ? ProtoOutputStream.FIELD_COUNT_REPEATED
    122                 : ProtoOutputStream.FIELD_COUNT_SINGLE;
    123         final String badCountString = badCount == ProtoOutputStream.FIELD_COUNT_SINGLE
    124                 ? ""
    125                 : "Repeated";
    126         final long goodType = ProtoOutputStream.FIELD_TYPE_FIXED32;
    127 
    128         // Try it in the provided name
    129         try {
    130             ProtoOutputStream.checkFieldId(44 | badCount | goodType, fieldCount | goodType);
    131             fail("Should have thrown an exception.");
    132         } catch (IllegalArgumentException ex) {
    133             assertEquals("write" + string
    134                     + "Fixed32 called for field 44 which should be used"
    135                     + " with write" + badCountString + "Fixed32.",
    136                     ex.getMessage());
    137         }
    138 
    139         // Try it in the expected name
    140         try {
    141             ProtoOutputStream.checkFieldId(45 | fieldCount | goodType, badCount | goodType);
    142             fail("Should have thrown an exception.");
    143         } catch (IllegalArgumentException ex) {
    144             String extraString = "";
    145             if (fieldCount == ProtoOutputStream.FIELD_COUNT_PACKED) {
    146                 extraString = " or writeRepeatedFixed32";
    147             }
    148             assertEquals("write" + badCountString
    149                     + "Fixed32 called for field 45 which should be used"
    150                     + " with write" + string + "Fixed32" + extraString + ".",
    151                     ex.getMessage());
    152         }
    153     }
    154 
    155     /**
    156      * Validate one call to checkFieldId that is expected to throw.
    157      */
    158     public void assertCheckFieldIdThrows(long fieldId, long expectedFlags)
    159             throws Exception {
    160         try {
    161             ProtoOutputStream.checkFieldId(fieldId, expectedFlags);
    162             throw new Exception("checkFieldId(0x" + Long.toHexString(fieldId)
    163                     + ", 0x" + Long.toHexString(expectedFlags) + ") did not throw.");
    164         } catch (IllegalArgumentException ex) {
    165             // good
    166         }
    167     }
    168 }
    169