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