github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/behind/akamai/akamai_test.go (about)

     1  // Package akamai provideds a gramework.Behind implementation
     2  // developed for Gramework.
     3  // This is not an official Akamai-supported implementation.
     4  // If you having any issues with this package, please
     5  // consider to contact Gramework support first.
     6  // Akamai doesn't provide any official support nor guaranties
     7  // about this package.
     8  //
     9  // Akamai is a trademark of Akamai Technologies, Inc.
    10  //
    11  // Copyright 2017-present Kirill Danshin and Gramework contributors
    12  // Copyright 2019-present Highload LTD (UK CN: 11893420)
    13  //
    14  // Licensed under the Apache License, Version 2.0 (the "License");
    15  // you may not use this file except in compliance with the License.
    16  // You may obtain a copy of the License at
    17  //
    18  //     http://www.apache.org/licenses/LICENSE-2.0
    19  //
    20  
    21  package akamai
    22  
    23  import (
    24  	"net"
    25  	"testing"
    26  )
    27  
    28  // Note: those IP CIDRs are fake.
    29  // Please, download a fresh CIDRs you need to whitelist
    30  // in your Luna Control Panel.
    31  const csvData = `Service Name,CIDR Block,Port,Activation Date,CIDR Status
    32  "Log Delivery","120.33.22.0/24","21","Tue Dec 18 2021 02:00:00 GMT+0200 (Москва, стандартное время)","current"
    33  "Log Delivery","120.33.21.0/24","80,443","Tue Dec 18 2107 02:00:00 GMT+0200 (Москва, стандартное время)","current"
    34  "Log Delivery","120.33.23.0/24","80-8080","Tue Dec 18 1507 02:00:00 GMT+0200 (Москва, стандартное время)","current"
    35  "Log Delivery","120.33.24.0/24","980-3300","Tue Dec 18 5507 02:00:00 GMT+0200 (Москва, стандартное время)","current"
    36  "Log Delivery","120.17.33.0/24","21","Tue Dec 18 6507 02:00:00 GMT+0200 (Москва, стандартное время)","current"
    37  `
    38  
    39  func TestParseCIDRBlocksCSV(t *testing.T) {
    40  	cidrs, err := ParseCIDRBlocksCSV([]byte(csvData), true, true)
    41  	if err != nil {
    42  		t.Error(err)
    43  	}
    44  
    45  	type tcase struct {
    46  		cidr     *net.IPNet
    47  		expected bool
    48  	}
    49  	cases := []tcase{
    50  		{
    51  			expected: true,
    52  			cidr:     parseCIDR("120.33.21.0/24"),
    53  		},
    54  		{
    55  			expected: true,
    56  			cidr:     parseCIDR("120.33.23.0/24"),
    57  		},
    58  
    59  		{
    60  			expected: false,
    61  			cidr:     parseCIDR("120.33.22.0/24"),
    62  		},
    63  		{
    64  			expected: false,
    65  			cidr:     parseCIDR("120.33.24.0/24"),
    66  		},
    67  		{
    68  			expected: false,
    69  			cidr:     parseCIDR("120.17.33.0/24"),
    70  		},
    71  	}
    72  
    73  	for _, testcase := range cases {
    74  		found := false
    75  
    76  		for _, cidr := range cidrs {
    77  			if cidr.String() == testcase.cidr.String() {
    78  				found = true
    79  				break
    80  			}
    81  		}
    82  
    83  		if found != testcase.expected {
    84  			t.Errorf("unexpected result: CIDR %q expected=%v", testcase.cidr.String(), testcase.expected)
    85  			return
    86  		}
    87  	}
    88  }
    89  
    90  func parseCIDR(raw string) *net.IPNet {
    91  	_, cidr, _ := net.ParseCIDR(raw)
    92  	return cidr
    93  }