Merge master
[libdcp.git] / test / tests.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cmath>
21 #include <boost/filesystem.hpp>
22 #include <libxml++/libxml++.h>
23 #include "KM_prng.h"
24 #include "dcp.h"
25 #include "util.h"
26 #include "metadata.h"
27 #include "types.h"
28 #include "exceptions.h"
29 #include "subtitle_asset.h"
30 #include "picture_asset.h"
31 #include "sound_asset.h"
32 #include "reel.h"
33 #include "certificates.h"
34 #include "crypt_chain.h"
35
36 #define BOOST_TEST_DYN_LINK
37 #define BOOST_TEST_MODULE libdcp_test
38 #include <boost/test/unit_test.hpp>
39
40 using std::string;
41 using std::cout;
42 using std::vector;
43 using std::list;
44 using boost::shared_ptr;
45
46 string
47 j2c (int)
48 {
49         return "test/data/32x32_red_square.j2c";
50 }
51
52 string
53 wav (libdcp::Channel)
54 {
55         return "test/data/1s_24-bit_48k_silence.wav";
56 }
57                 
58
59 BOOST_AUTO_TEST_CASE (dcp_test)
60 {
61         libdcp::init ();
62         
63         Kumu::libdcp_test = true;
64         
65         libdcp::Metadata* t = libdcp::Metadata::instance ();
66         t->issuer = "OpenDCP 0.0.25";
67         t->creator = "OpenDCP 0.0.25";
68         t->company_name = "OpenDCP";
69         t->product_name = "OpenDCP";
70         t->product_version = "0.0.25";
71         t->issue_date = "2012-07-17T04:45:18+00:00";
72         boost::filesystem::remove_all ("build/test/foo");
73         boost::filesystem::create_directories ("build/test/foo");
74         libdcp::DCP d ("build/test/foo");
75         shared_ptr<libdcp::CPL> cpl (new libdcp::CPL ("build/test/foo", "A Test DCP", libdcp::FEATURE, 24, 24));
76
77         shared_ptr<libdcp::MonoPictureAsset> mp (new libdcp::MonoPictureAsset (
78                                                          j2c,
79                                                          "build/test/foo",
80                                                          "video.mxf",
81                                                          &d.Progress,
82                                                          24,
83                                                          24,
84                                                          32,
85                                                          32,
86                                                          false
87                                                          ));
88
89         shared_ptr<libdcp::SoundAsset> ms (new libdcp::SoundAsset (
90                                                    wav,
91                                                    "build/test/foo",
92                                                    "audio.mxf",
93                                                    &(d.Progress),
94                                                    24,
95                                                    24,
96                                                    0,
97                                                    2,
98                                                    false
99                                                    ));
100         
101         cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (mp, ms, shared_ptr<libdcp::SubtitleAsset> ())));
102         d.add_cpl (cpl);
103
104         d.write_xml ();
105 }
106
107 BOOST_AUTO_TEST_CASE (error_test)
108 {
109         libdcp::DCP d ("build/test/fred");
110         vector<string> p;
111         p.push_back ("frobozz");
112
113         BOOST_CHECK_THROW (new libdcp::MonoPictureAsset (p, "build/test/bar", "video.mxf", &d.Progress, 24, 24, 32, 32, false), libdcp::FileError);
114         BOOST_CHECK_THROW (new libdcp::SoundAsset (p, "build/test/bar", "audio.mxf", &d.Progress, 24, 24, 0, false), libdcp::FileError);
115 }
116
117 BOOST_AUTO_TEST_CASE (read_dcp)
118 {
119         libdcp::DCP d ("test/ref/DCP/foo");
120         d.read ();
121
122         list<shared_ptr<const libdcp::CPL> > cpls = d.cpls ();
123         BOOST_CHECK_EQUAL (cpls.size(), 1);
124
125         BOOST_CHECK_EQUAL (cpls.front()->name(), "A Test DCP");
126         BOOST_CHECK_EQUAL (cpls.front()->content_kind(), libdcp::FEATURE);
127         BOOST_CHECK_EQUAL (cpls.front()->frames_per_second(), 24);
128         BOOST_CHECK_EQUAL (cpls.front()->length(), 24);
129 }
130         
131 BOOST_AUTO_TEST_CASE (subtitles1)
132 {
133         libdcp::SubtitleAsset subs ("test/data", "subs1.xml");
134
135         BOOST_CHECK_EQUAL (subs.language(), "French");
136
137         list<shared_ptr<libdcp::Subtitle> > s = subs.subtitles_at (libdcp::Time (0, 0, 6, 1));
138         BOOST_CHECK_EQUAL (s.size(), 1);
139         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
140                                    "Arial",
141                                    false,
142                                    libdcp::Color (255, 255, 255),
143                                    39,
144                                    libdcp::Time (0, 0, 5, 198),
145                                    libdcp::Time (0, 0, 7, 115),
146                                    15,
147                                    libdcp::BOTTOM,
148                                    "My jacket was Idi Amin's",
149                                    libdcp::BORDER,
150                                    libdcp::Color (0, 0, 0),
151                                    libdcp::Time (0, 0, 0, 1),
152                                    libdcp::Time (0, 0, 0, 1)
153                                    ));
154                                                          
155         s = subs.subtitles_at (libdcp::Time (0, 0, 7, 190));
156         BOOST_CHECK_EQUAL (s.size(), 2);
157         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
158                                    "Arial",
159                                    true,
160                                    libdcp::Color (255, 255, 255),
161                                    39,
162                                    libdcp::Time (0, 0, 7, 177),
163                                    libdcp::Time (0, 0, 11, 31),
164                                    21,
165                                    libdcp::BOTTOM,
166                                    "My corset was H.M. The Queen's",
167                                    libdcp::BORDER,
168                                    libdcp::Color (0, 0, 0),
169                                    libdcp::Time (0, 0, 0, 1),
170                                    libdcp::Time (0, 0, 0, 1)
171                                    ));
172         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
173                                    "Arial",
174                                    false,
175                                    libdcp::Color (255, 255, 255),
176                                    39,
177                                    libdcp::Time (0, 0, 7, 177),
178                                    libdcp::Time (0, 0, 11, 31),
179                                    15,
180                                    libdcp::BOTTOM,
181                                    "My large wonderbra",
182                                    libdcp::BORDER,
183                                    libdcp::Color (0, 0, 0),
184                                    libdcp::Time (0, 0, 0, 1),
185                                    libdcp::Time (0, 0, 0, 1)
186                                    ));
187
188         s = subs.subtitles_at (libdcp::Time (0, 0, 11, 95));
189         BOOST_CHECK_EQUAL (s.size(), 1);
190         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
191                                    "Arial",
192                                    false,
193                                    libdcp::Color (255, 255, 255),
194                                    39,
195                                    libdcp::Time (0, 0, 11, 94),
196                                    libdcp::Time (0, 0, 13, 63),
197                                    15,
198                                    libdcp::BOTTOM,
199                                    "Once belonged to the Shah",
200                                    libdcp::BORDER,
201                                    libdcp::Color (0, 0, 0),
202                                    libdcp::Time (0, 0, 0, 1),
203                                    libdcp::Time (0, 0, 0, 1)
204                                    ));
205
206         s = subs.subtitles_at (libdcp::Time (0, 0, 14, 42));
207         BOOST_CHECK_EQUAL (s.size(), 1);
208         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
209                                    "Arial",
210                                    false,
211                                    libdcp::Color (255, 255, 255),
212                                    39,
213                                    libdcp::Time (0, 0, 13, 104),
214                                    libdcp::Time (0, 0, 15, 177),
215                                    15,
216                                    libdcp::BOTTOM,
217                                    "And these are Roy Hattersley's jeans",
218                                    libdcp::BORDER,
219                                    libdcp::Color (0, 0, 0),
220                                    libdcp::Time (0, 0, 0, 1),
221                                    libdcp::Time (0, 0, 0, 1)
222                                    ));
223 }
224
225 BOOST_AUTO_TEST_CASE (subtitles2)
226 {
227         libdcp::SubtitleAsset subs ("test/data", "subs2.xml");
228
229         list<shared_ptr<libdcp::Subtitle> > s = subs.subtitles_at (libdcp::Time (0, 0, 42, 100));
230         BOOST_CHECK_EQUAL (s.size(), 2);
231         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
232                                    "Arial",
233                                    true,
234                                    libdcp::Color (255, 255, 255),
235                                    42,
236                                    libdcp::Time (0, 0, 41, 62),
237                                    libdcp::Time (0, 0, 43, 52),
238                                    89,
239                                    libdcp::TOP,
240                                    "At afternoon tea with John Peel",
241                                    libdcp::BORDER,
242                                    libdcp::Color (0, 0, 0),
243                                    libdcp::Time (0, 0, 0, 0),
244                                    libdcp::Time (0, 0, 0, 0)
245                                    ));
246         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
247                                    "Arial",
248                                    true,
249                                    libdcp::Color (255, 255, 255),
250                                    42,
251                                    libdcp::Time (0, 0, 41, 62),
252                                    libdcp::Time (0, 0, 43, 52),
253                                    95,
254                                    libdcp::TOP,
255                                    "I enquired if his accent was real",
256                                    libdcp::BORDER,
257                                    libdcp::Color (0, 0, 0),
258                                    libdcp::Time (0, 0, 0, 0),
259                                    libdcp::Time (0, 0, 0, 0)
260                                    ));
261
262         s = subs.subtitles_at (libdcp::Time (0, 0, 50, 50));
263         BOOST_CHECK_EQUAL (s.size(), 2);
264         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
265                                    "Arial",
266                                    true,
267                                    libdcp::Color (255, 255, 255),
268                                    42,
269                                    libdcp::Time (0, 0, 50, 42),
270                                    libdcp::Time (0, 0, 52, 21),
271                                    89,
272                                    libdcp::TOP,
273                                    "He said \"out of the house",
274                                    libdcp::BORDER,
275                                    libdcp::Color (0, 0, 0),
276                                    libdcp::Time (0, 0, 0, 0),
277                                    libdcp::Time (0, 0, 0, 0)
278                                    ));
279         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
280                                    "Arial",
281                                    true,
282                                    libdcp::Color (255, 255, 255),
283                                    42,
284                                    libdcp::Time (0, 0, 50, 42),
285                                    libdcp::Time (0, 0, 52, 21),
286                                    95,
287                                    libdcp::TOP,
288                                    "I'm incredibly scouse",
289                                    libdcp::BORDER,
290                                    libdcp::Color (0, 0, 0),
291                                    libdcp::Time (0, 0, 0, 0),
292                                    libdcp::Time (0, 0, 0, 0)
293                                    ));
294
295         s = subs.subtitles_at (libdcp::Time (0, 1, 2, 300));
296         BOOST_CHECK_EQUAL (s.size(), 2);
297         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
298                                    "Arial",
299                                    true,
300                                    libdcp::Color (255, 255, 255),
301                                    42,
302                                    libdcp::Time (0, 1, 2, 208),
303                                    libdcp::Time (0, 1, 4, 10),
304                                    89,
305                                    libdcp::TOP,
306                                    "At home it depends how I feel.\"",
307                                    libdcp::BORDER,
308                                    libdcp::Color (0, 0, 0),
309                                    libdcp::Time (0, 0, 0, 0),
310                                    libdcp::Time (0, 0, 0, 0)
311                                    ));
312         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
313                                    "Arial",
314                                    true,
315                                    libdcp::Color (255, 255, 255),
316                                    42,
317                                    libdcp::Time (0, 1, 2, 208),
318                                    libdcp::Time (0, 1, 4, 10),
319                                    95,
320                                    libdcp::TOP,
321                                    "I spent a long weekend in Brighton",
322                                    libdcp::BORDER,
323                                    libdcp::Color (0, 0, 0),
324                                    libdcp::Time (0, 0, 0, 0),
325                                    libdcp::Time (0, 0, 0, 0)
326                                    ));
327
328         s = subs.subtitles_at (libdcp::Time (0, 1, 15, 50));
329         BOOST_CHECK_EQUAL (s.size(), 2);
330         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
331                                    "Arial",
332                                    true,
333                                    libdcp::Color (255, 255, 255),
334                                    42,
335                                    libdcp::Time (0, 1, 15, 42),
336                                    libdcp::Time (0, 1, 16, 42),
337                                    89,
338                                    libdcp::TOP,
339                                    "With the legendary Miss Enid Blyton",
340                                    libdcp::BORDER,
341                                    libdcp::Color (0, 0, 0),
342                                    libdcp::Time (0, 0, 0, 0),
343                                    libdcp::Time (0, 0, 0, 0)
344                                    ));
345         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
346                                    "Arial",
347                                    true,
348                                    libdcp::Color (255, 255, 255),
349                                    42,
350                                    libdcp::Time (0, 1, 15, 42),
351                                    libdcp::Time (0, 1, 16, 42),
352                                    95,
353                                    libdcp::TOP,
354                                    "She said \"you be Noddy",
355                                    libdcp::BORDER,
356                                    libdcp::Color (0, 0, 0),
357                                    libdcp::Time (0, 0, 0, 0),
358                                    libdcp::Time (0, 0, 0, 0)
359                                    ));
360
361         s = subs.subtitles_at (libdcp::Time (0, 1, 27, 200));
362         BOOST_CHECK_EQUAL (s.size(), 2);
363         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
364                                    "Arial",
365                                    true,
366                                    libdcp::Color (255, 255, 255),
367                                    42,
368                                    libdcp::Time (0, 1, 27, 115),
369                                    libdcp::Time (0, 1, 28, 208),
370                                    89,
371                                    libdcp::TOP,
372                                    "That curious creature the Sphinx",
373                                    libdcp::BORDER,
374                                    libdcp::Color (0, 0, 0),
375                                    libdcp::Time (0, 0, 0, 0),
376                                    libdcp::Time (0, 0, 0, 0)
377                                    ));
378         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
379                                    "Arial",
380                                    true,
381                                    libdcp::Color (255, 255, 255),
382                                    42,
383                                    libdcp::Time (0, 1, 27, 115),
384                                    libdcp::Time (0, 1, 28, 208),
385                                    95,
386                                    libdcp::TOP,
387                                    "Is smarter than anyone thinks",
388                                    libdcp::BORDER,
389                                    libdcp::Color (0, 0, 0),
390                                    libdcp::Time (0, 0, 0, 0),
391                                    libdcp::Time (0, 0, 0, 0)
392                                    ));
393
394         s = subs.subtitles_at (libdcp::Time (0, 1, 42, 300));
395         BOOST_CHECK_EQUAL (s.size(), 2);
396         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
397                                    "Arial",
398                                    false,
399                                    libdcp::Color (255, 255, 255),
400                                    42,
401                                    libdcp::Time (0, 1, 42, 229),
402                                    libdcp::Time (0, 1, 45, 62),
403                                    89,
404                                    libdcp::TOP,
405                                    "It sits there and smirks",
406                                    libdcp::BORDER,
407                                    libdcp::Color (0, 0, 0),
408                                    libdcp::Time (0, 0, 0, 0),
409                                    libdcp::Time (0, 0, 0, 0)
410                                    ));
411         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
412                                    "Arial",
413                                    false,
414                                    libdcp::Color (255, 255, 255),
415                                    42,
416                                    libdcp::Time (0, 1, 42, 229),
417                                    libdcp::Time (0, 1, 45, 62),
418                                    95,
419                                    libdcp::TOP,
420                                    "And you don't think it works",
421                                    libdcp::BORDER,
422                                    libdcp::Color (0, 0, 0),
423                                    libdcp::Time (0, 0, 0, 0),
424                                    libdcp::Time (0, 0, 0, 0)
425                                    ));
426
427         s = subs.subtitles_at (libdcp::Time (0, 1, 45, 200));
428         BOOST_CHECK_EQUAL (s.size(), 2);
429         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
430                                    "Arial",
431                                    false,
432                                    libdcp::Color (255, 255, 255),
433                                    42,
434                                    libdcp::Time (0, 1, 45, 146),
435                                    libdcp::Time (0, 1, 47, 94),
436                                    89,
437                                    libdcp::TOP,
438                                    "Then when you're not looking, it winks.",
439                                    libdcp::BORDER,
440                                    libdcp::Color (0, 0, 0),
441                                    libdcp::Time (0, 0, 0, 0),
442                                    libdcp::Time (0, 0, 0, 0)
443                                    ));
444         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
445                                    "Arial",
446                                    false,
447                                    libdcp::Color (255, 255, 255),
448                                    42,
449                                    libdcp::Time (0, 1, 45, 146),
450                                    libdcp::Time (0, 1, 47, 94),
451                                    95,
452                                    libdcp::TOP,
453                                    "When it snows you will find Sister Sledge",
454                                    libdcp::BORDER,
455                                    libdcp::Color (0, 0, 0),
456                                    libdcp::Time (0, 0, 0, 0),
457                                    libdcp::Time (0, 0, 0, 0)
458                                    ));
459
460         s = subs.subtitles_at (libdcp::Time (0, 1, 47, 249));
461         BOOST_CHECK_EQUAL (s.size(), 2);
462         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
463                                    "Arial",
464                                    false,
465                                    libdcp::Color (255, 255, 255),
466                                    42,
467                                    libdcp::Time (0, 1, 47, 146),
468                                    libdcp::Time (0, 1, 48, 167),
469                                    89,
470                                    libdcp::TOP,
471                                    "Out mooning, at night, on the ledge",
472                                    libdcp::BORDER,
473                                    libdcp::Color (0, 0, 0),
474                                    libdcp::Time (0, 0, 0, 0),
475                                    libdcp::Time (0, 0, 0, 0)
476                                    ));
477         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
478                                    "Arial",
479                                    false,
480                                    libdcp::Color (255, 255, 255),
481                                    42,
482                                    libdcp::Time (0, 1, 47, 146),
483                                    libdcp::Time (0, 1, 48, 167),
484                                    95,
485                                    libdcp::TOP,
486                                    "One storey down",
487                                    libdcp::BORDER,
488                                    libdcp::Color (0, 0, 0),
489                                    libdcp::Time (0, 0, 0, 0),
490                                    libdcp::Time (0, 0, 0, 0)
491                                    ));
492
493         s = subs.subtitles_at (libdcp::Time (0, 2, 6, 210));
494         BOOST_CHECK_EQUAL (s.size(), 2);
495         BOOST_CHECK_EQUAL (*(s.front().get()), libdcp::Subtitle (
496                                    "Arial",
497                                    true,
498                                    libdcp::Color (255, 255, 255),
499                                    42,
500                                    libdcp::Time (0, 2, 5, 208),
501                                    libdcp::Time (0, 2, 7, 31),
502                                    89,
503                                    libdcp::TOP,
504                                    "HELLO",
505                                    libdcp::BORDER,
506                                    libdcp::Color (0, 0, 0),
507                                    libdcp::Time (0, 0, 0, 0),
508                                    libdcp::Time (0, 0, 0, 0)
509                                    ));
510         BOOST_CHECK_EQUAL (*(s.back().get()), libdcp::Subtitle (
511                                    "Arial",
512                                    true,
513                                    libdcp::Color (255, 255, 255),
514                                    42,
515                                    libdcp::Time (0, 2, 5, 208),
516                                    libdcp::Time (0, 2, 7, 31),
517                                    95,
518                                    libdcp::TOP,
519                                    "WORLD",
520                                    libdcp::BORDER,
521                                    libdcp::Color (0, 0, 0),
522                                    libdcp::Time (0, 0, 0, 0),
523                                    libdcp::Time (0, 0, 0, 0)
524                                    ));
525
526         
527         
528 }
529
530 BOOST_AUTO_TEST_CASE (dcp_time)
531 {
532         libdcp::Time t (977143, 24);
533
534         BOOST_CHECK_EQUAL (t.t, 73);
535         BOOST_CHECK_EQUAL (t.s, 34);
536         BOOST_CHECK_EQUAL (t.m, 18);
537         BOOST_CHECK_EQUAL (t.h, 11);
538         BOOST_CHECK_EQUAL (t.to_string(), "11:18:34:73");
539         BOOST_CHECK_EQUAL (t.to_ticks(), 1017923);
540
541         libdcp::Time a (3, 2, 3, 4);
542         libdcp::Time b (2, 3, 4, 5);
543
544         libdcp::Time r = a - b;
545         BOOST_CHECK_EQUAL (r.h, 0);
546         BOOST_CHECK_EQUAL (r.m, 58);
547         BOOST_CHECK_EQUAL (r.s, 58);
548         BOOST_CHECK_EQUAL (r.t, 249);
549         BOOST_CHECK_EQUAL (r.to_string(), "0:58:58:249");
550         BOOST_CHECK_EQUAL (r.to_ticks(), 88699);
551
552         a = libdcp::Time (1, 58, 56, 240);
553         b = libdcp::Time (1, 7, 12, 120);
554         r = a + b;
555         BOOST_CHECK_EQUAL (r.h, 3);
556         BOOST_CHECK_EQUAL (r.m, 6);
557         BOOST_CHECK_EQUAL (r.s, 9);
558         BOOST_CHECK_EQUAL (r.t, 110);
559         BOOST_CHECK_EQUAL (r.to_string(), "3:6:9:110");
560         BOOST_CHECK_EQUAL (r.to_ticks(), 279335);
561
562         a = libdcp::Time (24, 12, 6, 3);
563         b = libdcp::Time (16, 8, 4, 2);
564         BOOST_CHECK_CLOSE (a / b, 1.5, 1e-5);
565 }
566
567 BOOST_AUTO_TEST_CASE (color)
568 {
569         libdcp::Color c ("FFFF0000");
570
571         BOOST_CHECK_EQUAL (c.r, 255);
572         BOOST_CHECK_EQUAL (c.g, 0);
573         BOOST_CHECK_EQUAL (c.b, 0);
574         BOOST_CHECK_EQUAL (c.to_argb_string(), "FFFF0000");
575
576         c = libdcp::Color ("FF00FF00");
577
578         BOOST_CHECK_EQUAL (c.r, 0);
579         BOOST_CHECK_EQUAL (c.g, 255);
580         BOOST_CHECK_EQUAL (c.b, 0);
581         BOOST_CHECK_EQUAL (c.to_argb_string(), "FF00FF00");
582
583         c = libdcp::Color ("FF0000FF");
584
585         BOOST_CHECK_EQUAL (c.r, 0);
586         BOOST_CHECK_EQUAL (c.g, 0);
587         BOOST_CHECK_EQUAL (c.b, 255);
588         BOOST_CHECK_EQUAL (c.to_argb_string(), "FF0000FF");
589         
590 }
591
592 BOOST_AUTO_TEST_CASE (encryption)
593 {
594         Kumu::libdcp_test = true;
595         
596         libdcp::Metadata* t = libdcp::Metadata::instance ();
597         t->issuer = "OpenDCP 0.0.25";
598         t->creator = "OpenDCP 0.0.25";
599         t->company_name = "OpenDCP";
600         t->product_name = "OpenDCP";
601         t->product_version = "0.0.25";
602         t->issue_date = "2012-07-17T04:45:18+00:00";
603         boost::filesystem::remove_all ("build/test/bar");
604         boost::filesystem::create_directories ("build/test/bar");
605         libdcp::DCP d ("build/test/bar");
606
607         libdcp::CertificateChain chain;
608         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate ("test/data/ca.self-signed.pem")));
609         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate ("test/data/intermediate.signed.pem")));
610         chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate ("test/data/leaf.signed.pem")));
611
612         shared_ptr<libdcp::Encryption> crypt (
613                 new libdcp::Encryption (
614                         chain,
615                         "test/data/signer.key"
616                         )
617                 );
618
619         shared_ptr<libdcp::CPL> cpl (new libdcp::CPL ("build/test/bar", "A Test DCP", libdcp::FEATURE, 24, 24));
620         
621         shared_ptr<libdcp::MonoPictureAsset> mp (new libdcp::MonoPictureAsset (
622                                                          j2c,
623                                                          "build/test/bar",
624                                                          "video.mxf",
625                                                          &d.Progress,
626                                                          24,
627                                                          24,
628                                                          32,
629                                                          32,
630                                                          true
631                                                          ));
632
633         shared_ptr<libdcp::SoundAsset> ms (new libdcp::SoundAsset (
634                                                    wav,
635                                                    "build/test/bar",
636                                                    "audio.mxf",
637                                                    &(d.Progress),
638                                                    24,
639                                                    24,
640                                                    2,
641                                                    true
642                                                    ));
643         
644         cpl->add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (mp, ms, shared_ptr<libdcp::SubtitleAsset> ())));
645         d.add_cpl (cpl);
646
647         d.write_xml (crypt);
648
649         shared_ptr<xmlpp::Document> kdm = cpl->make_kdm (
650                 crypt->certificates,
651                 crypt->signer_key,
652                 crypt->certificates.leaf(),
653                 boost::posix_time::time_from_string ("2013-01-01 00:00:00"),
654                 boost::posix_time::time_from_string ("2013-01-08 00:00:00")
655                 );
656
657         kdm->write_to_file_formatted ("build/test/bar.kdm.xml", "UTF-8");
658 }
659
660 BOOST_AUTO_TEST_CASE (certificates)
661 {
662         libdcp::CertificateChain c;
663
664         c.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate ("test/data/ca.self-signed.pem")));
665         c.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate ("test/data/intermediate.signed.pem")));
666         c.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate ("test/data/leaf.signed.pem")));
667         
668         BOOST_CHECK_EQUAL (
669                 c.root()->issuer(),
670                 "/O=example.org/OU=example.org/CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION/dnQualifier=rTeK7x+nopFkyphflooz6p2ZM7A="
671                 );
672         
673         BOOST_CHECK_EQUAL (
674                 libdcp::Certificate::name_for_xml (c.root()->issuer()),
675                 "dnQualifier=rTeK7x\\+nopFkyphflooz6p2ZM7A=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
676                 );
677
678         BOOST_CHECK_EQUAL (c.root()->serial(), "5");
679
680         BOOST_CHECK_EQUAL (
681                 libdcp::Certificate::name_for_xml (c.root()->subject()),
682                 "dnQualifier=rTeK7x\\+nopFkyphflooz6p2ZM7A=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
683                 );
684 }
685
686 BOOST_AUTO_TEST_CASE (crypt_chain)
687 {
688         boost::filesystem::remove_all ("build/test/crypt");
689         boost::filesystem::create_directory ("build/test/crypt");
690         libdcp::make_crypt_chain ("build/test/crypt");
691 }