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
|
// encrypter.rs
//
// Copyright 2019 Jordan Petridis <jordan@centricular.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
// SPDX-License-Identifier: MIT
use gst::glib;
use gst::prelude::*;
use once_cell::sync::Lazy;
use pretty_assertions::assert_eq;
static RECEIVER_PUBLIC: Lazy<glib::Bytes> = Lazy::new(|| {
let public = [
28, 95, 33, 124, 28, 103, 80, 78, 7, 28, 234, 40, 226, 179, 253, 166, 169, 64, 78, 5, 57,
92, 151, 179, 221, 89, 68, 70, 44, 225, 219, 19,
];
glib::Bytes::from_owned(public)
});
static SENDER_PRIVATE: Lazy<glib::Bytes> = Lazy::new(|| {
let secret = [
154, 227, 90, 239, 206, 184, 202, 234, 176, 161, 14, 91, 218, 98, 142, 13, 145, 223, 210,
222, 224, 240, 98, 51, 142, 165, 255, 1, 159, 100, 242, 162,
];
glib::Bytes::from_owned(secret)
});
static NONCE: Lazy<glib::Bytes> = Lazy::new(|| {
let nonce = [
144, 187, 179, 230, 15, 4, 241, 15, 37, 133, 22, 30, 50, 106, 70, 159, 243, 218, 173, 22,
18, 36, 4, 45,
];
glib::Bytes::from_owned(nonce)
});
fn init() {
use std::sync::Once;
static INIT: Once = Once::new();
INIT.call_once(|| {
gst::init().unwrap();
gstsodium::plugin_register_static().unwrap();
// set the nonce
std::env::set_var("GST_SODIUM_ENCRYPT_NONCE", hex::encode(&*NONCE));
});
}
#[test]
fn encrypt_file() {
init();
let input = include_bytes!("sample.mp3");
let expected_output = include_bytes!("encrypted_sample.enc");
let mut adapter = gst_base::UniqueAdapter::new();
let enc = gst::ElementFactory::make("sodiumencrypter")
.property("sender-key", &*SENDER_PRIVATE)
.property("receiver-key", &*RECEIVER_PUBLIC)
.property("block-size", 1024u32)
.build()
.unwrap();
let mut h = gst_check::Harness::with_element(&enc, None, None);
h.add_element_src_pad(&enc.static_pad("src").expect("failed to get src pad"));
h.add_element_sink_pad(&enc.static_pad("sink").expect("failed to get src pad"));
h.set_src_caps_str("application/x-sodium-encrypted");
let buf = gst::Buffer::from_mut_slice(Vec::from(&input[..]));
assert_eq!(h.push(buf), Ok(gst::FlowSuccess::Ok));
h.push_event(gst::event::Eos::new());
println!("Pulling buffer...");
while let Ok(buf) = h.pull() {
adapter.push(buf);
if adapter.available() >= expected_output.len() {
break;
}
}
let buf = adapter
.take_buffer(adapter.available())
.expect("failed to take buffer");
let map = buf.map_readable().expect("Couldn't map buffer readable");
assert_eq!(map.as_ref(), expected_output.as_ref());
}
#[test]
fn test_state_changes() {
init();
// NullToReady without keys provided
{
let enc = gst::ElementFactory::make("sodiumencrypter")
.build()
.unwrap();
assert!(enc.change_state(gst::StateChange::NullToReady).is_err());
// Set only receiver key
let enc = gst::ElementFactory::make("sodiumencrypter")
.property("receiver-key", &*RECEIVER_PUBLIC)
.build()
.unwrap();
assert!(enc.change_state(gst::StateChange::NullToReady).is_err());
// Set only sender key
let enc = gst::ElementFactory::make("sodiumencrypter")
.property("sender-key", &*SENDER_PRIVATE)
.build()
.unwrap();
assert!(enc.change_state(gst::StateChange::NullToReady).is_err());
}
// NullToReady
{
let enc = gst::ElementFactory::make("sodiumencrypter")
.property("sender-key", &*SENDER_PRIVATE)
.property("receiver-key", &*RECEIVER_PUBLIC)
.build()
.unwrap();
assert!(enc.change_state(gst::StateChange::NullToReady).is_ok());
}
// ReadyToNull
{
let enc = gst::ElementFactory::make("sodiumencrypter")
.property("sender-key", &*SENDER_PRIVATE)
.property("receiver-key", &*RECEIVER_PUBLIC)
.build()
.unwrap();
assert!(enc.change_state(gst::StateChange::NullToReady).is_ok());
}
}
|