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