Gitfed
bastien-mrq/gitfed / cmd / gitfed-web / license_test.go
package main

import "testing"

func TestDetectLicenseType(t *testing.T) {
	cases := []struct {
		name string
		text string
		want string
	}{
		{"MIT", `MIT License

Copyright (c) 2024 Someone

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...`, "MIT"},

		{"Apache-2.0", `                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION`, "Apache-2.0"},

		{"GPL-3.0", `                    GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>`, "GPL-3.0"},

		{"GPL-2.0", `                    GNU GENERAL PUBLIC LICENSE
                       Version 2, June 1991

 Copyright (C) 1989, 1991 Free Software Foundation, Inc.`, "GPL-2.0"},

		{"LGPL-3.0", `                   GNU LESSER GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

  This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.`, "LGPL-3.0"},

		{"AGPL-3.0", `                    GNU AFFERO GENERAL PUBLIC LICENSE
                       Version 3, 19 November 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>`, "AGPL-3.0"},

		{"MPL-2.0", `Mozilla Public License Version 2.0
==================================

1. Definitions
--------------`, "MPL-2.0"},

		{"BSD-3-Clause", `Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.`, "BSD-3-Clause"},

		{"BSD-2-Clause", `Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.`, "BSD-2-Clause"},

		{"ISC", `Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.`, "ISC"},

		{"Unlicense", `This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.`, "Unlicense"},

		{"CC0-1.0", `Creative Commons Legal Code

CC0 1.0 Universal

    CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM...`, "CC0-1.0"},

		{"unrecognized", `All rights reserved. Contact legal@example.com for licensing terms.`, ""},

		{"empty", "", ""},
	}

	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if got := detectLicenseType(c.text); got != c.want {
				t.Errorf("detectLicenseType(%s) = %q, want %q", c.name, got, c.want)
			}
		})
	}
}

// TestDetectLicenseTypeLGPLNotMisreadAsGPL reproduces the exact ordering
// hazard the switch's case order guards against: LGPL/AGPL license bodies
// legitimately reference "GNU General Public License" as explanatory text
// (LGPL incorporates GPL terms by reference), so checking plain GPL before
// LGPL/AGPL would misclassify them.
func TestDetectLicenseTypeLGPLNotMisreadAsGPL(t *testing.T) {
	text := `GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed in section 3.`
	if got := detectLicenseType(text); got != "LGPL-3.0" {
		t.Errorf("detectLicenseType = %q, want %q (must not fall through to plain GPL-3.0)", got, "LGPL-3.0")
	}
}