license_detection.rs

  1use regex::Regex;
  2
  3/// The most common license locations, with US and UK English spelling.
  4pub const LICENSE_FILES_TO_CHECK: &[&str] = &["LICENSE", "LICENCE", "LICENSE.txt", "LICENCE.txt"];
  5
  6pub fn is_license_eligible_for_data_collection(license: &str) -> bool {
  7    // TODO: Include more licenses later (namely, Apache)
  8    for pattern in [MIT_LICENSE_REGEX, ISC_LICENSE_REGEX] {
  9        let regex = Regex::new(pattern.trim()).unwrap();
 10        if regex.is_match(license.trim()) {
 11            return true;
 12        }
 13    }
 14    false
 15}
 16
 17const MIT_LICENSE_REGEX: &str = r#"
 18^.*MIT License.*
 19
 20Copyright.*?
 21
 22Permission is hereby granted, free of charge, to any person obtaining a copy
 23of this software and associated documentation files \(the "Software"\), to deal
 24in the Software without restriction, including without limitation the rights
 25to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 26copies of the Software, and to permit persons to whom the Software is
 27furnished to do so, subject to the following conditions:
 28
 29The above copyright notice and this permission notice shall be included in all
 30copies or substantial portions of the Software\.
 31
 32THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 33IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 34FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\. IN NO EVENT SHALL THE
 35AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 36LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 37OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 38SOFTWARE\.$
 39"#;
 40
 41const ISC_LICENSE_REGEX: &str = r#"
 42^ISC License
 43
 44Copyright.*?
 45
 46Permission to use, copy, modify, and/or distribute this software for any
 47purpose with or without fee is hereby granted, provided that the above
 48copyright notice and this permission notice appear in all copies\.
 49
 50THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 51WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 52MERCHANTABILITY AND FITNESS\. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 53ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 54WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 55ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 56OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE\.$
 57"#;
 58
 59#[cfg(test)]
 60mod tests {
 61    use unindent::unindent;
 62
 63    use crate::is_license_eligible_for_data_collection;
 64
 65    #[test]
 66    fn test_mit_positive_detection() {
 67        let example_license = unindent(
 68            r#"
 69                MIT License
 70
 71                Copyright (c) 2024 John Doe
 72
 73                Permission is hereby granted, free of charge, to any person obtaining a copy
 74                of this software and associated documentation files (the "Software"), to deal
 75                in the Software without restriction, including without limitation the rights
 76                to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 77                copies of the Software, and to permit persons to whom the Software is
 78                furnished to do so, subject to the following conditions:
 79
 80                The above copyright notice and this permission notice shall be included in all
 81                copies or substantial portions of the Software.
 82
 83                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 84                IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 85                FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 86                AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 87                LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 88                OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 89                SOFTWARE.
 90            "#
 91            .trim(),
 92        );
 93
 94        assert!(is_license_eligible_for_data_collection(&example_license));
 95
 96        let example_license = unindent(
 97            r#"
 98                The MIT License (MIT)
 99
100                Copyright (c) 2019 John Doe
101
102                Permission is hereby granted, free of charge, to any person obtaining a copy
103                of this software and associated documentation files (the "Software"), to deal
104                in the Software without restriction, including without limitation the rights
105                to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
106                copies of the Software, and to permit persons to whom the Software is
107                furnished to do so, subject to the following conditions:
108
109                The above copyright notice and this permission notice shall be included in all
110                copies or substantial portions of the Software.
111
112                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
113                IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
114                FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
115                AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
116                LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
117                OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
118                SOFTWARE.
119            "#
120            .trim(),
121        );
122
123        assert!(is_license_eligible_for_data_collection(&example_license));
124    }
125
126    #[test]
127    fn test_mit_negative_detection() {
128        let example_license = unindent(
129            r#"
130                MIT License
131
132                Copyright (c) 2024 John Doe
133
134                Permission is hereby granted, free of charge, to any person obtaining a copy
135                of this software and associated documentation files (the "Software"), to deal
136                in the Software without restriction, including without limitation the rights
137                to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
138                copies of the Software, and to permit persons to whom the Software is
139                furnished to do so, subject to the following conditions:
140
141                The above copyright notice and this permission notice shall be included in all
142                copies or substantial portions of the Software.
143
144                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
145                IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
146                FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
147                AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
148                LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
149                OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
150                SOFTWARE.
151
152                This project is dual licensed under the MIT License and the Apache License, Version 2.0.
153            "#
154            .trim(),
155        );
156
157        assert!(!is_license_eligible_for_data_collection(&example_license));
158    }
159
160    #[test]
161    fn test_isc_positive_detection() {
162        let example_license = unindent(
163            r#"
164                ISC License
165
166                Copyright (c) 2024, John Doe
167
168                Permission to use, copy, modify, and/or distribute this software for any
169                purpose with or without fee is hereby granted, provided that the above
170                copyright notice and this permission notice appear in all copies.
171
172                THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
173                WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
174                MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
175                ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
176                WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
177                ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
178                OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
179            "#
180            .trim(),
181        );
182
183        assert!(is_license_eligible_for_data_collection(&example_license));
184    }
185
186    #[test]
187    fn test_isc_negative_detection() {
188        let example_license = unindent(
189            r#"
190                ISC License
191
192                Copyright (c) 2024, John Doe
193
194                Permission to use, copy, modify, and/or distribute this software for any
195                purpose with or without fee is hereby granted, provided that the above
196                copyright notice and this permission notice appear in all copies.
197
198                THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
199                WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
200                MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
201                ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
202                WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
203                ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
204                OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
205
206                This project is dual licensed under the ISC License and the MIT License.
207            "#
208            .trim(),
209        );
210
211        assert!(!is_license_eligible_for_data_collection(&example_license));
212    }
213}