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
// SPDX-FileCopyrightText: 2020-2021 HH Partners
//
// SPDX-License-Identifier: MIT

use serde::{Deserialize, Serialize};
use spdx_expression::SpdxExpression;

use super::{Algorithm, Checksum};

/// ## File Information
///
/// SPDX's [File Information](https://spdx.github.io/spdx-spec/4-file-information/)
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct FileInformation {
    /// <https://spdx.github.io/spdx-spec/4-file-information/#41-file-name>
    pub file_name: String,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#42-file-spdx-identifier>
    #[serde(rename = "SPDXID")]
    pub file_spdx_identifier: String,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#43-file-type>
    #[serde(rename = "fileTypes", skip_serializing_if = "Vec::is_empty", default)]
    pub file_type: Vec<FileType>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#44-file-checksum>
    #[serde(rename = "checksums")]
    pub file_checksum: Vec<Checksum>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#45-concluded-license>
    #[serde(
        rename = "licenseConcluded",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub concluded_license: Option<SpdxExpression>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#46-license-information-in-file>
    #[serde(
        rename = "licenseInfoInFiles",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub license_information_in_file: Vec<SpdxExpression>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#47-comments-on-license>
    #[serde(
        rename = "licenseComments",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub comments_on_license: Option<String>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#48-copyright-text>
    #[serde(
        rename = "copyrightText",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub copyright_text: Option<String>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#412-file-comment>
    #[serde(rename = "comment", skip_serializing_if = "Option::is_none", default)]
    pub file_comment: Option<String>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#413-file-notice>
    #[serde(
        rename = "noticeText",
        skip_serializing_if = "Option::is_none",
        default
    )]
    pub file_notice: Option<String>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#414-file-contributor>
    #[serde(
        rename = "fileContributors",
        skip_serializing_if = "Vec::is_empty",
        default
    )]
    pub file_contributor: Vec<String>,

    /// <https://spdx.github.io/spdx-spec/4-file-information/#415-file-attribution-text>
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub file_attribution_text: Option<Vec<String>>,
    // TODO: Snippet Information.
}

impl Default for FileInformation {
    fn default() -> Self {
        Self {
            file_name: "NOASSERTION".to_string(),
            file_spdx_identifier: "NOASSERTION".to_string(),
            file_type: Vec::new(),
            file_checksum: Vec::new(),
            concluded_license: None,
            license_information_in_file: Vec::new(),
            comments_on_license: None,
            copyright_text: None,
            file_comment: None,
            file_notice: None,
            file_contributor: Vec::new(),
            file_attribution_text: None,
        }
    }
}

impl FileInformation {
    /// Create new file.
    pub fn new(name: &str, id: &mut i32) -> Self {
        *id += 1;
        Self {
            file_name: name.to_string(),
            file_spdx_identifier: format!("SPDXRef-{id}"),
            ..Self::default()
        }
    }

    /// Check if hash equals.
    pub fn equal_by_hash(&self, algorithm: Algorithm, value: &str) -> bool {
        let checksum = self
            .file_checksum
            .iter()
            .find(|&checksum| checksum.algorithm == algorithm);

        checksum.map_or(false, |checksum| {
            checksum.value.to_ascii_lowercase() == value.to_ascii_lowercase()
        })
    }

    /// Get checksum
    pub fn checksum(&self, algorithm: Algorithm) -> Option<&str> {
        let checksum = self
            .file_checksum
            .iter()
            .find(|&checksum| checksum.algorithm == algorithm);

        checksum.map(|checksum| checksum.value.as_str())
    }
}

/// <https://spdx.github.io/spdx-spec/4-file-information/#43-file-type>
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Clone, Copy)]
#[serde(rename_all = "UPPERCASE")]
pub enum FileType {
    Source,
    Binary,
    Archive,
    Application,
    Audio,
    Image,
    Text,
    Video,
    Documentation,
    SPDX,
    Other,
}

