-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcontext.test.cpp
More file actions
743 lines (599 loc) · 25.5 KB
/
context.test.cpp
File metadata and controls
743 lines (599 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// Copyright 2026 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
#include <gtest/gtest.h>
#include <algorithm>
#include <atomic>
#include <fstream>
#include <sstream>
#include <string>
#include <thread>
#include <nlohmann/json.hpp>
#include "c2pa.hpp"
#include "include/test_utils.hpp"
namespace fs = std::filesystem;
// Test fixture for context tests with automatic cleanup
class ContextTest : public ::testing::Test {
protected:
std::vector<fs::path> temp_files;
bool cleanup_temp_files = true; // Set to false to keep temp files for debugging
// Get path for temp context test files in build directory
fs::path get_temp_path(const std::string& name) {
fs::path current_dir = fs::path(__FILE__).parent_path();
fs::path build_dir = current_dir.parent_path() / "build";
if (!fs::exists(build_dir)) {
fs::create_directories(build_dir);
}
fs::path temp_path = build_dir / ("context-" + name);
temp_files.push_back(temp_path);
return temp_path;
}
void TearDown() override {
if (cleanup_temp_files) {
for (const auto& path : temp_files) {
if (fs::exists(path)) {
fs::remove(path);
}
}
}
temp_files.clear();
}
};
static fs::path fixture_path(const std::string &name)
{
return c2pa_test::get_fixture_path(name);
}
static std::string load_fixture(const std::string &name)
{
return c2pa_test::read_text_file(fixture_path(name));
}
// Can create a context using JSON settings
TEST(Context, ContextFromJsonValid)
{
std::string json = R"({"settings": {}})";
c2pa::Context context(json);
EXPECT_TRUE(context.is_valid());
}
// Can create a context using Settings object
TEST(Context, ContextFromSettingsValid)
{
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "false");
c2pa::Context context(settings);
EXPECT_TRUE(context.is_valid());
}
// Context with invalid JSON throws
TEST(Context, ContextFromJsonInvalidThrows)
{
EXPECT_THROW(
{ c2pa::Context context("{bad"); },
c2pa::C2paException
);
}
// Default context can be used with a Builder
TEST(Context, SettingsDefaultConstruction)
{
c2pa::Settings settings;
auto manifest = load_fixture("training.json");
c2pa::Context context;
// Should not crash when building with default settings
EXPECT_NO_THROW({
c2pa::Builder builder(context, manifest);
});
}
// Can update settings with any valid JSON
TEST(Context, SettingsUpdateJson)
{
c2pa::Settings settings;
EXPECT_NO_THROW({
settings.update(R"({"key": "val"})", "json");
});
}
// ContextBuilder empty build returns default/empty context
TEST(Context, ContextBuilderEmptyBuild)
{
auto builder = c2pa::Context::ContextBuilder();
auto context = builder.create_context();
EXPECT_TRUE(context.is_valid());
}
// Context move constructor transfers ownership, moved-from has no context
TEST(Context, MoveConstructor)
{
auto original = c2pa::Context::ContextBuilder().create_context();
ASSERT_TRUE(original.is_valid());
c2pa::Context moved_to(std::move(original));
EXPECT_TRUE(moved_to.is_valid());
EXPECT_FALSE(original.is_valid());
// Moved-to context is usable
auto manifest = load_fixture("training.json");
EXPECT_NO_THROW({
c2pa::Builder builder(moved_to, manifest);
});
}
// Context move assignment transfers ownership
TEST(Context, MoveAssignment)
{
auto a = c2pa::Context::ContextBuilder().create_context();
auto b = c2pa::Context::ContextBuilder().create_context();
ASSERT_TRUE(a.is_valid());
ASSERT_TRUE(b.is_valid());
a = std::move(b);
EXPECT_TRUE(a.is_valid());
EXPECT_FALSE(b.is_valid());
}
// Context move assignment frees existing context when overwriting
TEST(Context, MoveAssignmentOverwrites)
{
auto a = c2pa::Context::ContextBuilder().create_context();
auto b = c2pa::Context::ContextBuilder().create_context();
ASSERT_TRUE(a.is_valid());
ASSERT_TRUE(b.is_valid());
a = std::move(b);
EXPECT_TRUE(a.is_valid());
EXPECT_FALSE(b.is_valid());
// Use a to ensure the adopted context works (no double-free of old a)
auto manifest = load_fixture("training.json");
EXPECT_NO_THROW({
c2pa::Builder builder(a, manifest);
});
}
// Helper function to check if thumbnail is present in signed manifest
static bool has_thumbnail(const std::string& manifest_json) {
auto parsed = nlohmann::json::parse(manifest_json);
std::string active = parsed["active_manifest"];
return parsed["manifests"][active].contains("thumbnail");
}
// Helper function to sign with context and return manifest JSON
static std::string sign_with_context(c2pa::IContextProvider& context, const fs::path& dest_path) {
auto manifest = c2pa_test::read_text_file(c2pa_test::get_fixture_path("training.json"));
auto certs = c2pa_test::read_text_file(c2pa_test::get_fixture_path("es256_certs.pem"));
auto private_key = c2pa_test::read_text_file(c2pa_test::get_fixture_path("es256_private.key"));
auto asset_path = c2pa_test::get_fixture_path("A.jpg");
c2pa::Builder builder(context, manifest);
c2pa::Signer signer("es256", certs, private_key);
builder.sign(asset_path, dest_path, signer);
c2pa::Reader reader(context, dest_path);
auto result = reader.json();
return result;
}
TEST_F(ContextTest, SetOverridesLastWins) {
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "true");
settings.set("builder.thumbnail.enabled", "false");
auto context = c2pa::Context::ContextBuilder().with_settings(settings).create_context();
auto manifest_json = sign_with_context(context, get_temp_path("set_overrides_last_wins.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
TEST_F(ContextTest, UpdateOverridesSetJson) {
auto settings_json = c2pa_test::read_text_file(c2pa_test::get_fixture_path("settings/test_settings_no_thumbnail.json"));
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "true");
settings.update(settings_json, "json");
auto context = c2pa::Context::ContextBuilder().with_settings(settings).create_context();
auto manifest_json = sign_with_context(context, get_temp_path("update_overrides_set_json.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
TEST_F(ContextTest, SetOverridesUpdateJson) {
auto settings_json = c2pa_test::read_text_file(c2pa_test::get_fixture_path("settings/test_settings_no_thumbnail.json"));
c2pa::Settings settings;
settings.update(settings_json, "json");
settings.set("builder.thumbnail.enabled", "true");
auto context = c2pa::Context::ContextBuilder().with_settings(settings).create_context();
auto manifest_json = sign_with_context(context, get_temp_path("set_overrides_update_json.jpg"));
EXPECT_TRUE(has_thumbnail(manifest_json));
}
TEST_F(ContextTest, WithSettingsThenWithJson) {
auto settings_json = c2pa_test::read_text_file(c2pa_test::get_fixture_path("settings/test_settings_no_thumbnail.json"));
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "true");
auto context = c2pa::Context::ContextBuilder()
.with_settings(settings)
.with_json(settings_json)
.create_context();
auto manifest_json = sign_with_context(context, get_temp_path("with_settings_then_json.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
TEST_F(ContextTest, WithJsonThenWithSettings) {
auto settings_json = c2pa_test::read_text_file(c2pa_test::get_fixture_path("settings/test_settings_with_thumbnail.json"));
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "false");
auto context = c2pa::Context::ContextBuilder()
.with_json(settings_json)
.with_settings(settings)
.create_context();
auto manifest_json = sign_with_context(context, get_temp_path("with_json_then_settings.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
TEST(Context, ContextBuilderMoveConstructor) {
auto b1 = c2pa::Context::ContextBuilder();
auto b2 = std::move(b1);
EXPECT_FALSE(b1.is_valid());
EXPECT_TRUE(b2.is_valid());
auto context = b2.create_context();
EXPECT_TRUE(context.is_valid());
}
TEST(Context, ContextBuilderMoveAssignment) {
auto b1 = c2pa::Context::ContextBuilder();
auto b2 = c2pa::Context::ContextBuilder();
b1 = std::move(b2);
EXPECT_FALSE(b2.is_valid());
EXPECT_TRUE(b1.is_valid());
auto context = b1.create_context();
EXPECT_TRUE(context.is_valid());
}
TEST(Context, SettingsMoveConstructor) {
c2pa::Settings s1;
s1.set("builder.thumbnail.enabled", "false");
auto s2 = std::move(s1);
EXPECT_EQ(s1.c_settings(), nullptr);
// Verify s2 is functional
auto context = c2pa::Context::ContextBuilder().with_settings(s2).create_context();
EXPECT_TRUE(context.is_valid());
}
TEST(Context, SettingsMoveAssignment) {
c2pa::Settings s1;
c2pa::Settings s2;
s2.set("builder.thumbnail.enabled", "true");
s1 = std::move(s2);
EXPECT_EQ(s2.c_settings(), nullptr);
// Verify s1 is functional and can call set()
EXPECT_NO_THROW(s1.set("builder.thumbnail.enabled", "false"));
}
TEST(Context, ContextBuilderUseAfterMoveThrows) {
auto b1 = c2pa::Context::ContextBuilder();
auto b2 = std::move(b1);
EXPECT_THROW(b1.with_json("{}"), c2pa::C2paException);
}
TEST(Context, UseAfterConsumeThrows) {
auto builder = c2pa::Context::ContextBuilder();
auto context = builder.create_context();
EXPECT_THROW(builder.with_json("{}"), c2pa::C2paException);
}
TEST(Context, DoubleConsumeThrows) {
auto builder = c2pa::Context::ContextBuilder();
auto context1 = builder.create_context();
EXPECT_THROW({
auto context2 = builder.create_context();
(void)context2; // Suppress unused variable warning
}, c2pa::C2paException);
}
// Default constructor creates a valid context
TEST(Context, DirectConstructDefault) {
c2pa::Context context;
EXPECT_TRUE(context.is_valid());
EXPECT_NE(context.c_context(), nullptr);
}
// Constructor with Settings creates a valid context
TEST(Context, DirectConstructWithSettings) {
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "false");
c2pa::Context context(settings);
EXPECT_TRUE(context.is_valid());
}
// Default constructor can be used with Builder
TEST(Context, DirectConstructDefaultWithBuilder) {
auto manifest = load_fixture("training.json");
c2pa::Context context;
EXPECT_NO_THROW({
c2pa::Builder builder(context, manifest);
});
}
// 1) Direct construction with Settings: sign and verify thumbnail is disabled
TEST_F(ContextTest, DirectConstructSettingsSignVerify) {
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "false");
c2pa::Context context(settings);
auto manifest_json = sign_with_context(context, get_temp_path("direct_construct_settings.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
// 2) Direct default construction: sign and verify thumbnail is enabled (default)
TEST_F(ContextTest, DirectConstructDefaultSignVerify) {
c2pa::Context context;
auto manifest_json = sign_with_context(context, get_temp_path("direct_construct_default.jpg"));
EXPECT_TRUE(has_thumbnail(manifest_json));
}
// 3) JSON string constructor: sign and verify thumbnail is disabled
TEST_F(ContextTest, JsonConstructorSignVerify) {
c2pa::Context context(R"({"builder": {"thumbnail": {"enabled": false}}})");
auto manifest_json = sign_with_context(context, get_temp_path("json_constructor.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
// 4) ContextBuilder with Settings: sign and verify thumbnail is disabled
TEST_F(ContextTest, ContextBuilderWithSettingsSignVerify) {
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "false");
auto context = c2pa::Context::ContextBuilder()
.with_settings(settings)
.create_context();
auto manifest_json = sign_with_context(context, get_temp_path("builder_with_settings.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
// 5) ContextBuilder with JSON: sign and verify thumbnail is disabled
TEST_F(ContextTest, ContextBuilderWithJsonSignVerify) {
auto context = c2pa::Context::ContextBuilder()
.with_json(R"({"builder": {"thumbnail": {"enabled": false}}})")
.create_context();
auto manifest_json = sign_with_context(context, get_temp_path("builder_with_json.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
// 6) ContextBuilder empty (default): sign and verify thumbnail is enabled (default)
TEST_F(ContextTest, ContextBuilderDefaultSignVerify) {
auto context = c2pa::Context::ContextBuilder().create_context();
auto manifest_json = sign_with_context(context, get_temp_path("builder_default.jpg"));
EXPECT_TRUE(has_thumbnail(manifest_json));
}
// 7) Direct construction with Settings enabling thumbnail: sign and verify
TEST_F(ContextTest, DirectConstructSettingsEnableThumbnailSignVerify) {
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "true");
c2pa::Context context(settings);
auto manifest_json = sign_with_context(context, get_temp_path("direct_construct_enable_thumb.jpg"));
EXPECT_TRUE(has_thumbnail(manifest_json));
}
// Test with_json_settings_file method: loads settings from file path
TEST_F(ContextTest, ContextBuilderWithJsonSettingsFile) {
auto settings_path = c2pa_test::get_fixture_path("settings/test_settings_no_thumbnail.json");
auto context = c2pa::Context::ContextBuilder()
.with_json_settings_file(settings_path)
.create_context();
auto manifest_json = sign_with_context(context, get_temp_path("with_json_settings_file.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
// Test with_json_settings_file with invalid path throws exception
TEST(Context, ContextBuilderWithJsonSettingsFileInvalidPath) {
auto builder = c2pa::Context::ContextBuilder();
EXPECT_THROW({
builder.with_json_settings_file("/nonexistent/path/to/settings.json");
}, c2pa::C2paException);
}
// Test with_json_settings_file can be chained with other methods
TEST_F(ContextTest, ContextBuilderWithJsonSettingsFileChaining) {
auto settings_path = c2pa_test::get_fixture_path("settings/test_settings_with_thumbnail.json");
// File enables thumbnail, then we disable it with set()
c2pa::Settings override_settings;
override_settings.set("builder.thumbnail.enabled", "false");
auto context = c2pa::Context::ContextBuilder()
.with_json_settings_file(settings_path)
.with_settings(override_settings)
.create_context();
auto manifest_json = sign_with_context(context, get_temp_path("with_json_settings_file_chained.jpg"));
EXPECT_FALSE(has_thumbnail(manifest_json));
}
// Context is copied at construction, Reader still works after context goes out of scope
TEST_F(ContextTest, ReaderWorksAfterContextOutOfScope) {
fs::path signed_path = get_temp_path("reader_after_context_scope.jpg");
std::unique_ptr<c2pa::Reader> reader;
{
c2pa::Context context;
sign_with_context(context, signed_path);
reader = std::make_unique<c2pa::Reader>(context, signed_path);
}
// context is out of scope, implementation copies context state so reader still works
EXPECT_NO_THROW(reader->json());
}
// ContextBuilder::with_signer can be chained with with_settings
TEST(Context, ContextBuilderWithSettingsAndSigner) {
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "false");
auto signer = c2pa_test::create_test_signer();
auto context = c2pa::Context::ContextBuilder()
.with_settings(settings)
.with_signer(std::move(signer))
.create_context();
EXPECT_TRUE(context.is_valid());
}
// --- Progress / Cancel API tests ---
// Progress/cancel tests, available since c2pa-rs == 0.78.7.
// Helper: sign a file and return the signed path, using a context with a progress callback.
static fs::path sign_with_progress_context(c2pa::IContextProvider& context, const fs::path& dest) {
auto manifest = c2pa_test::read_text_file(c2pa_test::get_fixture_path("training.json"));
auto certs = c2pa_test::read_text_file(c2pa_test::get_fixture_path("es256_certs.pem"));
auto private_key = c2pa_test::read_text_file(c2pa_test::get_fixture_path("es256_private.key"));
auto asset_path = c2pa_test::get_fixture_path("A.jpg");
c2pa::Builder builder(context, manifest);
c2pa::Signer signer("es256", certs, private_key);
builder.sign(asset_path, dest, signer);
return dest;
}
// Callback is invoked at least once during a sign operation.
TEST_F(ContextTest, ProgressCallback_InvokedDuringSigning) {
std::atomic<int> call_count{0};
auto context = c2pa::Context::ContextBuilder()
.with_progress_callback([&](c2pa::ProgressPhase /*phase*/, uint32_t /*step*/, uint32_t /*total*/) {
++call_count;
return true;
})
.create_context();
ASSERT_TRUE(context.is_valid());
EXPECT_NO_THROW(sign_with_progress_context(context, get_temp_path("progress_signing.jpg")));
EXPECT_GT(call_count.load(), 0);
}
// Callback is invoked at least once during a read operation.
TEST_F(ContextTest, ProgressCallback_InvokedDuringReading) {
// First sign a file without a callback so we have something to read.
{
c2pa::Context sign_ctx;
sign_with_progress_context(sign_ctx, get_temp_path("progress_read_src.jpg"));
}
std::atomic<int> call_count{0};
auto context = c2pa::Context::ContextBuilder()
.with_progress_callback([&](c2pa::ProgressPhase /*phase*/, uint32_t /*step*/, uint32_t /*total*/) {
++call_count;
return true;
})
.create_context();
ASSERT_TRUE(context.is_valid());
EXPECT_NO_THROW({
c2pa::Reader reader(context, get_temp_path("progress_read_src.jpg"));
(void)reader.json();
});
EXPECT_GT(call_count.load(), 0);
}
// Callback receives non-negative step and total values.
TEST_F(ContextTest, ProgressCallback_StepAndTotalValues) {
bool saw_valid_step = false;
auto context = c2pa::Context::ContextBuilder()
.with_progress_callback([&](c2pa::ProgressPhase /*phase*/, uint32_t step, uint32_t total) {
// step is 1-based when total > 0; both may be 0 for indeterminate phases.
if (total > 0) {
EXPECT_GE(step, 1u);
EXPECT_LE(step, total);
saw_valid_step = true;
}
return true;
})
.create_context();
ASSERT_TRUE(context.is_valid());
EXPECT_NO_THROW(sign_with_progress_context(context, get_temp_path("progress_step_total.jpg")));
EXPECT_TRUE(saw_valid_step);
}
// Returning false from the callback causes the operation to be cancelled.
TEST_F(ContextTest, ProgressCallback_ReturnFalseCancels) {
// Cancel on the very first callback invocation.
auto context = c2pa::Context::ContextBuilder()
.with_progress_callback([](c2pa::ProgressPhase /*phase*/, uint32_t /*step*/, uint32_t /*total*/) {
return false; // request cancellation
})
.create_context();
ASSERT_TRUE(context.is_valid());
EXPECT_THROW(
sign_with_progress_context(context, get_temp_path("progress_cancel_false.jpg")),
c2pa::C2paException
);
}
// Context::cancel() called before an operation prevents that operation from completing.
TEST_F(ContextTest, ProgressCallback_CancelMethodAbortsOperation) {
auto context = c2pa::Context::ContextBuilder()
.with_progress_callback([](c2pa::ProgressPhase /*phase*/, uint32_t /*step*/, uint32_t /*total*/) {
return true;
})
.create_context();
ASSERT_TRUE(context.is_valid());
// Cancel is called from within the callback (simulates a cross-thread cancel).
c2pa::Context* ctx_ptr = &context;
bool cancel_called = false;
auto context2 = c2pa::Context::ContextBuilder()
.with_progress_callback([&](c2pa::ProgressPhase /*phase*/, uint32_t /*step*/, uint32_t /*total*/) {
if (!cancel_called) {
cancel_called = true;
ctx_ptr->cancel();
}
return true; // continue returning true; cancellation is handled via cancel()
})
.create_context();
ASSERT_TRUE(context2.is_valid());
ctx_ptr = &context2;
EXPECT_THROW(
sign_with_progress_context(context2, get_temp_path("progress_cancel_method.jpg")),
c2pa::C2paException
);
}
// Context::cancel() is safe to call on a context without a callback.
TEST(Context, CancelWithoutCallback_IsNoOp) {
c2pa::Context context;
ASSERT_TRUE(context.is_valid());
EXPECT_NO_THROW(context.cancel());
}
// with_progress_callback can be chained with with_settings.
TEST_F(ContextTest, ProgressCallback_ChainWithSettings) {
std::atomic<int> call_count{0};
c2pa::Settings settings;
settings.set("builder.thumbnail.enabled", "false");
auto context = c2pa::Context::ContextBuilder()
.with_settings(settings)
.with_progress_callback([&](c2pa::ProgressPhase /*phase*/, uint32_t /*step*/, uint32_t /*total*/) {
++call_count;
return true;
})
.create_context();
ASSERT_TRUE(context.is_valid());
auto manifest_json = sign_with_context(context, get_temp_path("progress_chain_settings.jpg"));
EXPECT_GT(call_count.load(), 0);
EXPECT_FALSE(has_thumbnail(manifest_json));
}
// Context with a progress callback can be move-constructed; callback is still valid.
TEST_F(ContextTest, ProgressCallback_SurvivesContextMove) {
std::atomic<int> call_count{0};
auto original = c2pa::Context::ContextBuilder()
.with_progress_callback([&](c2pa::ProgressPhase /*phase*/, uint32_t /*step*/, uint32_t /*total*/) {
++call_count;
return true;
})
.create_context();
c2pa::Context moved_to(std::move(original));
EXPECT_FALSE(original.is_valid());
ASSERT_TRUE(moved_to.is_valid());
EXPECT_NO_THROW(sign_with_progress_context(moved_to, get_temp_path("progress_move.jpg")));
EXPECT_GT(call_count.load(), 0);
}
// ContextBuilder with a progress callback can be move-constructed; callback is transferred.
TEST_F(ContextTest, ProgressCallback_SurvivesBuilderMove) {
std::atomic<int> call_count{0};
c2pa::Context::ContextBuilder b1;
b1.with_progress_callback([&](c2pa::ProgressPhase /*phase*/, uint32_t /*step*/, uint32_t /*total*/) {
++call_count;
return true;
});
auto b2 = std::move(b1);
EXPECT_FALSE(b1.is_valid());
ASSERT_TRUE(b2.is_valid());
auto context = b2.create_context();
ASSERT_TRUE(context.is_valid());
EXPECT_NO_THROW(sign_with_progress_context(context, get_temp_path("progress_builder_move.jpg")));
EXPECT_GT(call_count.load(), 0);
}
// Context destroyed before Builder is used; callback must still work.
TEST_F(ContextTest, ProgressCallback_SurvivesContextDestruction_Builder) {
std::atomic<int> call_count{0};
// Build a Builder inside a nested scope so the Context is destroyed first.
c2pa::Builder builder = [&]() {
auto context = c2pa::Context::ContextBuilder()
.with_progress_callback([&](c2pa::ProgressPhase, uint32_t, uint32_t) {
++call_count;
return true;
})
.create_context();
auto manifest = c2pa_test::read_text_file(c2pa_test::get_fixture_path("training.json"));
return c2pa::Builder(context, manifest);
// context is destroyed here
}();
auto certs = c2pa_test::read_text_file(c2pa_test::get_fixture_path("es256_certs.pem"));
auto private_key = c2pa_test::read_text_file(c2pa_test::get_fixture_path("es256_private.key"));
auto asset_path = c2pa_test::get_fixture_path("A.jpg");
c2pa::Signer signer("es256", certs, private_key);
// Context is already destroyed; signing must not crash.
EXPECT_NO_THROW(builder.sign(asset_path, get_temp_path("callback_survives_builder.jpg"), signer));
EXPECT_GT(call_count.load(), 0);
}
// Context destroyed before Reader is used; callback must still work.
TEST_F(ContextTest, ProgressCallback_SurvivesContextDestruction_Reader) {
// First sign a file so we have something to read.
{
c2pa::Context sign_ctx;
sign_with_progress_context(sign_ctx, get_temp_path("progress_read_survive_src.jpg"));
}
std::atomic<int> call_count{0};
auto signed_path = get_temp_path("progress_read_survive_src.jpg");
// Build a Reader inside a nested scope so the Context is destroyed first.
c2pa::Reader reader = [&]() {
auto context = c2pa::Context::ContextBuilder()
.with_progress_callback([&](c2pa::ProgressPhase, uint32_t, uint32_t) {
++call_count;
return true;
})
.create_context();
return c2pa::Reader(context, signed_path);
// context is destroyed here
}();
// Context is already destroyed; accessing the reader must not crash.
EXPECT_NO_THROW((void)reader.json());
EXPECT_GT(call_count.load(), 0);
}