"Fossies" - the Fresh Open Source Software Archive

Member "geoipupdate-4.3.0/pkg/geoipupdate/config_test.go" (16 Apr 2020, 13115 Bytes) of package /linux/misc/geoipupdate-4.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "config_test.go": 4.1.5_vs_4.2.0.

    1 package geoipupdate
    2 
    3 import (
    4     "fmt"
    5     "io/ioutil"
    6     "net/url"
    7     "os"
    8     "path/filepath"
    9     "testing"
   10 
   11     "github.com/stretchr/testify/assert"
   12     "github.com/stretchr/testify/require"
   13 )
   14 
   15 func TestNewConfig(t *testing.T) {
   16     tests := []struct {
   17         Description string
   18         Input       string
   19         Output      *Config
   20         Err         string
   21     }{
   22         {
   23             Description: "Default config",
   24             Input: `# Please see https://dev.maxmind.com/geoip/geoipupdate/ for instructions
   25 # on setting up geoipupdate, including information on how to download a
   26 # pre-filled GeoIP.conf file.
   27 
   28 # Enter your account ID and license key below. These are available from
   29 # https://www.maxmind.com/en/my_license_key. If you are only using free
   30 # GeoLite databases, you may leave the 0 values.
   31 AccountID 42
   32 LicenseKey 000000000001
   33 
   34 # Enter the edition IDs of the databases you would like to update.
   35 # Multiple edition IDs are separated by spaces.
   36 EditionIDs GeoLite2-Country GeoLite2-City
   37 
   38 # The remaining settings are OPTIONAL.
   39 
   40 # The directory to store the database files. Defaults to DATADIR
   41 # DatabaseDirectory DATADIR
   42 
   43 # The server to use. Defaults to "updates.maxmind.com".
   44 # Host updates.maxmind.com
   45 
   46 # The proxy host name or IP address. You may optionally specify a
   47 # port number, e.g., 127.0.0.1:8888. If no port number is specified, 1080
   48 # will be used.
   49 # Proxy 127.0.0.1:8888
   50 
   51 # The user name and password to use with your proxy server.
   52 # ProxyUserPassword username:password
   53 
   54 # Whether to preserve modification times of files downloaded from the server.
   55 # Defaults to "0".
   56 # PreserveFileTimes 0
   57 
   58 # The lock file to use. This ensures only one geoipupdate process can run at a
   59 # time.
   60 # Note: Once created, this lockfile is not removed from the filesystem.
   61 # Defaults to ".geoipupdate.lock" under the DatabaseDirectory.
   62 # LockFile DATADIR/.geoipupdate.lock
   63 `,
   64             Output: &Config{
   65                 AccountID:         42,
   66                 LicenseKey:        "000000000001",
   67                 DatabaseDirectory: filepath.Clean("/tmp"),
   68                 EditionIDs:        []string{"GeoLite2-Country", "GeoLite2-City"},
   69                 LockFile:          filepath.Clean("/tmp/.geoipupdate.lock"),
   70                 URL:               "https://updates.maxmind.com",
   71             },
   72         },
   73         {
   74             Description: "Default config, old names",
   75             Input: `# Please see https://dev.maxmind.com/geoip/geoipupdate/ for instructions
   76 # on setting up geoipupdate, including information on how to download a
   77 # pre-filled GeoIP.conf file.
   78 
   79 # Enter your account ID and license key below. These are available from
   80 # https://www.maxmind.com/en/my_license_key. If you are only using free
   81 # GeoLite databases, you may leave the 0 values.
   82 UserId 42
   83 LicenseKey 000000000001
   84 
   85 # Enter the edition IDs of the databases you would like to update.
   86 # Multiple edition IDs are separated by spaces.
   87 ProductIds GeoLite2-Country GeoLite2-City
   88 
   89 # The remaining settings are OPTIONAL.
   90 
   91 # The directory to store the database files. Defaults to DATADIR
   92 # DatabaseDirectory DATADIR
   93 
   94 # The server to use. Defaults to "updates.maxmind.com".
   95 # Host updates.maxmind.com
   96 
   97 # The proxy host name or IP address. You may optionally specify a
   98 # port number, e.g., 127.0.0.1:8888. If no port number is specified, 1080
   99 # will be used.
  100 # Proxy 127.0.0.1:8888
  101 
  102 # The user name and password to use with your proxy server.
  103 # ProxyUserPassword username:password
  104 
  105 # Whether to preserve modification times of files downloaded from the server.
  106 # Defaults to "0".
  107 # PreserveFileTimes 0
  108 
  109 # The lock file to use. This ensures only one geoipupdate process can run at a
  110 # time.
  111 # Note: Once created, this lockfile is not removed from the filesystem.
  112 # Defaults to ".geoipupdate.lock" under the DatabaseDirectory.
  113 # LockFile DATADIR/.geoipupdate.lock
  114 `,
  115             Output: &Config{
  116                 AccountID:         42,
  117                 LicenseKey:        "000000000001",
  118                 DatabaseDirectory: filepath.Clean("/tmp"),
  119                 EditionIDs:        []string{"GeoLite2-Country", "GeoLite2-City"},
  120                 LockFile:          filepath.Clean("/tmp/.geoipupdate.lock"),
  121                 URL:               "https://updates.maxmind.com",
  122             },
  123         },
  124         {
  125             Description: "Everything populated",
  126             Input: `# Please see https://dev.maxmind.com/geoip/geoipupdate/ for instructions
  127 # on setting up geoipupdate, including information on how to download a
  128 # pre-filled GeoIP.conf file.
  129 
  130 # Enter your account ID and license key below. These are available from
  131 # https://www.maxmind.com/en/my_license_key. If you are only using free
  132 # GeoLite databases, you may leave the 0 values.
  133 AccountID 1234
  134 LicenseKey abcdefghi
  135 
  136 # Enter the edition IDs of the databases you would like to update.
  137 # Multiple edition IDs are separated by spaces.
  138 EditionIDs GeoLite2-Country GeoLite2-City GeoIP2-City
  139 
  140 # The remaining settings are OPTIONAL.
  141 
  142 # The directory to store the database files. Defaults to DATADIR
  143 DatabaseDirectory /home
  144 
  145 # The server to use. Defaults to "updates.maxmind.com".
  146 Host updates.example.com
  147 
  148 # The proxy host name or IP address. You may optionally specify a
  149 # port number, e.g., 127.0.0.1:8888. If no port number is specified, 1080
  150 # will be used.
  151 Proxy 127.0.0.1:8888
  152 
  153 # The user name and password to use with your proxy server.
  154 ProxyUserPassword username:password
  155 
  156 # Whether to preserve modification times of files downloaded from the server.
  157 # Defaults to "0".
  158 PreserveFileTimes 1
  159 
  160 # The lock file to use. This ensures only one geoipupdate process can run at a
  161 # time.
  162 # Note: Once created, this lockfile is not removed from the filesystem.
  163 # Defaults to ".geoipupdate.lock" under the DatabaseDirectory.
  164 LockFile /usr/lock
  165 `,
  166             Output: &Config{
  167                 AccountID:         1234,
  168                 DatabaseDirectory: filepath.Clean("/tmp"), // Argument takes precedence
  169                 EditionIDs:        []string{"GeoLite2-Country", "GeoLite2-City", "GeoIP2-City"},
  170                 LicenseKey:        "abcdefghi",
  171                 LockFile:          filepath.Clean("/usr/lock"),
  172                 Proxy: &url.URL{
  173                     Scheme: "http",
  174                     User:   url.UserPassword("username", "password"),
  175                     Host:   "127.0.0.1:8888",
  176                 },
  177                 PreserveFileTimes: true,
  178                 URL:               "https://updates.example.com",
  179             },
  180         },
  181         {
  182             Description: "Invalid line",
  183             Input: `AccountID 123
  184 LicenseKey
  185 # Host updates.maxmind.com
  186 `,
  187             Err: "invalid format on line 2",
  188         },
  189         {
  190             Description: "Option is there multiple times",
  191             Input: `AccountID 123
  192 AccountID 456
  193 `,
  194             Err: "`AccountID' is in the config multiple times",
  195         },
  196         {
  197             Description: "Option is there multiple times with different names",
  198             Input: `AccountID 123
  199 UserId 456
  200 `,
  201             Err: "`UserId' is in the config multiple times",
  202         },
  203         {
  204             Description: "Invalid account ID",
  205             Input: `AccountID 1a
  206 `,
  207             Err: `invalid account ID format: strconv.Atoi: parsing "1a": invalid syntax`,
  208         },
  209         {
  210             Description: "Invalid PreserveFileTimes",
  211             Input: `PreserveFileTimes true
  212 `,
  213             Err: "`PreserveFileTimes' must be 0 or 1",
  214         },
  215         {
  216             Description: "Unknown option",
  217             Input: `AccountID 123
  218 EditionID GeoIP2-City
  219 `,
  220             Err: "unknown option on line 2",
  221         },
  222         {
  223             Description: "Missing required key in options",
  224             Input:       ``,
  225             Err:         "the `EditionIDs` option is required",
  226         },
  227         {
  228             Description: "LicenseKey is found but AccountID is not",
  229             Input: `LicenseKey abcd
  230 EditionIDs GeoIP2-City
  231 `,
  232             Err: "the `AccountID` option is required",
  233         },
  234         {
  235             Description: "AccountID is found but LicenseKey is not",
  236             Input: `AccountID 123
  237 EditionIDs GeoIP2-City`,
  238             Err: "the `LicenseKey` option is required",
  239         },
  240         {
  241             Description: "AccountID 0 with the LicenseKey 000000000000 is treated as no AccountID/LicenseKey",
  242             Input: `AccountID 0
  243 LicenseKey 000000000000
  244 EditionIDs GeoIP2-City`,
  245             Err: "geoipupdate requires a valid AccountID and LicenseKey combination",
  246         },
  247         {
  248             Description: "AccountID 999999 with the LicenseKey 000000000000 is treated as no AccountID/LicenseKey",
  249             Input: `AccountID 999999
  250 LicenseKey 000000000000
  251 EditionIDs GeoIP2-City`,
  252             Err: "geoipupdate requires a valid AccountID and LicenseKey combination",
  253         },
  254         {
  255             Description: "AccountID 999999 with a non-000000000000 LicenseKey is treated normally",
  256             Input: `AccountID 999999
  257 LicenseKey abcd
  258 EditionIDs GeoIP2-City`,
  259             Output: &Config{
  260                 AccountID:         999999,
  261                 DatabaseDirectory: filepath.Clean("/tmp"),
  262                 EditionIDs:        []string{"GeoIP2-City"},
  263                 LicenseKey:        "abcd",
  264                 LockFile:          filepath.Clean("/tmp/.geoipupdate.lock"),
  265                 URL:               "https://updates.maxmind.com",
  266             },
  267         },
  268         {
  269             Description: "Deprecated options",
  270             Input: `AccountID 123
  271 LicenseKey abcd
  272 EditionIDs GeoIP2-City
  273 Protocol http
  274 SkipHostnameVerification 1
  275 SkipPeerVerification 1
  276 `,
  277             Output: &Config{
  278                 AccountID:         123,
  279                 DatabaseDirectory: filepath.Clean("/tmp"),
  280                 EditionIDs:        []string{"GeoIP2-City"},
  281                 LicenseKey:        "abcd",
  282                 LockFile:          filepath.Clean("/tmp/.geoipupdate.lock"),
  283                 URL:               "https://updates.maxmind.com",
  284             },
  285         },
  286         {
  287             Description: "CRLF line ending works",
  288             Input:       "AccountID 123\r\nLicenseKey 123\r\nEditionIDs GeoIP2-City\r\n",
  289             Output: &Config{
  290                 AccountID:         123,
  291                 DatabaseDirectory: filepath.Clean("/tmp"),
  292                 EditionIDs:        []string{"GeoIP2-City"},
  293                 LicenseKey:        "123",
  294                 LockFile:          filepath.Clean("/tmp/.geoipupdate.lock"),
  295                 URL:               "https://updates.maxmind.com",
  296             },
  297         },
  298         {
  299             Description: "CR line ending does not work",
  300             Input:       "AccountID 0\rLicenseKey 123\rEditionIDs GeoIP2-City\r",
  301             Err:         `invalid account ID format: strconv.Atoi: parsing "0 LicenseKey 123 EditionIDs GeoIP2-City": invalid syntax`,
  302         },
  303         {
  304             Description: "Multiple spaces between option and value works",
  305             Input: `AccountID  123
  306 LicenseKey  456
  307 EditionIDs    GeoLite2-City      GeoLite2-Country
  308 `,
  309             Output: &Config{
  310                 AccountID:         123,
  311                 DatabaseDirectory: filepath.Clean("/tmp"),
  312                 EditionIDs:        []string{"GeoLite2-City", "GeoLite2-Country"},
  313                 LicenseKey:        "456",
  314                 LockFile:          filepath.Clean("/tmp/.geoipupdate.lock"),
  315                 URL:               "https://updates.maxmind.com",
  316             },
  317         },
  318         {
  319             Description: "Tabs between options and values works",
  320             Input:       "AccountID\t123\nLicenseKey\t\t456\nEditionIDs\t\t\tGeoLite2-City\t\t\t\tGeoLite2-Country\t\t\t\t\n",
  321             Output: &Config{
  322                 AccountID:         123,
  323                 DatabaseDirectory: filepath.Clean("/tmp"),
  324                 EditionIDs:        []string{"GeoLite2-City", "GeoLite2-Country"},
  325                 LicenseKey:        "456",
  326                 LockFile:          filepath.Clean("/tmp/.geoipupdate.lock"),
  327                 URL:               "https://updates.maxmind.com",
  328             },
  329         },
  330     }
  331 
  332     tempFh, err := ioutil.TempFile("", "conf-test")
  333     require.NoError(t, err)
  334     tempName := tempFh.Name()
  335     require.NoError(t, tempFh.Close())
  336     defer func() {
  337         _ = os.Remove(tempName)
  338     }()
  339 
  340     for _, test := range tests {
  341         t.Run(test.Description, func(t *testing.T) {
  342             require.NoError(t, ioutil.WriteFile(tempName, []byte(test.Input), 0600))
  343             config, err := NewConfig(tempName, DefaultDatabaseDirectory, "/tmp", false)
  344             if test.Err == "" {
  345                 assert.NoError(t, err, test.Description)
  346             } else {
  347                 assert.EqualError(t, err, test.Err, test.Description)
  348             }
  349             assert.Equal(t, test.Output, config, test.Description)
  350         })
  351     }
  352 }
  353 
  354 func TestParseProxy(t *testing.T) {
  355     tests := []struct {
  356         Proxy        string
  357         UserPassword string
  358         Output       string
  359         Err          string
  360     }{
  361         {
  362             Proxy:  "127.0.0.1",
  363             Output: "http://127.0.0.1:1080",
  364         },
  365         {
  366             Proxy:  "127.0.0.1:8888",
  367             Output: "http://127.0.0.1:8888",
  368         },
  369         {
  370             Proxy:  "http://127.0.0.1:8888",
  371             Output: "http://127.0.0.1:8888",
  372         },
  373         {
  374             Proxy:  "socks5://127.0.0.1",
  375             Output: "socks5://127.0.0.1:1080",
  376         },
  377         {
  378             Proxy:  "socks5://127.0.0.1:8888",
  379             Output: "socks5://127.0.0.1:8888",
  380         },
  381         {
  382             Proxy:  "Garbage",
  383             Output: "http://Garbage:1080",
  384         },
  385         {
  386             Proxy: "ftp://127.0.0.1",
  387             Err:   "unsupported proxy type: ftp",
  388         },
  389         {
  390             Proxy: "ftp://127.0.0.1:8888",
  391             Err:   "unsupported proxy type: ftp",
  392         },
  393         {
  394             Proxy:  "login:password@127.0.0.1",
  395             Output: "http://login:password@127.0.0.1:1080",
  396         },
  397         {
  398             Proxy:        "login:password@127.0.0.1",
  399             UserPassword: "something:else",
  400             Output:       "http://login:password@127.0.0.1:1080",
  401         },
  402         {
  403             Proxy:        "127.0.0.1",
  404             UserPassword: "something:else",
  405             Output:       "http://something:else@127.0.0.1:1080",
  406         },
  407         {
  408             Proxy:        "127.0.0.1:8888",
  409             UserPassword: "something:else",
  410             Output:       "http://something:else@127.0.0.1:8888",
  411         },
  412         {
  413             Proxy:        "user:password@127.0.0.1:8888",
  414             UserPassword: "user2:password2",
  415             Output:       "http://user:password@127.0.0.1:8888",
  416         },
  417         {
  418             Proxy:        "http://user:password@127.0.0.1:8888",
  419             UserPassword: "user2:password2",
  420             Output:       "http://user:password@127.0.0.1:8888",
  421         },
  422     }
  423 
  424     for _, test := range tests {
  425         t.Run(
  426             fmt.Sprintf("%s - %s", test.Proxy, test.UserPassword),
  427             func(t *testing.T) {
  428                 output, err := parseProxy(test.Proxy, test.UserPassword)
  429                 if test.Err != "" {
  430                     assert.EqualError(t, err, test.Err)
  431                     assert.Nil(t, output)
  432                 } else {
  433                     assert.NoError(t, err)
  434                     assert.Equal(t, test.Output, output.String())
  435                 }
  436             },
  437         )
  438     }
  439 }