#[cfg(test)]
mod test {
    use std::fs::read_to_string;

    use super::*;
    use crate::models::{Checksum, FileType, SPDX};

    #[test]
    fn checksum_equality() {
        let mut id = 1;
        let mut file_sha256 = FileInformation::new("sha256", &mut id);
        file_sha256
            .file_checksum
            .push(Checksum::new(Algorithm::SHA256, "test"));

        assert!(file_sha256.equal_by_hash(Algorithm::SHA256, "test"));
        assert!(!file_sha256.equal_by_hash(Algorithm::SHA256, "no_test"));

        let mut file_md5 = FileInformation::new("md5", &mut id);
        file_md5
            .file_checksum
            .push(Checksum::new(Algorithm::MD5, "test"));
        assert!(file_md5.equal_by_hash(Algorithm::MD5, "test"));
        assert!(!file_md5.equal_by_hash(Algorithm::MD5, "no_test"));
        assert!(!file_md5.equal_by_hash(Algorithm::SHA1, "test"));
    }

    #[test]
    fn get_checksum() {
        let mut id = 1;
        let mut file_sha256 = FileInformation::new("sha256", &mut id);
        file_sha256
            .file_checksum
            .push(Checksum::new(Algorithm::SHA256, "test"));

        assert_eq!(file_sha256.checksum(Algorithm::SHA256), Some("test"));
        assert_eq!(file_sha256.checksum(Algorithm::MD2), None);

        let mut file_md5 = FileInformation::new("md5", &mut id);
        file_md5
            .file_checksum
            .push(Checksum::new(Algorithm::MD5, "test"));

        assert_eq!(file_md5.checksum(Algorithm::MD5), Some("test"));
    }

    #[test]
    fn file_name() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[0].file_name,
            "./src/org/spdx/parser/DOAPProject.java"
        );
    }
    #[test]
    fn file_spdx_identifier() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[0].file_spdx_identifier,
            "SPDXRef-DoapSource"
        );
    }
    #[test]
    fn file_type() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(spdx.file_information[0].file_type, vec![FileType::Source]);
    }
    #[test]
    fn file_checksum() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[0].file_checksum,
            vec![Checksum {
                algorithm: Algorithm::SHA1,
                value: "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12".to_string()
            }]
        );
    }
    #[test]
    fn concluded_license() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[0].concluded_license,
            Some(SpdxExpression::parse("Apache-2.0").unwrap())
        );
    }
    #[test]
    fn license_information_in_file() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[0].license_information_in_file,
            vec![SpdxExpression::parse("Apache-2.0").unwrap()]
        );
    }
    #[test]
    fn comments_on_license() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[2].comments_on_license,
            Some("This license is used by Jena".to_string())
        );
    }
    #[test]
    fn copyright_text() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[0]
                .copyright_text
                .as_ref()
                .unwrap()
                .clone(),
            "Copyright 2010, 2011 Source Auditor Inc.".to_string()
        );
    }
    #[test]
    fn file_comment() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[1].file_comment,
            Some("This file is used by Jena".to_string())
        );
    }
    #[test]
    fn file_notice() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
                    spdx.file_information[1].file_notice,
                    Some("Apache Commons Lang\nCopyright 2001-2011 The Apache Software Foundation\n\nThis product includes software developed by\nThe Apache Software Foundation (http://www.apache.org/).\n\nThis product includes software from the Spring Framework,\nunder the Apache License 2.0 (see: StringUtils.containsWhitespace())".to_string())
                );
    }
    #[test]
    fn file_contributor() {
        let spdx: SPDX = serde_json::from_str(
            &read_to_string("tests/data/SPDXJSONExample-v2.2.spdx.json").unwrap(),
        )
        .unwrap();
        assert_eq!(
            spdx.file_information[1].file_contributor,
            vec!["Apache Software Foundation".to_string()]
        );
    }
}