Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2012 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 #define LOG_NDEBUG 1
     18 #define LOG_TAG "camera_metadata_tests"
     19 #include "cutils/log.h"
     20 
     21 #include <errno.h>
     22 
     23 #include <vector>
     24 #include <algorithm>
     25 #include "gtest/gtest.h"
     26 #include "system/camera_metadata.h"
     27 #include "camera_metadata_hidden.h"
     28 
     29 #include "camera_metadata_tests_fake_vendor.h"
     30 
     31 #define EXPECT_NULL(x)     EXPECT_EQ((void*)0, x)
     32 #define EXPECT_NOT_NULL(x) EXPECT_NE((void*)0, x)
     33 #define ARRAY_SIZE(a)      (sizeof(a) / sizeof((a)[0]))
     34 
     35 #define OK    0
     36 #define ERROR 1
     37 #define NOT_FOUND (-ENOENT)
     38 
     39 #define _Alignas(T) \
     40     ({struct _AlignasStruct { char c; T field; };       \
     41         offsetof(struct _AlignasStruct, field); })
     42 
     43 #define FINISH_USING_CAMERA_METADATA(m)                         \
     44     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL)); \
     45     free_camera_metadata(m);                                    \
     46 
     47 TEST(camera_metadata, allocate_normal) {
     48     camera_metadata_t *m = NULL;
     49     const size_t entry_capacity = 5;
     50     const size_t data_capacity = 32;
     51 
     52     m = allocate_camera_metadata(entry_capacity, data_capacity);
     53 
     54     EXPECT_NOT_NULL(m);
     55     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
     56     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
     57     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
     58     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
     59 
     60     FINISH_USING_CAMERA_METADATA(m);
     61 }
     62 
     63 TEST(camera_metadata, allocate_nodata) {
     64     camera_metadata_t *m = NULL;
     65 
     66     m = allocate_camera_metadata(1, 0);
     67 
     68     EXPECT_NOT_NULL(m);
     69     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
     70     EXPECT_EQ((size_t)1, get_camera_metadata_entry_capacity(m));
     71     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
     72     EXPECT_EQ((size_t)0, get_camera_metadata_data_capacity(m));
     73 
     74     FINISH_USING_CAMERA_METADATA(m);
     75 }
     76 
     77 TEST(camera_metadata, clone_nodata) {
     78     camera_metadata_t *src = NULL;
     79     camera_metadata_t *copy = NULL;
     80 
     81     src = allocate_camera_metadata(10, 0);
     82 
     83     ASSERT_NE((void*)NULL, (void*)src);
     84     copy = clone_camera_metadata(src);
     85     ASSERT_NE((void*)NULL, (void*)copy);
     86     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(copy));
     87     EXPECT_EQ((size_t)0, get_camera_metadata_entry_capacity(copy));
     88     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(copy));
     89     EXPECT_EQ((size_t)0, get_camera_metadata_data_capacity(copy));
     90 
     91     FINISH_USING_CAMERA_METADATA(src);
     92     FINISH_USING_CAMERA_METADATA(copy);
     93 }
     94 
     95 TEST(camera_metadata, allocate_nothing) {
     96     camera_metadata_t *m = NULL;
     97 
     98     m = allocate_camera_metadata(0, 0);
     99 
    100     ASSERT_NE((void*)NULL, (void*)m);
    101     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
    102     EXPECT_EQ((size_t)0, get_camera_metadata_entry_capacity(m));
    103     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
    104     EXPECT_EQ((size_t)0, get_camera_metadata_data_capacity(m));
    105 }
    106 
    107 TEST(camera_metadata, place_normal) {
    108     camera_metadata_t *m = NULL;
    109     void *buf = NULL;
    110 
    111     const size_t entry_capacity = 5;
    112     const size_t data_capacity = 32;
    113 
    114     size_t buf_size = calculate_camera_metadata_size(entry_capacity,
    115             data_capacity);
    116 
    117     EXPECT_TRUE(buf_size > 0);
    118 
    119     buf = malloc(buf_size);
    120 
    121     EXPECT_NOT_NULL(buf);
    122 
    123     m = place_camera_metadata(buf, buf_size, entry_capacity, data_capacity);
    124 
    125     EXPECT_EQ(buf, (uint8_t*)m);
    126     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
    127     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
    128     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
    129     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
    130 
    131     EXPECT_EQ(OK, validate_camera_metadata_structure(m, &buf_size));
    132 
    133     free(buf);
    134 }
    135 
    136 TEST(camera_metadata, place_nospace) {
    137     camera_metadata_t *m = NULL;
    138     void *buf = NULL;
    139 
    140     const size_t entry_capacity = 5;
    141     const size_t data_capacity = 32;
    142 
    143     size_t buf_size = calculate_camera_metadata_size(entry_capacity,
    144             data_capacity);
    145 
    146     EXPECT_GT(buf_size, (size_t)0);
    147 
    148     buf_size--;
    149 
    150     buf = malloc(buf_size);
    151 
    152     EXPECT_NOT_NULL(buf);
    153 
    154     m = place_camera_metadata(buf, buf_size, entry_capacity, data_capacity);
    155 
    156     EXPECT_NULL(m);
    157 
    158     free(buf);
    159 }
    160 
    161 TEST(camera_metadata, place_extraspace) {
    162     camera_metadata_t *m = NULL;
    163     uint8_t *buf = NULL;
    164 
    165     const size_t entry_capacity = 5;
    166     const size_t data_capacity = 32;
    167     const size_t extra_space = 10;
    168 
    169     size_t buf_size = calculate_camera_metadata_size(entry_capacity,
    170             data_capacity);
    171 
    172     EXPECT_GT(buf_size, (size_t)0);
    173 
    174     buf_size += extra_space;
    175 
    176     buf = (uint8_t*)malloc(buf_size);
    177 
    178     EXPECT_NOT_NULL(buf);
    179 
    180     m = place_camera_metadata(buf, buf_size, entry_capacity, data_capacity);
    181 
    182     EXPECT_EQ((uint8_t*)m, buf);
    183     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
    184     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
    185     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
    186     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
    187     EXPECT_EQ(buf + buf_size - extra_space, (uint8_t*)m + get_camera_metadata_size(m));
    188 
    189     EXPECT_EQ(OK, validate_camera_metadata_structure(m, &buf_size));
    190 
    191     free(buf);
    192 }
    193 
    194 TEST(camera_metadata, get_size) {
    195     camera_metadata_t *m = NULL;
    196     const size_t entry_capacity = 5;
    197     const size_t data_capacity = 32;
    198 
    199     m = allocate_camera_metadata(entry_capacity, data_capacity);
    200 
    201     EXPECT_EQ(calculate_camera_metadata_size(entry_capacity, data_capacity),
    202             get_camera_metadata_size(m) );
    203 
    204     EXPECT_EQ(calculate_camera_metadata_size(0,0),
    205             get_camera_metadata_compact_size(m) );
    206 
    207     FINISH_USING_CAMERA_METADATA(m);
    208 }
    209 
    210 TEST(camera_metadata, add_get_normal) {
    211     camera_metadata_t *m = NULL;
    212     const size_t entry_capacity = 5;
    213     const size_t data_capacity = 128;
    214 
    215     m = allocate_camera_metadata(entry_capacity, data_capacity);
    216 
    217     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    218 
    219     int result;
    220     size_t data_used = 0;
    221     size_t entries_used = 0;
    222 
    223     // INT64
    224 
    225     int64_t exposure_time = 1000000000;
    226     result = add_camera_metadata_entry(m,
    227             ANDROID_SENSOR_EXPOSURE_TIME,
    228             &exposure_time, 1);
    229     EXPECT_EQ(OK, result);
    230     data_used += calculate_camera_metadata_entry_data_size(
    231             get_camera_metadata_tag_type(ANDROID_SENSOR_EXPOSURE_TIME), 1);
    232     entries_used++;
    233 
    234     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    235 
    236     // INT32
    237 
    238     int32_t sensitivity = 800;
    239     result = add_camera_metadata_entry(m,
    240             ANDROID_SENSOR_SENSITIVITY,
    241             &sensitivity, 1);
    242     EXPECT_EQ(OK, result);
    243     data_used += calculate_camera_metadata_entry_data_size(
    244             get_camera_metadata_tag_type(ANDROID_SENSOR_SENSITIVITY), 1);
    245     entries_used++;
    246 
    247     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    248 
    249     // FLOAT
    250 
    251     float focusDistance = 0.5f;
    252     result = add_camera_metadata_entry(m,
    253             ANDROID_LENS_FOCUS_DISTANCE,
    254             &focusDistance, 1);
    255     EXPECT_EQ(OK, result);
    256     data_used += calculate_camera_metadata_entry_data_size(
    257             get_camera_metadata_tag_type(ANDROID_LENS_FOCUS_DISTANCE), 1);
    258     entries_used++;
    259 
    260     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    261 
    262     // Array of FLOAT
    263 
    264     float colorCorrectionGains[] = {1.69f,  1.00f,  1.00f,  2.41f};
    265     result = add_camera_metadata_entry(m,
    266             ANDROID_COLOR_CORRECTION_GAINS,
    267             colorCorrectionGains, ARRAY_SIZE(colorCorrectionGains));
    268     EXPECT_EQ(OK, result);
    269     data_used += calculate_camera_metadata_entry_data_size(
    270            get_camera_metadata_tag_type(ANDROID_COLOR_CORRECTION_GAINS),
    271            ARRAY_SIZE(colorCorrectionGains));
    272     entries_used++;
    273 
    274     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    275 
    276 
    277     // Array of RATIONAL
    278 
    279     camera_metadata_rational_t colorTransform[] = {
    280         {9, 10}, {0, 1}, {0, 1},
    281         {1, 5}, {1, 2}, {0, 1},
    282         {0, 1}, {1, 10}, {7, 10}
    283     };
    284     result = add_camera_metadata_entry(m,
    285             ANDROID_COLOR_CORRECTION_TRANSFORM,
    286             colorTransform, ARRAY_SIZE(colorTransform));
    287     EXPECT_EQ(OK, result);
    288     data_used += calculate_camera_metadata_entry_data_size(
    289            get_camera_metadata_tag_type(ANDROID_COLOR_CORRECTION_TRANSFORM),
    290            ARRAY_SIZE(colorTransform));
    291     entries_used++;
    292 
    293     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    294 
    295     // Check added entries
    296 
    297     size_t index = 0;
    298     camera_metadata_entry entry;
    299 
    300     result = get_camera_metadata_entry(m,
    301             index, &entry);
    302     EXPECT_EQ(OK, result);
    303     EXPECT_EQ(index, (int)entry.index);
    304     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, entry.tag);
    305     EXPECT_EQ(TYPE_INT64, entry.type);
    306     EXPECT_EQ((size_t)1, entry.count);
    307     EXPECT_EQ(exposure_time, *entry.data.i64);
    308     index++;
    309 
    310     result = get_camera_metadata_entry(m,
    311             index, &entry);
    312     EXPECT_EQ(OK, result);
    313     EXPECT_EQ(index, entry.index);
    314     EXPECT_EQ(ANDROID_SENSOR_SENSITIVITY, entry.tag);
    315     EXPECT_EQ(TYPE_INT32, entry.type);
    316     EXPECT_EQ((size_t)1, entry.count);
    317     EXPECT_EQ(sensitivity, *entry.data.i32);
    318     index++;
    319 
    320     result = get_camera_metadata_entry(m,
    321             index, &entry);
    322     EXPECT_EQ(OK, result);
    323     EXPECT_EQ(index, entry.index);
    324     EXPECT_EQ(ANDROID_LENS_FOCUS_DISTANCE, entry.tag);
    325     EXPECT_EQ(TYPE_FLOAT, entry.type);
    326     EXPECT_EQ((size_t)1, entry.count);
    327     EXPECT_EQ(focusDistance, *entry.data.f);
    328     index++;
    329 
    330     result = get_camera_metadata_entry(m,
    331             index, &entry);
    332     EXPECT_EQ(OK, result);
    333     EXPECT_EQ(index, entry.index);
    334     EXPECT_EQ(ANDROID_COLOR_CORRECTION_GAINS, entry.tag);
    335     EXPECT_EQ(TYPE_FLOAT, entry.type);
    336     EXPECT_EQ(ARRAY_SIZE(colorCorrectionGains), entry.count);
    337     for (unsigned int i=0; i < entry.count; i++) {
    338         EXPECT_EQ(colorCorrectionGains[i], entry.data.f[i]);
    339     }
    340     index++;
    341 
    342     result = get_camera_metadata_entry(m,
    343             index, &entry);
    344     EXPECT_EQ(OK, result);
    345     EXPECT_EQ(index, entry.index);
    346     EXPECT_EQ(ANDROID_COLOR_CORRECTION_TRANSFORM, entry.tag);
    347     EXPECT_EQ(TYPE_RATIONAL, entry.type);
    348     EXPECT_EQ(ARRAY_SIZE(colorTransform), entry.count);
    349     for (unsigned int i=0; i < entry.count; i++) {
    350         EXPECT_EQ(colorTransform[i].numerator, entry.data.r[i].numerator);
    351         EXPECT_EQ(colorTransform[i].denominator, entry.data.r[i].denominator);
    352     }
    353     index++;
    354 
    355     EXPECT_EQ(calculate_camera_metadata_size(entry_capacity, data_capacity),
    356             get_camera_metadata_size(m) );
    357 
    358     EXPECT_EQ(calculate_camera_metadata_size(entries_used, data_used),
    359             get_camera_metadata_compact_size(m) );
    360 
    361     IF_ALOGV() {
    362         dump_camera_metadata(m, 0, 2);
    363     }
    364 
    365     FINISH_USING_CAMERA_METADATA(m);
    366 }
    367 
    368 void add_test_metadata(camera_metadata_t *m, int entry_count) {
    369 
    370     EXPECT_NOT_NULL(m);
    371 
    372     int result;
    373     size_t data_used = 0;
    374     size_t entries_used = 0;
    375     int64_t exposure_time;
    376     for (int i=0; i < entry_count; i++ ) {
    377         exposure_time = 100 + i * 100;
    378         result = add_camera_metadata_entry(m,
    379                 ANDROID_SENSOR_EXPOSURE_TIME,
    380                 &exposure_time, 1);
    381         EXPECT_EQ(OK, result);
    382         data_used += calculate_camera_metadata_entry_data_size(
    383                 get_camera_metadata_tag_type(ANDROID_SENSOR_EXPOSURE_TIME), 1);
    384         entries_used++;
    385     }
    386     EXPECT_EQ(data_used, get_camera_metadata_data_count(m));
    387     EXPECT_EQ(entries_used, get_camera_metadata_entry_count(m));
    388     EXPECT_GE(get_camera_metadata_data_capacity(m),
    389             get_camera_metadata_data_count(m));
    390 }
    391 
    392 TEST(camera_metadata, add_get_toomany) {
    393     camera_metadata_t *m = NULL;
    394     const size_t entry_capacity = 5;
    395     const size_t data_capacity = 50;
    396     int result;
    397 
    398     m = allocate_camera_metadata(entry_capacity, data_capacity);
    399 
    400     add_test_metadata(m, entry_capacity);
    401 
    402     int32_t sensitivity = 100;
    403     result = add_camera_metadata_entry(m,
    404             ANDROID_SENSOR_SENSITIVITY,
    405             &sensitivity, 1);
    406 
    407     EXPECT_EQ(ERROR, result);
    408 
    409     camera_metadata_entry entry;
    410     for (unsigned int i=0; i < entry_capacity; i++) {
    411         int64_t exposure_time = 100 + i * 100;
    412         result = get_camera_metadata_entry(m,
    413                 i, &entry);
    414         EXPECT_EQ(OK, result);
    415         EXPECT_EQ(i, entry.index);
    416         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, entry.tag);
    417         EXPECT_EQ(TYPE_INT64, entry.type);
    418         EXPECT_EQ((size_t)1, entry.count);
    419         EXPECT_EQ(exposure_time, *entry.data.i64);
    420     }
    421     entry.tag = 1234;
    422     entry.type = 56;
    423     entry.data.u8 = NULL;
    424     entry.count = 7890;
    425     result = get_camera_metadata_entry(m,
    426             entry_capacity, &entry);
    427     EXPECT_EQ(ERROR, result);
    428     EXPECT_EQ((uint32_t)1234, entry.tag);
    429     EXPECT_EQ((uint8_t)56, entry.type);
    430     EXPECT_EQ(NULL, entry.data.u8);
    431     EXPECT_EQ((size_t)7890, entry.count);
    432 
    433     IF_ALOGV() {
    434         dump_camera_metadata(m, 0, 2);
    435     }
    436 
    437     FINISH_USING_CAMERA_METADATA(m);
    438 }
    439 
    440 TEST(camera_metadata, add_too_much_data) {
    441     camera_metadata_t *m = NULL;
    442     const size_t entry_capacity = 5;
    443     int result;
    444     size_t data_used = entry_capacity * calculate_camera_metadata_entry_data_size(
    445         get_camera_metadata_tag_type(ANDROID_SENSOR_EXPOSURE_TIME), 1);
    446     m = allocate_camera_metadata(entry_capacity + 1, data_used);
    447 
    448 
    449     add_test_metadata(m, entry_capacity);
    450 
    451     int64_t exposure_time = 12345;
    452     result = add_camera_metadata_entry(m,
    453             ANDROID_SENSOR_EXPOSURE_TIME,
    454             &exposure_time, 1);
    455     EXPECT_EQ(ERROR, result);
    456 
    457     FINISH_USING_CAMERA_METADATA(m);
    458 }
    459 
    460 TEST(camera_metadata, copy_metadata) {
    461     camera_metadata_t *m = NULL;
    462     const size_t entry_capacity = 50;
    463     const size_t data_capacity = 450;
    464 
    465     int result;
    466 
    467     m = allocate_camera_metadata(entry_capacity, data_capacity);
    468 
    469     add_test_metadata(m, entry_capacity);
    470 
    471     size_t buf_size = get_camera_metadata_compact_size(m);
    472     EXPECT_LT((size_t)0, buf_size);
    473 
    474     uint8_t *buf = (uint8_t*)malloc(buf_size);
    475     EXPECT_NOT_NULL(buf);
    476 
    477     camera_metadata_t *m2 = copy_camera_metadata(buf, buf_size, m);
    478     EXPECT_NOT_NULL(m2);
    479     EXPECT_EQ(buf, (uint8_t*)m2);
    480     EXPECT_EQ(get_camera_metadata_entry_count(m),
    481             get_camera_metadata_entry_count(m2));
    482     EXPECT_EQ(get_camera_metadata_data_count(m),
    483             get_camera_metadata_data_count(m2));
    484     EXPECT_EQ(get_camera_metadata_entry_capacity(m2),
    485             get_camera_metadata_entry_count(m2));
    486     EXPECT_EQ(get_camera_metadata_data_capacity(m2),
    487             get_camera_metadata_data_count(m2));
    488 
    489     for (unsigned int i=0; i < get_camera_metadata_entry_count(m); i++) {
    490         camera_metadata_entry e1, e2;
    491         int result;
    492         result = get_camera_metadata_entry(m, i, &e1);
    493         EXPECT_EQ(OK, result);
    494         result = get_camera_metadata_entry(m2, i, &e2);
    495         EXPECT_EQ(OK, result);
    496         EXPECT_EQ(e1.index, e2.index);
    497         EXPECT_EQ(e1.tag, e2.tag);
    498         EXPECT_EQ(e1.type, e2.type);
    499         EXPECT_EQ(e1.count, e2.count);
    500         for (unsigned int j=0;
    501              j < e1.count * camera_metadata_type_size[e1.type];
    502              j++) {
    503             EXPECT_EQ(e1.data.u8[j], e2.data.u8[j]);
    504         }
    505     }
    506 
    507     EXPECT_EQ(OK, validate_camera_metadata_structure(m2, &buf_size));
    508     free(buf);
    509 
    510     FINISH_USING_CAMERA_METADATA(m);
    511 }
    512 
    513 TEST(camera_metadata, copy_metadata_extraspace) {
    514     camera_metadata_t *m = NULL;
    515     const size_t entry_capacity = 12;
    516     const size_t data_capacity = 100;
    517 
    518     const size_t extra_space = 10;
    519 
    520     int result;
    521 
    522     m = allocate_camera_metadata(entry_capacity, data_capacity);
    523 
    524     add_test_metadata(m, entry_capacity);
    525 
    526     size_t buf_size = get_camera_metadata_compact_size(m);
    527     EXPECT_LT((size_t)0, buf_size);
    528     buf_size += extra_space;
    529 
    530     uint8_t *buf = (uint8_t*)malloc(buf_size);
    531     EXPECT_NOT_NULL(buf);
    532 
    533     camera_metadata_t *m2 = copy_camera_metadata(buf, buf_size, m);
    534     EXPECT_NOT_NULL(m2);
    535     EXPECT_EQ(buf, (uint8_t*)m2);
    536     EXPECT_EQ(get_camera_metadata_entry_count(m),
    537             get_camera_metadata_entry_count(m2));
    538     EXPECT_EQ(get_camera_metadata_data_count(m),
    539             get_camera_metadata_data_count(m2));
    540     EXPECT_EQ(get_camera_metadata_entry_capacity(m2),
    541             get_camera_metadata_entry_count(m2));
    542     EXPECT_EQ(get_camera_metadata_data_capacity(m2),
    543             get_camera_metadata_data_count(m2));
    544     EXPECT_EQ(buf + buf_size - extra_space,
    545             (uint8_t*)m2 + get_camera_metadata_size(m2) );
    546 
    547     for (unsigned int i=0; i < get_camera_metadata_entry_count(m); i++) {
    548         camera_metadata_entry e1, e2;
    549 
    550         int result;
    551         result = get_camera_metadata_entry(m, i, &e1);
    552         EXPECT_EQ(OK, result);
    553         EXPECT_EQ(i, e1.index);
    554         result = get_camera_metadata_entry(m2, i, &e2);
    555         EXPECT_EQ(OK, result);
    556         EXPECT_EQ(e1.index, e2.index);
    557         EXPECT_EQ(e1.tag, e2.tag);
    558         EXPECT_EQ(e1.type, e2.type);
    559         EXPECT_EQ(e1.count, e2.count);
    560         for (unsigned int j=0;
    561              j < e1.count * camera_metadata_type_size[e1.type];
    562              j++) {
    563             EXPECT_EQ(e1.data.u8[j], e2.data.u8[j]);
    564         }
    565     }
    566 
    567     EXPECT_EQ(OK, validate_camera_metadata_structure(m2, &buf_size));
    568     free(buf);
    569 
    570     FINISH_USING_CAMERA_METADATA(m);
    571 }
    572 
    573 TEST(camera_metadata, copy_metadata_nospace) {
    574     camera_metadata_t *m = NULL;
    575     const size_t entry_capacity = 5;
    576     const size_t data_capacity = 50;
    577 
    578     int result;
    579 
    580     m = allocate_camera_metadata(entry_capacity, data_capacity);
    581 
    582     add_test_metadata(m, entry_capacity);
    583 
    584     size_t buf_size = get_camera_metadata_compact_size(m);
    585     EXPECT_LT((size_t)0, buf_size);
    586 
    587     buf_size--;
    588 
    589     uint8_t *buf = (uint8_t*)malloc(buf_size);
    590     EXPECT_NOT_NULL(buf);
    591 
    592     camera_metadata_t *m2 = copy_camera_metadata(buf, buf_size, m);
    593     EXPECT_NULL(m2);
    594 
    595     free(buf);
    596 
    597     FINISH_USING_CAMERA_METADATA(m);
    598 }
    599 
    600 TEST(camera_metadata, append_metadata) {
    601     camera_metadata_t *m = NULL;
    602     const size_t entry_capacity = 5;
    603     const size_t data_capacity = 50;
    604 
    605     int result;
    606 
    607     m = allocate_camera_metadata(entry_capacity, data_capacity);
    608 
    609     add_test_metadata(m, entry_capacity);
    610 
    611     camera_metadata_t *m2 = NULL;
    612 
    613     m2 = allocate_camera_metadata(entry_capacity*2, data_capacity*2);
    614     EXPECT_NOT_NULL(m2);
    615 
    616     result = append_camera_metadata(m2, m);
    617 
    618     EXPECT_EQ(OK, result);
    619 
    620     EXPECT_EQ(get_camera_metadata_entry_count(m),
    621             get_camera_metadata_entry_count(m2));
    622     EXPECT_EQ(get_camera_metadata_data_count(m),
    623             get_camera_metadata_data_count(m2));
    624     EXPECT_EQ(entry_capacity*2, get_camera_metadata_entry_capacity(m2));
    625     EXPECT_EQ(data_capacity*2,  get_camera_metadata_data_capacity(m2));
    626 
    627     for (unsigned int i=0; i < get_camera_metadata_entry_count(m); i++) {
    628         camera_metadata_entry e1, e2;
    629         int result;
    630         result = get_camera_metadata_entry(m, i, &e1);
    631         EXPECT_EQ(OK, result);
    632         EXPECT_EQ(i, e1.index);
    633         result = get_camera_metadata_entry(m2, i, &e2);
    634         EXPECT_EQ(OK, result);
    635         EXPECT_EQ(e1.index, e2.index);
    636         EXPECT_EQ(e1.tag, e2.tag);
    637         EXPECT_EQ(e1.type, e2.type);
    638         EXPECT_EQ(e1.count, e2.count);
    639         for (unsigned int j=0;
    640              j < e1.count * camera_metadata_type_size[e1.type];
    641              j++) {
    642             EXPECT_EQ(e1.data.u8[j], e2.data.u8[j]);
    643         }
    644     }
    645 
    646     result = append_camera_metadata(m2, m);
    647 
    648     EXPECT_EQ(OK, result);
    649 
    650     EXPECT_EQ(get_camera_metadata_entry_count(m)*2,
    651             get_camera_metadata_entry_count(m2));
    652     EXPECT_EQ(get_camera_metadata_data_count(m)*2,
    653             get_camera_metadata_data_count(m2));
    654     EXPECT_EQ(entry_capacity*2, get_camera_metadata_entry_capacity(m2));
    655     EXPECT_EQ(data_capacity*2,  get_camera_metadata_data_capacity(m2));
    656 
    657     for (unsigned int i=0; i < get_camera_metadata_entry_count(m2); i++) {
    658         camera_metadata_entry e1, e2;
    659 
    660         int result;
    661         result = get_camera_metadata_entry(m,
    662                 i % entry_capacity, &e1);
    663         EXPECT_EQ(OK, result);
    664         EXPECT_EQ(i % entry_capacity, e1.index);
    665         result = get_camera_metadata_entry(m2,
    666                 i, &e2);
    667         EXPECT_EQ(OK, result);
    668         EXPECT_EQ(i, e2.index);
    669         EXPECT_EQ(e1.tag, e2.tag);
    670         EXPECT_EQ(e1.type, e2.type);
    671         EXPECT_EQ(e1.count, e2.count);
    672         for (unsigned int j=0;
    673              j < e1.count * camera_metadata_type_size[e1.type];
    674              j++) {
    675             EXPECT_EQ(e1.data.u8[j], e2.data.u8[j]);
    676         }
    677     }
    678 
    679     FINISH_USING_CAMERA_METADATA(m);
    680     FINISH_USING_CAMERA_METADATA(m2);
    681 }
    682 
    683 TEST(camera_metadata, append_metadata_nospace) {
    684     camera_metadata_t *m = NULL;
    685     const size_t entry_capacity = 5;
    686     const size_t data_capacity = 50;
    687 
    688     int result;
    689 
    690     m = allocate_camera_metadata(entry_capacity, data_capacity);
    691 
    692     add_test_metadata(m, entry_capacity);
    693 
    694     camera_metadata_t *m2 = NULL;
    695 
    696     m2 = allocate_camera_metadata(entry_capacity-1, data_capacity);
    697     EXPECT_NOT_NULL(m2);
    698 
    699     result = append_camera_metadata(m2, m);
    700 
    701     EXPECT_EQ(ERROR, result);
    702     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m2));
    703     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m2));
    704 
    705     FINISH_USING_CAMERA_METADATA(m);
    706     FINISH_USING_CAMERA_METADATA(m2);
    707 }
    708 
    709 TEST(camera_metadata, append_metadata_onespace) {
    710     camera_metadata_t *m = NULL;
    711     const size_t entry_capacity = 5;
    712     const size_t data_capacity = 50;
    713     const size_t entry_capacity2 = entry_capacity * 2 - 2;
    714     const size_t data_capacity2 = data_capacity * 2;
    715     int result;
    716 
    717     m = allocate_camera_metadata(entry_capacity, data_capacity);
    718 
    719     add_test_metadata(m, entry_capacity);
    720 
    721     camera_metadata_t *m2 = NULL;
    722 
    723     m2 = allocate_camera_metadata(entry_capacity2, data_capacity2);
    724     EXPECT_NOT_NULL(m2);
    725 
    726     result = append_camera_metadata(m2, m);
    727 
    728     EXPECT_EQ(OK, result);
    729 
    730     EXPECT_EQ(get_camera_metadata_entry_count(m),
    731             get_camera_metadata_entry_count(m2));
    732     EXPECT_EQ(get_camera_metadata_data_count(m),
    733             get_camera_metadata_data_count(m2));
    734     EXPECT_EQ(entry_capacity2, get_camera_metadata_entry_capacity(m2));
    735     EXPECT_EQ(data_capacity2,  get_camera_metadata_data_capacity(m2));
    736 
    737     for (unsigned int i=0; i < get_camera_metadata_entry_count(m); i++) {
    738         camera_metadata_entry e1, e2;
    739 
    740         int result;
    741         result = get_camera_metadata_entry(m, i, &e1);
    742         EXPECT_EQ(OK, result);
    743         EXPECT_EQ(i, e1.index);
    744         result = get_camera_metadata_entry(m2, i, &e2);
    745         EXPECT_EQ(OK, result);
    746         EXPECT_EQ(e1.index, e2.index);
    747         EXPECT_EQ(e1.tag, e2.tag);
    748         EXPECT_EQ(e1.type, e2.type);
    749         EXPECT_EQ(e1.count, e2.count);
    750         for (unsigned int j=0;
    751              j < e1.count * camera_metadata_type_size[e1.type];
    752              j++) {
    753             EXPECT_EQ(e1.data.u8[j], e2.data.u8[j]);
    754         }
    755     }
    756 
    757     result = append_camera_metadata(m2, m);
    758 
    759     EXPECT_EQ(ERROR, result);
    760     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_count(m2));
    761     EXPECT_EQ(get_camera_metadata_data_count(m),
    762             get_camera_metadata_data_count(m2));
    763     EXPECT_EQ(entry_capacity2, get_camera_metadata_entry_capacity(m2));
    764     EXPECT_EQ(data_capacity2,  get_camera_metadata_data_capacity(m2));
    765 
    766     for (unsigned int i=0; i < get_camera_metadata_entry_count(m2); i++) {
    767         camera_metadata_entry e1, e2;
    768 
    769         int result;
    770         result = get_camera_metadata_entry(m,
    771                 i % entry_capacity, &e1);
    772         EXPECT_EQ(OK, result);
    773         EXPECT_EQ(i % entry_capacity, e1.index);
    774         result = get_camera_metadata_entry(m2, i, &e2);
    775         EXPECT_EQ(OK, result);
    776         EXPECT_EQ(i, e2.index);
    777         EXPECT_EQ(e1.tag, e2.tag);
    778         EXPECT_EQ(e1.type, e2.type);
    779         EXPECT_EQ(e1.count, e2.count);
    780         for (unsigned int j=0;
    781              j < e1.count * camera_metadata_type_size[e1.type];
    782              j++) {
    783             EXPECT_EQ(e1.data.u8[j], e2.data.u8[j]);
    784         }
    785     }
    786 
    787     FINISH_USING_CAMERA_METADATA(m);
    788     FINISH_USING_CAMERA_METADATA(m2);
    789 }
    790 
    791 TEST(camera_metadata, vendor_tags) {
    792     camera_metadata_t *m = NULL;
    793     const size_t entry_capacity = 5;
    794     const size_t data_capacity = 50;
    795     int result;
    796 
    797     m = allocate_camera_metadata(entry_capacity, data_capacity);
    798 
    799     uint8_t superMode = 5;
    800     result = add_camera_metadata_entry(m,
    801             FAKEVENDOR_SENSOR_SUPERMODE,
    802             &superMode, 1);
    803     EXPECT_EQ(ERROR, result);
    804     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    805 
    806     result = add_camera_metadata_entry(m,
    807             ANDROID_REQUEST_METADATA_MODE,
    808             &superMode, 1);
    809     EXPECT_EQ(OK, result);
    810     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    811 
    812     EXPECT_NULL(get_camera_metadata_section_name(FAKEVENDOR_SENSOR_SUPERMODE));
    813     EXPECT_NULL(get_camera_metadata_tag_name(FAKEVENDOR_SENSOR_SUPERMODE));
    814     EXPECT_EQ(-1, get_camera_metadata_tag_type(FAKEVENDOR_SENSOR_SUPERMODE));
    815 
    816     set_camera_metadata_vendor_ops(&fakevendor_ops);
    817 
    818     result = add_camera_metadata_entry(m,
    819             FAKEVENDOR_SENSOR_SUPERMODE,
    820             &superMode, 1);
    821     EXPECT_EQ(OK, result);
    822     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    823 
    824     result = add_camera_metadata_entry(m,
    825             ANDROID_REQUEST_METADATA_MODE,
    826             &superMode, 1);
    827     EXPECT_EQ(OK, result);
    828     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    829 
    830     result = add_camera_metadata_entry(m,
    831             FAKEVENDOR_SCALER_END,
    832             &superMode, 1);
    833     EXPECT_EQ(ERROR, result);
    834     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    835 
    836     EXPECT_STREQ("com.fakevendor.sensor",
    837             get_camera_metadata_section_name(FAKEVENDOR_SENSOR_SUPERMODE));
    838     EXPECT_STREQ("superMode",
    839             get_camera_metadata_tag_name(FAKEVENDOR_SENSOR_SUPERMODE));
    840     EXPECT_EQ(TYPE_BYTE,
    841             get_camera_metadata_tag_type(FAKEVENDOR_SENSOR_SUPERMODE));
    842 
    843     EXPECT_STREQ("com.fakevendor.scaler",
    844             get_camera_metadata_section_name(FAKEVENDOR_SCALER_END));
    845     EXPECT_NULL(get_camera_metadata_tag_name(FAKEVENDOR_SCALER_END));
    846     EXPECT_EQ(-1, get_camera_metadata_tag_type(FAKEVENDOR_SCALER_END));
    847 
    848     set_camera_metadata_vendor_ops(NULL);
    849     // TODO: fix vendor ops. Then the below 3 validations should fail.
    850     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    851 
    852     result = add_camera_metadata_entry(m,
    853             FAKEVENDOR_SENSOR_SUPERMODE,
    854             &superMode, 1);
    855     EXPECT_EQ(ERROR, result);
    856     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    857 
    858     result = add_camera_metadata_entry(m,
    859             ANDROID_REQUEST_METADATA_MODE,
    860             &superMode, 1);
    861     EXPECT_EQ(OK, result);
    862     EXPECT_EQ(OK, validate_camera_metadata_structure(m, NULL));
    863 
    864     EXPECT_NULL(get_camera_metadata_section_name(FAKEVENDOR_SENSOR_SUPERMODE));
    865     EXPECT_NULL(get_camera_metadata_tag_name(FAKEVENDOR_SENSOR_SUPERMODE));
    866     EXPECT_EQ(-1, get_camera_metadata_tag_type(FAKEVENDOR_SENSOR_SUPERMODE));
    867 
    868     // Remove all vendor entries so validation passes
    869     {
    870         camera_metadata_ro_entry_t entry;
    871         EXPECT_EQ(OK, find_camera_metadata_ro_entry(m,
    872                                                     FAKEVENDOR_SENSOR_SUPERMODE,
    873                                                     &entry));
    874         EXPECT_EQ(OK, delete_camera_metadata_entry(m, entry.index));
    875     }
    876 
    877     FINISH_USING_CAMERA_METADATA(m);
    878 }
    879 
    880 TEST(camera_metadata, add_all_tags) {
    881     int total_tag_count = 0;
    882     for (int i = 0; i < ANDROID_SECTION_COUNT; i++) {
    883         total_tag_count += camera_metadata_section_bounds[i][1] -
    884                 camera_metadata_section_bounds[i][0];
    885     }
    886     int entry_data_count = 3;
    887     int conservative_data_space = total_tag_count * entry_data_count * 8;
    888     uint8_t data[entry_data_count * 8];
    889     int32_t *data_int32 = (int32_t *)data;
    890     float *data_float   = (float *)data;
    891     int64_t *data_int64 = (int64_t *)data;
    892     double *data_double = (double *)data;
    893     camera_metadata_rational_t *data_rational =
    894             (camera_metadata_rational_t *)data;
    895 
    896     camera_metadata_t *m = allocate_camera_metadata(total_tag_count,
    897             conservative_data_space);
    898 
    899     ASSERT_NE((void*)NULL, (void*)m);
    900 
    901     int result;
    902 
    903     int counter = 0;
    904     for (int i = 0; i < ANDROID_SECTION_COUNT; i++) {
    905         for (uint32_t tag = camera_metadata_section_bounds[i][0];
    906                 tag < camera_metadata_section_bounds[i][1];
    907              tag++, counter++) {
    908             int type = get_camera_metadata_tag_type(tag);
    909             ASSERT_NE(-1, type);
    910 
    911             switch (type) {
    912                 case TYPE_BYTE:
    913                     data[0] = tag & 0xFF;
    914                     data[1] = (tag >> 8) & 0xFF;
    915                     data[2] = (tag >> 16) & 0xFF;
    916                     break;
    917                 case TYPE_INT32:
    918                     data_int32[0] = tag;
    919                     data_int32[1] = i;
    920                     data_int32[2] = counter;
    921                     break;
    922                 case TYPE_FLOAT:
    923                     data_float[0] = tag;
    924                     data_float[1] = i;
    925                     data_float[2] = counter / (float)total_tag_count;
    926                     break;
    927                 case TYPE_INT64:
    928                     data_int64[0] = (int64_t)tag | ( (int64_t)tag << 32);
    929                     data_int64[1] = i;
    930                     data_int64[2] = counter;
    931                     break;
    932                 case TYPE_DOUBLE:
    933                     data_double[0] = tag;
    934                     data_double[1] = i;
    935                     data_double[2] = counter / (double)total_tag_count;
    936                     break;
    937                 case TYPE_RATIONAL:
    938                     data_rational[0].numerator = tag;
    939                     data_rational[0].denominator = 1;
    940                     data_rational[1].numerator = i;
    941                     data_rational[1].denominator = 1;
    942                     data_rational[2].numerator = counter;
    943                     data_rational[2].denominator = total_tag_count;
    944                     break;
    945                 default:
    946                     FAIL() << "Unknown type field encountered:" << type;
    947                     break;
    948             }
    949             result = add_camera_metadata_entry(m,
    950                     tag,
    951                     data,
    952                     entry_data_count);
    953             ASSERT_EQ(OK, result);
    954 
    955         }
    956     }
    957 
    958     IF_ALOGV() {
    959         dump_camera_metadata(m, 0, 2);
    960     }
    961 
    962     FINISH_USING_CAMERA_METADATA(m);
    963 }
    964 
    965 TEST(camera_metadata, sort_metadata) {
    966     camera_metadata_t *m = NULL;
    967     const size_t entry_capacity = 5;
    968     const size_t data_capacity = 100;
    969 
    970     int result;
    971 
    972     m = allocate_camera_metadata(entry_capacity, data_capacity);
    973 
    974     // Add several unique entries in non-sorted order
    975 
    976     camera_metadata_rational_t colorTransform[] = {
    977         {9, 10}, {0, 1}, {0, 1},
    978         {1, 5}, {1, 2}, {0, 1},
    979         {0, 1}, {1, 10}, {7, 10}
    980     };
    981     result = add_camera_metadata_entry(m,
    982             ANDROID_COLOR_CORRECTION_TRANSFORM,
    983             colorTransform, ARRAY_SIZE(colorTransform));
    984     EXPECT_EQ(OK, result);
    985 
    986     float focus_distance = 0.5f;
    987     result = add_camera_metadata_entry(m,
    988             ANDROID_LENS_FOCUS_DISTANCE,
    989             &focus_distance, 1);
    990     EXPECT_EQ(OK, result);
    991 
    992     int64_t exposure_time = 1000000000;
    993     result = add_camera_metadata_entry(m,
    994             ANDROID_SENSOR_EXPOSURE_TIME,
    995             &exposure_time, 1);
    996     EXPECT_EQ(OK, result);
    997 
    998     int32_t sensitivity = 800;
    999     result = add_camera_metadata_entry(m,
   1000             ANDROID_SENSOR_SENSITIVITY,
   1001             &sensitivity, 1);
   1002     EXPECT_EQ(OK, result);
   1003 
   1004     // Test unsorted find
   1005     camera_metadata_entry_t entry;
   1006     result = find_camera_metadata_entry(m,
   1007             ANDROID_LENS_FOCUS_DISTANCE,
   1008             &entry);
   1009     EXPECT_EQ(OK, result);
   1010     EXPECT_EQ(ANDROID_LENS_FOCUS_DISTANCE, entry.tag);
   1011     EXPECT_EQ((size_t)1, entry.index);
   1012     EXPECT_EQ(TYPE_FLOAT, entry.type);
   1013     EXPECT_EQ((size_t)1, entry.count);
   1014     EXPECT_EQ(focus_distance, *entry.data.f);
   1015 
   1016     result = find_camera_metadata_entry(m,
   1017             ANDROID_NOISE_REDUCTION_STRENGTH,
   1018             &entry);
   1019     EXPECT_EQ(NOT_FOUND, result);
   1020     EXPECT_EQ((size_t)1, entry.index);
   1021     EXPECT_EQ(ANDROID_LENS_FOCUS_DISTANCE, entry.tag);
   1022     EXPECT_EQ(TYPE_FLOAT, entry.type);
   1023     EXPECT_EQ((size_t)1, entry.count);
   1024     EXPECT_EQ(focus_distance, *entry.data.f);
   1025 
   1026     // Sort
   1027     IF_ALOGV() {
   1028         std::cout << "Pre-sorted metadata" << std::endl;
   1029         dump_camera_metadata(m, 0, 2);
   1030     }
   1031 
   1032     result = sort_camera_metadata(m);
   1033     EXPECT_EQ(OK, result);
   1034 
   1035     IF_ALOGV() {
   1036         std::cout << "Sorted metadata" << std::endl;
   1037         dump_camera_metadata(m, 0, 2);
   1038     }
   1039 
   1040     // Test sorted find
   1041     size_t lensFocusIndex = -1;
   1042     {
   1043         std::vector<uint32_t> tags;
   1044         tags.push_back(ANDROID_COLOR_CORRECTION_TRANSFORM);
   1045         tags.push_back(ANDROID_LENS_FOCUS_DISTANCE);
   1046         tags.push_back(ANDROID_SENSOR_EXPOSURE_TIME);
   1047         tags.push_back(ANDROID_SENSOR_SENSITIVITY);
   1048         std::sort(tags.begin(), tags.end());
   1049 
   1050         lensFocusIndex =
   1051             std::find(tags.begin(), tags.end(), ANDROID_LENS_FOCUS_DISTANCE)
   1052             - tags.begin();
   1053     }
   1054 
   1055     result = find_camera_metadata_entry(m,
   1056             ANDROID_LENS_FOCUS_DISTANCE,
   1057             &entry);
   1058     EXPECT_EQ(OK, result);
   1059     EXPECT_EQ(lensFocusIndex, entry.index);
   1060     EXPECT_EQ(ANDROID_LENS_FOCUS_DISTANCE, entry.tag);
   1061     EXPECT_EQ(TYPE_FLOAT, entry.type);
   1062     EXPECT_EQ((size_t)1, (size_t)entry.count);
   1063     EXPECT_EQ(focus_distance, *entry.data.f);
   1064 
   1065     result = find_camera_metadata_entry(m,
   1066             ANDROID_NOISE_REDUCTION_STRENGTH,
   1067             &entry);
   1068     EXPECT_EQ(NOT_FOUND, result);
   1069     EXPECT_EQ(lensFocusIndex, entry.index);
   1070     EXPECT_EQ(ANDROID_LENS_FOCUS_DISTANCE, entry.tag);
   1071     EXPECT_EQ(TYPE_FLOAT, entry.type);
   1072     EXPECT_EQ((size_t)1, entry.count);
   1073     EXPECT_EQ(focus_distance, *entry.data.f);
   1074 
   1075 
   1076     FINISH_USING_CAMERA_METADATA(m);
   1077 }
   1078 
   1079 TEST(camera_metadata, delete_metadata) {
   1080     camera_metadata_t *m = NULL;
   1081     const size_t entry_capacity = 50;
   1082     const size_t data_capacity = 450;
   1083 
   1084     int result;
   1085 
   1086     m = allocate_camera_metadata(entry_capacity, data_capacity);
   1087 
   1088     size_t num_entries = 5;
   1089     size_t data_per_entry =
   1090             calculate_camera_metadata_entry_data_size(TYPE_INT64, 1);
   1091     size_t num_data = num_entries * data_per_entry;
   1092 
   1093     // Delete an entry with data
   1094 
   1095     add_test_metadata(m, num_entries);
   1096     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1097     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1098 
   1099     result = delete_camera_metadata_entry(m, 1);
   1100     EXPECT_EQ(OK, result);
   1101     num_entries--;
   1102     num_data -= data_per_entry;
   1103 
   1104     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1105     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
   1106     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1107     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
   1108 
   1109     result = delete_camera_metadata_entry(m, 4);
   1110     EXPECT_EQ(ERROR, result);
   1111 
   1112     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1113     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
   1114     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1115     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
   1116 
   1117     for (size_t i = 0; i < num_entries; i++) {
   1118         camera_metadata_entry e;
   1119         result = get_camera_metadata_entry(m, i, &e);
   1120         EXPECT_EQ(OK, result);
   1121         EXPECT_EQ(i, e.index);
   1122         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1123         EXPECT_EQ(TYPE_INT64, e.type);
   1124         int64_t exposureTime = i < 1 ? 100 : 200 + 100 * i;
   1125         EXPECT_EQ(exposureTime, *e.data.i64);
   1126     }
   1127 
   1128     // Delete an entry with no data, at end of array
   1129 
   1130     int32_t frameCount = 12;
   1131     result = add_camera_metadata_entry(m,
   1132             ANDROID_REQUEST_FRAME_COUNT,
   1133             &frameCount, 1);
   1134     EXPECT_EQ(OK, result);
   1135     num_entries++;
   1136 
   1137     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1138     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
   1139     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1140     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
   1141 
   1142     camera_metadata_entry e;
   1143     result = get_camera_metadata_entry(m, 4, &e);
   1144     EXPECT_EQ(OK, result);
   1145 
   1146     EXPECT_EQ((size_t)4, e.index);
   1147     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1148     EXPECT_EQ(TYPE_INT32, e.type);
   1149     EXPECT_EQ((size_t)1, e.count);
   1150     EXPECT_EQ(frameCount, *e.data.i32);
   1151 
   1152     result = delete_camera_metadata_entry(m, 4);
   1153     EXPECT_EQ(OK, result);
   1154 
   1155     num_entries--;
   1156     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1157     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
   1158     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1159     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
   1160 
   1161     result = delete_camera_metadata_entry(m, 4);
   1162     EXPECT_EQ(ERROR, result);
   1163 
   1164     result = get_camera_metadata_entry(m, 4, &e);
   1165     EXPECT_EQ(ERROR, result);
   1166 
   1167     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1168     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
   1169     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1170     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
   1171 
   1172     // Delete with extra data on end of array
   1173     result = delete_camera_metadata_entry(m, 3);
   1174     EXPECT_EQ(OK, result);
   1175     num_entries--;
   1176     num_data -= data_per_entry;
   1177 
   1178     for (size_t i = 0; i < num_entries; i++) {
   1179         camera_metadata_entry e2;
   1180         result = get_camera_metadata_entry(m, i, &e2);
   1181         EXPECT_EQ(OK, result);
   1182         EXPECT_EQ(i, e2.index);
   1183         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1184         EXPECT_EQ(TYPE_INT64, e2.type);
   1185         int64_t exposureTime = i < 1 ? 100 : 200 + 100 * i;
   1186         EXPECT_EQ(exposureTime, *e2.data.i64);
   1187     }
   1188 
   1189     // Delete without extra data in front of array
   1190 
   1191     frameCount = 1001;
   1192     result = add_camera_metadata_entry(m,
   1193             ANDROID_REQUEST_FRAME_COUNT,
   1194             &frameCount, 1);
   1195     EXPECT_EQ(OK, result);
   1196     num_entries++;
   1197 
   1198     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1199     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
   1200     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1201     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
   1202 
   1203     result = sort_camera_metadata(m);
   1204     EXPECT_EQ(OK, result);
   1205 
   1206     result = find_camera_metadata_entry(m,
   1207             ANDROID_REQUEST_FRAME_COUNT, &e);
   1208     EXPECT_EQ(OK, result);
   1209     EXPECT_EQ((size_t)0, e.index);
   1210     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1211     EXPECT_EQ(TYPE_INT32, e.type);
   1212     EXPECT_EQ((size_t)1, e.count);
   1213     EXPECT_EQ(frameCount, *e.data.i32);
   1214 
   1215     result = delete_camera_metadata_entry(m, e.index);
   1216     EXPECT_EQ(OK, result);
   1217     num_entries--;
   1218 
   1219     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1220     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
   1221     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1222     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
   1223 
   1224     for (size_t i = 0; i < num_entries; i++) {
   1225         camera_metadata_entry e2;
   1226         result = get_camera_metadata_entry(m, i, &e2);
   1227         EXPECT_EQ(OK, result);
   1228         EXPECT_EQ(i, e2.index);
   1229         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1230         EXPECT_EQ(TYPE_INT64, e2.type);
   1231         int64_t exposureTime = i < 1 ? 100 : 200 + 100 * i;
   1232         EXPECT_EQ(exposureTime, *e2.data.i64);
   1233     }
   1234 }
   1235 
   1236 TEST(camera_metadata, update_metadata) {
   1237     camera_metadata_t *m = NULL;
   1238     const size_t entry_capacity = 50;
   1239     const size_t data_capacity = 450;
   1240 
   1241     int result;
   1242 
   1243     m = allocate_camera_metadata(entry_capacity, data_capacity);
   1244 
   1245     size_t num_entries = 5;
   1246     size_t data_per_entry =
   1247             calculate_camera_metadata_entry_data_size(TYPE_INT64, 1);
   1248     size_t num_data = num_entries * data_per_entry;
   1249 
   1250     add_test_metadata(m, num_entries);
   1251     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1252     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1253 
   1254     // Update with same-size data, doesn't fit in entry
   1255 
   1256     int64_t newExposureTime = 1000;
   1257     camera_metadata_entry_t e;
   1258     result = update_camera_metadata_entry(m,
   1259             0, &newExposureTime, 1, &e);
   1260     EXPECT_EQ(OK, result);
   1261 
   1262     EXPECT_EQ((size_t)0, e.index);
   1263     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1264     EXPECT_EQ(TYPE_INT64, e.type);
   1265     EXPECT_EQ((size_t)1, e.count);
   1266     EXPECT_EQ(newExposureTime, *e.data.i64);
   1267 
   1268     e.count = 0;
   1269     result = get_camera_metadata_entry(m,
   1270             0, &e);
   1271 
   1272     EXPECT_EQ((size_t)0, e.index);
   1273     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1274     EXPECT_EQ(TYPE_INT64, e.type);
   1275     EXPECT_EQ((size_t)1, e.count);
   1276     EXPECT_EQ(newExposureTime, *e.data.i64);
   1277 
   1278     for (size_t i = 1; i < num_entries; i++) {
   1279         camera_metadata_entry e2;
   1280         result = get_camera_metadata_entry(m, i, &e2);
   1281         EXPECT_EQ(OK, result);
   1282         EXPECT_EQ(i, e2.index);
   1283         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1284         EXPECT_EQ(TYPE_INT64, e2.type);
   1285         int64_t exposureTime = 100 + 100 * i;
   1286         EXPECT_EQ(exposureTime, *e2.data.i64);
   1287     }
   1288 
   1289     // Update with larger data
   1290     int64_t newExposures[2] = { 5000, 6000 };
   1291     result = update_camera_metadata_entry(m,
   1292             0, newExposures, 2, &e);
   1293     EXPECT_EQ(OK, result);
   1294     num_data += data_per_entry;
   1295 
   1296     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1297     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1298 
   1299     EXPECT_EQ((size_t)0, e.index);
   1300     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1301     EXPECT_EQ(TYPE_INT64, e.type);
   1302     EXPECT_EQ((size_t)2, e.count);
   1303     EXPECT_EQ(newExposures[0], e.data.i64[0]);
   1304     EXPECT_EQ(newExposures[1], e.data.i64[1]);
   1305 
   1306     e.count = 0;
   1307     result = get_camera_metadata_entry(m,
   1308             0, &e);
   1309 
   1310     EXPECT_EQ((size_t)0, e.index);
   1311     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1312     EXPECT_EQ(TYPE_INT64, e.type);
   1313     EXPECT_EQ((size_t)2, e.count);
   1314     EXPECT_EQ(newExposures[0], e.data.i64[0]);
   1315     EXPECT_EQ(newExposures[1], e.data.i64[1]);
   1316 
   1317     for (size_t i = 1; i < num_entries; i++) {
   1318         camera_metadata_entry e2;
   1319         result = get_camera_metadata_entry(m, i, &e2);
   1320         EXPECT_EQ(OK, result);
   1321         EXPECT_EQ(i, e2.index);
   1322         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1323         EXPECT_EQ(TYPE_INT64, e2.type);
   1324         int64_t exposureTime = 100 + 100 * i;
   1325         EXPECT_EQ(exposureTime, *e2.data.i64);
   1326     }
   1327 
   1328     // Update with smaller data
   1329     newExposureTime = 100;
   1330     result = update_camera_metadata_entry(m,
   1331             0, &newExposureTime, 1, &e);
   1332     EXPECT_EQ(OK, result);
   1333 
   1334     num_data -= data_per_entry;
   1335 
   1336     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1337     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1338 
   1339     EXPECT_EQ((size_t)0, e.index);
   1340     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1341     EXPECT_EQ(TYPE_INT64, e.type);
   1342     EXPECT_EQ((size_t)1, e.count);
   1343     EXPECT_EQ(newExposureTime, *e.data.i64);
   1344 
   1345     e.count = 0;
   1346     result = get_camera_metadata_entry(m,
   1347             0, &e);
   1348 
   1349     EXPECT_EQ((size_t)0, e.index);
   1350     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1351     EXPECT_EQ(TYPE_INT64, e.type);
   1352     EXPECT_EQ((size_t)1, e.count);
   1353     EXPECT_EQ(newExposureTime, *e.data.i64);
   1354 
   1355     for (size_t i = 1; i < num_entries; i++) {
   1356         camera_metadata_entry e2;
   1357         result = get_camera_metadata_entry(m, i, &e2);
   1358         EXPECT_EQ(OK, result);
   1359         EXPECT_EQ(i, e2.index);
   1360         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1361         EXPECT_EQ(TYPE_INT64, e2.type);
   1362         int64_t exposureTime = 100 + 100 * i;
   1363         EXPECT_EQ(exposureTime, *e2.data.i64);
   1364     }
   1365 
   1366     // Update with size fitting in entry
   1367 
   1368     int32_t frameCount = 1001;
   1369     result = add_camera_metadata_entry(m,
   1370             ANDROID_REQUEST_FRAME_COUNT,
   1371             &frameCount, 1);
   1372     EXPECT_EQ(OK, result);
   1373     num_entries++;
   1374 
   1375     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1376     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
   1377     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1378     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
   1379 
   1380     result = sort_camera_metadata(m);
   1381     EXPECT_EQ(OK, result);
   1382 
   1383     result = find_camera_metadata_entry(m,
   1384             ANDROID_REQUEST_FRAME_COUNT, &e);
   1385     EXPECT_EQ(OK, result);
   1386     EXPECT_EQ((size_t)0, e.index);
   1387     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1388     EXPECT_EQ(TYPE_INT32, e.type);
   1389     EXPECT_EQ((size_t)1, e.count);
   1390     EXPECT_EQ(frameCount, *e.data.i32);
   1391 
   1392     int32_t newFrameCount = 0x12349876;
   1393     result = update_camera_metadata_entry(m,
   1394             0, &newFrameCount, 1, &e);
   1395 
   1396     EXPECT_EQ(OK, result);
   1397     EXPECT_EQ((size_t)0, e.index);
   1398     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1399     EXPECT_EQ(TYPE_INT32, e.type);
   1400     EXPECT_EQ((size_t)1, e.count);
   1401     EXPECT_EQ(newFrameCount, *e.data.i32);
   1402 
   1403     result = find_camera_metadata_entry(m,
   1404             ANDROID_REQUEST_FRAME_COUNT, &e);
   1405 
   1406     EXPECT_EQ(OK, result);
   1407     EXPECT_EQ((size_t)0, e.index);
   1408     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1409     EXPECT_EQ(TYPE_INT32, e.type);
   1410     EXPECT_EQ((size_t)1, e.count);
   1411     EXPECT_EQ(newFrameCount, *e.data.i32);
   1412 
   1413     for (size_t i = 1; i < num_entries; i++) {
   1414         camera_metadata_entry e2;
   1415         result = get_camera_metadata_entry(m, i, &e2);
   1416         EXPECT_EQ(OK, result);
   1417         EXPECT_EQ(i, e2.index);
   1418         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1419         EXPECT_EQ(TYPE_INT64, e2.type);
   1420         int64_t exposureTime = 100 * i;
   1421         EXPECT_EQ(exposureTime, *e2.data.i64);
   1422     }
   1423 
   1424     // Update to bigger than entry
   1425 
   1426     int32_t newFrameCounts[4] = { 0x0, 0x1, 0x10, 0x100 };
   1427 
   1428     result = update_camera_metadata_entry(m,
   1429             0, &newFrameCounts, 4, &e);
   1430 
   1431     EXPECT_EQ(OK, result);
   1432 
   1433     num_data += calculate_camera_metadata_entry_data_size(TYPE_INT32,
   1434             4);
   1435 
   1436     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1437     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1438 
   1439     EXPECT_EQ((size_t)0, e.index);
   1440     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1441     EXPECT_EQ(TYPE_INT32, e.type);
   1442     EXPECT_EQ((size_t)4, e.count);
   1443     EXPECT_EQ(newFrameCounts[0], e.data.i32[0]);
   1444     EXPECT_EQ(newFrameCounts[1], e.data.i32[1]);
   1445     EXPECT_EQ(newFrameCounts[2], e.data.i32[2]);
   1446     EXPECT_EQ(newFrameCounts[3], e.data.i32[3]);
   1447 
   1448     e.count = 0;
   1449 
   1450     result = find_camera_metadata_entry(m,
   1451             ANDROID_REQUEST_FRAME_COUNT, &e);
   1452 
   1453     EXPECT_EQ(OK, result);
   1454     EXPECT_EQ((size_t)0, e.index);
   1455     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1456     EXPECT_EQ(TYPE_INT32, e.type);
   1457     EXPECT_EQ((size_t)4, e.count);
   1458     EXPECT_EQ(newFrameCounts[0], e.data.i32[0]);
   1459     EXPECT_EQ(newFrameCounts[1], e.data.i32[1]);
   1460     EXPECT_EQ(newFrameCounts[2], e.data.i32[2]);
   1461     EXPECT_EQ(newFrameCounts[3], e.data.i32[3]);
   1462 
   1463     for (size_t i = 1; i < num_entries; i++) {
   1464         camera_metadata_entry e2;
   1465         result = get_camera_metadata_entry(m, i, &e2);
   1466         EXPECT_EQ(OK, result);
   1467         EXPECT_EQ(i, e2.index);
   1468         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1469         EXPECT_EQ(TYPE_INT64, e2.type);
   1470         int64_t exposureTime = 100 * i;
   1471         EXPECT_EQ(exposureTime, *e2.data.i64);
   1472     }
   1473 
   1474     // Update to smaller than entry
   1475     result = update_camera_metadata_entry(m,
   1476             0, &newFrameCount, 1, &e);
   1477 
   1478     EXPECT_EQ(OK, result);
   1479 
   1480     num_data -= camera_metadata_type_size[TYPE_INT32] * 4;
   1481 
   1482     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1483     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1484 
   1485     EXPECT_EQ((size_t)0, e.index);
   1486     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1487     EXPECT_EQ(TYPE_INT32, e.type);
   1488     EXPECT_EQ((size_t)1, e.count);
   1489     EXPECT_EQ(newFrameCount, *e.data.i32);
   1490 
   1491     result = find_camera_metadata_entry(m,
   1492             ANDROID_REQUEST_FRAME_COUNT, &e);
   1493 
   1494     EXPECT_EQ(OK, result);
   1495     EXPECT_EQ((size_t)0, e.index);
   1496     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1497     EXPECT_EQ(TYPE_INT32, e.type);
   1498     EXPECT_EQ((size_t)1, e.count);
   1499     EXPECT_EQ(newFrameCount, *e.data.i32);
   1500 
   1501     for (size_t i = 1; i < num_entries; i++) {
   1502         camera_metadata_entry_t e2;
   1503         result = get_camera_metadata_entry(m, i, &e2);
   1504         EXPECT_EQ(OK, result);
   1505         EXPECT_EQ(i, e2.index);
   1506         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1507         EXPECT_EQ(TYPE_INT64, e2.type);
   1508         int64_t exposureTime = 100 * i;
   1509         EXPECT_EQ(exposureTime, *e2.data.i64);
   1510     }
   1511 
   1512     // Setup new buffer with no spare data space
   1513 
   1514     result = update_camera_metadata_entry(m,
   1515             1, newExposures, 2, &e);
   1516     EXPECT_EQ(OK, result);
   1517 
   1518     num_data += data_per_entry;
   1519 
   1520     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m));
   1521     EXPECT_EQ(num_data, get_camera_metadata_data_count(m));
   1522 
   1523     EXPECT_EQ((size_t)1, e.index);
   1524     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1525     EXPECT_EQ(TYPE_INT64, e.type);
   1526     EXPECT_EQ((size_t)2, e.count);
   1527     EXPECT_EQ(newExposures[0], e.data.i64[0]);
   1528     EXPECT_EQ(newExposures[1], e.data.i64[1]);
   1529 
   1530     camera_metadata_t *m2;
   1531     m2 = allocate_camera_metadata(get_camera_metadata_entry_count(m),
   1532             get_camera_metadata_data_count(m));
   1533     EXPECT_NOT_NULL(m2);
   1534 
   1535     result = append_camera_metadata(m2, m);
   1536     EXPECT_EQ(OK, result);
   1537 
   1538     result = find_camera_metadata_entry(m2,
   1539             ANDROID_REQUEST_FRAME_COUNT, &e);
   1540 
   1541     EXPECT_EQ(OK, result);
   1542     EXPECT_EQ((size_t)0, e.index);
   1543     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1544     EXPECT_EQ(TYPE_INT32, e.type);
   1545     EXPECT_EQ((size_t)1, e.count);
   1546     EXPECT_EQ(newFrameCount, *e.data.i32);
   1547 
   1548     // Update when there's no more room
   1549 
   1550     result = update_camera_metadata_entry(m2,
   1551             0, &newFrameCounts, 4, &e);
   1552     EXPECT_EQ(ERROR, result);
   1553 
   1554     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m2));
   1555     EXPECT_EQ(num_data, get_camera_metadata_data_count(m2));
   1556 
   1557     EXPECT_EQ((size_t)0, e.index);
   1558     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1559     EXPECT_EQ(TYPE_INT32, e.type);
   1560     EXPECT_EQ((size_t)1, e.count);
   1561     EXPECT_EQ(newFrameCount, *e.data.i32);
   1562 
   1563     // Update when there's no data room, but change fits into entry
   1564 
   1565     newFrameCount = 5;
   1566     result = update_camera_metadata_entry(m2,
   1567             0, &newFrameCount, 1, &e);
   1568     EXPECT_EQ(OK, result);
   1569 
   1570     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m2));
   1571     EXPECT_EQ(num_data, get_camera_metadata_data_count(m2));
   1572 
   1573     EXPECT_EQ((size_t)0, e.index);
   1574     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1575     EXPECT_EQ(TYPE_INT32, e.type);
   1576     EXPECT_EQ((size_t)1, e.count);
   1577     EXPECT_EQ(newFrameCount, *e.data.i32);
   1578 
   1579     result = find_camera_metadata_entry(m2,
   1580             ANDROID_REQUEST_FRAME_COUNT, &e);
   1581 
   1582     EXPECT_EQ(OK, result);
   1583     EXPECT_EQ((size_t)0, e.index);
   1584     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1585     EXPECT_EQ(TYPE_INT32, e.type);
   1586     EXPECT_EQ((size_t)1, e.count);
   1587     EXPECT_EQ(newFrameCount, *e.data.i32);
   1588 
   1589     result = get_camera_metadata_entry(m2, 1, &e);
   1590     EXPECT_EQ((size_t)1, e.index);
   1591     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1592     EXPECT_EQ(TYPE_INT64, e.type);
   1593     EXPECT_EQ((size_t)2, e.count);
   1594     EXPECT_EQ(newExposures[0], e.data.i64[0]);
   1595     EXPECT_EQ(newExposures[1], e.data.i64[1]);
   1596 
   1597     for (size_t i = 2; i < num_entries; i++) {
   1598         camera_metadata_entry_t e2;
   1599         result = get_camera_metadata_entry(m2, i, &e2);
   1600         EXPECT_EQ(OK, result);
   1601         EXPECT_EQ(i, e2.index);
   1602         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1603         EXPECT_EQ(TYPE_INT64, e2.type);
   1604         int64_t exposureTime = 100 * i;
   1605         EXPECT_EQ(exposureTime, *e2.data.i64);
   1606     }
   1607 
   1608     // Update when there's no data room, but data size doesn't change
   1609 
   1610     newExposures[0] = 1000;
   1611 
   1612     result = update_camera_metadata_entry(m2,
   1613             1, newExposures, 2, &e);
   1614     EXPECT_EQ(OK, result);
   1615 
   1616     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m2));
   1617     EXPECT_EQ(num_data, get_camera_metadata_data_count(m2));
   1618 
   1619     EXPECT_EQ((size_t)1, e.index);
   1620     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1621     EXPECT_EQ(TYPE_INT64, e.type);
   1622     EXPECT_EQ((size_t)2, e.count);
   1623     EXPECT_EQ(newExposures[0], e.data.i64[0]);
   1624     EXPECT_EQ(newExposures[1], e.data.i64[1]);
   1625 
   1626     result = find_camera_metadata_entry(m2,
   1627             ANDROID_REQUEST_FRAME_COUNT, &e);
   1628 
   1629     EXPECT_EQ(OK, result);
   1630     EXPECT_EQ((size_t)0, e.index);
   1631     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1632     EXPECT_EQ(TYPE_INT32, e.type);
   1633     EXPECT_EQ((size_t)1, e.count);
   1634     EXPECT_EQ(newFrameCount, *e.data.i32);
   1635 
   1636     for (size_t i = 2; i < num_entries; i++) {
   1637         camera_metadata_entry_t e2;
   1638         result = get_camera_metadata_entry(m2, i, &e2);
   1639         EXPECT_EQ(OK, result);
   1640         EXPECT_EQ(i, e2.index);
   1641         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1642         EXPECT_EQ(TYPE_INT64, e2.type);
   1643         int64_t exposureTime = 100 * i;
   1644         EXPECT_EQ(exposureTime, *e2.data.i64);
   1645     }
   1646 
   1647     // Update when there's no data room, but data size shrinks
   1648 
   1649     result = update_camera_metadata_entry(m2,
   1650             1, &newExposureTime, 1, &e);
   1651     EXPECT_EQ(OK, result);
   1652 
   1653     num_data -= calculate_camera_metadata_entry_data_size(TYPE_INT64, 2);
   1654     num_data += calculate_camera_metadata_entry_data_size(TYPE_INT64, 1);
   1655 
   1656     EXPECT_EQ(num_entries, get_camera_metadata_entry_count(m2));
   1657     EXPECT_EQ(num_data, get_camera_metadata_data_count(m2));
   1658 
   1659     EXPECT_EQ((size_t)1, e.index);
   1660     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e.tag);
   1661     EXPECT_EQ(TYPE_INT64, e.type);
   1662     EXPECT_EQ((size_t)1, e.count);
   1663     EXPECT_EQ(newExposureTime, e.data.i64[0]);
   1664 
   1665     result = find_camera_metadata_entry(m2,
   1666             ANDROID_REQUEST_FRAME_COUNT, &e);
   1667 
   1668     EXPECT_EQ(OK, result);
   1669     EXPECT_EQ((size_t)0, e.index);
   1670     EXPECT_EQ(ANDROID_REQUEST_FRAME_COUNT, e.tag);
   1671     EXPECT_EQ(TYPE_INT32, e.type);
   1672     EXPECT_EQ((size_t)1, e.count);
   1673     EXPECT_EQ(newFrameCount, *e.data.i32);
   1674 
   1675     for (size_t i = 2; i < num_entries; i++) {
   1676         camera_metadata_entry_t e2;
   1677         result = get_camera_metadata_entry(m2, i, &e2);
   1678         EXPECT_EQ(OK, result);
   1679         EXPECT_EQ(i, e2.index);
   1680         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, e2.tag);
   1681         EXPECT_EQ(TYPE_INT64, e2.type);
   1682         int64_t exposureTime = 100 * i;
   1683         EXPECT_EQ(exposureTime, *e2.data.i64);
   1684     }
   1685 
   1686 }
   1687 
   1688 TEST(camera_metadata, memcpy) {
   1689     camera_metadata_t *m = NULL;
   1690     const size_t entry_capacity = 50;
   1691     const size_t data_capacity = 450;
   1692 
   1693     int result;
   1694 
   1695     m = allocate_camera_metadata(entry_capacity, data_capacity);
   1696 
   1697     add_test_metadata(m, 5);
   1698 
   1699     size_t m_size = get_camera_metadata_size(m);
   1700     uint8_t *dst = new uint8_t[m_size];
   1701 
   1702     memcpy(dst, m, m_size);
   1703 
   1704     camera_metadata_t *m2 = reinterpret_cast<camera_metadata_t*>(dst);
   1705 
   1706     ASSERT_EQ(get_camera_metadata_size(m),
   1707             get_camera_metadata_size(m2));
   1708     EXPECT_EQ(get_camera_metadata_compact_size(m),
   1709             get_camera_metadata_compact_size(m2));
   1710     ASSERT_EQ(get_camera_metadata_entry_count(m),
   1711             get_camera_metadata_entry_count(m2));
   1712     EXPECT_EQ(get_camera_metadata_entry_capacity(m),
   1713             get_camera_metadata_entry_capacity(m2));
   1714     EXPECT_EQ(get_camera_metadata_data_count(m),
   1715             get_camera_metadata_data_count(m2));
   1716     EXPECT_EQ(get_camera_metadata_data_capacity(m),
   1717             get_camera_metadata_data_capacity(m2));
   1718 
   1719     camera_metadata_entry_t e1, e2;
   1720     for (size_t i = 0; i < get_camera_metadata_entry_count(m); i++) {
   1721         result = get_camera_metadata_entry(m, i, &e1);
   1722         ASSERT_EQ(OK, result);
   1723         result = get_camera_metadata_entry(m2, i, &e2);
   1724         ASSERT_EQ(OK, result);
   1725 
   1726         EXPECT_EQ(e1.index, e2.index);
   1727         EXPECT_EQ(e1.tag, e2.tag);
   1728         ASSERT_EQ(e1.type, e2.type);
   1729         ASSERT_EQ(e1.count, e2.count);
   1730 
   1731         ASSERT_TRUE(!memcmp(e1.data.u8, e2.data.u8,
   1732                         camera_metadata_type_size[e1.type] * e1.count));
   1733     }
   1734 
   1735     // Make sure updating one metadata buffer doesn't change the other
   1736 
   1737     int64_t double_exposure_time[] = { 100, 200 };
   1738 
   1739     result = update_camera_metadata_entry(m, 0,
   1740             double_exposure_time,
   1741             sizeof(double_exposure_time)/sizeof(int64_t), NULL);
   1742     EXPECT_EQ(OK, result);
   1743 
   1744     result = get_camera_metadata_entry(m, 0, &e1);
   1745     ASSERT_EQ(OK, result);
   1746     result = get_camera_metadata_entry(m2, 0, &e2);
   1747     ASSERT_EQ(OK, result);
   1748 
   1749     EXPECT_EQ(e1.index, e2.index);
   1750     EXPECT_EQ(e1.tag, e2.tag);
   1751     ASSERT_EQ(e1.type, e2.type);
   1752     ASSERT_EQ((size_t)2, e1.count);
   1753     ASSERT_EQ((size_t)1, e2.count);
   1754     EXPECT_EQ(100, e1.data.i64[0]);
   1755     EXPECT_EQ(200, e1.data.i64[1]);
   1756     EXPECT_EQ(100, e2.data.i64[0]);
   1757 
   1758     // And in the reverse direction as well
   1759 
   1760     double_exposure_time[0] = 300;
   1761     result = update_camera_metadata_entry(m2, 0,
   1762             double_exposure_time,
   1763             sizeof(double_exposure_time)/sizeof(int64_t), NULL);
   1764     EXPECT_EQ(OK, result);
   1765 
   1766     result = get_camera_metadata_entry(m, 0, &e1);
   1767     ASSERT_EQ(OK, result);
   1768     result = get_camera_metadata_entry(m2, 0, &e2);
   1769     ASSERT_EQ(OK, result);
   1770 
   1771     EXPECT_EQ(e1.index, e2.index);
   1772     EXPECT_EQ(e1.tag, e2.tag);
   1773     ASSERT_EQ(e1.type, e2.type);
   1774     ASSERT_EQ((size_t)2, e1.count);
   1775     ASSERT_EQ((size_t)2, e2.count);
   1776     EXPECT_EQ(100, e1.data.i64[0]);
   1777     EXPECT_EQ(200, e1.data.i64[1]);
   1778     EXPECT_EQ(300, e2.data.i64[0]);
   1779     EXPECT_EQ(200, e2.data.i64[1]);
   1780 
   1781     EXPECT_EQ(OK, validate_camera_metadata_structure(m2, &m_size));
   1782 
   1783     delete dst;
   1784     FINISH_USING_CAMERA_METADATA(m);
   1785 }
   1786 
   1787 TEST(camera_metadata, data_alignment) {
   1788     // Verify that when we store the data, the data aligned as we expect
   1789     camera_metadata_t *m = NULL;
   1790     const size_t entry_capacity = 50;
   1791     const size_t data_capacity = 450;
   1792     char dummy_data[data_capacity] = {0,};
   1793 
   1794     int m_types[] = {
   1795         TYPE_BYTE,
   1796         TYPE_INT32,
   1797         TYPE_FLOAT,
   1798         TYPE_INT64,
   1799         TYPE_DOUBLE,
   1800         TYPE_RATIONAL
   1801     };
   1802     const size_t (&m_type_sizes)[NUM_TYPES] = camera_metadata_type_size;
   1803     size_t m_type_align[] = {
   1804         _Alignas(uint8_t),                    // BYTE
   1805         _Alignas(int32_t),                    // INT32
   1806         _Alignas(float),                      // FLOAT
   1807         _Alignas(int64_t),                    // INT64
   1808         _Alignas(double),                     // DOUBLE
   1809         _Alignas(camera_metadata_rational_t), // RATIONAL
   1810     };
   1811     /* arbitrary tags. the important thing is that their type
   1812        corresponds to m_type_sizes[i]
   1813        */
   1814     int m_type_tags[] = {
   1815         ANDROID_REQUEST_TYPE,
   1816         ANDROID_REQUEST_ID,
   1817         ANDROID_LENS_FOCUS_DISTANCE,
   1818         ANDROID_SENSOR_EXPOSURE_TIME,
   1819         ANDROID_JPEG_GPS_COORDINATES,
   1820         ANDROID_CONTROL_AE_COMPENSATION_STEP
   1821     };
   1822 
   1823     /*
   1824     if the asserts fail, its because we added more types.
   1825         this means the test should be updated to include more types.
   1826     */
   1827     ASSERT_EQ((size_t)NUM_TYPES, sizeof(m_types)/sizeof(m_types[0]));
   1828     ASSERT_EQ((size_t)NUM_TYPES, sizeof(m_type_align)/sizeof(m_type_align[0]));
   1829     ASSERT_EQ((size_t)NUM_TYPES, sizeof(m_type_tags)/sizeof(m_type_tags[0]));
   1830 
   1831     for (int m_type = 0; m_type < (int)NUM_TYPES; ++m_type) {
   1832 
   1833         ASSERT_EQ(m_types[m_type],
   1834             get_camera_metadata_tag_type(m_type_tags[m_type]));
   1835 
   1836         // misalignment possibilities are [0,type_size) for any type pointer
   1837         for (size_t i = 0; i < m_type_sizes[m_type]; ++i) {
   1838 
   1839             /* data_count = 1, we may store data in the index.
   1840                data_count = 10, we will store data separately
   1841              */
   1842             for (int data_count = 1; data_count <= 10; data_count += 9) {
   1843 
   1844                 m = allocate_camera_metadata(entry_capacity, data_capacity);
   1845 
   1846                 // add dummy data to test various different padding requirements
   1847                 ASSERT_EQ(OK,
   1848                     add_camera_metadata_entry(m,
   1849                                               m_type_tags[TYPE_BYTE],
   1850                                               &dummy_data[0],
   1851                                               data_count + i));
   1852                 // insert the type we care to test
   1853                 ASSERT_EQ(OK,
   1854                     add_camera_metadata_entry(m, m_type_tags[m_type],
   1855                                              &dummy_data[0], data_count));
   1856 
   1857                 // now check the alignment for our desired type. it should be ok
   1858                 camera_metadata_ro_entry_t entry = camera_metadata_ro_entry_t();
   1859                 ASSERT_EQ(OK,
   1860                     find_camera_metadata_ro_entry(m, m_type_tags[m_type],
   1861                                                  &entry));
   1862 
   1863                 void* data_ptr = (void*)entry.data.u8;
   1864                 void* aligned_ptr = (void*)((uintptr_t)data_ptr & ~(m_type_align[m_type] - 1));
   1865                 EXPECT_EQ(aligned_ptr, data_ptr) <<
   1866                     "Wrong alignment for type " <<
   1867                     camera_metadata_type_names[m_type] <<
   1868                     " with " << (data_count + i) << " dummy bytes and " <<
   1869                     " data_count " << data_count <<
   1870                     " expected alignment was: " << m_type_align[m_type];
   1871 
   1872                 FINISH_USING_CAMERA_METADATA(m);
   1873             }
   1874         }
   1875     }
   1876 }
   1877