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