Home | History | Annotate | Download | only in anqp
      1 /*
      2  * Copyright (C) 2017 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 com.android.server.wifi.hotspot2.anqp;
     18 
     19 import java.io.ByteArrayOutputStream;
     20 import java.io.IOException;
     21 import java.nio.charset.StandardCharsets;
     22 
     23 /**
     24  * Utility class containing test data and object for {@link IconInfo}.
     25  */
     26 public class IconInfoTestUtil {
     27     // Test data
     28     private static final int TEST_WIDTH = 9811;
     29     private static final int TEST_HEIGHT = 4523;
     30     private static final String TEST_LANGUAGE = "en";
     31     private static final String TEST_TYPE = "png";
     32     private static final String TEST_FILENAME = "testicon.png";
     33 
     34     /**
     35      * {@link IconInfo} object with pre-defined test data.
     36      */
     37     public static final IconInfo TEST_ICON_INFO =
     38             new IconInfo(TEST_WIDTH, TEST_HEIGHT, TEST_LANGUAGE, TEST_TYPE, TEST_FILENAME);
     39 
     40     /**
     41      * Raw bytes of icon info with pre-defined test data.
     42      */
     43     public static final byte[] TEST_ICON_INFO_RAW_BYTES = getTestData();
     44 
     45     /**
     46      * Generate raw bytes based on the pre-defined test data.
     47      *
     48      * @return array of bytes
     49      */
     50     private static byte[] getTestData() {
     51         try {
     52             ByteArrayOutputStream out = new ByteArrayOutputStream();
     53             writeShortLE(out, TEST_WIDTH);
     54             writeShortLE(out, TEST_HEIGHT);
     55             out.write(TEST_LANGUAGE.getBytes(StandardCharsets.US_ASCII));
     56             out.write((byte) 0);    // Padding for language code.
     57             writeByteArrayWithLength(out, TEST_TYPE.getBytes(StandardCharsets.US_ASCII));
     58             writeByteArrayWithLength(out, TEST_FILENAME.getBytes(StandardCharsets.UTF_8));
     59             return out.toByteArray();
     60         } catch (IOException e) {
     61             return null;
     62         }
     63     }
     64 
     65     /**
     66      * Write the lower 2-bytes of an integer to the given output stream in Little-Endian.
     67      *
     68      * @param out The output stream to write to
     69      * @param value The integer value to write
     70      */
     71     private static void writeShortLE(ByteArrayOutputStream out, int value) {
     72         out.write(value & 0xFF);
     73         out.write((value >> 8) & 0xFF);
     74     }
     75 
     76     /**
     77      * Write the given byte array to the given output stream, the array data is prefixed with a
     78      * byte specifying the length of the byte array.
     79      *
     80      * @param out The output stream to write to
     81      * @param data The byte array to write
     82      * @throws IOException
     83      */
     84     private static void writeByteArrayWithLength(ByteArrayOutputStream out, byte[] data)
     85             throws IOException {
     86         out.write((byte) data.length);
     87         out.write(data);
     88     }
     89 }
     90